import { test, expect, describe, afterEach } from "bun:test"; import { mkdtempSync, mkdirSync, rmSync, existsSync, readFileSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { materializeAttachments, decodeExistingAlogs, isAlogArchive, stripAttachmentSections, type AlogDecoder, type AttachmentDownloader, } from "../src/attachments"; /** * 工单附件里的 ALog:建工作区时就解成明文,和图片一样落在 images/ 里。 * * 真实事故:bug 6160ec99 带了 alog-2h.zip,Agent 对加密的 BDALog 跑 strings 只看到 * 乱码,写下「日志加密读不了」就判了 UNREPRODUCIBLE;同一份日志解开后,另一个 Agent * 靠它 + 代码就修掉了。 */ // 形状和生产附件一致:zip 魔数 + 成员名里带 `.alog.iOS.1233`(zip 的成员名是明文)。 const ALOG_ZIP = new Uint8Array(Buffer.concat([ Buffer.from("504b0304", "hex"), Buffer.from("....BDALog_20261026_153648.alog.iOS.1233....encrypted"), ])); const PLAIN = new TextEncoder().encode("2026-10-26 15:36:50.123 [EditLivePhoto] cropRatio=1:1 slot=video\n"); const MP4 = new Uint8Array(Buffer.from("000000186674797069736f6d", "hex")); const MANIFEST = { inlineImages: [], attachments: [ { uuid: "u-alog", name: "alog-2h.zip", type: "application/zip" }, { uuid: "u-mp4", name: "screen.mp4", type: "video/mp4" }, ], }; const download: AttachmentDownloader = async (uuid) => uuid === "u-alog" ? { ok: true, bytes: ALOG_ZIP, contentType: "application/zip" } : { ok: true, bytes: MP4, contentType: "video/mp4" }; const okDecoder: AlogDecoder = async () => ({ ok: true, text: PLAIN }); const failDecoder: AlogDecoder = async () => ({ ok: false, error: "bytedcli 未授权或授权已过期" }); const dirs: string[] = []; function workspace(): { dir: string; imgDir: string } { const dir = mkdtempSync(path.join(tmpdir(), "alog-attach-")); dirs.push(dir); return { dir, imgDir: path.join(dir, "images") }; } afterEach(() => { while (dirs.length) rmSync(dirs.pop()!, { recursive: true, force: true }); }); describe("isAlogArchive", () => { test("zip + 成员名带 .alog → 是(附件名叫什么都行)", () => { expect(isAlogArchive("00.zip", ALOG_ZIP)).toBe(true); }); test("名字带 alog 的 zip → 是", () => { expect(isAlogArchive("alog-2h.zip", new Uint8Array(Buffer.from("504b0304aaaa", "hex")))).toBe(true); }); test("不是 zip → 否,哪怕名字带 alog", () => { expect(isAlogArchive("alog.txt", new TextEncoder().encode("plain alog text"))).toBe(false); expect(isAlogArchive("screen.mp4", MP4)).toBe(false); }); }); describe("materializeAttachments · ALog", () => { test("解开的明文落在 zip 旁边,并在附件清单里紧跟着列出", async () => { const { dir, imgDir } = workspace(); const r = await materializeAttachments({ manifest: MANIFEST, description: "story 相机裁剪 Live 图跳变", imgDir, workspaceDir: dir, download, decodeAlog: okDecoder, }); expect(r.alogDecoded).toBe(1); expect(readFileSync(path.join(imgDir, "00.alog.txt"))).toEqual(Buffer.from(PLAIN)); expect(existsSync(path.join(imgDir, "00.zip"))).toBe(true); // 原件保留 const lines = r.description.split("\n"); const zipAt = lines.findIndex((l) => l.startsWith("- images/00.zip")); expect(lines[zipAt + 1]).toBe("- images/00.alog.txt (alog-2h.zip 的 ALog 明文,Slardar 解密)"); expect(r.description).toContain("- images/01.mp4 (screen.mp4)"); }); test("解不开:zip 那一行写明是证据缺口,不留让 Agent 自己 strings 的余地", async () => { const { dir, imgDir } = workspace(); const r = await materializeAttachments({ manifest: MANIFEST, description: "x", imgDir, workspaceDir: dir, download, decodeAlog: failDecoder, }); expect(r.alogFailed).toBe(1); expect(existsSync(path.join(imgDir, "00.alog.txt"))).toBe(false); const zipLine = r.description.split("\n").find((l) => l.startsWith("- images/00.zip"))!; expect(zipLine).toContain("(alog-2h.zip) — ALog 解密失败:bytedcli 未授权或授权已过期"); expect(zipLine).toContain("不要用 strings"); }); test("没给解码器时行为与之前一致", async () => { const { dir, imgDir } = workspace(); const r = await materializeAttachments({ manifest: MANIFEST, description: "x", imgDir, workspaceDir: dir, download }); expect(r.description).toContain("- images/00.zip (alog-2h.zip)\n"); expect(existsSync(path.join(imgDir, "00.alog.txt"))).toBe(false); }); }); describe("换节点重建工作区:旧附件段不叠加", () => { // 生产上 6160ec99 的描述里就叠了两段:评估节点 /Users/bytedance/… 一段,执行节点 /Users/fei/… 一段。 const OLD = [ "story 相机裁剪 Live 图跳变", "", "## 附件 / Attachments", "工作区目录(绝对路径) / Workspace: /Users/bytedance/bug_workspace/7386666710", "", "纯附件 / Standalone attachments:", "- images/00.zip (alog-2h.zip) — ALog 解密失败:旧节点的原因。这是平台侧的证据缺口", "- images/03.mp4 (easyoncall_media.mp4)", "", ].join("\n"); test("stripAttachmentSections 只去掉我们写的段,用户正文原样保留", () => { expect(stripAttachmentSections(OLD)).toBe("story 相机裁剪 Live 图跳变"); expect(stripAttachmentSections(OLD + "\n" + OLD.split("\n").slice(1).join("\n"))).toBe("story 相机裁剪 Live 图跳变"); const userList = "要点:\n- 第一条\n- 第二条"; expect(stripAttachmentSections(userList)).toBe(userList); // 老工作区补解时追加的段也一并去掉 const rescued = "正文\n\n## ALog 明文 / Decoded ALog\n- images/00.alog.txt (images/00.zip 的 ALog 明文,Slardar 解密)\n"; expect(stripAttachmentSections(rescued)).toBe("正文"); }); test("重新落盘后只剩一段,路径和解密结果都是这台节点的", async () => { const { dir, imgDir } = workspace(); const r = await materializeAttachments({ manifest: MANIFEST, description: OLD, imgDir, workspaceDir: dir, download, decodeAlog: okDecoder, }); expect(r.description.match(/## 附件 \/ Attachments/g)).toHaveLength(1); expect(r.description).toContain(`Workspace: ${dir}`); expect(r.description).not.toContain("/Users/bytedance/bug_workspace"); expect(r.description).not.toContain("旧节点的原因"); }); test("首轮已替换进正文的内联图,不再在尾部重复列一遍", async () => { const { dir, imgDir } = workspace(); const r = await materializeAttachments({ manifest: { inlineImages: [{ uuid: "u-img" }], attachments: [] } as never, description: "截图:![image](images/00.png)", imgDir, workspaceDir: dir, download: async () => ({ ok: true, bytes: new Uint8Array([0x89, 0x50]), contentType: "image/png" }), }); expect(r.description).not.toContain("正文内联图"); }); }); describe("decodeExistingAlogs · 能力上线前建好的工作区", () => { test("补解 images/NN.zip,返回要追加进正文的行;已解过的跳过", async () => { const { imgDir } = workspace(); mkdirSync(imgDir, { recursive: true }); writeFileSync(path.join(imgDir, "00.zip"), ALOG_ZIP); writeFileSync(path.join(imgDir, "03.mp4"), MP4); let calls = 0; const counting: AlogDecoder = async (b, n) => { calls++; return okDecoder(b, n); }; const lines = await decodeExistingAlogs(imgDir, counting); expect(lines).toEqual(["- images/00.alog.txt (images/00.zip 的 ALog 明文,Slardar 解密)"]); expect(readFileSync(path.join(imgDir, "00.alog.txt"))).toEqual(Buffer.from(PLAIN)); expect(await decodeExistingAlogs(imgDir, counting)).toEqual([]); expect(calls).toBe(1); }); test("解不开不写任何东西,下次还会再试", async () => { const { imgDir } = workspace(); mkdirSync(imgDir, { recursive: true }); writeFileSync(path.join(imgDir, "00.zip"), ALOG_ZIP); expect(await decodeExistingAlogs(imgDir, failDecoder)).toEqual([]); expect(existsSync(path.join(imgDir, "00.alog.txt"))).toBe(false); }); });