Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Generate images via OpenAI, Google, OpenRouter, DashScope, Jimeng, Seedream, and Replicate APIs with batch support.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
scripts/providers/azure.test.ts
1import assert from "node:assert/strict";2import fs from "node:fs/promises";3import os from "node:os";4import path from "node:path";5import test, { type TestContext } from "node:test";67import type { CliArgs } from "../types.ts";8import {9generateImage,10getDefaultModel,11parseAzureBaseURL,12validateArgs,13} from "./azure.ts";1415function useEnv(16t: TestContext,17values: Record<string, string | null>,18): void {19const previous = new Map<string, string | undefined>();20for (const [key, value] of Object.entries(values)) {21previous.set(key, process.env[key]);22if (value == null) {23delete process.env[key];24} else {25process.env[key] = value;26}27}2829t.after(() => {30for (const [key, value] of previous.entries()) {31if (value == null) {32delete process.env[key];33} else {34process.env[key] = value;35}36}37});38}3940function makeArgs(overrides: Partial<CliArgs> = {}): CliArgs {41return {42prompt: null,43promptFiles: [],44imagePath: null,45provider: null,46model: null,47aspectRatio: null,48size: null,49quality: null,50imageSize: null,51referenceImages: [],52n: 1,53batchFile: null,54jobs: null,55json: false,56help: false,57...overrides,58};59}6061async function makeTempDir(prefix: string): Promise<string> {62return fs.mkdtemp(path.join(os.tmpdir(), prefix));63}6465test("Azure endpoint parsing and default deployment selection follow env precedence", (t) => {66assert.deepEqual(parseAzureBaseURL("https://example.openai.azure.com"), {67resourceBaseURL: "https://example.openai.azure.com/openai",68deployment: null,69});70assert.deepEqual(71parseAzureBaseURL("https://example.openai.azure.com/openai/deployments/from-url"),72{73resourceBaseURL: "https://example.openai.azure.com/openai",74deployment: "from-url",75},76);7778useEnv(t, {79AZURE_OPENAI_BASE_URL: "https://example.openai.azure.com/openai/deployments/from-url",80AZURE_OPENAI_DEPLOYMENT: "explicit-deploy",81AZURE_OPENAI_IMAGE_MODEL: "env-fallback",82});83assert.equal(getDefaultModel(), "explicit-deploy");84});8586test("Azure validateArgs rejects unsupported edit input formats before the API call", () => {87assert.doesNotThrow(() =>88validateArgs("demo-deployment", makeArgs({ referenceImages: ["hero.png", "photo.jpeg"] })),89);90assert.throws(91() => validateArgs("demo-deployment", makeArgs({ referenceImages: ["hero.webp"] })),92/PNG or JPG\/JPEG/,93);94});9596test("Azure image generation routes model to deployment and sends mapped quality", async (t) => {97useEnv(t, {98AZURE_OPENAI_API_KEY: "azure-key",99AZURE_OPENAI_BASE_URL: "https://example.openai.azure.com/openai/deployments/default-deploy",100AZURE_API_VERSION: null,101AZURE_OPENAI_DEPLOYMENT: null,102AZURE_OPENAI_IMAGE_MODEL: null,103});104105const originalFetch = globalThis.fetch;106t.after(() => {107globalThis.fetch = originalFetch;108});109110const calls: Array<{ url: string; body: string }> = [];111globalThis.fetch = async (input, init) => {112calls.push({113url: String(input),114body: String(init?.body ?? ""),115});116return Response.json({117data: [{ b64_json: Buffer.from("azure-image").toString("base64") }],118});119};120121const bytes = await generateImage(122"A calm lake at sunset",123"custom-deploy",124makeArgs({ quality: "normal" }),125);126127assert.equal(Buffer.from(bytes).toString("utf8"), "azure-image");128assert.equal(129calls[0]?.url,130"https://example.openai.azure.com/openai/deployments/custom-deploy/images/generations?api-version=2025-04-01-preview",131);132133const body = JSON.parse(calls[0]!.body) as Record<string, string>;134assert.equal(body.quality, "medium");135assert.equal(body.size, "1024x1024");136});137138test("Azure image edits include quality in multipart requests", async (t) => {139const root = await makeTempDir("baoyu-image-gen-azure-");140t.after(() => fs.rm(root, { recursive: true, force: true }));141142const pngPath = path.join(root, "ref.png");143const jpgPath = path.join(root, "ref.jpg");144await fs.writeFile(pngPath, "png-bytes");145await fs.writeFile(jpgPath, "jpg-bytes");146147useEnv(t, {148AZURE_OPENAI_API_KEY: "azure-key",149AZURE_OPENAI_BASE_URL: "https://example.openai.azure.com",150AZURE_API_VERSION: "2025-04-01-preview",151AZURE_OPENAI_DEPLOYMENT: null,152AZURE_OPENAI_IMAGE_MODEL: null,153});154155const originalFetch = globalThis.fetch;156t.after(() => {157globalThis.fetch = originalFetch;158});159160const calls: Array<{ url: string; form: FormData }> = [];161globalThis.fetch = async (input, init) => {162calls.push({163url: String(input),164form: init?.body as FormData,165});166return Response.json({167data: [{ b64_json: Buffer.from("edited-image").toString("base64") }],168});169};170171const bytes = await generateImage(172"Add warm lighting",173"edit-deploy",174makeArgs({175quality: "2k",176referenceImages: [pngPath, jpgPath],177}),178);179180assert.equal(Buffer.from(bytes).toString("utf8"), "edited-image");181assert.equal(182calls[0]?.url,183"https://example.openai.azure.com/openai/deployments/edit-deploy/images/edits?api-version=2025-04-01-preview",184);185assert.equal(calls[0]?.form.get("quality"), "high");186assert.equal(calls[0]?.form.get("size"), "1024x1024");187assert.equal(calls[0]?.form.getAll("image[]").length, 2);188});189