ai-chatbot/tests/routes/canvas-chat-helpers.test.ts
2026-05-27 10:32:04 +08:00

263 lines
8.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { expect, test } from "@playwright/test";
import {
collectMessageImageUrls,
extractAssistantImageUrls,
extractVideoUrls,
normalizeMediaUrl,
stripMediaMarkup,
} from "@/components/canvas/chat-panel/canvas-chat-media";
import {
buildDetailResultMarkdown,
buildDetailReviewAppendMarkdown,
buildDetailReviewPlans,
} from "@/components/canvas/chat-panel/canvas-detail-markdown";
import {
DETAIL_BUNDLE_SLOT_INDEXES,
DETAIL_GENERATION_MODE_OPTIONS,
DETAIL_MODEL_SLOT_INDEX,
DETAIL_PRODUCT_SLOT_INDEXES,
DETAIL_SLOT_CONFIGS,
DETAIL_TARGET_HEIGHT,
DETAIL_TARGET_WIDTH,
extractDetailFrameCount,
extractFirstNumber,
extractFontWeight,
normalizeDetailGenerationMode,
sanitizeDetailImageModelPrompt,
splitDetailKeywords,
} from "@/components/canvas/chat-panel/canvas-detail-config";
import type { ChatMessage } from "@/lib/types";
test.describe("canvas chat pure helpers", () => {
test("normalizes media urls without query strings or trailing slashes", () => {
expect(normalizeMediaUrl(" HTTPS://EXAMPLE.COM/assets/image.png?sig=secret#preview ")).toBe(
"https://example.com/assets/image.png"
);
expect(normalizeMediaUrl("not-a-url?cache=1#hash/")).toBe("not-a-url");
});
test("collects message image urls from parts, attachments, and legacy content", () => {
const message = {
id: "message-1",
role: "user",
parts: [
{
type: "file",
url: "https://cdn.example.com/upload-a.png?x=1",
mediaType: "image/png",
},
{
type: "text",
text: "参考 ![a](https://cdn.example.com/markdown-a.webp) 和 https://cdn.example.com/plain-a.jpg?token=1",
},
],
attachments: [
{
name: "attachment",
url: "https://cdn.example.com/attachment-a.jpeg?download=1",
contentType: "image/jpeg",
},
],
content: "legacy https://cdn.example.com/legacy-a.png",
} as unknown as ChatMessage;
expect(collectMessageImageUrls(message)).toEqual([
"https://cdn.example.com/upload-a.png?x=1",
"https://cdn.example.com/markdown-a.webp",
"https://cdn.example.com/plain-a.jpg?token=1",
"https://cdn.example.com/attachment-a.jpeg?download=1",
"https://cdn.example.com/legacy-a.png",
]);
});
test("filters assistant result images that match nearest user references", () => {
const referenceKeys = new Set(["https://cdn.example.com/reference.png"]);
const urls = extractAssistantImageUrls(
[
"![ref](https://cdn.example.com/reference.png?token=secret)",
"![result](https://cdn.example.com/result.png?token=secret)",
].join("\n"),
referenceKeys
);
expect(urls).toEqual(["https://cdn.example.com/result.png?token=secret"]);
});
test("extracts and strips video markup without removing surrounding text", () => {
const text = [
"生成完成",
'<video src="https://cdn.example.com/video-a.mp4?sig=1"></video>',
"[下载视频](https://cdn.example.com/video-b.mp4)",
"继续优化",
].join("\n");
expect(extractVideoUrls(text)).toEqual([
"https://cdn.example.com/video-a.mp4?sig=1",
"https://cdn.example.com/video-b.mp4",
]);
expect(stripMediaMarkup(text)).toBe("生成完成\n\n\n继续优化");
});
test("builds detail review plans with clamped count and localized fallbacks", () => {
const plans = buildDetailReviewPlans({
language: "zh",
count: 9,
summary: " 已验证卖点 ",
brief: {
productSpu: "",
demandType: "",
productName: "无线吸尘器",
targetAudience: "养宠家庭",
mainUser: "",
usageScenario: "客厅清洁",
triggerMoment: "",
coreNeed: "快速清理毛发",
riskConcern: "吸力不足",
expectedResult: "地面干净",
coreSellingPoint: "强吸力,轻量,低噪音",
positioningStatement: "",
userVoice: "",
competitorReference: "",
priceBand: "",
brandTone: "",
bannedWords: "",
},
});
expect(plans).toHaveLength(5);
expect(plans[0]).toMatchObject({
index: 1,
title: "真实需求反馈1",
});
expect(plans[0]?.reviewText).toContain("养宠家庭");
expect(plans[0]?.reviewText).toContain("已验证卖点");
expect(plans[0]?.imagePrompt).toContain("无线吸尘器");
});
test("keeps detail slot indexes and generation mode options stable", () => {
expect(DETAIL_TARGET_WIDTH).toBe(790);
expect(DETAIL_TARGET_HEIGHT).toBe(1300);
expect(DETAIL_SLOT_CONFIGS.map((slot) => slot.role)).toEqual([
"product",
"product",
"product",
"product",
"product",
"model",
"bundle",
"bundle",
"bundle",
]);
expect(DETAIL_PRODUCT_SLOT_INDEXES).toEqual([0, 1, 2, 3, 4]);
expect(DETAIL_MODEL_SLOT_INDEX).toBe(5);
expect(DETAIL_BUNDLE_SLOT_INDEXES).toEqual([6, 7, 8]);
expect(DETAIL_GENERATION_MODE_OPTIONS.map((option) => option.value)).toEqual([
"classic",
"agent_layout",
]);
});
test("normalizes detail config parser helpers defensively", () => {
expect(normalizeDetailGenerationMode("agent_layout")).toBe("agent_layout");
expect(normalizeDetailGenerationMode("unknown")).toBe("classic");
expect(extractDetailFrameCount("需要 23 张详情页")).toBe(20);
expect(extractDetailFrameCount("需要 2 屏")).toBe(5);
expect(extractDetailFrameCount("没有明确数量")).toBe(15);
expect(extractFirstNumber("标题 64px / 700", 32)).toBe(64);
expect(extractFirstNumber("无数字", 32)).toBe(32);
expect(extractFontWeight("主标题 64px / 700 / 行高1.15", "600")).toBe("700");
expect(extractFontWeight("weight: 500", "600")).toBe("500");
expect(extractFontWeight("无权重", "600")).toBe("600");
expect(splitDetailKeywords("强吸力,轻量; 低噪音 | 易清洁", 3)).toEqual([
"强吸力",
"轻量",
"低噪音",
]);
});
test("sanitizes detail image model prompts away from pixel-locked layout instructions", () => {
const sanitized = sanitizeDetailImageModelPrompt(
[
"字体规范:主标题 64px / 700 / 行高1.15",
"输出为竖版电商详情图,尺寸固定 790x1300 像素。",
"安全区 y=64~360,左右边距56px,模块间距24px。",
"商业摄影质感,留出清晰文案排版区,不要水印。",
"突出产品真实材质与使用场景。",
].join("\n")
);
expect(sanitized).not.toContain("790x1300");
expect(sanitized).not.toContain("64px");
expect(sanitized).not.toContain("56px");
expect(sanitized).toContain("输出为竖版电商详情图。");
expect(sanitized).toContain("顶部标题安全区");
expect(sanitized).toContain("左右留白充足");
expect(sanitized).toContain("突出产品真实材质与使用场景。");
});
test("builds stable detail result and appended review markdown", () => {
const markdown = buildDetailResultMarkdown({
generationMode: "agent_layout",
summary: "详情页生成成功",
images: [
{
originalUrl: "https://cdn.example.com/detail-1.png",
thumbnailUrl: "https://cdn.example.com/detail-1-thumb.webp",
},
],
prompts: ["商业摄影质感"],
skillFlow: {
name: "详情页生成",
version: "1.0",
steps: ["拆解卖点", "生成分镜"],
},
qualityChecks: {
passed: true,
},
screens: [
{
screenType: "A",
title: "核心卖点",
useModel: true,
fabe: {
b: "收益",
},
},
],
reviewGroups: [
{
reviewText: "真实好用",
images: [
{
originalUrl: "https://cdn.example.com/review-1.png",
thumbnailUrl: "https://cdn.example.com/review-1-thumb.webp",
},
],
},
],
});
expect(markdown).toContain("生成路线:AGENT排版(画面+可编辑文字层)");
expect(markdown).toContain("质检结果:通过");
expect(markdown).toContain("![thumb|||https://cdn.example.com/detail-1.png|||2K]");
expect(markdown).toContain("评价 1:真实好用");
expect(
buildDetailReviewAppendMarkdown({
failedCount: 1,
groups: [
{
reviewText: "追加评价",
images: [
{
originalUrl: "https://cdn.example.com/review-2.png",
thumbnailUrl: "https://cdn.example.com/review-2-thumb.webp",
},
],
},
],
})
).toContain("失败 1 条,已跳过。");
});
});