|
| 1 | +// Units for the debug-leftover analyzer (#2015). Own file (not enrichment.test.ts) so concurrent analyzer PRs |
| 2 | +// don't collide. No network — pure, stateless per-line detection. Runs against the compiled dist/. |
| 3 | +import { test } from "node:test"; |
| 4 | +import assert from "node:assert/strict"; |
| 5 | +import { |
| 6 | + detectDebugLeftover, |
| 7 | + scanDebugLeftover, |
| 8 | + scanPatchForDebugLeftover, |
| 9 | +} from "../dist/analyzers/debug-leftover.js"; |
| 10 | +import { renderBrief } from "../dist/render.js"; |
| 11 | + |
| 12 | +const patchOf = (lines: string[]) => |
| 13 | + `@@ -1,0 +1,${lines.length} @@\n${lines.map((l) => `+${l}`).join("\n")}`; |
| 14 | + |
| 15 | +test("detectDebugLeftover: recognizes debugger, console sinks, and print()", () => { |
| 16 | + assert.equal(detectDebugLeftover(" debugger;"), "debugger"); |
| 17 | + assert.equal(detectDebugLeftover("console.log('hi')"), "console"); |
| 18 | + assert.equal(detectDebugLeftover(" console.debug(state)"), "console"); |
| 19 | + assert.equal(detectDebugLeftover("print('debug')", "lib/b.py"), "print"); |
| 20 | +}); |
| 21 | + |
| 22 | +test("detectDebugLeftover: print() is Python-only and does not match method calls like document.print()", () => { |
| 23 | + assert.equal(detectDebugLeftover("document.print()"), null); |
| 24 | + assert.equal(detectDebugLeftover("printer.print('x')"), null); |
| 25 | + assert.equal(detectDebugLeftover("print('debug')", "src/widget.ts"), null); |
| 26 | + assert.equal(detectDebugLeftover("obj.print('x')", "pkg/widget.py"), null); |
| 27 | +}); |
| 28 | + |
| 29 | +test("detectDebugLeftover: a console call inside a string literal is not flagged", () => { |
| 30 | + assert.equal(detectDebugLeftover('const s = "console.log(\\"nope\\")"'), null); |
| 31 | + assert.equal(detectDebugLeftover("log(`hint: console.log(here)`);"), null); |
| 32 | +}); |
| 33 | + |
| 34 | +test("detectDebugLeftover: debugger inside a string is not flagged", () => { |
| 35 | + assert.equal(detectDebugLeftover('const msg = "debugger;"'), null); |
| 36 | +}); |
| 37 | + |
| 38 | +test("scanPatchForDebugLeftover: flags added lines with correct locations", () => { |
| 39 | + const findings = scanPatchForDebugLeftover( |
| 40 | + "src/widget.ts", |
| 41 | + patchOf(["function f() {", " debugger;", " console.log('x');", " return g();", "}"]), |
| 42 | + ); |
| 43 | + assert.deepEqual(findings, [ |
| 44 | + { file: "src/widget.ts", line: 2, kind: "debugger" }, |
| 45 | + { file: "src/widget.ts", line: 3, kind: "console" }, |
| 46 | + ]); |
| 47 | +}); |
| 48 | + |
| 49 | +test("scanPatchForDebugLeftover: only ADDED lines are scanned", () => { |
| 50 | + const patch = [ |
| 51 | + "@@ -10,2 +10,2 @@", |
| 52 | + " function f() {", |
| 53 | + "- console.log('old');", |
| 54 | + "+ print('new')", |
| 55 | + ].join("\n"); |
| 56 | + assert.deepEqual(scanPatchForDebugLeftover("pkg/widget.py", patch), [ |
| 57 | + { file: "pkg/widget.py", line: 11, kind: "print" }, |
| 58 | + ]); |
| 59 | +}); |
| 60 | + |
| 61 | +test("scanPatchForDebugLeftover: skips test/spec files", () => { |
| 62 | + assert.deepEqual( |
| 63 | + scanPatchForDebugLeftover("src/widget.test.ts", patchOf(["console.log('in test')"])), |
| 64 | + [], |
| 65 | + ); |
| 66 | + assert.deepEqual( |
| 67 | + scanPatchForDebugLeftover("tests/widget.spec.js", patchOf(["debugger;"])), |
| 68 | + [], |
| 69 | + ); |
| 70 | +}); |
| 71 | + |
| 72 | +test("scanPatchForDebugLeftover: respects the findings cap", () => { |
| 73 | + const lines = Array.from({ length: 30 }, (_, i) => `console.log(${i});`); |
| 74 | + assert.equal(scanPatchForDebugLeftover("src/a.ts", patchOf(lines), { maxFindings: 3 }).length, 3); |
| 75 | +}); |
| 76 | + |
| 77 | +test("scanDebugLeftover: aggregates across files and renders in the brief", async () => { |
| 78 | + const findings = await scanDebugLeftover({ |
| 79 | + files: [ |
| 80 | + { path: "src/a.ts", patch: patchOf(["debugger;"]) }, |
| 81 | + { path: "lib/b.py", patch: patchOf(["print('x')"]) }, |
| 82 | + ], |
| 83 | + }); |
| 84 | + assert.deepEqual(findings, [ |
| 85 | + { file: "src/a.ts", line: 1, kind: "debugger" }, |
| 86 | + { file: "lib/b.py", line: 1, kind: "print" }, |
| 87 | + ]); |
| 88 | + |
| 89 | + const { promptSection } = renderBrief({ |
| 90 | + debugLeftover: findings, |
| 91 | + }); |
| 92 | + assert.match(promptSection, /Debug leftovers/); |
| 93 | + assert.match(promptSection, /src\/a\.ts:1/); |
| 94 | + assert.match(promptSection, /lib\/b\.py:1/); |
| 95 | +}); |
0 commit comments