63 lines
3.8 KiB
TypeScript
63 lines
3.8 KiB
TypeScript
import { expect, test } from "@playwright/test";
|
||
import { findImageGenerationFailure } from "@/lib/canvas/image-generation-status";
|
||
import { getRequestFailureDetails } from "@/lib/request-error";
|
||
import { serverLoader } from "../helpers/canvas-download-server-loader";
|
||
|
||
test("a terminal image error reaches the stream and persists for disconnected clients", async () => {
|
||
const chunks: any[] = [];
|
||
const saved: any[] = [];
|
||
const load = serverLoader({ "@/lib/db/queries": { saveMessages: async ({ messages }: any) => saved.push(...messages) } });
|
||
const { reportImageGenerationFailure } = load("app/(chat)/api/chat/_lib/image-generation-failure.ts");
|
||
await reportImageGenerationFailure({
|
||
writer: { write: (chunk: any) => chunks.push(chunk) },
|
||
chatId: "chat", userMessageId: "original-user", messageId: "request-id",
|
||
error: new Error("get image url failed"), label: "test",
|
||
});
|
||
const streamed = JSON.parse(chunks.find(chunk => chunk.type === "data-imageGenerationStatus").data);
|
||
const persisted = findImageGenerationFailure(saved, "original-user");
|
||
expect(persisted).toEqual(streamed);
|
||
expect(persisted).toMatchObject({ phase: "failed", requestId: "request-id", failure: { code: "SOURCE_IMAGE_UNAVAILABLE", outcome: "failed" } });
|
||
const text = saved[0].parts[0].text;
|
||
expect(text).toContain("重新上传原图");
|
||
expect(text).toContain("请求编号:request-id");
|
||
expect(JSON.parse(chunks.at(-1).data).parts[0].text).toContain("IMAGE_GENERATION_END");
|
||
|
||
await reportImageGenerationFailure({
|
||
writer: { write: () => { throw new Error("browser disconnected"); } },
|
||
chatId: "chat", userMessageId: "next-user", messageId: "next-request",
|
||
error: new Error("get image url failed"), label: "test",
|
||
});
|
||
expect(findImageGenerationFailure(saved, "next-user")?.requestId).toBe("next-request");
|
||
});
|
||
|
||
test("late failures remain associated with their original user request", () => {
|
||
const status = { phase: "failed" as const, userMessageId: "first", failure: getRequestFailureDetails(new Error("HTTP 524")) };
|
||
const messages = [{ role: "user", parts: [] }, { role: "assistant", parts: [{ type: "data-imageGenerationStatus", data: status }] }];
|
||
expect(findImageGenerationFailure(messages, "first")).toEqual(status);
|
||
expect(findImageGenerationFailure(messages, "second")).toBeNull();
|
||
expect(findImageGenerationFailure([{ role: "assistant", parts: [{ type: "text", text: "请分析失败原因" }] }], "second")).toBeNull();
|
||
});
|
||
|
||
test("quota rejection reports the same terminal state to the browser and history and links the upstream request", async () => {
|
||
const chunks: any[] = [];
|
||
const saved: any[] = [];
|
||
const logs: any[] = [];
|
||
const load = serverLoader({
|
||
"@/lib/db/queries": { saveMessages: async ({ messages }: any) => saved.push(...messages) },
|
||
"@/lib/observability/logger": { createLogger: () => ({ warn: (_: string, fields: any) => logs.push(fields) }) },
|
||
});
|
||
const { reportImageGenerationFailure } = load("app/(chat)/api/chat/_lib/image-generation-failure.ts");
|
||
await reportImageGenerationFailure({
|
||
writer: { write: (chunk: any) => chunks.push(chunk) },
|
||
chatId: "chat", userMessageId: "original-user", messageId: "local-request",
|
||
error: Object.assign(new Error("The API key does not have enough available quota. (HTTP 429)"), {
|
||
status: 429, providerRequestId: "upstream-request",
|
||
}), label: "test",
|
||
});
|
||
const streamed = JSON.parse(chunks.find(chunk => chunk.type === "data-imageGenerationStatus").data);
|
||
expect(findImageGenerationFailure(saved, "original-user")).toEqual(streamed);
|
||
expect(streamed).toMatchObject({ phase: "failed", failure: { code: "PROVIDER_BALANCE_EXHAUSTED", outcome: "failed" } });
|
||
expect(saved[0].parts[0].text).toContain("与您的积分余额无关");
|
||
expect(logs).toContainEqual(expect.objectContaining({ requestId: "local-request", providerRequestId: "upstream-request" }));
|
||
});
|