|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from "vitest"; |
| 2 | +import { spawnSync } from "node:child_process"; |
| 3 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync, readFileSync } from "node:fs"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { join } from "node:path"; |
| 6 | +import { fileURLToPath } from "node:url"; |
| 7 | +import { dirname, resolve } from "node:path"; |
| 8 | + |
| 9 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 10 | +const MERGE_SCRIPT = resolve(__dirname, "../../skills/understand/merge-batch-graphs.py"); |
| 11 | + |
| 12 | +let projectRoot; |
| 13 | +let intermediateDir; |
| 14 | + |
| 15 | +function runMerge() { |
| 16 | + const result = spawnSync("python3", [MERGE_SCRIPT, projectRoot], { |
| 17 | + encoding: "utf-8", |
| 18 | + }); |
| 19 | + if (result.status !== 0) { |
| 20 | + throw new Error(`merge script failed: status=${result.status}\nstderr:\n${result.stderr}`); |
| 21 | + } |
| 22 | + const assembled = JSON.parse( |
| 23 | + readFileSync(join(intermediateDir, "assembled-graph.json"), "utf-8"), |
| 24 | + ); |
| 25 | + return { assembled, stderr: result.stderr }; |
| 26 | +} |
| 27 | + |
| 28 | +function fileNode(path) { |
| 29 | + return { |
| 30 | + id: `file:${path}`, |
| 31 | + type: "file", |
| 32 | + name: path.split("/").pop(), |
| 33 | + filePath: path, |
| 34 | + summary: "", |
| 35 | + tags: [], |
| 36 | + complexity: "simple", |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +function importsEdge(src, tgt) { |
| 41 | + return { |
| 42 | + source: `file:${src}`, |
| 43 | + target: `file:${tgt}`, |
| 44 | + type: "imports", |
| 45 | + direction: "forward", |
| 46 | + weight: 0.7, |
| 47 | + }; |
| 48 | +} |
| 49 | + |
| 50 | +beforeEach(() => { |
| 51 | + projectRoot = mkdtempSync(join(tmpdir(), "ua-merge-test-")); |
| 52 | + intermediateDir = join(projectRoot, ".understand-anything", "intermediate"); |
| 53 | + mkdirSync(intermediateDir, { recursive: true }); |
| 54 | +}); |
| 55 | + |
| 56 | +afterEach(() => { |
| 57 | + rmSync(projectRoot, { recursive: true, force: true }); |
| 58 | +}); |
| 59 | + |
| 60 | +describe("merge-batch-graphs.py imports recovery", () => { |
| 61 | + it("recovers imports edges that batches dropped despite importMap having them", () => { |
| 62 | + // Batch contains all the file nodes but only emits ONE of three imports edges. |
| 63 | + writeFileSync( |
| 64 | + join(intermediateDir, "batch-0.json"), |
| 65 | + JSON.stringify({ |
| 66 | + nodes: [fileNode("src/a.py"), fileNode("src/b.py"), fileNode("src/c.py"), fileNode("src/d.py")], |
| 67 | + edges: [importsEdge("src/a.py", "src/b.py")], |
| 68 | + }), |
| 69 | + ); |
| 70 | + // scan-result.json has the full importMap — agent dropped 2/3 of these. |
| 71 | + writeFileSync( |
| 72 | + join(intermediateDir, "scan-result.json"), |
| 73 | + JSON.stringify({ |
| 74 | + importMap: { |
| 75 | + "src/a.py": ["src/b.py", "src/c.py", "src/d.py"], |
| 76 | + "src/b.py": [], |
| 77 | + }, |
| 78 | + }), |
| 79 | + ); |
| 80 | + |
| 81 | + const { assembled, stderr } = runMerge(); |
| 82 | + const importsEdges = assembled.edges.filter((e) => e.type === "imports"); |
| 83 | + expect(importsEdges).toHaveLength(3); |
| 84 | + const targets = new Set(importsEdges.map((e) => e.target)); |
| 85 | + expect(targets).toEqual(new Set(["file:src/b.py", "file:src/c.py", "file:src/d.py"])); |
| 86 | + // Recovered edges are tagged so downstream consumers can audit. |
| 87 | + const recovered = importsEdges.filter((e) => e.recoveredFromImportMap); |
| 88 | + expect(recovered).toHaveLength(2); |
| 89 | + expect(stderr).toContain("Recovered 2 `imports` edges"); |
| 90 | + }); |
| 91 | + |
| 92 | + it("does not duplicate edges the batch already emitted", () => { |
| 93 | + writeFileSync( |
| 94 | + join(intermediateDir, "batch-0.json"), |
| 95 | + JSON.stringify({ |
| 96 | + nodes: [fileNode("src/a.py"), fileNode("src/b.py")], |
| 97 | + edges: [importsEdge("src/a.py", "src/b.py")], |
| 98 | + }), |
| 99 | + ); |
| 100 | + writeFileSync( |
| 101 | + join(intermediateDir, "scan-result.json"), |
| 102 | + JSON.stringify({ |
| 103 | + importMap: { "src/a.py": ["src/b.py"], "src/b.py": [] }, |
| 104 | + }), |
| 105 | + ); |
| 106 | + |
| 107 | + const { assembled, stderr } = runMerge(); |
| 108 | + const importsEdges = assembled.edges.filter((e) => e.type === "imports"); |
| 109 | + expect(importsEdges).toHaveLength(1); |
| 110 | + expect(stderr).toContain("Recovered 0 `imports` edges"); |
| 111 | + }); |
| 112 | + |
| 113 | + it("skips importMap entries whose source file is missing from the graph", () => { |
| 114 | + // src/missing.py is in importMap but has no file: node — must not produce a dangling edge. |
| 115 | + writeFileSync( |
| 116 | + join(intermediateDir, "batch-0.json"), |
| 117 | + JSON.stringify({ |
| 118 | + nodes: [fileNode("src/b.py")], |
| 119 | + edges: [], |
| 120 | + }), |
| 121 | + ); |
| 122 | + writeFileSync( |
| 123 | + join(intermediateDir, "scan-result.json"), |
| 124 | + JSON.stringify({ |
| 125 | + importMap: { "src/missing.py": ["src/b.py"] }, |
| 126 | + }), |
| 127 | + ); |
| 128 | + |
| 129 | + const { assembled, stderr } = runMerge(); |
| 130 | + expect(assembled.edges.filter((e) => e.type === "imports")).toHaveLength(0); |
| 131 | + expect(stderr).toContain("Skipped 1 importMap source files with no `file:` node"); |
| 132 | + }); |
| 133 | + |
| 134 | + it("skips importMap targets that don't have a file: node", () => { |
| 135 | + writeFileSync( |
| 136 | + join(intermediateDir, "batch-0.json"), |
| 137 | + JSON.stringify({ |
| 138 | + nodes: [fileNode("src/a.py")], |
| 139 | + edges: [], |
| 140 | + }), |
| 141 | + ); |
| 142 | + writeFileSync( |
| 143 | + join(intermediateDir, "scan-result.json"), |
| 144 | + JSON.stringify({ |
| 145 | + importMap: { "src/a.py": ["src/dropped.py", "src/also-missing.py"] }, |
| 146 | + }), |
| 147 | + ); |
| 148 | + |
| 149 | + const { assembled, stderr } = runMerge(); |
| 150 | + expect(assembled.edges.filter((e) => e.type === "imports")).toHaveLength(0); |
| 151 | + expect(stderr).toContain("Skipped 2 importMap target paths with no `file:` node"); |
| 152 | + }); |
| 153 | + |
| 154 | + it("works when scan-result.json is missing (incremental update path)", () => { |
| 155 | + writeFileSync( |
| 156 | + join(intermediateDir, "batch-0.json"), |
| 157 | + JSON.stringify({ |
| 158 | + nodes: [fileNode("src/a.py"), fileNode("src/b.py")], |
| 159 | + edges: [importsEdge("src/a.py", "src/b.py")], |
| 160 | + }), |
| 161 | + ); |
| 162 | + // No scan-result.json written. |
| 163 | + |
| 164 | + const { assembled, stderr } = runMerge(); |
| 165 | + expect(assembled.edges.filter((e) => e.type === "imports")).toHaveLength(1); |
| 166 | + expect(stderr).toContain("importMap recovery skipped — scan-result.json not found"); |
| 167 | + }); |
| 168 | + |
| 169 | + it("never produces self-import edges", () => { |
| 170 | + writeFileSync( |
| 171 | + join(intermediateDir, "batch-0.json"), |
| 172 | + JSON.stringify({ |
| 173 | + nodes: [fileNode("src/a.py")], |
| 174 | + edges: [], |
| 175 | + }), |
| 176 | + ); |
| 177 | + writeFileSync( |
| 178 | + join(intermediateDir, "scan-result.json"), |
| 179 | + JSON.stringify({ |
| 180 | + importMap: { "src/a.py": ["src/a.py"] }, // pathological self-reference |
| 181 | + }), |
| 182 | + ); |
| 183 | + |
| 184 | + const { assembled } = runMerge(); |
| 185 | + expect(assembled.edges.filter((e) => e.type === "imports")).toHaveLength(0); |
| 186 | + }); |
| 187 | +}); |
0 commit comments