Initial import of streamline

This commit is contained in:
2026-11-16 17:26:44 -08:00
commit 8e4aec1855
1221 changed files with 274256 additions and 0 deletions
+308
View File
@@ -0,0 +1,308 @@
import { test, expect, describe } from "bun:test";
import {
changedLinesByFile, extractMethods, normalizeSymbol, symbolMatches,
parseHitSymbols, parseReportedCoverage, reconcile, renderMethodCoverageMarkdown,
summarizeMethodCoverage, langOf,
} from "@bbbug/shared/method_coverage";
/**
* 变更方法对账。
*
* 这些用例钉的是**分母**和**状态来源**,不是覆盖率数字本身:
* - 分母(diff 到底改了哪些方法)是这个模块唯一有权威的产出,算错它整节都是错的;
* - `source` 区分「命中文件」与「报告自述」,混掉两者就等于把模型自述包装成机器证据,
* 而那正是这个模块存在的理由。
* 另外两条底线单独钉死:拿不准一律 `unknown`,绝不猜成 `miss`;局部函数不能进表。
*/
const KT = `package com.x
class CommentActionViewHolder(view: View) {
fun enableClick(enable: Boolean) {
isEnabled = enable && !forceDisabled
if (forceDisabled && enable) {
itemView.alpha = FORCE_DISABLED_ALPHA
}
}
override fun onDetachedFromWindow() {
permissionLock.release()
super.onDetachedFromWindow()
}
fun withLocal() {
fun helper(): String {
return "}"
}
log(helper())
}
}
`;
describe("方法边界解析", () => {
test("Kotlin:成员方法各自成段,行区间用花括号配平算出", () => {
const spans = extractMethods(KT, "kotlin");
const names = spans.map((s) => s.name).sort();
expect(names).toEqual(["enableClick", "onDetachedFromWindow", "withLocal"]);
const enable = spans.find((s) => s.name === "enableClick")!;
expect(enable.owner).toBe("CommentActionViewHolder");
expect(enable.startLine).toBe(5);
expect(enable.endLine).toBe(10);
});
test("局部函数不进表 —— Frida hook 不到它,收进来就是凭空造一条永远未执行的假记录", () => {
const spans = extractMethods(KT, "kotlin");
expect(spans.some((s) => s.name === "helper")).toBe(false);
});
test("字符串里的花括号不打断配平", () => {
const spans = extractMethods(KT, "kotlin");
const withLocal = spans.find((s) => s.name === "withLocal")!;
// 方法体里有一个 `return "}"`,配平算错的话 endLine 会停在那一行。
expect(withLocal.endLine).toBeGreaterThan(withLocal.startLine + 3);
});
test("Swift 的 func 与 init", () => {
const spans = extractMethods(`class A {
init(x: Int) {
self.x = x
}
private func doThing() -> Bool {
return true
}
}
`, "swift");
expect(spans.map((s) => s.name).sort()).toEqual(["doThing", "init"]);
expect(spans[0].owner).toBe("A");
});
test("Java:控制流语句不会被当成方法声明", () => {
const spans = extractMethods(`public class B {
public void run(int n) {
if (n > 0) {
step();
}
for (int i = 0; i < n; i++) { step(); }
}
}
`, "java");
expect(spans.map((s) => s.name)).toEqual(["run"]);
});
test("ObjC:取第一段 selector,与 Frida 的 -[Cls sel:] 对得上", () => {
const spans = extractMethods(`@implementation AWEFeedCell
- (void)configureWithModel:(id)model extra:(id)extra {
self.model = model;
}
@end
`, "objc");
expect(spans[0]).toMatchObject({ owner: "AWEFeedCell", name: "configureWithModel" });
});
test("接口 / 抽象方法没有方法体,不产生区间", () => {
const spans = extractMethods(`interface I {
fun ping(): String
}
`, "kotlin");
expect(spans).toEqual([]);
});
test("语言判定:不认识的扩展名返回 null,由调用方点名列出", () => {
expect(langOf("a/b/C.kt")).toBe("kotlin");
expect(langOf("a/b/C.mm")).toBe("objc");
expect(langOf("a/b/c.xml")).toBeNull();
});
});
describe("diff 改动行归属", () => {
const diff = `diff --git a/app/V.kt b/app/V.kt
--- a/app/V.kt
+++ b/app/V.kt
@@ -4,3 +4,4 @@ class V {
fun a() {
- old()
+ neu()
+ more()
}
`;
test("只认改动后的行号,删除行不占位", () => {
const m = changedLinesByFile(diff);
expect([...m.get("app/V.kt")!].sort((x, y) => x - y)).toEqual([5, 6]);
});
test("改名取 b 侧路径", () => {
const m = changedLinesByFile(`diff --git a/old/Name.kt b/new/Name.kt
--- a/old/Name.kt
+++ b/new/Name.kt
@@ -1 +1 @@
-a
+b
`);
expect([...m.keys()]).toEqual(["new/Name.kt"]);
});
});
describe("符号归一化与命中判定", () => {
test("四种写法压成同一个 {owner, name}", () => {
expect(normalizeSymbol("com.a.b.Cls.method")).toEqual({ owner: "Cls", name: "method" });
expect(normalizeSymbol("Cls.method(int)")).toEqual({ owner: "Cls", name: "method" });
expect(normalizeSymbol("-[AWEFeedCell configureWithModel:extra:]")).toEqual({ owner: "AWEFeedCell", name: "configureWithModel" });
expect(normalizeSymbol("bareMethod")).toEqual({ owner: "", name: "bareMethod" });
});
test("owner 缺一边就算命中 —— 拿不准偏向沉默,不偏向报警", () => {
expect(symbolMatches({ owner: "", name: "f" }, { owner: "Cls", name: "f" })).toBe(true);
expect(symbolMatches({ owner: "Cls", name: "f" }, { owner: "", name: "f" })).toBe(true);
});
test("两边 owner 都确定且不同才拒绝", () => {
expect(symbolMatches({ owner: "Other", name: "f" }, { owner: "Cls", name: "f" })).toBe(false);
});
test("命中文件:裸符号行、JSON 行、显式 hit:false 混排", () => {
const hits = parseHitSymbols([
"com.x.CommentActionViewHolder.enableClick",
'{"symbol":"com.x.Other.ping","hits":3}',
'{"symbol":"com.x.Other.never","hit":false}',
"",
"not json {",
].join("\n"));
expect(hits.map((h) => h.name)).toEqual(["enableClick", "ping"]);
});
});
describe("报告自述小节", () => {
const md = `# FixReport
## 代码覆盖检查
覆盖率 1/2 个方法
- 命中:\`CommentActionViewHolder.enableClick\`
- 未执行:\`CommentActionViewHolder.onDetachedFromWindow\`
- 没记录到:\`StoryPermissionCache.invalidate\`(attach 失败)
## 验证局限
无
`;
test("三态按行内提示词分类", () => {
const r = parseReportedCoverage(md);
expect(r.absent).toBe(false);
expect(r.hit).toEqual(["CommentActionViewHolder.enableClick"]);
expect(r.miss).toEqual(["CommentActionViewHolder.onDetachedFromWindow"]);
expect(r.unrecorded).toEqual(["StoryPermissionCache.invalidate"]);
});
test("没有这一节 → absent(与「有节但空」不同)", () => {
expect(parseReportedCoverage("# R\n\n## 验证局限\n无\n").absent).toBe(true);
});
test("不读自己写回去的机械对账小节,免得自己跟自己对账", () => {
const r = parseReportedCoverage(`## 代码覆盖检查(机械对账)
- 命中:\`A.b\`
`);
expect(r.absent).toBe(true);
expect(r.hit).toEqual([]);
});
});
describe("对账", () => {
const methods = [
{ file: "app/V.kt", owner: "CommentActionViewHolder", name: "enableClick", startLine: 5, endLine: 10, changedLines: 2 },
{ file: "app/V.kt", owner: "CommentActionViewHolder", name: "onDetachedFromWindow", startLine: 12, endLine: 15, changedLines: 1 },
];
test("命中文件优先于报告自述,并在 source 上留痕", () => {
const c = reconcile({
methods,
hits: [{ owner: "CommentActionViewHolder", name: "enableClick" }],
reported: { hit: [], miss: ["CommentActionViewHolder.onDetachedFromWindow"], unrecorded: [], absent: false },
unparsedFiles: [],
});
expect(c.methods[0]).toMatchObject({ state: "hit", source: "hits-file" });
expect(c.methods[1]).toMatchObject({ state: "miss", source: "report" });
expect(c.percent).toBe(50);
expect(c.verdict).toBe("consistent");
});
test("命中文件里没有、报告也没提 → unknown,绝不判 miss", () => {
const c = reconcile({
methods,
hits: [{ owner: "CommentActionViewHolder", name: "enableClick" }],
reported: { hit: [], miss: [], unrecorded: [], absent: false },
unparsedFiles: [],
});
expect(c.methods[1]).toMatchObject({ state: "unknown", source: "none" });
expect(c.miss).toBe(0);
expect(c.unreported).toEqual(["CommentActionViewHolder.onDetachedFromWindow"]);
expect(c.verdict).toBe("mismatch");
});
test("没记录到不进分母", () => {
const c = reconcile({
methods,
hits: null,
reported: {
hit: ["CommentActionViewHolder.enableClick"],
miss: [],
unrecorded: ["CommentActionViewHolder.onDetachedFromWindow"],
absent: false,
},
unparsedFiles: [],
});
expect(c.percent).toBe(100);
expect(c.unrecorded).toBe(1);
});
test("报告提到、diff 没改的方法单独列出", () => {
const c = reconcile({
methods: [methods[0]],
hits: null,
reported: { hit: ["CommentActionViewHolder.enableClick", "Ghost.method"], miss: [], unrecorded: [], absent: false },
unparsedFiles: [],
});
expect(c.reportedOnly).toEqual(["Ghost.method"]);
expect(c.verdict).toBe("mismatch");
});
test("既无命中文件也无报告小节 → no-coverage-data", () => {
const c = reconcile({ methods, hits: null, reported: { hit: [], miss: [], unrecorded: [], absent: true }, unparsedFiles: [] });
expect(c.verdict).toBe("no-coverage-data");
});
test("一个方法都没解析出来 → unparsed,并点名未解析的文件", () => {
const c = reconcile({ methods: [], hits: null, reported: { hit: [], miss: [], unrecorded: [], absent: true }, unparsedFiles: ["res/layout/a.xml"] });
expect(c.verdict).toBe("unparsed");
expect(renderMethodCoverageMarkdown(c)).toContain("res/layout/a.xml");
});
});
describe("产出的那份 markdown", () => {
const c = reconcile({
methods: [
{ file: "app/V.kt", owner: "CommentActionViewHolder", name: "enableClick", startLine: 5, endLine: 10, changedLines: 2 },
{ file: "app/V.kt", owner: "CommentActionViewHolder", name: "onDetachedFromWindow", startLine: 12, endLine: 15, changedLines: 1 },
],
hits: [{ owner: "CommentActionViewHolder", name: "enableClick" }],
reported: { hit: [], miss: [], unrecorded: [], absent: false },
unparsedFiles: [],
});
test("标题与 Agent 自写的那节不同名 —— 两份都留着,对不上才看得见", () => {
expect(renderMethodCoverageMarkdown(c)).toContain("## 代码覆盖检查(机械对账)");
});
test("带文件:行区间 —— 下游按方法切段染色就靠它", () => {
expect(renderMethodCoverageMarkdown(c)).toContain("`app/V.kt:5-10`");
});
test("状态来源逐行可见,并写明覆盖率不是门槛", () => {
const md = renderMethodCoverageMarkdown(c);
expect(md).toContain("命中文件");
expect(md).toContain("不构成验收门槛");
});
test("一行摘要能脱离上下文读懂", () => {
expect(summarizeMethodCoverage(c)).toContain("diff 实际改动 2 个方法");
expect(summarizeMethodCoverage(null)).toBe("未做变更方法对账");
});
});