From 9d6f4ac24bd72d05ce2d2fd3978775c92bd10ecd Mon Sep 17 00:00:00 2001 From: suyx Date: Tue, 4 Aug 2026 17:15:25 +0800 Subject: [PATCH] test(wp7-02): require exact image normalization --- .../wp7-02-controlled-executor.test.mjs | 2 +- .../wp7-02-image-output-normalizer.test.ts | 42 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 tests/unit/wp7-02-image-output-normalizer.test.ts diff --git a/tests/package/wp7-02-controlled-executor.test.mjs b/tests/package/wp7-02-controlled-executor.test.mjs index 75c65a9..baa438b 100644 --- a/tests/package/wp7-02-controlled-executor.test.mjs +++ b/tests/package/wp7-02-controlled-executor.test.mjs @@ -93,7 +93,7 @@ test("TDD-WP7-EXT-001 builds protocol-specific requests without auth in argument model: "gpt-image-2", prompt: "controlled fixture prompt", response_format: "b64_json", - size: "1080x1920", + size: "1008x1792", }); assert.equal("authorization" in openai.headers, false); }); diff --git a/tests/unit/wp7-02-image-output-normalizer.test.ts b/tests/unit/wp7-02-image-output-normalizer.test.ts new file mode 100644 index 0000000..3a5ddc8 --- /dev/null +++ b/tests/unit/wp7-02-image-output-normalizer.test.ts @@ -0,0 +1,42 @@ +import { describe, expect, it } from "vitest"; + +import { + gptImageRequestSizeForRatio, + normalizeImageOutputToRatio, + productDimensionsForRatio, +} from "../../apps/worker/src/image-output-normalizer.js"; + +const onePixelPng = Buffer.from( + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=", + "base64", +); + +describe("TDD-WP7-EXT-001 exact image output normalization", () => { + it("uses only GPT Image 2 request sizes allowed by the upstream API", () => { + expect(["3:4", "1:1", "4:3", "9:16"].map((ratio) => gptImageRequestSizeForRatio(ratio))).toEqual([ + "1056x1408", + "1088x1088", + "1408x1056", + "1008x1792", + ]); + for (const ratio of ["3:4", "1:1", "4:3", "9:16"] as const) { + const [width, height] = gptImageRequestSizeForRatio(ratio).split("x").map(Number); + expect(width % 16).toBe(0); + expect(height % 16).toBe(0); + } + }); + + it("normalizes provider output to the frozen product dimensions", async () => { + const output = await normalizeImageOutputToRatio({ bytes: onePixelPng, mimeType: "image/png", ratio: "1:1" }); + expect(output).toMatchObject({ + mimeType: "image/png", + normalized: true, + pixelHeight: 1080, + pixelWidth: 1080, + upstreamPixelHeight: 1, + upstreamPixelWidth: 1, + }); + expect(output.bytes.subarray(0, 8).toString("hex")).toBe("89504e470d0a1a0a"); + expect(productDimensionsForRatio("9:16")).toEqual({ pixelHeight: 1920, pixelWidth: 1080 }); + }); +});