import { expect, test } from "@playwright/test"; import { completedDetailReplacementCardIds, detailReplacementCardMessageId, detailReplacementSubmissionKey, isDetailReplacementBatchRequest, type DetailReplacementBatchResult, } from "@/lib/agents/detail-replacement-batch"; test("recognizes only durable detail replacement batches", () => { expect(isDetailReplacementBatchRequest({ taskKind: "agent-detail-replacement-batch" })).toBe(true); expect(isDetailReplacementBatchRequest({ taskKind: "style-pro" })).toBe(false); expect(isDetailReplacementBatchRequest(null)).toBe(false); }); test("card message identity stays stable across retries and page reloads", () => { const id = detailReplacementCardMessageId("batch-a", "card-01"); expect(id).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/); expect(detailReplacementCardMessageId("batch-a", "card-01")).toBe(id); expect(detailReplacementCardMessageId("batch-a", "card-02")).not.toBe(id); }); test("restart skips successful cards but retries failed ones", () => { const result: DetailReplacementBatchResult = { taskKind: "agent-detail-replacement-batch", currentCardId: null, cards: [ { cardId: "one", status: "completed", imageUrl: "https://example.com/one.png", thumbnailUrl: "https://example.com/one.png", text: "" }, { cardId: "two", status: "fallback", imageUrl: "https://example.com/two.png", thumbnailUrl: "https://example.com/two.png", text: "生成失败" }, { cardId: "three", status: "passthrough", imageUrl: "https://example.com/three.png", thumbnailUrl: "https://example.com/three.png", text: "" }, ], }; expect([...completedDetailReplacementCardIds(result)]).toEqual(["one", "three"]); }); const submission = { projectId: "11111111-1111-1111-1111-111111111111", model: "gpt-image-2.5", prompt: "保持版式,替换产品主体", userPrompt: "详情替换", imageSize: "2K", replacementStrategy: "scene_replace", productImageUrls: ["https://example.com/p2.png", "https://example.com/p1.png"], modelImageUrl: null, references: [ { id: "card-01", label: "首屏", imageUrl: "https://example.com/a.png" }, { id: "card-02", label: "第二屏", imageUrl: "https://example.com/b.png" }, ], }; test("an identical double click resolves to the same submission key", () => { expect(detailReplacementSubmissionKey(submission)).toBe( detailReplacementSubmissionKey({ ...submission, productImageUrls: [...submission.productImageUrls].reverse(), references: [...submission.references].reverse(), }), ); }); test("re-running a single card while a batch runs is not treated as a duplicate", () => { const full = detailReplacementSubmissionKey(submission); expect(detailReplacementSubmissionKey({ ...submission, references: [submission.references[0]!], })).not.toBe(full); expect(detailReplacementSubmissionKey({ ...submission, prompt: "换一个背景", })).not.toBe(full); });