ai-chatbot/tests/routes/batch-instruction-edit.test.ts

61 lines
2.7 KiB
TypeScript

import { test, expect } from "@playwright/test";
import { randomUUID } from "node:crypto";
import sharp from "sharp";
import { serverLoader } from "../helpers/canvas-download-server-loader";
test("square padding keeps every pixel at the same scale in both orientations", async () => {
const { padImageToSquare } = serverLoader({
"@/lib/ai/rmbg/service": {},
"@/lib/storage/service": {},
})("lib/canvas/batch-instruction-output.ts");
for (const [width, height] of [[401, 301], [277, 408]]) {
const pixels = Buffer.alloc(width * height * 4);
for (let index = 0; index < pixels.length; index++) {
pixels[index] = (index + Math.floor(index / (4 * width))) % 251;
}
const source = await sharp(pixels, { raw: { width, height, channels: 4 } }).png().toBuffer();
const padded = await padImageToSquare(source);
const side = Math.max(width, height);
const restored = await sharp(padded).extract({
width, height,
left: Math.floor((side - width) / 2),
top: Math.floor((side - height) / 2),
}).raw().toBuffer();
const dimensions = await sharp(padded).metadata();
expect([dimensions.width, dimensions.height]).toEqual([side, side]);
expect(Buffer.compare(restored, pixels)).toBe(0);
}
});
test("instruction requests preserve base and shared reference roles and task identifier", async () => {
let request: Record<string, any> | undefined;
let submitted = "";
const id = randomUUID();
const { POST } = serverLoader({
"@/app/(auth)/auth": { auth: async () => ({ user: { id: "test-owner" } }) },
"@/lib/db/queries": { getSystemSetting: async () => null },
"@/lib/canvas/image-task-queue": {
createCanvasImageTask: async (params: { request: Record<string, unknown>; taskId: string }) => {
request = params.request;
return { id: params.taskId, status: "pending" };
},
enqueueCanvasImageTask: async (taskId: string) => { submitted = taskId; },
},
})("app/api/canvas/batch-instruction/route.ts");
const base = "https://media.example/base.png";
const ref = "https://media.example/reference.png";
const response = await POST(new Request("https://app.example/api/canvas/batch-instruction", {
method: "POST",
body: JSON.stringify({ taskId: id, image: base, reference: ref, instruction: "只换五官", square: true }),
}));
expect(response.status).toBe(202);
expect(submitted).toBe(id);
expect(request).toMatchObject({
referenceImages: [base, ref], requiredReferenceImageCount: 2, imageCount: 1,
aspectRatio: "auto", padBatchInstructionToSquare: true,
});
expect(request?.prompt).toContain("第二张图只提供指令指定");
expect(request?.prompt).toContain("只换五官");
});