182 lines
8.2 KiB
TypeScript
182 lines
8.2 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]);
|
|
});
|
|
|
|
test("custom width and height keep the whole image and pad both orientations", async () => {
|
|
const { fitBatchInstructionImage } = serverLoader({
|
|
"@/lib/ai/rmbg/service": {},
|
|
"@/lib/storage/service": {},
|
|
})("lib/canvas/batch-instruction-output.ts");
|
|
|
|
for (const [width, height] of [[400, 200], [200, 400]]) {
|
|
const subject = await sharp({
|
|
create: { width: 100, height: 100, channels: 3, background: "#f00000" },
|
|
}).png().toBuffer();
|
|
const source = await sharp({
|
|
create: { width, height, channels: 3, background: "#132639" },
|
|
}).composite([{ input: subject, left: (width - 100) / 2, top: (height - 100) / 2 }]).png().toBuffer();
|
|
const result = await fitBatchInstructionImage(source, { width: 800, height: 1000 });
|
|
const { data, info } = await sharp(result).removeAlpha().raw().toBuffer({ resolveWithObject: true });
|
|
expect([info.width, info.height]).toEqual([800, 1000]);
|
|
const red = { minX: 800, maxX: 0, minY: 1000, maxY: 0 };
|
|
for (let y = 0; y < info.height; y++) {
|
|
for (let x = 0; x < info.width; x++) {
|
|
const index = (y * info.width + x) * info.channels;
|
|
if (data[index] <= 180 || data[index + 1] >= 70) continue;
|
|
red.minX = Math.min(red.minX, x);
|
|
red.maxX = Math.max(red.maxX, x);
|
|
red.minY = Math.min(red.minY, y);
|
|
red.maxY = Math.max(red.maxY, y);
|
|
}
|
|
}
|
|
const subjectSize = width > height ? 200 : 250;
|
|
expect(Math.abs(red.maxX - red.minX + 1 - subjectSize)).toBeLessThanOrEqual(1);
|
|
expect(Math.abs(red.maxY - red.minY + 1 - subjectSize)).toBeLessThanOrEqual(1);
|
|
expect(Array.from(data.subarray(0, 3))).toEqual([19, 38, 57]);
|
|
}
|
|
});
|
|
|
|
test("custom dimensions allow a resize-only task and reject invalid inputs", async () => {
|
|
const queued: Array<Record<string, unknown>> = [];
|
|
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 }) => {
|
|
queued.push(params.request);
|
|
return { id: params.taskId, status: "pending" };
|
|
},
|
|
enqueueCanvasImageTask: async () => {},
|
|
},
|
|
})("app/api/canvas/batch-instruction/route.ts");
|
|
|
|
const request = {
|
|
taskId: randomUUID(),
|
|
image: "https://media.example/source.png",
|
|
instruction: "",
|
|
square: false,
|
|
outputSize: { width: 800, height: 1000 },
|
|
};
|
|
const submit = (options: Record<string, unknown> = {}) => POST(new Request("https://app.example/api/canvas/batch-instruction", {
|
|
method: "POST",
|
|
body: JSON.stringify({ ...request, ...options }),
|
|
}));
|
|
|
|
expect((await submit()).status).toBe(202);
|
|
expect(queued[0]).toMatchObject({
|
|
prompt: "",
|
|
referenceImages: [request.image],
|
|
batchInstructionOutputSize: { width: 800, height: 1000 },
|
|
});
|
|
expect((await submit({ outputSize: { width: 8192, height: 8192 } })).status).toBe(400);
|
|
expect((await submit({ outputSize: { width: 0, height: 1000 } })).status).toBe(400);
|
|
expect((await submit({ reference: "https://media.example/reference.png" })).status).toBe(400);
|
|
expect(queued).toHaveLength(1);
|
|
});
|