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 }); }); });