diff --git a/test/client.test.ts b/test/client.test.ts
new file mode 100644
index 0000000..3cf2431
--- /dev/null
+++ b/test/client.test.ts
@@ -0,0 +1,230 @@
+import assert from "node:assert/strict";
+import { readFileSync } from "node:fs";
+import { after, before, describe, it } from "node:test";
+import { chromium, type Browser, type Page } from "playwright";
+import {
+ attachReferences, collectImageCandidates, downloadImage, ensureSession, firstAttached, firstVisible, imageSources, isLoggedIn,
+ lastAssistantText, raiseIfBlocked, sendMessage, waitForGeneratedImage, waitForTurnComplete, type Timing,
+} from "../src/chatgpt/client.js";
+import { ContentPolicyBlocked, ImageGenerationError, SessionNotReady, UsageLimitReached } from "../src/chatgpt/errors.js";
+import { makePng, pngDataUri } from "./helpers.js";
+
+const BIG_IMG = pngDataUri(300, 300);
+const SMALL_IMG = pngDataUri(32, 32);
+const fast: Timing = { pollIntervalMs: 50, turnStartTimeoutMs: 200, settleAfterTurnMs: 10, stablePolls: 2, textOnlyGraceMs: 300 };
+
+function fixture(name: string): string {
+ return readFileSync(new URL(`./fixtures/${name}`, import.meta.url), "utf8").replaceAll("{{BIG_IMG}}", BIG_IMG).replaceAll("{{SMALL_IMG}}", SMALL_IMG);
+}
+
+let browser: Browser | undefined;
+let skipReason: string | undefined;
+try {
+ browser = await chromium.launch({ headless: true });
+} catch (error) {
+ skipReason = `Chromium could not be launched (${(error as Error).message.split("\n")[0]}); set PLAYWRIGHT_BROWSERS_PATH to a Playwright browsers directory`;
+ console.warn(`SKIPPING test/client.test.ts: ${skipReason}`);
+}
+
+describe("chatgpt client (DOM fixtures)", { skip: skipReason }, () => {
+ let page: Page;
+ before(async () => { page = await (await browser!.newContext()).newPage(); });
+ after(async () => { await browser?.close(); });
+
+ const load = (html: string) => page.setContent(html, { waitUntil: "load" });
+
+ describe("session", () => {
+ it("isLoggedIn is false for a guest and true for a logged-in page", async () => {
+ await load(fixture("chatgpt-login.html"));
+ assert.equal(await isLoggedIn(page), false);
+ await load(fixture("chatgpt-new-chat.html"));
+ assert.equal(await isLoggedIn(page), true);
+ });
+ it("a composer alone does not count as logged in", async () => {
+ await load('
');
+ assert.equal(await isLoggedIn(page), false);
+ });
+ it("ensureSession throws SessionNotReady on the guest page only", async () => {
+ await load(fixture("chatgpt-login.html"));
+ await assert.rejects(ensureSession(page), SessionNotReady);
+ await load(fixture("chatgpt-new-chat.html"));
+ await assert.doesNotReject(ensureSession(page));
+ });
+ });
+
+ describe("locators", () => {
+ it("firstVisible skips missing, hidden and invalid selectors", async () => {
+ await load('h
s
');
+ const found = await firstVisible(page, ["#missing", "#hidden", "div[[invalid", ".shown"]);
+ assert.equal(await found?.getAttribute("id"), "second");
+ assert.equal(await firstVisible(page, ["#missing", "#hidden"]), undefined);
+ });
+ it("firstVisible waits for an element that appears later", async () => {
+ await load('');
+ assert.ok(await firstVisible(page, ["#late"], 2000));
+ });
+ it("firstAttached finds hidden elements", async () => {
+ await load(fixture("chatgpt-new-chat.html"));
+ assert.ok(await firstAttached(page, ["#nope", 'input[type="file"]']));
+ assert.equal(await firstAttached(page, ["#nope"]), undefined);
+ });
+ });
+
+ describe("images", () => {
+ it("collectImageCandidates marks images inside user messages", async () => {
+ await load(fixture("chatgpt-conversation-with-image.html"));
+ const candidates = await collectImageCandidates(page);
+ const reference = candidates.filter((candidate) => candidate.inUserMessage);
+ assert.equal(reference.length, 1);
+ assert.equal(reference[0].width, 32);
+ const generated = candidates.find((candidate) => candidate.width === 300);
+ assert.ok(generated);
+ assert.equal(generated.inUserMessage, false);
+ assert.equal(generated.complete, true);
+ });
+ it("imageSources returns the set of current srcs", async () => {
+ await load(fixture("chatgpt-conversation-with-image.html"));
+ const sources = await imageSources(page);
+ assert.ok(sources.has(BIG_IMG));
+ assert.ok(sources.has(SMALL_IMG));
+ });
+ it("downloadImage decodes data URIs", async () => {
+ const bytes = await downloadImage(page, BIG_IMG);
+ assert.ok(bytes.equals(makePng(300, 300)));
+ await assert.rejects(downloadImage(page, "data:image/png,not-base64"), ImageGenerationError);
+ });
+ it("downloadImage reads blob URLs in the page", async () => {
+ await load("blob
");
+ const png = makePng(300, 300);
+ const blobUrl = await page.evaluate((base64) => URL.createObjectURL(new Blob([Uint8Array.from(atob(base64), (c) => c.charCodeAt(0))], { type: "image/png" })), png.toString("base64"));
+ assert.ok((await downloadImage(page, blobUrl)).equals(png));
+ });
+ });
+
+ describe("waitForGeneratedImage", () => {
+ it("returns the assistant image src, ignoring the user's reference", async () => {
+ await load(fixture("chatgpt-conversation-with-image.html"));
+ assert.equal(await waitForGeneratedImage(page, new Set(), 5000, undefined, fast), BIG_IMG);
+ });
+ it("ignores images present in the baseline (text reply only)", async () => {
+ await load(fixture("chatgpt-conversation-with-image.html"));
+ await assert.rejects(waitForGeneratedImage(page, new Set([BIG_IMG]), 5000, undefined, fast), { code: "NO_IMAGE_RETURNED" });
+ });
+ it("times out while the image never finishes", async () => {
+ await load('Creating image
');
+ await assert.rejects(waitForGeneratedImage(page, new Set(), 600, undefined, fast), { code: "TURN_TIMED_OUT" });
+ });
+ it("waits while the image is still being created", async () => {
+ await load(`Creating image
+ `);
+ const started = Date.now();
+ assert.equal(await waitForGeneratedImage(page, new Set(), 5000, undefined, fast), BIG_IMG);
+ assert.ok(Date.now() - started >= 250);
+ });
+ it("throws ContentPolicyBlocked for a refusal without an image", async () => {
+ await load('I can’t help with that request because it may violate our content policy.
');
+ await assert.rejects(waitForGeneratedImage(page, new Set(), 5000, undefined, fast), ContentPolicyBlocked);
+ });
+ it("throws NO_IMAGE_RETURNED for an unrelated text reply", async () => {
+ await load('What colour should the fox be?
');
+ await assert.rejects(waitForGeneratedImage(page, new Set(), 5000, undefined, fast), { code: "NO_IMAGE_RETURNED" });
+ });
+ it("throws a generic error for a transient error reply", async () => {
+ await load('Something went wrong. Please try again.
');
+ await assert.rejects(waitForGeneratedImage(page, new Set(), 5000, undefined, fast), (error: ImageGenerationError) => {
+ assert.equal(error.code, "GENERATION_FAILED");
+ assert.match(error.message, /returned an error/);
+ return true;
+ });
+ });
+ });
+
+ describe("raiseIfBlocked / lastAssistantText", () => {
+ it("lastAssistantText returns the last assistant message", async () => {
+ await load('first
mine
second
');
+ assert.equal(await lastAssistantText(page), "second");
+ await load("nothing
");
+ assert.equal(await lastAssistantText(page), "");
+ });
+ it("throws UsageLimitReached from the assistant message", async () => {
+ await load("You've hit the plus plan limit for image generations. Try again later.
");
+ await assert.rejects(raiseIfBlocked(page), UsageLimitReached);
+ });
+ it("throws UsageLimitReached from an alert banner", async () => {
+ await load('You’ve reached our limit of messages
');
+ await assert.rejects(raiseIfBlocked(page), UsageLimitReached);
+ });
+ it("ignores usage-limit words in the user's own prompt", async () => {
+ await load('draw a usage limit sign
Sure.
');
+ await assert.doesNotReject(raiseIfBlocked(page));
+ });
+ });
+
+ describe("waitForTurnComplete", () => {
+ it("resolves after the stop button disappears", async () => {
+ await load(`
+ `);
+ const started = Date.now();
+ await waitForTurnComplete(page, 5000, undefined, fast);
+ assert.ok(Date.now() - started >= 350, "returned before the stop button was removed");
+ });
+ it("resolves quickly when no turn ever starts", async () => {
+ await load("idle
");
+ const started = Date.now();
+ await waitForTurnComplete(page, 5000, undefined, fast);
+ assert.ok(Date.now() - started < 1500);
+ });
+ it("times out while the stop button stays", async () => {
+ await load('');
+ await assert.rejects(waitForTurnComplete(page, 400, undefined, fast), { code: "TURN_TIMED_OUT" });
+ });
+ it("surfaces a usage limit shown during the turn", async () => {
+ await load("You've hit the plus plan limit
");
+ await assert.rejects(waitForTurnComplete(page, 2000, undefined, fast), UsageLimitReached);
+ });
+ });
+
+ describe("composer", () => {
+ it("sendMessage fills the contenteditable and clicks send", async () => {
+ await load(fixture("chatgpt-new-chat.html"));
+ await sendMessage(page, "Draw a fox\nwith two lines");
+ const turns = await page.locator('[data-message-author-role="user"]').allInnerTexts();
+ assert.equal(turns.length, 1);
+ assert.match(turns[0], /Draw a fox\s+with two lines/);
+ assert.equal((await page.locator("#prompt-textarea").innerText()).trim(), "", "composer cleared by the fixture's send handler");
+ });
+ it("sendMessage falls back to Enter without a send button", async () => {
+ await load(`
+ `);
+ await sendMessage(page, "hello");
+ assert.equal(await page.locator("#log").innerText(), "enter:hello");
+ });
+ });
+
+ describe("attachReferences", () => {
+ it("is a no-op without references", async () => {
+ await load("nothing
");
+ await assert.doesNotReject(attachReferences(page, [], undefined, fast));
+ });
+ it("sets the files on the input", async () => {
+ await load(fixture("chatgpt-new-chat.html"));
+ const png = makePng(8, 8);
+ await attachReferences(page, [
+ { base64: png.toString("base64"), mime_type: "image/png" },
+ { base64: png.toString("base64"), mime_type: "image/jpeg" },
+ ], undefined, fast);
+ assert.equal(await page.locator("#files").evaluate((input) => (input as HTMLInputElement).files!.length), 2);
+ const previews = await page.locator(".preview").allInnerTexts();
+ assert.deepEqual(previews, [`reference-1.png:image/png:${png.length}`, `reference-2.jpg:image/jpeg:${png.length}`]);
+ });
+ it("throws UI_CHANGED without a file input", async () => {
+ await load("no input
");
+ await assert.rejects(
+ attachReferences(page, [{ base64: makePng(8, 8).toString("base64"), mime_type: "image/png" }], undefined, fast),
+ { code: "UI_CHANGED" },
+ );
+ });
+ });
+});
diff --git a/test/fixtures/chatgpt-conversation-with-image.html b/test/fixtures/chatgpt-conversation-with-image.html
new file mode 100644
index 0000000..6779f86
--- /dev/null
+++ b/test/fixtures/chatgpt-conversation-with-image.html
@@ -0,0 +1,22 @@
+
+ChatGPT (conversation)
+
+
+
+
+
+
+ Make it look like this.
+

+
+
+
+
+ Here is your image.
+

+
+
+
+
+
+
diff --git a/test/fixtures/chatgpt-login.html b/test/fixtures/chatgpt-login.html
new file mode 100644
index 0000000..74d0824
--- /dev/null
+++ b/test/fixtures/chatgpt-login.html
@@ -0,0 +1,9 @@
+
+ChatGPT (guest)
+
+
+
+
+
+
+
diff --git a/test/fixtures/chatgpt-new-chat.html b/test/fixtures/chatgpt-new-chat.html
new file mode 100644
index 0000000..885da3f
--- /dev/null
+++ b/test/fixtures/chatgpt-new-chat.html
@@ -0,0 +1,32 @@
+
+ChatGPT (new chat)
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/test/helpers.ts b/test/helpers.ts
new file mode 100644
index 0000000..4795308
--- /dev/null
+++ b/test/helpers.ts
@@ -0,0 +1,44 @@
+import { deflateSync } from "node:zlib";
+
+const CRC_TABLE = Array.from({ length: 256 }, (_, n) => {
+ let c = n;
+ for (let k = 0; k < 8; k += 1) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
+ return c >>> 0;
+});
+
+export function crc32(data: Buffer): number {
+ let c = 0xffffffff;
+ for (const byte of data) c = CRC_TABLE[(c ^ byte) & 0xff] ^ (c >>> 8);
+ return (c ^ 0xffffffff) >>> 0;
+}
+
+function chunk(type: string, data: Buffer): Buffer {
+ const body = Buffer.concat([Buffer.from(type, "ascii"), data]);
+ const length = Buffer.alloc(4);
+ length.writeUInt32BE(data.length);
+ const crc = Buffer.alloc(4);
+ crc.writeUInt32BE(crc32(body));
+ return Buffer.concat([length, body, crc]);
+}
+
+/** Minimal valid 8-bit RGB PNG filled with one colour. */
+export function makePng(width: number, height: number, rgb: [number, number, number] = [200, 60, 60]): Buffer {
+ const header = Buffer.alloc(13);
+ header.writeUInt32BE(width, 0);
+ header.writeUInt32BE(height, 4);
+ header[8] = 8; // bit depth
+ header[9] = 2; // colour type: RGB
+ const row = Buffer.alloc(1 + width * 3);
+ for (let x = 0; x < width; x += 1) row.set(rgb, 1 + x * 3);
+ const raw = Buffer.concat(Array.from({ length: height }, () => row));
+ return Buffer.concat([
+ Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
+ chunk("IHDR", header),
+ chunk("IDAT", deflateSync(raw)),
+ chunk("IEND", Buffer.alloc(0)),
+ ]);
+}
+
+export function pngDataUri(width: number, height: number): string {
+ return `data:image/png;base64,${makePng(width, height).toString("base64")}`;
+}
diff --git a/test/unit.test.ts b/test/unit.test.ts
new file mode 100644
index 0000000..bbe8768
--- /dev/null
+++ b/test/unit.test.ts
@@ -0,0 +1,413 @@
+import assert from "node:assert/strict";
+import { randomUUID } from "node:crypto";
+import { after, before, describe, it } from "node:test";
+import { normalizeArguments, resolveModel } from "../src/arguments.js";
+import {
+ containsAny, decodeDataUri, isProbablyImageUrl, pickGeneratedImage, type ImageCandidate,
+} from "../src/chatgpt/client.js";
+import { ContentPolicyBlocked, ImageGenerationError, SessionNotReady, TurnTimedOut, UsageLimitReached } from "../src/chatgpt/errors.js";
+import type { ImageProvider } from "../src/chatgpt/provider.js";
+import { buildImageRequest } from "../src/chatgpt/prompts.js";
+import { extensionFor, imageDimensions, sniffImageMime } from "../src/image.js";
+import { JobManager, toJobError, type JobManagerOptions } from "../src/jobs.js";
+import { ensureMediaDir, loadPersistedJob, outputPayload, storeImage } from "../src/media.js";
+import type { GenerationJob, ImageRequest, StoredOutput } from "../src/types.js";
+import { makePng } from "./helpers.js";
+
+before(async () => { await ensureMediaDir(); });
+
+// Real images generated with Pillow: JPEG 13x7, lossy WebP 11x5, lossless WebP 9x6.
+const JPEG_13x7 = Buffer.from("/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAHAA0DASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDkKKKK8U/TD//Z", "base64");
+const WEBP_LOSSY_11x5 = Buffer.from("UklGRjoAAABXRUJQVlA4IC4AAACwAQCdASoLAAUAAUAmJaACdLoABDAAAP7x3I/4DdfFtMv/vYL/3YL/3YL/WwAA", "base64");
+const WEBP_LOSSLESS_9x6 = Buffer.from("UklGRh4AAABXRUJQVlA4TBEAAAAvCEABAAdQjyLXo/+BiOh/AAA=", "base64");
+
+describe("image", () => {
+ it("sniffs and measures a PNG", () => {
+ const png = makePng(17, 9);
+ assert.equal(sniffImageMime(png), "image/png");
+ assert.deepEqual(imageDimensions(png), { width: 17, height: 9 });
+ });
+ it("sniffs and measures a JPEG", () => {
+ assert.equal(sniffImageMime(JPEG_13x7), "image/jpeg");
+ assert.deepEqual(imageDimensions(JPEG_13x7), { width: 13, height: 7 });
+ });
+ it("sniffs and measures WebP (lossy and lossless)", () => {
+ assert.equal(sniffImageMime(WEBP_LOSSY_11x5), "image/webp");
+ assert.deepEqual(imageDimensions(WEBP_LOSSY_11x5), { width: 11, height: 5 });
+ assert.equal(sniffImageMime(WEBP_LOSSLESS_9x6), "image/webp");
+ assert.deepEqual(imageDimensions(WEBP_LOSSLESS_9x6), { width: 9, height: 6 });
+ });
+ it("rejects non-images and truncated data", () => {
+ assert.equal(sniffImageMime(Buffer.from("hello world, not an image")), undefined);
+ assert.equal(sniffImageMime(Buffer.alloc(0)), undefined);
+ assert.equal(imageDimensions(Buffer.from("GIF89a....")), undefined);
+ assert.equal(imageDimensions(makePng(4, 4).subarray(0, 12)), undefined);
+ });
+ it("maps mime types to extensions", () => {
+ assert.equal(extensionFor("image/png"), "png");
+ assert.equal(extensionFor("image/jpeg"), "jpg");
+ assert.equal(extensionFor("image/webp"), "webp");
+ });
+});
+
+describe("resolveModel", () => {
+ it("accepts the name and aliases case-insensitively", () => {
+ assert.equal(resolveModel("chatgpt-web"), "chatgpt-web");
+ assert.equal(resolveModel(" GPT-Image "), "chatgpt-web");
+ assert.equal(resolveModel("chatgpt"), "chatgpt-web");
+ assert.equal(resolveModel("chatgpt-image"), "chatgpt-web");
+ });
+ it("rejects unknown models", () => {
+ assert.throws(() => resolveModel("dall-e-3"), /Unknown model 'dall-e-3'/);
+ });
+});
+
+describe("normalizeArguments", () => {
+ const prompt = { name: "prompt", value: "a red fox" };
+ const pngB64 = makePng(4, 4).toString("base64");
+
+ it("applies defaults", () => {
+ assert.deepEqual(normalizeArguments([prompt]), { prompt: "a red fox", aspect_ratio: "auto", image_count: 1 });
+ });
+ it("accepts explicit values", () => {
+ const result = normalizeArguments([prompt, { name: "aspect_ratio", value: "16:9" }, { name: "image_count", value: 4 }]);
+ assert.equal(result.aspect_ratio, "16:9");
+ assert.equal(result.image_count, 4);
+ });
+ it("rejects bad input", () => {
+ assert.throws(() => normalizeArguments([prompt, { name: "bogus", value: 1 }]), /Unknown argument 'bogus'/);
+ assert.throws(() => normalizeArguments([prompt, prompt]), /Duplicate argument 'prompt'/);
+ assert.throws(() => normalizeArguments([]), /Missing required argument 'prompt'/);
+ assert.throws(() => normalizeArguments([{ name: "prompt", value: " " }]), /Missing required argument 'prompt'/);
+ assert.throws(() => normalizeArguments([{ name: "prompt", value: 42 }]), /Missing required argument 'prompt'/);
+ assert.throws(() => normalizeArguments([{ name: "prompt", value: "x".repeat(8001) }]), /at most 8000/);
+ assert.doesNotThrow(() => normalizeArguments([{ name: "prompt", value: "x".repeat(8000) }]));
+ assert.throws(() => normalizeArguments([prompt, { name: "aspect_ratio", value: "7:3" }]), /Invalid 'aspect_ratio'/);
+ assert.throws(() => normalizeArguments([prompt, { name: "aspect_ratio", value: 1 }]), /Invalid 'aspect_ratio'/);
+ for (const bad of [0, 5, 1.5, "2", -1]) {
+ assert.throws(() => normalizeArguments([prompt, { name: "image_count", value: bad }]), /image_count/, `image_count ${String(bad)}`);
+ }
+ });
+
+ describe("reference_images", () => {
+ const withRefs = (value: unknown) => normalizeArguments([prompt, { name: "reference_images", value }]).reference_images;
+
+ it("accepts inline base64 with mime_type", () => {
+ assert.deepEqual(withRefs([{ base64: pngB64, mime_type: "image/png" }]), [{ base64: pngB64, mime_type: "image/png" }]);
+ });
+ it("accepts a data URI and derives the mime type", () => {
+ assert.deepEqual(withRefs([{ base64: `data:image/png;base64,${pngB64}` }]), [{ base64: pngB64, mime_type: "image/png" }]);
+ });
+ it("rejects a mime type that does not match the content", () => {
+ assert.throws(() => withRefs([{ base64: pngB64, mime_type: "image/jpeg" }]), /does not match image\/jpeg/);
+ assert.throws(() => withRefs([{ base64: `data:image/webp;base64,${pngB64}` }]), /does not match image\/webp/);
+ });
+ it("rejects invalid base64, missing or unsupported mime type", () => {
+ assert.throws(() => withRefs([{ base64: "not base64!!", mime_type: "image/png" }]), /not valid standard base64/);
+ assert.throws(() => withRefs([{ base64: pngB64.slice(1), mime_type: "image/png" }]), /not valid standard base64/);
+ assert.throws(() => withRefs([{ base64: pngB64 }]), /mime_type must be/);
+ assert.throws(() => withRefs([{ base64: pngB64, mime_type: "image/gif" }]), /mime_type must be/);
+ assert.throws(() => withRefs([{ base64: "" , mime_type: "image/png" }]), /non-empty string/);
+ });
+ it("accepts generation_id references with default index 0", () => {
+ const id = randomUUID();
+ assert.deepEqual(withRefs([{ generation_id: id }, { generation_id: id, index: 2 }]), [
+ { generation_id: id, index: 0 }, { generation_id: id, index: 2 },
+ ]);
+ });
+ it("rejects bad generation references and shapes", () => {
+ assert.throws(() => withRefs([{ generation_id: "nope" }]), /valid UUID/);
+ assert.throws(() => withRefs([{ generation_id: randomUUID(), index: -1 }]), /non-negative integer/);
+ assert.throws(() => withRefs([{ generation_id: randomUUID(), base64: pngB64 }]), /not both/);
+ assert.throws(() => withRefs([]), /1 to 4/);
+ assert.throws(() => withRefs("x"), /1 to 4/);
+ assert.throws(() => withRefs(Array.from({ length: 5 }, () => ({ base64: pngB64, mime_type: "image/png" }))), /1 to 4/);
+ assert.throws(() => withRefs([null]), /must be an object/);
+ });
+ });
+});
+
+describe("buildImageRequest", () => {
+ it("omits the aspect ratio for auto", () => {
+ const text = buildImageRequest("a cat", "auto", 0);
+ assert.doesNotMatch(text, /aspect ratio/);
+ });
+ it("mentions the aspect ratio otherwise", () => {
+ const text = buildImageRequest("a cat", "9:16", 0);
+ assert.match(text, /9:16 aspect ratio \(vertical format\)/);
+ });
+ it("phrases reference counts", () => {
+ assert.doesNotMatch(buildImageRequest("a cat", "auto", 0), /attached/);
+ assert.match(buildImageRequest("a cat", "auto", 1), /Use the attached image as the reference/);
+ assert.match(buildImageRequest("a cat", "auto", 3), /Use the 3 attached images as the references/);
+ });
+ it("keeps the prompt verbatim at the end (trimmed)", () => {
+ const prompt = "line one\n\n line two with \"quotes\" and 9:16 inside";
+ assert.ok(buildImageRequest(` ${prompt}\n`, "1:1", 2).endsWith(`\n\n${prompt}`));
+ });
+});
+
+describe("client pure helpers", () => {
+ it("containsAny is case-insensitive and normalises curly apostrophes", () => {
+ assert.equal(containsAny("You’ve HIT the Plus Plan Limit", ["you've hit the plus plan limit"]), true);
+ assert.equal(containsAny("You`ve hit the limit", ["you've hit the limit"]), true);
+ assert.equal(containsAny("all good", ["usage limit"]), false);
+ assert.equal(containsAny(undefined, ["usage limit"]), false);
+ assert.equal(containsAny("anything", []), false);
+ });
+
+ it("isProbablyImageUrl", () => {
+ assert.equal(isProbablyImageUrl(""), false);
+ assert.equal(isProbablyImageUrl("data:image/svg+xml;base64,AAAA"), false);
+ assert.equal(isProbablyImageUrl("data:image/png;base64,AAAA"), true);
+ assert.equal(isProbablyImageUrl("blob:https://chatgpt.com/1234"), true);
+ assert.equal(isProbablyImageUrl("https://files.oaiusercontent.com/file-abc"), true);
+ assert.equal(isProbablyImageUrl("https://chatgpt.com/avatar/me.png"), false);
+ assert.equal(isProbablyImageUrl("https://example.com/profile-picture/1"), false);
+ assert.equal(isProbablyImageUrl("https://example.com/logo.svg"), false);
+ assert.equal(isProbablyImageUrl("https://example.com/favicon.ico"), false);
+ assert.equal(isProbablyImageUrl("ftp://example.com/a.png"), false);
+ assert.equal(isProbablyImageUrl("/relative/a.png"), false);
+ });
+
+ it("decodeDataUri", () => {
+ const png = makePng(2, 2);
+ assert.ok(decodeDataUri(`data:image/png;base64,${png.toString("base64")}`).equals(png));
+ assert.throws(() => decodeDataUri("data:image/png;base64"), /Malformed/);
+ assert.throws(() => decodeDataUri("data:image/png,abcd"), /not base64/);
+ });
+
+ it("pickGeneratedImage excludes user, baseline and small images and picks the last eligible", () => {
+ const make = (src: string, extra: Partial = {}): ImageCandidate => ({
+ src, width: 1024, height: 1024, complete: true, inUserMessage: false, ...extra,
+ });
+ const candidates = [
+ make("https://x.test/user.png", { inUserMessage: true }),
+ make("https://x.test/old.png"),
+ make("https://x.test/small.png", { width: 255 }),
+ make("https://x.test/flat.png", { height: 100 }),
+ make("https://x.test/first.png"),
+ make("https://x.test/avatar/me.png"),
+ make("https://x.test/last.png"),
+ ];
+ assert.equal(pickGeneratedImage(candidates, new Set(["https://x.test/old.png"]))?.src, "https://x.test/last.png");
+ assert.equal(pickGeneratedImage(candidates.slice(0, 4), new Set(["https://x.test/old.png"])), undefined);
+ assert.equal(pickGeneratedImage([make("a", { width: 256, height: 256 })].map((c) => ({ ...c, src: "https://x.test/edge.png" })), new Set())?.src, "https://x.test/edge.png");
+ });
+});
+
+describe("toJobError", () => {
+ it("maps ImageGenerationError codes and retryability", () => {
+ assert.deepEqual(toJobError(new UsageLimitReached("limit")), { code: "USAGE_LIMIT_REACHED", message: "limit", retryable: true });
+ assert.deepEqual(toJobError(new TurnTimedOut("slow")), { code: "TURN_TIMED_OUT", message: "slow", retryable: true });
+ assert.equal(toJobError(new SessionNotReady()).code, "SESSION_NOT_READY");
+ assert.equal(toJobError(new SessionNotReady()).retryable, false);
+ assert.deepEqual(toJobError(new ContentPolicyBlocked("no")), { code: "CONTENT_POLICY_BLOCKED", message: "no", retryable: false });
+ assert.deepEqual(toJobError(new ImageGenerationError("x", "NO_IMAGE_RETURNED")), { code: "NO_IMAGE_RETURNED", message: "x", retryable: true });
+ assert.deepEqual(toJobError(new ImageGenerationError("x", "UI_CHANGED")), { code: "UI_CHANGED", message: "x", retryable: false });
+ assert.deepEqual(toJobError(new ImageGenerationError("x")), { code: "GENERATION_FAILED", message: "x", retryable: true });
+ });
+ it("maps generic errors", () => {
+ assert.equal(toJobError(new Error("page.goto: Timeout 60000ms exceeded")).code, "BROWSER_ERROR");
+ assert.equal(toJobError(new Error("net::ERR_CONNECTION_RESET")).code, "BROWSER_ERROR");
+ assert.equal(toJobError(new Error("Executable doesn't exist at /x")).code, "SERVER_NOT_CONFIGURED");
+ assert.equal(toJobError(new Error("ProcessSingleton lock")).retryable, false);
+ assert.deepEqual(toJobError("boom"), { code: "GENERATION_FAILED", message: "boom", retryable: false });
+ assert.equal(toJobError(new Error("y".repeat(5000))).message.length, 2000);
+ });
+});
+
+describe("JobManager", () => {
+ const options: JobManagerOptions = { minIntervalMs: 0, maxIntervalMs: 0, usageLimitCooldownMs: 60000, sessionRecheckMs: 60000, maxQueueLength: 2 };
+ const png = makePng(32, 32);
+ const args = (extra: Record = {}) => ({ prompt: "p", aspect_ratio: "auto", image_count: 1, ...extra });
+
+ class FakeProvider implements ImageProvider {
+ calls: Array<{ generationId: string; request: ImageRequest }> = [];
+ active = 0;
+ maxActive = 0;
+ sessionResult = true;
+ gate?: Promise;
+ behaviour: (call: number) => Promise | void = () => undefined;
+
+ async generateImages(generationId: string, request: ImageRequest): Promise {
+ this.active += 1;
+ this.maxActive = Math.max(this.maxActive, this.active);
+ try {
+ const call = this.calls.push({ generationId, request });
+ await this.gate;
+ await new Promise((resolve) => setTimeout(resolve, 5));
+ await this.behaviour(call);
+ const outputs: StoredOutput[] = [];
+ for (let index = 0; index < request.imageCount; index += 1) outputs.push(await storeImage(generationId, index, png));
+ return outputs;
+ } finally {
+ this.active -= 1;
+ }
+ }
+ async checkSession(): Promise {
+ assert.equal(this.active, 0, "checkSession ran during a generation");
+ return this.sessionResult;
+ }
+ async close(): Promise {}
+ }
+
+ async function settled(manager: JobManager, id: string): Promise {
+ const deadline = Date.now() + 5000;
+ for (;;) {
+ const job = manager.get(id);
+ assert.ok(job, "job exists");
+ if (job.status !== "QUEUING" && job.status !== "RUNNING") return job;
+ assert.ok(Date.now() < deadline, `job stuck in ${job.status}`);
+ await new Promise((resolve) => setTimeout(resolve, 10));
+ }
+ }
+
+ it("completes a job and persists a reloadable manifest", async () => {
+ const provider = new FakeProvider();
+ const manager = new JobManager(provider, options);
+ const queued = await manager.submit("chatgpt-web", args({ image_count: 2 }), "trace-1");
+ assert.equal(queued.status, "QUEUING");
+ const job = await settled(manager, queued.generationId);
+ assert.equal(job.status, "COMPLETED");
+ assert.equal(job.taskTraceId, "trace-1");
+ assert.equal(job.outputs?.length, 2);
+ assert.equal(job.request, undefined);
+ assert.equal(provider.calls[0].request.imageCount, 2);
+ const reloaded = await loadPersistedJob(job.generationId);
+ assert.ok(reloaded);
+ assert.equal(reloaded.status, "COMPLETED");
+ assert.equal(reloaded.taskTraceId, "trace-1");
+ assert.deepEqual(reloaded.outputs?.map((o) => [o.index, o.filename, o.mimeType, o.width, o.height]), job.outputs?.map((o) => [o.index, o.filename, "image/png", 32, 32]));
+ assert.equal(manager.status().running, false);
+ });
+
+ it("runs generations strictly one at a time and in order", async () => {
+ const provider = new FakeProvider();
+ const manager = new JobManager(provider, { ...options, maxQueueLength: 5 });
+ const jobs = await Promise.all([1, 2, 3].map(() => manager.submit("chatgpt-web", args())));
+ assert.equal(manager.get(jobs[2].generationId)?.queuePosition, 3);
+ const done = await Promise.all(jobs.map((job) => settled(manager, job.generationId)));
+ assert.deepEqual(done.map((job) => job.status), ["COMPLETED", "COMPLETED", "COMPLETED"]);
+ assert.equal(provider.maxActive, 1);
+ assert.deepEqual(provider.calls.map((call) => call.generationId), jobs.map((job) => job.generationId));
+ });
+
+ it("opens the circuit on a usage limit, failing queued jobs and rejecting new submits", async () => {
+ const provider = new FakeProvider();
+ provider.behaviour = () => { throw new UsageLimitReached("You've hit the plus plan limit"); };
+ const manager = new JobManager(provider, options);
+ const [a, b] = await Promise.all([manager.submit("chatgpt-web", args()), manager.submit("chatgpt-web", args())]);
+ const [first, second] = await Promise.all([settled(manager, a.generationId), settled(manager, b.generationId)]);
+ assert.equal(first.status, "FAILED");
+ assert.equal(first.error?.code, "USAGE_LIMIT_REACHED");
+ assert.equal(first.error?.retryable, true);
+ assert.equal(second.status, "FAILED");
+ assert.equal(second.error?.code, "USAGE_LIMIT_REACHED");
+ assert.ok(second.error?.retryAfter);
+ assert.equal(provider.calls.length, 1, "second job never reached the provider");
+ await assert.rejects(manager.submit("chatgpt-web", args()), /^Error: USAGE_LIMIT_REACHED:/);
+ assert.equal((manager.status().circuit as { reason: string }).reason, "usage_limit");
+ });
+
+ it("maps SessionNotReady to SESSION_NOT_READY and opens a session circuit", async () => {
+ const provider = new FakeProvider();
+ provider.behaviour = () => { throw new SessionNotReady(); };
+ const manager = new JobManager(provider, options);
+ const job = await settled(manager, (await manager.submit("chatgpt-web", args())).generationId);
+ assert.equal(job.status, "FAILED");
+ assert.equal(job.error?.code, "SESSION_NOT_READY");
+ assert.equal(job.error?.retryable, false);
+ await assert.rejects(manager.submit("chatgpt-web", args()), /^Error: SESSION_NOT_READY:/);
+ assert.deepEqual((manager.status().session as { loggedIn?: boolean }).loggedIn, false);
+ // A successful session check closes the session circuit.
+ assert.equal(await manager.checkSession(), true);
+ await assert.doesNotReject(manager.submit("chatgpt-web", args()));
+ });
+
+ it("records ordinary failures without opening the circuit", async () => {
+ const provider = new FakeProvider();
+ provider.behaviour = (call) => { if (call === 1) throw new ContentPolicyBlocked("refused"); };
+ const manager = new JobManager(provider, options);
+ const a = await settled(manager, (await manager.submit("chatgpt-web", args())).generationId);
+ assert.equal(a.error?.code, "CONTENT_POLICY_BLOCKED");
+ const b = await settled(manager, (await manager.submit("chatgpt-web", args())).generationId);
+ assert.equal(b.status, "COMPLETED");
+ });
+
+ it("rejects with QUEUE_FULL when too many generations wait", async () => {
+ let release!: () => void;
+ const provider = new FakeProvider();
+ provider.gate = new Promise((resolve) => { release = resolve; });
+ const manager = new JobManager(provider, options);
+ const a = await manager.submit("chatgpt-web", args());
+ const b = await manager.submit("chatgpt-web", args());
+ await assert.rejects(manager.submit("chatgpt-web", args()), /^Error: QUEUE_FULL:/);
+ release();
+ await settled(manager, a.generationId);
+ await settled(manager, b.generationId);
+ });
+
+ it("resolves generation_id references to base64 for the provider", async () => {
+ const provider = new FakeProvider();
+ const manager = new JobManager(provider, options);
+ const source = await settled(manager, (await manager.submit("chatgpt-web", args())).generationId);
+ const derived = await manager.submit("chatgpt-web", args({ reference_images: [{ generation_id: source.generationId, index: 0 }] }));
+ await settled(manager, derived.generationId);
+ const references = provider.calls[1].request.references;
+ assert.equal(references.length, 1);
+ assert.equal(references[0].mime_type, "image/png");
+ assert.ok(Buffer.from(references[0].base64, "base64").equals(png));
+ });
+
+ it("rejects bad generation references", async () => {
+ const manager = new JobManager(new FakeProvider(), options);
+ await assert.rejects(manager.submit("chatgpt-web", args({ reference_images: [{ generation_id: randomUUID(), index: 0 }] })), /^Error: REFERENCE_GENERATION_NOT_FOUND:/);
+ const source = await settled(manager, (await manager.submit("chatgpt-web", args())).generationId);
+ await assert.rejects(manager.submit("chatgpt-web", args({ reference_images: [{ generation_id: source.generationId, index: 3 }] })), /^Error: REFERENCE_INDEX_OUT_OF_RANGE:/);
+ });
+
+ it("passes inline references through untouched", async () => {
+ const provider = new FakeProvider();
+ const manager = new JobManager(provider, options);
+ const inline = { base64: png.toString("base64"), mime_type: "image/png" as const };
+ await settled(manager, (await manager.submit("chatgpt-web", args({ reference_images: [inline] }))).generationId);
+ assert.deepEqual(provider.calls[0].request.references, [inline]);
+ });
+
+ it("checkSession goes through the queue, after a running generation", async () => {
+ const provider = new FakeProvider();
+ const manager = new JobManager(provider, options);
+ const job = await manager.submit("chatgpt-web", args());
+ provider.sessionResult = false;
+ assert.equal(await manager.checkSession(), false); // asserts inside the fake that nothing was active
+ assert.equal(manager.get(job.generationId)?.status, "COMPLETED");
+ assert.equal((manager.status().session as { loggedIn?: boolean }).loggedIn, false);
+ });
+});
+
+describe("outputPayload", () => {
+ const secret = "s".repeat(40);
+ let previous: string | undefined;
+ before(() => { previous = process.env.PUBLIC_MEDIA_SIGNING_SECRET; process.env.PUBLIC_MEDIA_SIGNING_SECRET = secret; });
+ after(() => { if (previous === undefined) delete process.env.PUBLIC_MEDIA_SIGNING_SECRET; else process.env.PUBLIC_MEDIA_SIGNING_SECRET = previous; });
+
+ it("builds signed and direct URLs and includes base64 only on request", async () => {
+ const png = makePng(20, 10);
+ const id = randomUUID();
+ const stored = await storeImage(id, 0, png);
+ const [plain] = await outputPayload([stored], "https://mcp.example.test/");
+ assert.equal(plain.index, 0);
+ assert.equal(plain.mimeType, "image/png");
+ assert.equal(plain.sizeBytes, png.length);
+ assert.equal(plain.width, 20);
+ assert.equal(plain.height, 10);
+ assert.equal(plain.downloadUrl, `https://mcp.example.test/media/${id}-0.png`);
+ assert.match(String(plain.publicUrl), new RegExp(`^https://mcp\\.example\\.test/public-media/${id}-0\\.png\\?expires=\\d{10}&signature=[A-Za-z0-9_-]{43}$`));
+ assert.ok(!Number.isNaN(Date.parse(String(plain.publicUrlExpiresAt))));
+ assert.equal("base64" in plain, false);
+
+ const [inline] = await outputPayload([stored], "https://mcp.example.test", true);
+ assert.equal(inline.base64, png.toString("base64"));
+ });
+});