import { describe, expect, test } from "bun:test"; import { readFileSync } from "node:fs"; import path from "node:path"; import { CI_COVERAGE_CHECKPOINT, CI_STATUS_FILE, MappingClassNames, MappingCollector, buildCiCoverageData, buildCiFridaScript, buildCiRelay, casesFromSourceSpec, changedMethodsFromDiff, ciClockOffset, classesInArgs, fridaTypeName, isCiCoverageData, methodHunk, parseCiEvents, renderCiCoverageSummaryHtml, renderCiStatus, resolveMethodTargets, specLineBlock, specSectionBody, splitOrdinalItems, type CiChangedMethod, type CiCoverageSpec, type CiEventRaw, type CiMethodsFile, type MappingClass, } from "@bbbug/shared/ci_coverage"; import { buildCaseRunData, parseCaseRunReport } from "@bbbug/shared/case_run_report"; import type { ManifestEntry } from "@bbbug/shared/evidence_capture"; import { parseVerifySpec, verifyReportFile, type VerifySpec } from "@bbbug/shared/verify_prompt"; import type { StreamlineCaseSource } from "@bbbug/shared/test_cases"; import type { Verification } from "@bbbug/shared/types"; import { buildVerificationFirstPrompt, buildVerificationSecondPrompt, verificationPromptStage } from "../src/verify_runner"; /** * 「CI 包覆盖率验证」的几段契约(shared/ci_coverage.ts): * 1. 来源单 TestSpec → 用例 / 检查点(接力的就是第一次验证过的路径) * 2. MR diff → 改动方法 → R8 mapping 里的混淆名(Frida 靠它 hook;认错一个就是把别人的命中算到它头上) * 3. Frida 事件(设备时钟)+ manifest(宿主时钟)→ 录像时间轴 / 检查点 ↔ 代码 * 4. 规约 ↔ 模板:节点按 format 挑模板,运行时块代入 * 夹具里的 mapping 片段和类名取自 MR 8439663 的真实 CI 包(2026-09-24)。 */ // ── 1. TestSpec → 用例 ────────────────────────────────────────────────────── const BUG_SPEC = `## 复现条件 // Reproduct condition - 账号/环境: - 主态 A(接收 mention 并执行 add to story 的账号) - 副账号 B:从账号池中选取,与 A 互相关注 - UI 入口指引: - A 端从 Story Feed 打开 B 那条 @A 的 story → Add to story ## 复现步骤 // Operations 1. B 在 story 里用贴纸面板的 @提及贴纸 @A 并发布。 2. A 打开这条 story,点 Add to story,存草稿。 3. 进入个人页草稿箱,重新打开草稿。 修复后验证(必须覆盖以下四类,缺一不可): 1. 负向:重新执行上述复现路径,重开草稿后不再多出 @ 贴纸。 2. 正向:草稿中保留预期的 @ 贴纸, mention 信息正确、可点击。 3. 机制级:判定点为 checkAndConvertMentionStickerDraft。 4. 对抗性条件:冷启动后再从草稿箱打开;多次往返后数量稳定。 ## 期望与实际 - 期望:只有正常内容。`; const FEATURE_SPEC = `## 目标 做一个新入口。 ## 验证环境 - 账号/权限:普通账号 - 入口路径:Profile → 设置 ## 验收标准 1. 功能达成(正向):入口出现并可点击。 2. 机制级(真实接入):点击后请求 /api/x 返回 200。 3. 无回归:老入口不受影响。 ## 风险点 - 无`; const SRC: StreamlineCaseSource = { kind: "streamline-task", url: "https://streamline.example?task_id=t1", taskKind: "bug", taskId: "t1", taskTitle: "add to story 草稿重复 mention", fetchedAt: 0, fetchedBy: "a@b.com", }; describe("来源单 TestSpec → 用例", () => { test("分节:标题带双语注记也认;修复后验证是一行正文不是标题", () => { expect(specSectionBody(BUG_SPEC, ["复现条件"])).toContain("副账号 B"); expect(specSectionBody(BUG_SPEC, ["复现步骤"])).toContain("重新打开草稿"); const block = specLineBlock(BUG_SPEC, ["修复后验证"])!; expect(block).toContain("负向"); expect(block).not.toContain("期望与实际"); }); test("编号列表按最外层拆,续行并进上一条", () => { const items = splitOrdinalItems(specLineBlock(BUG_SPEC, ["修复后验证"])!); expect(items).toHaveLength(4); expect(items[1]).toContain("mention 信息正确"); }); test("bug:C01 = 修复后验证四条,前置 / 步骤原文带上;C02 = 覆盖补测", () => { const s = casesFromSourceSpec({ kind: "bug", executionPlan: BUG_SPEC, source: SRC }); expect(s.source.kind).toBe("streamline-task"); expect(s.cases.map((c) => c.id)).toEqual(["C01", "C02"]); expect(s.cases[0].checkpoints.map((p) => p.no)).toEqual([1, 2, 3, 4]); expect(s.cases[0].pre).toContain("主态 A"); expect(s.cases[0].step).toContain("Add to story"); expect(s.cases[1].checkpoints).toEqual([{ no: 5, expect: CI_COVERAGE_CHECKPOINT, leafId: null }]); }); test("feature:检查点取验收标准,前置取验证环境", () => { const s = casesFromSourceSpec({ kind: "feature", executionPlan: FEATURE_SPEC, source: { ...SRC, taskKind: "feature" } }); expect(s.cases[0].checkpoints).toHaveLength(3); expect(s.cases[0].pre).toContain("入口路径"); expect(s.cases[0].step).toBeNull(); }); test("没有 TestSpec:不编检查点,退化成一条「走通主路径」", () => { const s = casesFromSourceSpec({ kind: "bug", executionPlan: null, source: SRC }); expect(s.cases[0].checkpoints).toHaveLength(1); expect(s.cases[0].checkpoints[0].expect).toContain("走通"); }); test("接力路径:TestSpec 的条件 / 步骤 + 报告里讲验证的小节", () => { const relay = buildCiRelay({ kind: "bug", executionPlan: BUG_SPEC, report: "# FixReport\n\n## 根因\n不相关\n\n## 修复验证对照\n- 冷启动后仍为 1\n\n## 其他\n不要", }); expect(relay).toContain("副账号 B"); expect(relay).toContain("冷启动后仍为 1"); expect(relay).not.toContain("不相关"); }); }); // ── 2. diff → 改动方法 → mapping ───────────────────────────────────────────── const FILE = "components/business/creative_tools/camera-editor-sticker/src/main/java/com/ss/android/ugc/aweme/sticker/utils/StickerNewEngineDraftUtils.kt"; const AFTER = `package com.ss.android.ugc.aweme.sticker.utils import dmt.av.video.canvas.isStoryShareStory object StickerNewEngineDraftUtils { fun checkAndConvertPollStickerDraft(editModel: VideoPublishEditModel, api: EditorInfiniStickerApi?) { doPoll(editModel) } fun checkAndConvertMentionStickerDraft( editModel: VideoPublishEditModel, infiniStickerApi: EditorInfiniStickerApi?, ) { removeStickerItemModelFromInfoStickerModel(1, editModel) val canvasVideoData = editModel.canvasVideoData if (canvasVideoData.isStoryShareStory || canvasVideoData?.isBackwardsMention() == true) { return } val (width, height) = infiniStickerApi?.getStickerContainerSize() ?: return } } `; const DIFF = `diff --git a/${FILE} b/${FILE} index 840febc..0286a3a 100644 --- a/${FILE} +++ b/${FILE} @@ -1,3 +1,4 @@ package com.ss.android.ugc.aweme.sticker.utils +import dmt.av.video.canvas.isStoryShareStory @@ -13,3 +14,7 @@ object StickerNewEngineDraftUtils { ) { removeStickerItemModelFromInfoStickerModel(1, editModel) + val canvasVideoData = editModel.canvasVideoData + if (canvasVideoData.isStoryShareStory || canvasVideoData?.isBackwardsMention() == true) { + return + } val (width, height) = infiniStickerApi?.getStickerContainerSize() ?: return `; const MAPPING = readFileSync(path.join(import.meta.dir, "fixtures/ci/mapping-sticker-utils.txt"), "utf8"); function collect(text: string, want: (pkg: string, sf: string | null, simple: string) => boolean): { classes: MappingClass[]; names: MappingClassNames } { const c = new MappingCollector(want); for (const l of text.split("\n")) c.feed(l); const classes = c.end(); const args = new Set(); for (const k of classes) for (const m of k.methods) for (const a of classesInArgs(m.args)) args.add(a); const names = new MappingClassNames(args); for (const l of text.split("\n")) names.feed(l); return { classes, names }; } describe("MR diff → 改动方法", () => { test("只收方法体里真正改了的方法;import 这类方法外的改动不算", () => { const { methods, unparsed } = changedMethodsFromDiff(DIFF, (f) => (f === FILE ? AFTER : null)); expect(unparsed).toEqual([]); expect(methods.map((m) => m.name)).toEqual(["checkAndConvertMentionStickerDraft"]); const m = methods[0]; expect(m.owner).toBe("StickerNewEngineDraftUtils"); expect(m.pkg).toBe("com.ss.android.ugc.aweme.sticker.utils"); expect([m.startLine, m.endLine, m.changedLines]).toEqual([11, 21, 4]); expect(m.id).toBe(`${FILE}#checkAndConvertMentionStickerDraft@11`); expect(m.hunk).toContain("+ val canvasVideoData = editModel.canvasVideoData"); expect(m.hunk).not.toContain("import dmt"); }); test("读不到文件就点名,不猜", () => { expect(changedMethodsFromDiff(DIFF, () => null).unparsed).toEqual([FILE]); }); test("methodHunk 只留改动行前后三行", () => { const h = methodHunk(DIFF, 11, 21).split("\n"); expect(h.filter((l) => l.startsWith("+"))).toHaveLength(4); expect(h.length).toBeLessThanOrEqual(4 + 6); }); }); describe("R8 mapping → hook 目标", () => { const { methods } = changedMethodsFromDiff(DIFF, () => AFTER); const m = methods[0]; test("按 sourceFile 认类,翻译方法名和参数类型(真实 CI 包的 mapping)", () => { const { classes, names } = collect(MAPPING, (pkg, sf) => pkg === m.pkg && (sf === null || sf === "StickerNewEngineDraftUtils.kt")); const r = resolveMethodTargets(m, classes, (c) => names.obfOf.get(c)); expect(r.status).toBe("hookable"); expect(r.origClass).toBe("com.ss.android.ugc.aweme.sticker.utils.StickerNewEngineDraftUtils"); expect(r.targets).toEqual([{ cls: "com.ss.android.ugc.aweme.sticker.utils.x", method: "LJFF", args: ["com.ss.android.ugc.aweme.shortvideo.edit.VideoPublishEditModel", "com.ss.android.ugc.gamora.editor.sticker.core.newengine.b"], }]); }); test("包里没有这个方法(被内联 / 裁掉)就不给 hook,原因写清", () => { const { classes, names } = collect(MAPPING, () => true); const r = resolveMethodTargets({ ...m, name: "gone" }, classes, (c) => names.obfOf.get(c)); expect(r.status).toBe("not-in-package"); expect(r.targets).toEqual([]); expect(r.reason).toContain("gone"); }); test("带行号的 mapping:同区间同混淆名的一串里,前面的是内联帧,不能 hook", () => { const text = [ "com.foo.Bar -> a.b:", '# {"id":"sourceFile","fileName":"Bar.kt"}', " 1:3:void inner():10:12 -> a", " 1:3:void outer():20 -> a", " 4:6:void solo(int[],com.foo.Bar):30:32 -> b", ].join("\n"); const { classes, names } = collect(text, () => true); const at = (name: string) => resolveMethodTargets({ file: "src/com/foo/Bar.kt", pkg: "com.foo", owner: "Bar", name }, classes, (c) => names.obfOf.get(c)); expect(at("inner").status).toBe("not-in-package"); expect(at("outer").targets).toEqual([{ cls: "a.b", method: "a", args: [] }]); expect(at("solo").targets[0].args).toEqual(["[I", "a.b"]); }); test("Frida 类型写法:基本类型原样,数组用描述符,类名翻译", () => { const obf = (c: string) => (c === "com.foo.Bar" ? "a.b" : undefined); expect(fridaTypeName("int", obf)).toBe("int"); expect(fridaTypeName("long[][]", obf)).toBe("[[J"); expect(fridaTypeName("com.foo.Bar[]", obf)).toBe("[La.b;"); expect(fridaTypeName("java.lang.String", obf)).toBe("java.lang.String"); }); }); // ── Frida 脚本 ────────────────────────────────────────────────────────────── describe("Frida 脚本", () => { const method = (over: Partial = {}): CiChangedMethod => ({ id: "f#m@1", file: "f.kt", pkg: "p", owner: "O", name: "m", startLine: 1, endLine: 2, changedLines: 1, hunk: "", status: "hookable", reason: null, origClass: "p.O", targets: [{ cls: "p.x", method: "LJFF", args: ["p.A", "[I"] }], ...over, }); test("语法正确;只挂可 hook 的;点击 / 触摸 / 屏幕尺寸都在", () => { const js = buildCiFridaScript([method(), method({ id: "g#n@1", status: "not-in-package", targets: [] })]); expect(() => new Function(js)).not.toThrow(); expect(js).toContain('"cls":"p.x"'); expect(js).not.toContain("g#n@1"); expect(js).toContain("performClick"); expect(js).toContain("dispatchTouchEvent"); expect(js).toContain("getRealMetrics"); }); }); // ── 3. 时间轴 ──────────────────────────────────────────────────────────────── const SPEC: CiCoverageSpec = { source: { kind: "bug", id: "t1", title: "add to story 草稿重复 mention", url: "https://s?task_id=t1" }, mr: { id: 8439663, url: "https://bits/x/8439663", spaceId: 1, projectId: 113818, iid: 300089, commit: "5f856ef24f56c6116c3cef021aa73bbb2640c243", baseCommit: "b3ac7b8", branch: "p/x", targetBranch: "main", title: "fix" }, pkg: { platform: "android", appId: "com.zhiliaoapp.musically", name: "M-5f856e-local_test-release", url: "https://v/x.apk", md5: null, sizeBytes: null, versionName: "47.2.0", channel: "local_test", buildType: "gradle_fast", jobUrl: null, mappingUrl: "https://v/mapping.txt", builtAt: null }, coverageUrl: "https://bits/devops/1/code/detail/8439663?tab=testing", relay: "### 第一次执行确认过的操作路径\n打开草稿", }; function timelineFixture() { const suite = casesFromSourceSpec({ kind: "bug", executionPlan: BUG_SPEC, source: SRC }); const report = parseCaseRunReport(`# 报告 ## 1. 逐条结论 ### C01 | # | 结论 | 实际 | 细节 | |---|---|---|---| | 1 | 通过 | 重开草稿后只有一个 @ | — | | 2 | 通过 | @ 贴纸还在 | — | ## 2. 阻塞与局限 | 用例 | 卡在哪 | 已做到哪一步 / 还差什么 | |---|---|---| ## 3. 覆盖补测 | 方法 | 怎么触发的 / 为什么触发不了 | |---|---| | X.other | 入口在实验后面 | `)!; const T0 = 1_000_000; const manifest = [ { v: 1, ts: T0, event: "start", captureId: "c1", out: "case-C01-r1.mp4", intent: "C01 重开草稿", plannedSteps: "打开草稿箱" }, { v: 1, ts: T0 + 6_000, takenAt: T0 + 5_000, event: "shot", captureId: "s1", out: "case-C01-r1-cp01.png" }, { v: 1, ts: T0 + 13_000, takenAt: T0 + 12_000, event: "shot", captureId: "s2", out: "case-C01-r1-cp02.png" }, { v: 1, ts: T0 + 20_000, event: "stop", captureId: "c1", out: "case-C01-r1.mp4", durationS: 20, probe: { durationS: 19 } }, ] as unknown as ManifestEntry[]; const files = ["case-C01-r1.mp4", "case-C01-r1-cp01.png", "case-C01-r1-cp02.png"]; const hit: CiChangedMethod = { id: `${FILE}#checkAndConvertMentionStickerDraft@11`, file: FILE, pkg: "p", owner: "StickerNewEngineDraftUtils", name: "checkAndConvertMentionStickerDraft", startLine: 11, endLine: 21, changedLines: 4, hunk: "+x", status: "hookable", reason: null, origClass: "p.S", targets: [{ cls: "p.x", method: "LJFF", args: [] }], }; const missed: CiChangedMethod = { ...hit, id: `${FILE}#other@30`, name: "other", startLine: 30, endLine: 40 }; const gone: CiChangedMethod = { ...hit, id: `${FILE}#gone@50`, name: "gone", status: "not-in-package", reason: "被内联", targets: [] }; const methodsFile: CiMethodsFile = { v: 1, commit: SPEC.mr.commit, baseCommit: "b", mappingUrl: null, methods: [hit, missed, gone], unparsed: [], error: null }; // 设备时钟比宿主慢 3s;code 那一条在 frida 的管道里多缓冲了 500ms —— 时刻必须按设备时间 + 偏移算。 const events: CiEventRaw[] = [ { sid: 1, recv: T0 + 1_000, dt: T0 - 2_000, k: "hello" }, { sid: 1, recv: T0 + 1_100, dt: T0 - 1_900, k: "screen", w: 1000, h: 2000 }, { sid: 1, recv: T0 + 1_200, dt: T0 - 1_800, k: "ready", hooks: 1 }, { sid: 1, recv: T0 + 9_100, dt: T0 + 6_000, k: "click", x: 100, y: 200, w: 300, h: 100, text: "Drafts" }, { sid: 1, recv: T0 + 13_500, dt: T0 + 7_000, k: "code", id: hit.id }, ]; return { suite, report, manifest, files, methodsFile, events, hit, missed, gone }; } describe("时间轴:Frida 事件 × manifest", () => { test("时钟偏移取 min(recv - dt):缓冲晚到的那条不算数", () => { const { events } = timelineFixture(); expect(ciClockOffset(events)).toBe(3_000); }); test("录像上的时刻、点击矩形、检查点 ↔ 代码、方法状态", () => { const f = timelineFixture(); const base = buildCaseRunData(f.suite, f.report, { files: f.files, manifest: f.manifest }); const d = buildCiCoverageData(base, { spec: SPEC, methodsFile: f.methodsFile, events: f.events, manifest: f.manifest, files: f.files, run: null }); expect(isCiCoverageData(d)).toBe(true); const tr = d.ci.tracks[0]; expect(tr.file).toBe("case-C01-r1.mp4"); // 录制器启动延迟 1s(挂钟 20s - 真实 19s)要扣掉。 const code = tr.events.find((e) => e.kind === "code")!; const click = tr.events.find((e) => e.kind === "click")!; expect(code.t).toBe(9); // 宿主 T0+10s - 1s expect(click.t).toBe(8); expect(click.kind === "click" && click.rect).toEqual({ x: 0.1, y: 0.1, w: 0.3, h: 0.05 }); expect(tr.events.filter((e) => e.kind === "shot").map((e) => e.t)).toEqual([4, 11]); expect(tr.narration.map((n) => n.kind)).toEqual(["start", "shot", "click", "code", "shot"]); expect(tr.narration.find((n) => n.kind === "click")!.text).toBe("点击「Drafts」"); // 代码在 #1 之后、#2 之前执行 → 挂在 #2 上。 expect(d.ci.checkpointCode).toEqual({ 2: [{ methodId: f.hit.id, n: 1 }] }); const states = Object.fromEntries(d.ci.methods.map((m) => [m.name, m.state])); expect(states).toEqual({ checkAndConvertMentionStickerDraft: "hit", other: "miss", gone: "not-in-package" }); expect(d.ci.methods[0].nearCheckpoints).toEqual([1, 2]); expect(d.ci.totals).toEqual({ methods: 3, hookable: 2, hit: 1, notInPackage: 1 }); // 第 3 节「覆盖补测」按附加章节原样带上。 expect(d.extra.map((x) => x.heading)).toContain("3. 覆盖补测"); // 体检:没装包记录 → FAIL;执行到 1/2 → WARN。 const h = Object.fromEntries(d.health.map((x) => [x.name, x.verdict])); expect(h["装的是 CI 插桩包"]).toBe("fail"); expect(h["改动方法执行到"]).toBe("warn"); expect(renderCiCoverageSummaryHtml(d)).toContain("执行到 1 个"); }); test("没收到任何事件:未执行不算 miss,记成「未采集」", () => { const f = timelineFixture(); const base = buildCaseRunData(f.suite, f.report, { files: f.files, manifest: f.manifest }); const d = buildCiCoverageData(base, { spec: SPEC, methodsFile: f.methodsFile, events: [], manifest: f.manifest, files: f.files, run: null }); expect(d.ci.methods.find((m) => m.name === "other")!.state).toBe("no-recorder"); }); test("事件文件容错:半行、非 JSON 跳过", () => { expect(parseCiEvents('{"sid":1,"recv":2,"dt":1,"k":"hello"}\n{"sid":1,"re\nnot json\n')).toHaveLength(1); }); test("状态清单:已执行 / 未执行 / 包里没有 三种都写明", () => { const f = timelineFixture(); const md = renderCiStatus(f.methodsFile.methods, new Map([[f.hit.id, 1_000]]), new Map([[f.hit.id, 3]])); expect(md).toContain("✓ 已执行"); expect(md).toContain("3 次"); expect(md).toContain("✗ 未执行"); expect(md).toContain("包里没有"); }); }); // ── 4. 规约与模板 ─────────────────────────────────────────────────────────── function verification(spec: Partial = {}): Verification { const full: VerifySpec = { format: "ci-coverage", scope: "", cases: casesFromSourceSpec({ kind: "bug", executionPlan: BUG_SPEC, source: SRC }), ci: SPEC, ...spec, }; return { id: "abcdef12-0000-0000-0000-000000000000", title: "CI 覆盖验证 · x", description: "d", platform: "android", baseRef: null, remark: null, attachments: null, verifySpec: JSON.stringify(full), reviewFeedback: null, reviewHistory: null, workspacePath: "/tmp/ws", pinned: 0, status: "awaiting_verify", priorityKey: 0, watchers: [], createdAt: 0, startedAt: null, finishedAt: null, durationMs: null, totalDurationMs: null, logPath: null, failureReason: null, attempts: 0, executorNodeId: null, baseline: null, }; } describe("ci-coverage 的规约与模板", () => { test("没有 CI 包信息的 ci-coverage 规约是坏规约,不降级成别的格式", () => { expect(parseVerifySpec(JSON.stringify({ format: "ci-coverage", scope: "" }))).toBeNull(); expect(parseVerifySpec(verification().verifySpec)!.format).toBe("ci-coverage"); expect(verifyReportFile("ci-coverage")).toBe("Case-Run-Report.md"); }); test("按格式挑 verify.cicov.*", () => { expect(verificationPromptStage(verification(), false)).toBe("verify.cicov.first"); expect(verificationPromptStage(verification(), true)).toBe("verify.cicov.second"); }); test("首轮 prompt:包、接力路径、用例、改动方法清单、覆盖补测一节都在,变量全部代入", () => { const ci: CiMethodsFile = { v: 1, commit: SPEC.mr.commit, baseCommit: "b", mappingUrl: null, unparsed: [], error: null, methods: [{ id: "f#m@1", file: FILE, pkg: "p", owner: "StickerNewEngineDraftUtils", name: "checkAndConvertMentionStickerDraft", startLine: 11, endLine: 21, changedLines: 4, hunk: "", status: "hookable", reason: null, origClass: "p.S", targets: [], }], }; const p = buildVerificationFirstPrompt(verification(), "/tmp/ws", null, { ci }); expect(p).toContain("M-5f856e-local_test-release"); expect(p).toContain("打开草稿"); // relay expect(p).toContain("### C01"); expect(p).toContain("StickerNewEngineDraftUtils.checkAndConvertMentionStickerDraft"); expect(p).toContain(`/tmp/ws/evidence/${CI_STATUS_FILE}`); expect(p).toContain("## 3. 覆盖补测"); expect(p).toContain("不要杀 App"); expect(p).not.toMatch(/\{\{\w+\}\}/); }); test("清单没算出来时 prompt 照样能用,并说明情况", () => { const p = buildVerificationSecondPrompt(verification(), "/tmp/ws", null, { ci: null }); expect(p).toContain("改动方法清单没有算出来"); expect(p).not.toMatch(/\{\{\w+\}\}/); }); });