import { readFileSync } from "node:fs"; import { describe, expect, it } from "vitest"; import { MACHGEN_I2I_MAX_IMAGES, MACHGEN_IMAGE_MODELS, MachGenGenerateRequestSchema, } from "@apicity/machgen"; import { computeEstimate, PRICING } from "@apicity/cost"; const tokenEvidence = JSON.parse( readFileSync( "tests/fixtures/machgen-evidence/machgen-gpt-image-2-6-2026-11-18.json", "utf8" ) ) as { contract: { models: Record }; examples: { label: string; task_type: string; width: number; height: number; quality: string; references: number; discountedMicros: number; }[]; }; const tokenModels = MACHGEN_IMAGE_MODELS.filter((id) => Object.hasOwn(tokenEvidence.contract.models, id) ); const payload = { model: "GPT-Image-2", task_type: "T2I", prompt: "A red circle.", image_config: { width: 1280, height: 544 }, optimization_level: "EXPRESS", }; const parse = (overrides: Record) => MachGenGenerateRequestSchema.safeParse({ ...payload, ...overrides }); const issues = (overrides: Record) => { const result = parse(overrides); return result.success ? [] : result.error.issues.map((i) => `${i.path.join(".")}: ${i.message}`); }; const sources = (count: number) => Array.from({ length: count }, (_, i) => `https://example.com/${i}.jpg`); const estimate = (overrides: Record = {}) => computeEstimate({ provider: "machgen", payload: { ...payload, ...overrides }, }); describe("MachGen image tasks", () => { it.each(MACHGEN_IMAGE_MODELS)( "accepts T2I and I2I for %s without video config", (model) => { expect(parse({ model }).success).toBe(true); expect( parse({ model, task_type: "I2I", src_image_urls: ["https://example.com/cat.jpg"], }).success ).toBe(true); expect( parse({ model, task_type: "I2I", src_task_ids: ["previous-image"] }) .success ).toBe(true); expect( parse({ model, task_type: "T2V", video_config: { height: 720, duration_secs: 3 }, }).success ).toBe(false); } ); it("requires the appropriate config and a source for edits", () => { expect(parse({ image_config: undefined }).success).toBe(false); expect(parse({ image_config: { width: 1280 } }).success).toBe(false); expect(parse({ image_config: { height: 0 } }).success).toBe(false); expect(parse({ task_type: "I2I" }).success).toBe(false); expect( parse({ task_type: "I2I", src_image_urls: ["data:image/png;base64,AA=="], }).success ).toBe(false); expect(parse({ model: "MiniMax-H3" }).success).toBe(false); expect(parse({ model: "MiniMax-H3", task_type: "T2V" }).success).toBe( false ); expect( parse({ model: "MiniMax-H3", task_type: "T2V", video_config: { height: 480, duration_secs: 1 }, }).success ).toBe(true); }); it("retains the model alias escape hatch for image tasks", () => { expect(parse({ model: "Future-Image-Model" }).success).toBe(true); }); it("pins each I2I source cap to MachGen's capability contract", () => { const evidence = JSON.parse( readFileSync( "tests/fixtures/machgen-evidence/machgen-image-caps-2026-11-01.json", "utf8" ) ) as { contract: Record }; expect(MACHGEN_I2I_MAX_IMAGES).toEqual({ ...Object.fromEntries( Object.entries({ ...evidence.contract, ...Object.fromEntries( tokenModels.map((id) => [id, tokenEvidence.contract.models[id]!]) ), }).map(([model, entry]) => [model, entry.i2i.maxInputImages]) ), // Registered 2026-11-20 from the Seedream 5.5 docs page. "Seedream-5.5": 10, }); expect(Object.keys(MACHGEN_I2I_MAX_IMAGES)).toEqual([ ...MACHGEN_IMAGE_MODELS, ]); }); it.each([ ["GPT-Image-2", 16], ["GPT-Image-2.5-Sunburst", 16], ["GPT-Image-2.5-Flare", 16], ["GPT-Image-2.6-Sunburst", 16], ["GPT-Image-2.6-Flare", 16], ["Nano-Banana-2", 14], ["Nano-Banana-2.1", 14], ["Seedream-5.5", 10], ])("caps %s I2I at %i source images", (model, cap) => { expect( issues({ model, task_type: "I2I", src_image_urls: sources(cap) }) ).toEqual([]); expect( issues({ model, task_type: "I2I", src_image_urls: sources(cap + 1) }) ).toEqual([ `src_image_urls: ${model} I2I accepts at most ${cap} source images; got ${cap + 1}`, ]); }); it("leaves T2I and image ids outside the table uncapped", () => { expect(issues({ src_image_urls: sources(17) })).toEqual([]); expect( issues({ model: "Future-Image-Model", task_type: "I2I", src_image_urls: sources(17), }) ).toEqual([]); }); it("prices Seedream 5.5 from the 2026-11-20 docs page", () => { const quote = (overrides: Record) => estimate({ model: "Seedream-5.5", optimization_level: undefined, image_config: { height: 1024 }, ...overrides, }); const one = quote({ task_type: "I2I", src_image_urls: ["https://example.com/cat.jpg"], }); expect(one.warnings).toEqual([]); expect(one.usd).toBeCloseTo(0.028, 6); expect(one.rateAsOf).toBe("2026-11-20"); expect( quote({ image_config: { width: 1024, height: 1024 } }).usd ).toBeCloseTo(0.02, 6); expect(quote({ image_config: { height: 1536 } }).usd).toBeCloseTo(0.02, 6); expect(quote({ image_config: { height: 2048 } }).usd).toBeCloseTo(0.035, 6); expect( quote({ image_config: { width: 2541, height: 1024 }, image_output: { layers: { max_images: 2 } }, }).usd ).toBeCloseTo(0.07, 6); expect( quote({ task_type: "I2I", src_image_urls: [ "https://example.com/a.jpg", "https://example.com/b.jpg", ], }).usd ).toBeCloseTo(0.036, 6); for (const [overrides, phrase] of [ [{ image_config: { size_mode: "auto" } }, "automatic image size"], [{ image_config: { width: 100, height: 100 } }, "pixel range"], [{ image_config: { height: 720 } }, "square preset"], [{ task_type: "I2I", src_image_urls: [] }, "I2I requires a source"], [{ src_task_ids: ["previous"] }, "src_task_ids"], [{ image_output: { layers: { count: 2 } } }, "layers.max_images"], ] as const) { const result = quote(overrides); expect(result.usd, phrase).toBe(0); expect(result.warnings.join(" "), phrase).toContain(phrase); } }); it("matches every captured image pricebook row for both task types", () => { const evidence = JSON.parse( readFileSync( "tests/fixtures/machgen-evidence/machgen-image-pricebook-2026-10-30.json", "utf8" ) ) as { prices: { model: string; width: number | null; height: number; optimization_level: string | null; unit_price_micros: number; }[]; }; expect(new Set(evidence.prices.map((row) => row.model))).toEqual( new Set( Object.entries(PRICING.machgen) .filter( ([id, row]) => id !== "Seedream-5.5" && row.kind === "perUnit" && row.unit === "images" ) .map(([id]) => id) ) ); for (const row of evidence.prices) { for (const task_type of ["T2I", "I2I"]) { const result = estimate({ model: row.model, task_type, image_config: { height: row.height, ...(row.width ? { width: row.width } : {}), }, optimization_level: row.optimization_level ?? undefined, }); expect(result.usd).toBeCloseTo(row.unit_price_micros / 1e6, 8); expect(result.warnings).toEqual([]); } } }); it("accepts all six quality settings, preserves additional output fields and rejects an unknown quality", () => { for (const quality of ["auto", "low", "medium", "high", "xhigh", "max"]) { const image_output = { quality, complexity: "medium", watermark: false }; expect( MachGenGenerateRequestSchema.parse({ ...payload, model: "GPT-Image-2.6-Sunburst", image_output, }).image_output ).toEqual(image_output); } expect(parse({ image_output: { quality: "lo" } }).success).toBe(false); }); it.each(tokenModels)("matches every output-token example for %s", (model) => { for (const row of tokenEvidence.examples) { const result = estimate({ model, task_type: row.task_type, image_config: { width: row.width, height: row.height }, image_output: { quality: row.quality }, ...(row.references ? { src_image_urls: sources(row.references) } : {}), }); expect(result.warnings, row.label).toEqual([]); expect(Math.round(result.usd * 1e6), row.label).toBe( row.discountedMicros ); } }); it.each(tokenModels)( "explains unresolved token estimates for %s", (model) => { const config = { width: 1280, height: 720 }; const settings = { quality: "low" }; const cases: [Record, string][] = [ [{ task_type: "R2V" }, "task_type"], [{ image_output: undefined }, "quality"], [{ image_output: { quality: "auto" } }, "quality"], [{ image_output: { quality: "other" } }, "quality"], [{ image_config: { ...config, size_mode: "auto" } }, "image_config"], ...[ { height: 720 }, { width: 1281, height: 720 }, { width: 1280, height: 720.5 }, { width: 4096, height: 2048 }, { width: 1280, height: 160 }, { width: 640, height: 640 }, { width: 3072, height: 3072 }, { width: -1024, height: -1024 }, ].map((image_config): [Record, string] => [ { image_config }, "image_config", ]), [{ image_output: { ...settings, output_format: "gif" } }, "GIF"], [ { image_output: { ...settings, partial_images: 1 } }, "partial_images", ], [{ continuation_task_id: "previous" }, "continuation_task_id"], [{ src_task_ids: ["previous"] }, "src_task_ids"], [{ task_type: "I2I" }, "src_image_urls"], [{ src_image_urls: sources(1) }, "src_image_urls"], ]; for (const [overrides, reason] of cases) { const result = estimate({ model, image_config: config, image_output: settings, ...overrides, }); expect(result.usd).toBe(0); expect(result.warnings).toEqual([expect.stringContaining(reason)]); } } ); it("defaults GPT Image 2 to high quality and declines unknown prices", () => { expect(estimate().usd).toBe(0.001385); expect(estimate({ optimization_level: undefined }).usd).toBe(0.048); for (const overrides of [ { optimization_level: "FAST" }, { image_config: { height: 544 } }, { image_config: { width: 1, height: 1 } }, { task_type: "T2V" }, ]) { expect(estimate(overrides).warnings.length).toBeGreaterThan(0); } }); });