ai-chatbot/tests/routes/aos-image-transfer.test.ts

96 lines
4.3 KiB
TypeScript

import { expect, test } from "@playwright/test";
import { serverLoader } from "../helpers/canvas-download-server-loader";
const cdn = "https://assets.example";
const input = `${cdn}/uploads/original.png`;
const output = "https://result.example/result.png";
const archived = `${cdn}/uploads/archived-result`;
const aos = "https://api.example";
function testImageTransfer(operation: "scale" | "segmentation") {
const endpoint = `${aos}/api/tasks/visual/${operation}`;
const calls: string[] = [];
const load = serverLoader({
"next/server": { NextResponse: Response },
"@/lib/db/queries": {
getSystemSetting: async () => ({ value: {
provider: "aoscdn", apiUrl: aos, apiKey: "test-key", aosApiBaseUrl: aos, aosApiKey: "test-key",
} }),
getMediaAssetsByUserAndUrls: async () => [],
upsertMediaAsset: async () => null,
},
"@/lib/db": { db: { insert: () => ({ values: () => ({ returning: async () => [{ id: "saved" }] }) }) } },
"@/lib/db/schema": { generatedImage: {} },
"@/lib/ai/vip98k-runtime": {},
"@/lib/storage/service": {},
"@/lib/storage/qiniu-image-archive": {
archiveImageThroughQiniu: async (url: string, key: string) => {
expect(url).toBe(output);
calls.push(key);
return { url: archived, contentType: "image/png" };
},
},
"@/lib/observability/active-work": { trackRuntimeWork: (fn: () => unknown) => fn() },
setTimeout: (callback: () => void, ms: number) => setTimeout(callback, ms <= 2000 ? 0 : ms),
fetch: async (rawUrl: string, options?: RequestInit) => {
const url = String(rawUrl);
calls.push(url);
if (url === `${input}?imageInfo`) {
return Response.json({ width: 1024, height: 768, size: 100_000, format: "png" });
}
if (url === endpoint && options?.method === "POST") {
const body = options.body instanceof FormData
? options.body
: await new Request(endpoint, options).formData();
expect(body.get("image_url")).toBe(input);
expect(body.has("image_file")).toBe(false);
expect(body.get("return_type")).toBe("1");
expect(body.get(operation === "scale" ? "scale_factor" : "format"))
.toBe(operation === "scale" ? "2" : "png");
return Response.json({ data: { task_id: "test-aos-task" } });
}
if (url === `${endpoint}/test-aos-task`) {
return Response.json({ status: 200, data: { state: 1, image: output } });
}
throw Error(`Unexpected request to ${url}`);
},
}, { NEXT_PUBLIC_CANVAS_CDN_ORIGIN: cdn });
return { endpoint, calls, load };
}
test("canvas enhancement sends a stored input URL and returns a stored result URL after polling", async () => {
const { load, calls, endpoint } = testImageTransfer("scale");
const enhance = load("lib/canvas/operations/enhance.ts");
const checkpoint: string[] = [];
const response = await enhance.executeEnhancePOST(
new Request("https://app.example/enhance", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ imageUrl: input, scaleFactor: 2, includeBase64: false }),
}),
"test-user",
async (id: string) => { checkpoint.push(id); },
);
expect(response.status).toBe(202);
expect(checkpoint).toEqual(["test-aos-task"]);
const poll = await enhance.executeEnhanceGET(
new Request("https://app.example/enhance?taskId=test-aos-task&includeBase64=false"), "test-user",
);
expect(poll.ok).toBe(true);
const result = await poll.json();
expect(result.original_url).toBe(archived);
expect(result).not.toHaveProperty("image_base64");
expect(calls).toEqual([`${input}?imageInfo`, endpoint, `${endpoint}/test-aos-task`, "aos-enhance-test-aos-task"]);
});
test("canvas cutout keeps the archived PNG as the original and skips production image transfers", async () => {
const { load, calls, endpoint } = testImageTransfer("segmentation");
const rmbg = load("lib/ai/rmbg/service.ts");
const result = await rmbg.executeRmbgJob({
userId: "test-user", inputImageUrl: input, includeBase64: false, skipGallerySave: true,
});
expect(result.original_url).toBe(archived);
expect(result.image_base64).toBeUndefined();
expect(calls).toEqual([`${input}?imageInfo`, endpoint, `${endpoint}/test-aos-task`, "aos-rmbg-test-aos-task"]);
});