");
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('
");
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("