software-review-report.test.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { expect, test } from "bun:test"
  2. import { candidateImpact, nextReviewStep, reviewReport } from "../src/util/software-review-report"
  3. import type { Review, Evidence } from "../src/util/software-review"
  4. import { businessText } from "../src/util/software-review-business"
  5. const fixture = (): Review => ({
  6. id: "a".repeat(32),
  7. revision: 5,
  8. project: "Synthetic fleet",
  9. phase: "awaiting_review",
  10. candidates: [
  11. {
  12. id: "c1",
  13. path: "Fleet.java",
  14. symbol: "Fleet#start",
  15. suggestion: "Extract responsibility",
  16. method_range: { start: 1, end: 106 },
  17. source_excerpt: [],
  18. test_hints: ["FleetTest.java"],
  19. evidence: [],
  20. },
  21. ],
  22. decisions: { c1: "selected" },
  23. coverage: { analyzed_files: 2, java_files: 2 },
  24. proposal: null,
  25. execution: { source_unchanged: true, result: { generation: 1, changed_files: ["Fleet.java"] } },
  26. })
  27. const comparison = {
  28. candidate_changes: {
  29. resolved: [{ symbol: "Fleet#start", metrics: { method_lines: 106 } }],
  30. remaining: [],
  31. introduced: [{ symbol: "Fleet#createProfile", metrics: { method_lines: 75 } }],
  32. complete: true,
  33. },
  34. }
  35. test("business review shows test bindings and exclusions before and after execution", () => {
  36. const spec = { profile: "automotive", status: "incomplete", passed: false,
  37. contracts: [{ category: "units_ranges", invariant: "Keep units", test_path: "Check.java", test_case: "Check#units",
  38. quote: "assert speed == 36;", status: "unverified", reason: "Assertion did not match" }],
  39. exclusions: [{ category: "variant_revision", reason: "No variant field in this fixture" }],
  40. missing_categories: ["protocol_compatibility"] }
  41. expect(businessText(spec, "plan")).toContain("断言:assert speed == 36;")
  42. const state = fixture()
  43. state.execution = { result: { business_acceptance: { baseline: spec, current: spec } } }
  44. const report = reviewReport(state, 2)
  45. for (const value of ["重构前 · 业务验收", "重构后 · 业务验收", "Check.java → Check#units", "Assertion did not match",
  46. "排除 · 车型与版本", "缺项 · 协议兼容"]) expect(report).toContain(value)
  47. expect(report).not.toContain("声明范围通过")
  48. expect(businessText(undefined, "result")).toContain("尚未取得结果")
  49. expect(businessText(undefined, "plan")).toContain("未声明")
  50. expect(businessText({ status: "not_declared" }, "result")).toContain("未声明")
  51. expect(businessText({ ...spec, contracts: [{ ...spec.contracts[0], executed_test_cases: ["actual::one", "actual::two"] }] }, "result"))
  52. .toContain("已绑定 2 个实际执行用例")
  53. })
  54. test("ontology failure stays visible even when build and native tests pass", () => {
  55. const state = fixture()
  56. state.phase = "verification_failed"
  57. const evidence: Evidence = { id: state.id, revision: state.revision, token: "t", files: [], checks: {
  58. "round-1/checks.json": { passed: false, build_status: "passed", test_status: "passed",
  59. ontology_error: "Declared rule target no longer resolves" },
  60. } }
  61. expect(reviewReport(state, 2, { evidence })).toContain("本体重建失败:Declared rule target no longer resolves")
  62. expect(reviewReport(state, 3, { evidence })).toContain("本体重建问题")
  63. })
  64. test("reports residual extracted methods without upgrading a disappearing finding to success", () => {
  65. const value = reviewReport(fixture(), 1, { comparison })
  66. expect(value).toContain("106 行")
  67. expect(value).toContain("75 行")
  68. expect(value).toContain("新增待审线索")
  69. expect(value).toContain("原仓库:保持不变")
  70. expect(value).not.toContain('"candidate_changes"')
  71. expect(reviewReport(fixture(), 3, { comparison })).toContain("新增线索:Fleet#createProfile")
  72. })
  73. test("impact paths retain source evidence and do not invent runtime calls or missing legacy context", () => {
  74. const c = fixture().candidates[0]
  75. c.ontology_context = {
  76. dependency_paths: [{ path: "contracts/Policy.java", via: [{ origin: { path: "Fleet.java", line: 8 } }] }],
  77. unresolved_references_total: 2,
  78. impact_paths: [
  79. {
  80. path: "FleetTest.java",
  81. via: [{ relation: "REFERENCES", origin: { path: "FleetTest.java", line: 2, quote: "import Fleet;" } }],
  82. },
  83. ],
  84. }
  85. const value = candidateImpact(c)
  86. expect(value).toContain("FleetTest.java:2")
  87. expect(value).toContain("import Fleet;")
  88. expect(value).toContain("不代表运行时调用")
  89. expect(value).toContain("下游依赖")
  90. expect(value).toContain("contracts/Policy.java")
  91. expect(value).toContain("Fleet.java:8")
  92. expect(value).toContain("未解析引用:2")
  93. expect(candidateImpact(fixture().candidates[0])).toContain("旧记录未采集")
  94. expect(reviewReport(fixture(), 0)).toContain("旧记录没有全库本体汇总")
  95. })
  96. test("missing test evidence is unknown while measured zero remains zero", () => {
  97. expect(reviewReport(fixture(), 2)).toContain("未取得测试明细,不计通过")
  98. const evidence: Evidence = {
  99. id: "a".repeat(32),
  100. revision: 5,
  101. token: "d".repeat(64),
  102. files: [],
  103. checks: {
  104. "baseline/checks.json": { build_status: "passed", test_status: "passed", executed_tests: 10 },
  105. "round-1/checks.json": {
  106. build_status: "passed",
  107. test_status: "failed",
  108. executed_tests: 10,
  109. case_counts: { failed: 1, skipped: 0 },
  110. failed_cases: [{ id: "FleetTest.denied", status: "failed" }],
  111. },
  112. },
  113. }
  114. const value = reviewReport(fixture(), 2, { evidence })
  115. expect(value).toContain("失败 未采集")
  116. expect(value).toContain("失败 1 · 跳过 0")
  117. expect(value).toContain("FleetTest.denied")
  118. })
  119. test("partial evidence and pending cancellation never appear as completion", () => {
  120. const state = fixture()
  121. state.phase = "running"
  122. state.cancel_requested = true
  123. expect(nextReviewStep(state)).toContain("等待当前检查结束")
  124. expect(reviewReport(state, 3, { warning: "校验失败" })).toContain("以下缺失项不计通过")
  125. state.phase = "accepted_for_delivery"
  126. expect(nextReviewStep(state)).toContain("原仓库尚未合并")
  127. })
  128. test("measured analysis progress and declared adapter scope guide the next step", () => {
  129. const state = fixture()
  130. state.phase = "analyzing"
  131. state.progress = { stage: "scan", completed_batches: 2, total_batches: 5 }
  132. state.coverage = { analyzed_files: 240, total_files: 500 }
  133. state.analysis_scope = { label: "TypeScript 文件", total_files: 500, adapter: "synthetic-typescript" }
  134. expect(nextReviewStep(state)).toContain("2/5 个分析批次")
  135. expect(reviewReport(state, 0)).toContain("240 / 500 个TypeScript 文件")
  136. expect(reviewReport(state, 0)).not.toContain("Java 文件")
  137. state.progress.stage = "link"
  138. expect(nextReviewStep(state)).toContain("正在关联跨文件本体")
  139. state.cancel_requested = true
  140. expect(nextReviewStep(state)).toContain("等待当前检查结束")
  141. })