ai-chatbot/tests/routes/replacement-layout-lock.test.ts

201 lines
5.8 KiB
TypeScript

import { expect, test } from "@playwright/test";
import sharp from "sharp";
import {
buildReplacementInputBoundingBoxList,
compositeReplacementWithReferenceLocks,
expandReplacementSubjectBoundingBoxForGeneration,
parseReplacementLayoutLockRegions,
} from "@/lib/ai/replacement-layout-lock";
test("parses normalized subject and protected text regions", () => {
const result = parseReplacementLayoutLockRegions(`
\`\`\`json
{
"subjectBoundingBox": [200, 250, 800, 900],
"textBoundingBoxes": [[100, 80, 700, 160], [20, 20, 20, 40], [-20, 900, 1100, 980]]
}
\`\`\`
`);
expect(result).toEqual({
subjectBoundingBox: [200, 250, 800, 900],
textBoundingBoxes: [
[100, 80, 700, 160],
[0, 900, 1000, 980],
],
analyzed: true,
});
});
test("marks incomplete layout analysis for a safe fallback", () => {
expect(
parseReplacementLayoutLockRegions({
subjectBoundingBox: [200, 250, 800, 900],
}),
).toEqual({
subjectBoundingBox: [200, 250, 800, 900],
textBoundingBoxes: [],
analyzed: false,
});
});
test("expands the replacement subject horizontally and aligns it with the layout input", () => {
expect(
expandReplacementSubjectBoundingBoxForGeneration([379, 240, 628, 917]),
).toEqual([354, 230, 653, 927]);
expect(
buildReplacementInputBoundingBoxList({
imageUrls: [
"https://example.com/product.png",
"https://example.com/layout.png",
],
referenceImageUrl: "https://example.com/layout.png",
subjectBoundingBox: [379, 240, 628, 917],
}),
).toEqual([[], [[354, 230, 653, 927]]]);
});
test("keeps the reference outside the subject and inside protected text", async () => {
const width = 100;
const height = 100;
const reference = await sharp({
create: {
width,
height,
channels: 3,
background: { r: 255, g: 0, b: 0 },
},
})
.png()
.toBuffer();
const generated = await sharp({
create: {
width,
height,
channels: 3,
background: { r: 0, g: 0, b: 255 },
},
})
.png()
.toBuffer();
const result = await compositeReplacementWithReferenceLocks({
generatedBuffer: generated,
referenceBuffer: reference,
width,
height,
preserveReferenceOutsideSubject: true,
subjectBoundingBox: [300, 300, 700, 700],
textBoundingBoxes: [[450, 450, 550, 550]],
});
const { data, info } = await sharp(result)
.removeAlpha()
.raw()
.toBuffer({ resolveWithObject: true });
const pixel = (x: number, y: number) =>
Array.from(
data.subarray(
(y * width + x) * info.channels,
(y * width + x) * info.channels + 3,
),
);
expect(pixel(5, 5)).toEqual([255, 0, 0]);
expect(pixel(35, 50)).toEqual([0, 0, 255]);
expect(pixel(50, 50)).toEqual([255, 0, 0]);
});
test("restores original text pixels while retaining a regenerated scene", async () => {
const width = 100;
const height = 100;
const reference = await sharp({
create: {
width,
height,
channels: 3,
background: { r: 255, g: 0, b: 0 },
},
})
.png()
.toBuffer();
const generated = await sharp({
create: {
width,
height,
channels: 3,
background: { r: 0, g: 0, b: 255 },
},
})
.png()
.toBuffer();
const result = await compositeReplacementWithReferenceLocks({
generatedBuffer: generated,
referenceBuffer: reference,
width,
height,
preserveReferenceOutsideSubject: false,
textBoundingBoxes: [[400, 400, 600, 600]],
});
const { data, info } = await sharp(result)
.removeAlpha()
.raw()
.toBuffer({ resolveWithObject: true });
const pixel = (x: number, y: number) =>
Array.from(
data.subarray(
(y * width + x) * info.channels,
(y * width + x) * info.channels + 3,
),
);
expect(pixel(5, 5)).toEqual([0, 0, 255]);
expect(pixel(50, 50)).toEqual([255, 0, 0]);
});
test("keeps the full title panel and corner caption over the regenerated photo", async () => {
const width = 120;
const height = 160;
const panelHeight = 54;
const reference = await sharp(Buffer.from(`
<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}">
<path fill="white" d="M0 0h120v54H0z"/>
<path fill="#f7c928" d="M8 5h18v6H8z"/>
<path fill="#111" d="M9 17h52v8H9zM9 31h70v7H9z"/>
<path fill="#57423a" d="M0 54h120v106H0z"/>
<path fill="#f7c928" d="M6 145h25v7H6z"/>
</svg>`)).png().toBuffer();
const generated = await sharp(Buffer.from(`
<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}">
<path fill="white" d="M0 0h120v54H0z"/>
<path fill="#111" d="M0 9h12v28H0zM60 12h52v8H60z"/>
<path fill="#446d87" d="M0 54h120v106H0z"/>
<path fill="#f7c928" d="M0 150h25v6H0z"/>
</svg>`)).png().toBuffer();
const result = await compositeReplacementWithReferenceLocks({
generatedBuffer: generated,
referenceBuffer: reference,
width,
height,
preserveReferenceOutsideSubject: false,
subjectBoundingBox: [160, 400, 850, 920],
textBoundingBoxes: [[75, 106, 510, 158], [75, 193, 660, 370], [50, 884, 259, 935]],
});
const pixels = (image: Buffer, top: number, sectionHeight: number, left = 0, sectionWidth = width) =>
sharp(image)
.extract({ left, top, width: sectionWidth, height: sectionHeight })
.removeAlpha()
.raw()
.toBuffer();
expect((await pixels(result, 0, panelHeight)).equals(await pixels(reference, 0, panelHeight)))
.toBe(true);
expect((await pixels(result, panelHeight, 60)).equals(await pixels(generated, panelHeight, 60)))
.toBe(true);
expect((await pixels(result, 145, 12, 0, 30)).equals(await pixels(reference, 145, 12, 0, 30)))
.toBe(true);
expect((await pixels(result, 145, 12, 55, 45)).equals(await pixels(generated, 145, 12, 55, 45)))
.toBe(true);
});