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

110 lines
5.0 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("只换五官");
});
test("shared face edits use the two full images and follow requested hair changes", async () => {
const reference = "https://media.example/face.png";
const base = "https://media.example/person.png";
const { prepareBatchInstructionWithReference } = serverLoader({
"@/lib/ai/qwen3-vl": { callQwen3Vl: async () => { throw new Error("Explicit face intent should use direct editing"); } },
"@/lib/ai/rmbg/service": {},
})("lib/canvas/batch-instruction-reference.ts");
const prepared = await prepareBatchInstructionWithReference({
displayPrompt: "将图里的人物的五官脸型发型换成参考图人物的五官脸型发型",
referenceImages: [base, reference],
});
expect(prepared).toMatchObject({
referenceImages: [base, reference],
requiredReferenceImageCount: 2,
batchInstructionFaceEdit: true,
});
expect(prepared.localEditComposite).toBeUndefined();
expect(prepared.referenceImageBuffers).toBeUndefined();
expect(prepared.prompt).toContain("保留原始画幅");
expect(prepared.prompt).toContain("将图里的人物的五官脸型发型换成参考图");
});
test("a product replacement that preserves the face still uses local product editing", async () => {
const reference = "https://media.example/product.png";
const base = "https://media.example/person.png";
const photo = await sharp({ create: { width: 500, height: 500, channels: 3, background: "white" } }).png().toBuffer();
const { prepareBatchInstructionWithReference } = serverLoader({
"@/lib/ai/qwen3-vl": { callQwen3Vl: async () => ({
text: '{"scope":"local","base_box":[300,110,690,490],"reference_box":[350,260,650,620]}',
}) },
"@/lib/ai/rmbg/service": { fetchImageBufferFromUrl: async (url: string) => {
expect(url).toBe(reference);
return photo;
} },
})("lib/canvas/batch-instruction-reference.ts");
const prepared = await prepareBatchInstructionWithReference({
displayPrompt: "只换商品,保留原脸和发型,不要换脸",
referenceImages: [base, reference],
});
expect(prepared.batchInstructionFaceEdit).toBeUndefined();
expect(prepared.localEditComposite).toMatchObject({ sourceImageUrl: base, expandRegions: false });
const cropped = await sharp(prepared.referenceImageBuffers.get(reference)).metadata();
expect([cropped.width, cropped.height]).toEqual([150, 180]);
});