238 lines
16 KiB
TypeScript
238 lines
16 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { deflateRawSync } from "node:zlib";
|
|
import { EDDY_SHARE_ACCEPT, eddyChangesSummary, eddyShareBaseName, eddyShareExt, isEddyShareName, readEddyShare } from "@bbbug/shared/eddy_share";
|
|
import { IDEA_SUMMARY_MAX } from "@bbbug/shared/ideas";
|
|
|
|
/**
|
|
* 测试用的最小 ZIP,现场拼:本地文件头 + 中央目录 + EOCD。内容都是编的,不放真实任务的东西。
|
|
* deflate=true 时条目用 deflate 压(Eddy 导出的包就是 deflate),否则原样存储。
|
|
*/
|
|
function zip(files: Record<string, string | Uint8Array>, deflate = true): Uint8Array<ArrayBuffer> {
|
|
const enc = new TextEncoder();
|
|
const u16 = (n: number) => [n & 0xff, (n >> 8) & 0xff];
|
|
const u32 = (n: number) => [...u16(n & 0xffff), ...u16((n >>> 16) & 0xffff)];
|
|
const local: number[] = [], central: number[] = [];
|
|
let count = 0;
|
|
for (const [name, content] of Object.entries(files)) {
|
|
const raw = typeof content === "string" ? enc.encode(content) : content;
|
|
const data = deflate ? new Uint8Array(deflateRawSync(raw)) : raw;
|
|
const nameBytes = enc.encode(name), offset = local.length, crc = Number(Bun.hash.crc32(raw));
|
|
const common = [...u16(deflate ? 8 : 0), ...u16(0), ...u16(0x21), ...u32(crc), ...u32(data.length), ...u32(raw.length), ...u16(nameBytes.length), ...u16(0)];
|
|
local.push(...u32(0x04034b50), ...u16(20), ...u16(0x0800), ...common, ...nameBytes, ...data);
|
|
central.push(...u32(0x02014b50), ...u16(20), ...u16(20), ...u16(0x0800), ...common, ...u16(0), ...u16(0), ...u16(0), ...u32(0), ...u32(offset), ...nameBytes);
|
|
count++;
|
|
}
|
|
return new Uint8Array([...local, ...central, ...u32(0x06054b50), ...u16(0), ...u16(0), ...u16(count), ...u16(count), ...u32(central.length), ...u32(local.length), ...u16(0)]);
|
|
}
|
|
const pkg = (name: string, files: Record<string, string | Uint8Array>, deflate = true) => new File([zip(files, deflate)], name);
|
|
const share = (extra: Record<string, unknown> = {}) => JSON.stringify({
|
|
format: "eddy-task-share", formatVersion: 1, title: "示例任务",
|
|
dependencies: { baseline: { key: "exampleapp", identity: { bundle: "com.example.app" } } }, ...extra,
|
|
});
|
|
const registry = (scripts: Record<string, string>) => JSON.stringify({ edits: Object.fromEntries(Object.entries(scripts).map(([k, script]) => [k, { script }])) });
|
|
const PNG = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 1, 2, 3, 4]);
|
|
/** Eddy 导出时写的改动说明的样子(eddyctl/share_notes.py 的三节),内容是编的 */
|
|
const NOTES = [
|
|
"## 改了什么", "",
|
|
"- **示例页**:标题由「旧标题」改为「新标题」,进入第一屏即可看到。",
|
|
"- `设置` 里多了一个 [开关](https://example.invalid/doc)。", "",
|
|
"## 怎么测试", "", "1. 打开 App。", "2. 进入示例页,应看到「新标题」。", "",
|
|
"## 注意", "", "- 只在英文界面生效。",
|
|
].join("\n");
|
|
|
|
describe("Eddy 分享包的文件名", () => {
|
|
test("认三种扩展名:Mac 版的 .eddy、命令行的 .eddy.zip、普通 .zip,不区分大小写", () => {
|
|
expect(eddyShareExt("示例任务.eddy")).toBe(".eddy");
|
|
expect(eddyShareExt("示例任务.eddy.zip")).toBe(".eddy.zip");
|
|
expect(eddyShareExt("示例任务.zip")).toBe(".zip");
|
|
expect(eddyShareExt("DEMO.EDDY")).toBe(".eddy");
|
|
expect(eddyShareExt("Demo.Eddy.ZIP")).toBe(".eddy.zip");
|
|
expect(eddyShareExt(" 带空格.eddy ")).toBe(".eddy");
|
|
for (const ok of ["a.eddy", "a.eddy.zip", "a.zip", "有 空格 · 和&符号.eddy"]) expect(isEddyShareName(ok)).toBe(true);
|
|
});
|
|
test("别的文件不认:截图、去掉点的、扩展名在中间的", () => {
|
|
for (const bad of ["shot.png", "notes.eddy.txt", "a.zip.bak", "eddy", "aeddy", "a.eddyx", "a.tar.gz", ""]) {
|
|
expect(eddyShareExt(bad)).toBeNull();
|
|
expect(isEddyShareName(bad)).toBe(false);
|
|
}
|
|
});
|
|
test("去扩展名:.eddy.zip 整个去掉,不会剩下 .eddy", () => {
|
|
expect(eddyShareBaseName("首页换个标题.eddy")).toBe("首页换个标题");
|
|
expect(eddyShareBaseName("首页换个标题.eddy.zip")).toBe("首页换个标题");
|
|
expect(eddyShareBaseName("首页换个标题.zip")).toBe("首页换个标题");
|
|
expect(eddyShareBaseName("v1.2 原型.EDDY")).toBe("v1.2 原型");
|
|
expect(eddyShareBaseName("shot.png")).toBe("shot.png");
|
|
});
|
|
test("文件选择框的 accept 带上 .eddy 和 .zip", () => {
|
|
expect(EDDY_SHARE_ACCEPT.split(",")).toEqual([".eddy", ".zip", "application/zip"]);
|
|
});
|
|
});
|
|
|
|
describe("readEddyShare:三种扩展名读出来一样", () => {
|
|
const files = { "share.json": share({ changes: { text: NOTES } }), "registry.json": registry({ "script:demo.example": "// 示例:改标题\nconsole.log(1);" }) };
|
|
for (const name of ["示例任务.eddy", "示例任务.eddy.zip", "示例任务.zip"]) {
|
|
test(name, async () => {
|
|
const info = await readEddyShare(pkg(name, files));
|
|
expect(info.title).toBe("示例任务");
|
|
expect(info.platform).toBe("ios");
|
|
expect(info.summary).toBe("示例页:标题由「旧标题」改为「新标题」,进入第一屏即可看到。\n设置 里多了一个 开关。");
|
|
expect(info.media).toEqual([]);
|
|
expect(info.scripts).toEqual([{ key: "script:demo.example", script: "// 示例:改标题\nconsole.log(1);" }]);
|
|
});
|
|
}
|
|
test("包里没有标题时,用去掉扩展名的文件名", async () => {
|
|
const noTitle = { "share.json": JSON.stringify({ format: "eddy-task-share" }) };
|
|
expect((await readEddyShare(pkg("首页换个标题.eddy", noTitle))).title).toBe("首页换个标题");
|
|
expect((await readEddyShare(pkg("首页换个标题.eddy.zip", noTitle))).title).toBe("首页换个标题");
|
|
expect((await readEddyShare(pkg("首页换个标题.zip", noTitle))).title).toBe("首页换个标题");
|
|
});
|
|
test("不压缩(stored)的条目也能读", async () => {
|
|
expect((await readEddyShare(pkg("a.eddy", files, false))).title).toBe("示例任务");
|
|
});
|
|
});
|
|
|
|
describe("readEddyShare:说明的回退顺序", () => {
|
|
const script = { "registry.json": registry({ "script:demo.example": "\n// example edit: swaps the example title.\n// second line\n(function () {})();" }) };
|
|
test("description / summary 有就用它(原来的行为)", async () => {
|
|
const a = await readEddyShare(pkg("a.eddy", { "share.json": share({ description: " 自己写的说明 ", changes: { text: NOTES }, instructions: "体验说明" }), ...script }));
|
|
expect(a.summary).toBe("自己写的说明");
|
|
const b = await readEddyShare(pkg("a.eddy", { "share.json": share({ summary: "另一种写法", changes: { text: NOTES } }) }));
|
|
expect(b.summary).toBe("另一种写法");
|
|
});
|
|
test("第一级:changes.text 的「改了什么」一节,去掉 Markdown 记号,不带「怎么测试 / 注意」", async () => {
|
|
const info = await readEddyShare(pkg("a.eddy", { "share.json": share({ changes: { text: NOTES }, instructions: "体验说明" }), ...script }));
|
|
expect(info.summary).toBe("示例页:标题由「旧标题」改为「新标题」,进入第一屏即可看到。\n设置 里多了一个 开关。");
|
|
});
|
|
test("第二级:没有 changes 时用 instructions", async () => {
|
|
const info = await readEddyShare(pkg("a.eddy", { "share.json": share({ instructions: " 从首页点右上角进入。\n\n- 需要先登录 " }), ...script }));
|
|
expect(info.summary).toBe("从首页点右上角进入。\n需要先登录");
|
|
const empty = await readEddyShare(pkg("a.eddy", { "share.json": share({ changes: { text: "" }, instructions: "只有体验说明" }) }));
|
|
expect(empty.summary).toBe("只有体验说明");
|
|
});
|
|
test("第三级:都没有才用补丁脚本首行注释(原来的行为)", async () => {
|
|
const info = await readEddyShare(pkg("a.eddy", { "share.json": share({ instructions: "", changes: null }), ...script }));
|
|
expect(info.summary).toBe("swaps the example title.");
|
|
const none = await readEddyShare(pkg("a.eddy", { "share.json": share(), "registry.json": JSON.stringify({ edits: {} }) }));
|
|
expect(none.summary).toBe("");
|
|
});
|
|
test("字段类型不对不会读崩", async () => {
|
|
const info = await readEddyShare(pkg("a.eddy", { "share.json": share({ description: 1, summary: {}, changes: "x", instructions: ["a"] }), ...script }));
|
|
expect(info.summary).toBe("swaps the example title.");
|
|
});
|
|
});
|
|
|
|
describe("eddyChangesSummary", () => {
|
|
test("只取「改了什么」一节", () => {
|
|
expect(eddyChangesSummary(NOTES)).toBe("示例页:标题由「旧标题」改为「新标题」,进入第一屏即可看到。\n设置 里多了一个 开关。");
|
|
expect(eddyChangesSummary("## 怎么测试\n\n1. 打开\n\n## 改了什么\n\n只有一句。\n")).toBe("只有一句。");
|
|
});
|
|
test("发送端没有 Agent 时的兜底格式:去掉来源说明那一行,保留版本记录,不带「改动项」", () => {
|
|
const fallback = [
|
|
"## 改了什么", "(发送端没有可用的 Agent CLI,改用版本记录;以下按发送者保存的版本记录列出)",
|
|
"- v1 示例任务:标题换成新的", "- v2 再调一下颜色", "", "## 改动项", "- `script:demo.example`", "", "## 怎么测试", "发送者没有补充", "", "## 注意", "无",
|
|
].join("\n");
|
|
expect(eddyChangesSummary(fallback)).toBe("v1 示例任务:标题换成新的\nv2 再调一下颜色");
|
|
});
|
|
test("没有「改了什么」标题时取第一个有正文的小节,但不拿「怎么测试 / 注意」顶替", () => {
|
|
expect(eddyChangesSummary("# 改动说明\n\n把标题换了。\n\n## 怎么测试\n\n1. 打开")).toBe("把标题换了。");
|
|
expect(eddyChangesSummary("没有标题的一段话。")).toBe("没有标题的一段话。");
|
|
expect(eddyChangesSummary("## 怎么测试\n\n1. 打开\n\n## 注意\n\n无")).toBe("");
|
|
});
|
|
test("Markdown 记号:列表、编号、引用、粗体、斜体、代码、链接、分隔线、代码围栏;星号乘号不动", () => {
|
|
const md = ["## 改了什么", "1. **粗体** 和 *斜体* 和 ~~删掉~~", "> 引用的一句", "* `code` 与 [链接](https://example.invalid)", "---", "```", "+ 2 * 3 * 4 不是斜体", "```", "\r"].join("\r\n");
|
|
expect(eddyChangesSummary(md)).toBe("粗体 和 斜体 和 删掉\n引用的一句\ncode 与 链接\n2 * 3 * 4 不是斜体");
|
|
});
|
|
test("按 summary 上限截断;不是字符串返回空", () => {
|
|
const long = `## 改了什么\n\n${"长".repeat(IDEA_SUMMARY_MAX + 50)}`;
|
|
expect(eddyChangesSummary(long)).toHaveLength(IDEA_SUMMARY_MAX);
|
|
expect(eddyChangesSummary(long, 10)).toBe("长".repeat(10));
|
|
expect(eddyChangesSummary(undefined)).toBe("");
|
|
expect(eddyChangesSummary({ text: "x" })).toBe("");
|
|
});
|
|
});
|
|
|
|
describe("readEddyShare:媒体", () => {
|
|
test("assets/ 是任务替换进 App 的资源,不当演示截图;真正的截图照收,cover 在前", async () => {
|
|
const sha = "0f".repeat(32);
|
|
const info = await readEddyShare(pkg("带资源.eddy", {
|
|
"share.json": share(), "registry.json": JSON.stringify({ edits: { "res:image:icon": { path: `eddy-asset://assets/${sha}/cover-icon.png` } } }),
|
|
[`assets/${sha}/cover-icon.png`]: PNG, [`assets/${sha}/demo.mp4`]: new Uint8Array([0, 0, 0, 1]),
|
|
"screens/after.png": new Uint8Array([9, 9, 9]), "cover.jpg": new Uint8Array([7, 7]),
|
|
}));
|
|
expect(info.media.map((m) => [m.name, m.type])).toEqual([["cover.jpg", "image/jpeg"], ["after.png", "image/png"]]);
|
|
expect([...info.media[1]!.data]).toEqual([9, 9, 9]);
|
|
});
|
|
test("只有 assets/ 里有图的包:媒体为空", async () => {
|
|
const info = await readEddyShare(pkg("带资源.eddy", { "share.json": share(), [`assets/${"ab".repeat(32)}/x.png`]: PNG }));
|
|
expect(info.media).toEqual([]);
|
|
});
|
|
test("整个包套在一层目录里时,那一层下面的 assets/ 同样不算;别处叫 assets 的目录不受影响", async () => {
|
|
const info = await readEddyShare(pkg("套一层.zip", {
|
|
"示例任务/share.json": share(), "示例任务/assets/aa/x.png": PNG, "示例任务/demo.png": PNG, "示例任务/screens/assets/kept.png": PNG,
|
|
}));
|
|
expect(info.media.map((m) => m.name)).toEqual(["demo.png", "kept.png"]);
|
|
});
|
|
test("新版 Eddy 包带 share.json 的 media 清单:只按清单取 assets/media/ 下的演示素材,截图在前当封面,别的 assets/ 照旧不算", async () => {
|
|
const shot = `assets/media/${"1a".repeat(32)}/home.png`, flow = `assets/media/${"2b".repeat(32)}/flow.mov`, other = `assets/media/${"3c".repeat(32)}/later.png`;
|
|
const info = await readEddyShare(pkg("带素材.eddy", {
|
|
"share.json": share({ media: [
|
|
{ path: flow, kind: "recording", name: "flow.mov", bytes: 3 },
|
|
{ path: shot, kind: "screenshot", name: "home.png", bytes: 3 },
|
|
{ path: other, kind: "screenshot", name: "later.png", bytes: 3 },
|
|
{ path: `assets/media/${"4d".repeat(32)}/gone.png`, kind: "screenshot", name: "gone.png", bytes: 3 },
|
|
] }),
|
|
[flow]: new Uint8Array([1, 1, 1]), [shot]: new Uint8Array([2, 2, 2]), [other]: new Uint8Array([3, 3, 3]),
|
|
[`assets/${"0f".repeat(32)}/icon.png`]: PNG, // 任务换进 App 的图:不是素材
|
|
"cover.jpg": new Uint8Array([7, 7]), // 有清单时,清单以外的图不算
|
|
}));
|
|
expect(info.media.map((m) => [m.name, m.type])).toEqual([["home.png", "image/png"], ["later.png", "image/png"], ["flow.mov", "video/quicktime"]]);
|
|
expect([...info.media[0]!.data]).toEqual([2, 2, 2]);
|
|
});
|
|
test("media 清单为空数组:发送者一个都没勾,就没有素材(不回退去扫)", async () => {
|
|
const info = await readEddyShare(pkg("没勾.eddy", { "share.json": share({ media: [] }), "demo.png": PNG }));
|
|
expect(info.media).toEqual([]);
|
|
});
|
|
test("清单里的路径按 share.json 所在目录算(整个包套在一层目录里也行),格式不对的条目跳过", async () => {
|
|
const shot = `assets/media/${"1a".repeat(32)}/home.png`;
|
|
const info = await readEddyShare(pkg("套一层.eddy", {
|
|
"示例任务/share.json": share({ media: [{ path: shot, kind: "screenshot" }, { kind: "screenshot" }, "x", null] }),
|
|
[`示例任务/${shot}`]: PNG,
|
|
}));
|
|
expect(info.media.map((m) => m.name)).toEqual(["home.png"]);
|
|
});
|
|
test("media:false 不读媒体内容(控制面接力只要脚本)", async () => {
|
|
const info = await readEddyShare(pkg("a.eddy", { "share.json": share(), "demo.png": PNG, "registry.json": registry({ "script:a": "// x\n1" }) }), { media: false });
|
|
expect(info.media).toEqual([]);
|
|
expect(info.scripts).toHaveLength(1);
|
|
});
|
|
});
|
|
|
|
describe("readEddyShare:报错和原有行为", () => {
|
|
test("没有 share.json 的 zip 仍然报原来的错,不管扩展名", async () => {
|
|
for (const name of ["别的压缩包.zip", "别的压缩包.eddy", "别的压缩包.eddy.zip"]) {
|
|
await expect(readEddyShare(pkg(name, { "readme.txt": "hello", "demo.png": PNG }))).rejects.toThrow("包里没有 share.json,不像是 Eddy 导出的分享包");
|
|
}
|
|
});
|
|
test("不是 zip 的文件、别的 format 都读不出", async () => {
|
|
await expect(readEddyShare(new File([new Uint8Array(64)], "假的.eddy"))).rejects.toThrow("不是 Eddy 分享包");
|
|
await expect(readEddyShare(pkg("a.eddy", { "share.json": JSON.stringify({ format: "something-else" }) }))).rejects.toThrow("不认识的包格式 something-else");
|
|
});
|
|
test("原来能读的 .eddy.zip:标题、description、Android 基线、脚本、cover / demo 排序都不变", async () => {
|
|
const info = await readEddyShare(pkg("老马识图.eddy.zip", {
|
|
"share.json": JSON.stringify({ format: "eddy-task-share", title: " 老马识图 ", description: "拍照识别贴纸", dependencies: { baseline: { key: "example-android", identity: { bundle: "com.example.android" } } } }),
|
|
"registry.json": registry({ "script:a": "// 标题: 第一句\ncode()", "script:b": "no comment" }),
|
|
"patches/0000.js": "// 标题: 第一句\ncode()", "RECEIVER-PROMPT.txt": "…",
|
|
"z-last.png": PNG, "demo-1.mp4": new Uint8Array([1]), "my-cover.webp": new Uint8Array([2]),
|
|
}));
|
|
expect(info.title).toBe("老马识图");
|
|
expect(info.summary).toBe("拍照识别贴纸");
|
|
expect(info.platform).toBe("android");
|
|
expect(info.scripts.map((s) => s.key)).toEqual(["script:a", "script:b"]);
|
|
expect(info.media.map((m) => [m.name, m.type])).toEqual([["my-cover.webp", "image/webp"], ["demo-1.mp4", "video/mp4"], ["z-last.png", "image/png"]]);
|
|
});
|
|
test("没有名字的 Blob(控制面从 TOS 取回来的)也能读,标题缺省时不带扩展名", async () => {
|
|
const info = await readEddyShare(new Blob([zip({ "share.json": JSON.stringify({ format: "eddy-task-share" }) })]));
|
|
expect(info.title).toBe("eddy");
|
|
});
|
|
});
|