import { test, expect, describe } from "bun:test"; import { mkdtempSync, rmSync, writeFileSync, mkdirSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { detectInheritedCommits } from "../src/tracking_scan"; import { summarizeTrackingImpact, renderTrackingImpactMarkdown, trackingBaselineUnreliable, trackingNeedsAttention, EMPTY_TRACKING_IMPACT, type TrackingImpact, } from "@bbbug/shared/tracking_guard"; /** * 基线污染检测 —— 这组测试钉住的是一次生产误报。 * * 一个明确写着「不改任何代码」的验证任务,收尾扫描却报「新增了 click_text_mode_entrance * 埋点」+ 13 个未提及的变更方法,全部来自同一节点上另一条 story 相机分支。原因是 * `$AI_WORKSPACE/TikTok` 是跨任务共享的检出,而 `git reset --hard origin/` 只是 * 执行提示词里的一句话:模型跳过它,分支就从上一个任务遗留的 HEAD 切出来,三点 diff * 于是忠实地把别人的 commit 算进了这一批。 * * 注意误报的方向:护栏的设计目的是防「模型瞒报埋点」,这次却是模型如实说了没改、扫描 * 给出误报。所以这里断言的不只是「能检出」,还有「检出之后不再给内容档位」——一个自信 * 的错误结论比没有结论更糟。 */ function git(dir: string, args: string[]) { const p = Bun.spawnSync({ cmd: ["git", ...args], cwd: dir, stdout: "pipe", stderr: "pipe" }); if (p.exitCode !== 0) throw new Error(`git ${args.join(" ")}: ${p.stderr?.toString()}`); return p.stdout?.toString() ?? ""; } /** 造一个带 origin 的本地仓:origin/develop 干净,另有一条别人的分支。 */ function makeRepo(): { dir: string; cleanup: () => void } { const root = mkdtempSync(path.join(tmpdir(), "baseline-")); const remote = path.join(root, "remote.git"); const work = path.join(root, "work"); mkdirSync(remote); mkdirSync(work); git(remote, ["init", "--bare", "-b", "develop"]); git(work, ["init", "-b", "develop"]); git(work, ["config", "user.email", "t@t"]); git(work, ["config", "user.name", "t"]); writeFileSync(path.join(work, "a.txt"), "base\n"); git(work, ["add", "-A"]); git(work, ["commit", "-m", "base"]); git(work, ["remote", "add", "origin", remote]); git(work, ["push", "-u", "origin", "develop"]); return { dir: work, cleanup: () => rmSync(root, { recursive: true, force: true }) }; } describe("detectInheritedCommits", () => { test("干净分支:从 origin/develop 切出、只有自己的 commit → 无污染", async () => { const { dir, cleanup } = makeRepo(); try { git(dir, ["checkout", "-b", "feature/mine", "origin/develop"]); writeFileSync(path.join(dir, "b.txt"), "mine\n"); git(dir, ["add", "-A"]); git(dir, ["commit", "-m", "mine"]); expect(await detectInheritedCommits(dir, "develop")).toEqual({ branches: [], commits: [] }); } finally { cleanup(); } }); test("分支上没有自己的 commit → 也算干净(验证类任务的正常形态)", async () => { const { dir, cleanup } = makeRepo(); try { git(dir, ["checkout", "-b", "feature/verify-only", "origin/develop"]); expect(await detectInheritedCommits(dir, "develop")).toEqual({ branches: [], commits: [] }); } finally { cleanup(); } }); test("从别人分支的 HEAD 切出 → 检出污染并点名那条分支", async () => { const { dir, cleanup } = makeRepo(); try { // 上一个任务留在共享检出上的分支 git(dir, ["checkout", "-b", "feature/story-camera-text-swipe-tab", "origin/develop"]); writeFileSync(path.join(dir, "textmode.kt"), "onEventV3(\"click_text_mode_entrance\")\n"); git(dir, ["add", "-A"]); git(dir, ["commit", "-m", "someone else"]); // 本任务没有 reset,直接从这里切 git(dir, ["checkout", "-b", "feature/ppe-verify"]); const got = await detectInheritedCommits(dir, "develop"); expect(got).not.toBeNull(); expect(got!.branches).toContain("feature/story-camera-text-swipe-tab"); expect(got!.commits.length).toBeGreaterThan(0); } finally { cleanup(); } }); test("仓库不可用时返回 null —— 与「查过了、很干净」严格区分", async () => { expect(await detectInheritedCommits(path.join(tmpdir(), "definitely-not-a-repo-xyz"), "develop")).toBeNull(); }); }); describe("unreliable 档的呈现", () => { const dirty: TrackingImpact = { ...EMPTY_TRACKING_IMPACT, verdict: "unreliable", added: 3, baseline: { branches: ["feature/story-camera-text-swipe-tab"], commits: ["deadbeef"] }, }; test("摘要说的是「结论不可信」,不是「新增了埋点」", () => { const s = summarizeTrackingImpact(dirty); expect(s).toContain("基线不干净"); expect(s).toContain("不可信"); expect(s).not.toContain("新增了埋点"); }); test("markdown 点名污染来源,并给出修复动作", () => { const md = renderTrackingImpactMarkdown(dirty); expect(md).toContain("feature/story-camera-text-swipe-tab"); expect(md).toContain("deadbeef"); expect(md).toContain("请勿据此判断埋点是否受影响"); }); test("与「埋点被改坏」是两个谓词,不互相冒充", () => { expect(trackingBaselineUnreliable(dirty)).toBe(true); // 基线不干净 ≠ 埋点出事:不能让它去点亮「需要提需人确认」那盏灯 expect(trackingNeedsAttention(dirty)).toBe(false); const removed: TrackingImpact = { ...EMPTY_TRACKING_IMPACT, verdict: "removed", removed: 2 }; expect(trackingNeedsAttention(removed)).toBe(true); expect(trackingBaselineUnreliable(removed)).toBe(false); }); test("扫描没跑成(error)不算基线不可信", () => { const err: TrackingImpact = { ...EMPTY_TRACKING_IMPACT, error: "仓库不存在" }; expect(trackingBaselineUnreliable(err)).toBe(false); }); });