|
| 1 | +/** |
| 2 | + * Regression test for #979: incremental rebuilds leak duplicate edges. |
| 3 | + * |
| 4 | + * Root cause: when `reparse_barrel_candidates` (Stage 6b, native engine) picks |
| 5 | + * up a file imported by a reverse-dep, it used to purge only the 'imports' and |
| 6 | + * 'reexports' edge kinds before Stage 7 re-emitted every edge kind, so every |
| 7 | + * rebuild appended new copies of 'calls', 'receiver', 'extends', 'implements', |
| 8 | + * 'imports-type', and 'dynamic-imports' edges. |
| 9 | + * |
| 10 | + * This test modifies a source file multiple times in a row and asserts: |
| 11 | + * 1. The total edge count does not grow across incremental rebuilds. |
| 12 | + * 2. The count of `(source_id, target_id, kind)` rows never exceeds the |
| 13 | + * pre-existing duplicates from a fresh full build (i.e. incremental |
| 14 | + * does not introduce new duplicates). |
| 15 | + */ |
| 16 | +import fs from 'node:fs'; |
| 17 | +import os from 'node:os'; |
| 18 | +import path from 'node:path'; |
| 19 | +import Database from 'better-sqlite3'; |
| 20 | +import { describe, expect, it } from 'vitest'; |
| 21 | +import { buildGraph } from '../../src/domain/graph/builder.js'; |
| 22 | + |
| 23 | +const FIXTURE_DIR = path.join(import.meta.dirname, '..', 'fixtures', 'issue-979-hybrid-barrel'); |
| 24 | + |
| 25 | +function copyDirSync(src: string, dest: string) { |
| 26 | + fs.mkdirSync(dest, { recursive: true }); |
| 27 | + for (const entry of fs.readdirSync(src, { withFileTypes: true })) { |
| 28 | + const s = path.join(src, entry.name); |
| 29 | + const d = path.join(dest, entry.name); |
| 30 | + if (entry.isDirectory()) copyDirSync(s, d); |
| 31 | + else fs.copyFileSync(s, d); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +function edgeStats(dbPath: string) { |
| 36 | + const db = new Database(dbPath, { readonly: true }); |
| 37 | + try { |
| 38 | + const total = (db.prepare('SELECT COUNT(*) AS c FROM edges').get() as { c: number }).c; |
| 39 | + const duplicates = ( |
| 40 | + db |
| 41 | + .prepare( |
| 42 | + `SELECT source_id, target_id, kind, COUNT(*) AS c FROM edges |
| 43 | + GROUP BY source_id, target_id, kind HAVING c > 1`, |
| 44 | + ) |
| 45 | + .all() as Array<{ c: number }> |
| 46 | + ).reduce((sum, row) => sum + row.c - 1, 0); |
| 47 | + return { total, duplicates }; |
| 48 | + } finally { |
| 49 | + db.close(); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +describe('Issue #979: incremental edges do not duplicate', () => { |
| 54 | + it('3 incremental rebuilds produce stable edge counts with no new duplicates', async () => { |
| 55 | + const tmpBase = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-979-')); |
| 56 | + const fullDir = path.join(tmpBase, 'full'); |
| 57 | + const incrDir = path.join(tmpBase, 'incr'); |
| 58 | + |
| 59 | + try { |
| 60 | + copyDirSync(FIXTURE_DIR, fullDir); |
| 61 | + copyDirSync(FIXTURE_DIR, incrDir); |
| 62 | + |
| 63 | + // Baseline full build on the incr copy so subsequent rebuilds are truly incremental. |
| 64 | + await buildGraph(incrDir, { incremental: false, skipRegistry: true }); |
| 65 | + |
| 66 | + // Apply 3 rounds of "change one file" + incremental rebuild, recording |
| 67 | + // edge totals and duplicate counts after each rebuild. |
| 68 | + const history: Array<{ total: number; duplicates: number }> = []; |
| 69 | + for (let i = 0; i < 3; i++) { |
| 70 | + fs.appendFileSync(path.join(incrDir, 'consumers', 'driver.js'), `\n// bump ${i}\n`); |
| 71 | + await buildGraph(incrDir, { incremental: true, skipRegistry: true }); |
| 72 | + history.push(edgeStats(path.join(incrDir, '.codegraph', 'graph.db'))); |
| 73 | + } |
| 74 | + |
| 75 | + // Mirror all 3 mutations on the full copy, then do a single clean full build. |
| 76 | + for (let i = 0; i < 3; i++) { |
| 77 | + fs.appendFileSync(path.join(fullDir, 'consumers', 'driver.js'), `\n// bump ${i}\n`); |
| 78 | + } |
| 79 | + await buildGraph(fullDir, { incremental: false, skipRegistry: true }); |
| 80 | + const freshFull = edgeStats(path.join(fullDir, '.codegraph', 'graph.db')); |
| 81 | + |
| 82 | + // Invariant 1: incremental edge count must not grow across rebuilds. |
| 83 | + expect(history[1].total).toBe(history[0].total); |
| 84 | + expect(history[2].total).toBe(history[0].total); |
| 85 | + |
| 86 | + // Invariant 2: incremental must not introduce new duplicates beyond the |
| 87 | + // pre-existing duplicates present in a clean full build. |
| 88 | + expect(history[2].duplicates).toBeLessThanOrEqual(freshFull.duplicates); |
| 89 | + |
| 90 | + // Invariant 3: after applying all 3 bumps, both dirs describe the same |
| 91 | + // code, so the incremental edge total must match a clean full build. |
| 92 | + // This catches stale edges that survive the scoped DELETE (e.g. edges |
| 93 | + // pointing at orphaned node ids) which would not be flagged as |
| 94 | + // (source, target, kind) duplicates. |
| 95 | + expect(history[2].total).toBe(freshFull.total); |
| 96 | + } finally { |
| 97 | + fs.rmSync(tmpBase, { recursive: true, force: true }); |
| 98 | + } |
| 99 | + }, 60_000); |
| 100 | +}); |
0 commit comments