ai-chatbot/tests/routes/openclaw-folder-attachments.test.ts

78 lines
2.2 KiB
TypeScript

import { expect, test } from "@playwright/test";
import {
countOpenClawArchiveRootFolders,
sanitizeOpenClawArchiveEntryPath,
sanitizeOpenClawArchiveName,
} from "@/lib/openclaw-chat/folder-archive";
import {
buildOpenClawArchiveDisplayText,
buildOpenClawArchiveReferenceText,
isOpenClawChatArchiveAttachment,
normalizeOpenClawChatMediaAttachments,
} from "@/lib/openclaw-chat/media-attachments";
test.describe("OpenClaw folder attachments", () => {
test("keeps safe folder structure while removing traversal segments", () => {
expect(
sanitizeOpenClawArchiveEntryPath(
"../../设计资料/主图:终稿.png",
"主图.png",
0
)
).toBe("设计资料/主图-终稿.png");
expect(sanitizeOpenClawArchiveName(" 商品/资料:* ")).toBe("商品-资料--");
});
test("counts multiple selected root folders", () => {
expect(
countOpenClawArchiveRootFolders([
"产品A/图片/1.png",
"产品A/文档/readme.md",
"产品B/图片/2.png",
])
).toBe(2);
});
test("normalizes ZIP URLs as file attachments", () => {
const attachments = normalizeOpenClawChatMediaAttachments([
{
type: "file",
url: "https://cdn.example.com/产品资料.zip",
name: "产品资料.zip",
contentType: "application/zip",
},
]);
expect(attachments).toEqual([
{
type: "file",
url: "https://cdn.example.com/产品资料.zip",
name: "产品资料.zip",
content_type: "application/zip",
},
]);
expect(
isOpenClawChatArchiveAttachment(
attachments[0].url,
attachments[0].content_type
)
).toBe(true);
});
test("adds the public archive URL to the OpenClaw fallback message", () => {
const attachments = normalizeOpenClawChatMediaAttachments([
{
type: "file",
url: "https://cdn.example.com/catalog.zip",
name: "catalog.zip",
},
]);
expect(buildOpenClawArchiveDisplayText(attachments)).toContain("catalog.zip");
expect(buildOpenClawArchiveReferenceText(attachments)).toContain(
"https://cdn.example.com/catalog.zip"
);
expect(buildOpenClawArchiveReferenceText(attachments)).toContain("解压");
});
});