import { test, expect, describe, afterEach } from "bun:test"; import { mkdirSync, writeFileSync, rmSync } from "node:fs"; import path from "node:path"; /** * 验证线走查报告的组装契约(见 verify_readable.ts)。任何一条错了,面板都会静默退回 * 一份普通报告,而不是报错: * - kind 必须是 "audit"(否则 generateReadableReport 不走确定性渲染); * - workspacePath 必须指向验证工作区(结构树和截图像素只在那里); * - item 必须是 verify(Lark 身份与回调归属)。 * * 另钉一条迁移兼容:0036 之前的验证任务是 feature,工作区在 `feature-/`, * workspaceDirForVerification 要能找回去。 * * WORKSPACE_ROOT 在模块加载时定死,所以问模块本身工作区落在哪,用一次性 id 建、跑完删。 */ const { workspaceDirForVerification, workspaceVerifyReportPath, workspaceEvidenceDir, workspaceDirForFeature } = await import("../src/workspace"); const { buildVerificationReadableInput } = await import("../src/verify_readable"); const PNG = Buffer.from("89504e470d0a1a0a", "hex"); const DIRS: string[] = []; afterEach(() => { for (const d of DIRS.splice(0)) { try { rmSync(d, { recursive: true, force: true }); } catch { /* ignore */ } } }); function seed(ws: string, report: string | null, evidence: string[] = []): void { DIRS.push(ws); mkdirSync(workspaceEvidenceDir(ws), { recursive: true }); if (report !== null) writeFileSync(workspaceVerifyReportPath(ws), report, "utf8"); for (const name of evidence) writeFileSync(path.join(workspaceEvidenceDir(ws), name), PNG); } describe("buildVerificationReadableInput", () => { test("kind=audit、工作区、item 都对", async () => { const id = `test-verify-${crypto.randomUUID()}`; const ws = path.join(path.dirname(workspaceDirForFeature("x")), `verify-${id}`); seed(ws, "# 走查\n", ["flow-r1-en-dm-inbox.png"]); const input = await buildVerificationReadableInput(id, { title: "走查" }); expect(input).not.toBeNull(); expect(input!.kind).toBe("audit"); expect(input!.workspacePath).toBe(ws); expect(input!.item).toEqual({ kind: "verify", id }); expect(input!.rawMarkdown).toBe("# 走查\n"); expect(input!.processShots).toContain("flow-r1-en-dm-inbox.png"); // flow-* 按约定归入过程截图 }); test("盘上没有报告、调用方也没给原文 → null(路由转 404)", async () => { const id = `test-verify-${crypto.randomUUID()}`; seed(path.join(path.dirname(workspaceDirForFeature("x")), `verify-${id}`), null); expect(await buildVerificationReadableInput(id, {})).toBeNull(); }); }); describe("workspaceDirForVerification", () => { test("迁移前的验证任务:新目录不存在时退回 feature-/", () => { const id = `test-verify-${crypto.randomUUID()}`; const legacy = workspaceDirForFeature(id); DIRS.push(legacy); mkdirSync(legacy, { recursive: true }); expect(workspaceDirForVerification(id)).toBe(legacy); }); test("新任务落在 verify-/", () => { const id = `test-verify-${crypto.randomUUID()}`; expect(path.basename(workspaceDirForVerification(id))).toBe(`verify-${id}`); }); });