index-command.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * Regression coverage for issue #874: `codegraph index` produced 0 nodes / 0
  3. * edges while `codegraph init` worked, and appeared to wipe the graph.
  4. *
  5. * Root cause: `index` ran a full extraction against the already-populated DB
  6. * without clearing it first. Every file's content hash still matched, so the
  7. * orchestrator skipped re-inserting all of them, and the run reported its delta
  8. * (after - before = 0) as "0 nodes, 0 edges". The fix makes `index` a true full
  9. * rebuild — clear, then re-index — so it produces the same complete result as a
  10. * fresh `init`.
  11. *
  12. * Exercised end-to-end against the built binary so the CLI wiring (not just the
  13. * library) is covered.
  14. */
  15. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  16. import { execFileSync } from 'child_process';
  17. import * as fs from 'fs';
  18. import * as path from 'path';
  19. import * as os from 'os';
  20. import { CodeGraph } from '../src';
  21. const BIN = path.resolve(__dirname, '../dist/bin/codegraph.js');
  22. function runCodegraph(args: string[], cwd: string): string {
  23. return execFileSync(process.execPath, [BIN, ...args], {
  24. cwd,
  25. encoding: 'utf-8',
  26. env: { ...process.env, CODEGRAPH_NO_DAEMON: '1' },
  27. stdio: ['ignore', 'pipe', 'pipe'],
  28. });
  29. }
  30. function graphCounts(dir: string): { nodes: number; edges: number } {
  31. const cg = CodeGraph.openSync(dir);
  32. try {
  33. const stats = cg.getStats();
  34. return { nodes: stats.nodeCount, edges: stats.edgeCount };
  35. } finally {
  36. cg.close();
  37. }
  38. }
  39. describe('codegraph index — full re-index keeps the graph populated (#874)', () => {
  40. let tempDir: string;
  41. beforeEach(() => {
  42. tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-index-cmd-'));
  43. // A couple of files with a call edge so there is a non-trivial graph to
  44. // (fail to) reproduce.
  45. fs.writeFileSync(
  46. path.join(tempDir, 'a.ts'),
  47. `export function greet(name: string) { return hello(name); }\n` +
  48. `export function hello(n: string) { return 'hi ' + n; }\n`,
  49. );
  50. fs.writeFileSync(
  51. path.join(tempDir, 'b.ts'),
  52. `import { greet } from './a';\nexport function main() { return greet('world'); }\n`,
  53. );
  54. });
  55. afterEach(() => {
  56. fs.rmSync(tempDir, { recursive: true, force: true });
  57. });
  58. it('reproduces init\'s node/edge counts instead of emptying the index', () => {
  59. runCodegraph(['init'], tempDir);
  60. const afterInit = graphCounts(tempDir);
  61. expect(afterInit.nodes).toBeGreaterThan(0);
  62. expect(afterInit.edges).toBeGreaterThan(0);
  63. const out = runCodegraph(['index'], tempDir);
  64. const afterIndex = graphCounts(tempDir);
  65. // The graph is still fully populated — `index` rebuilt it, it did not wipe it.
  66. expect(afterIndex.nodes).toBe(afterInit.nodes);
  67. expect(afterIndex.edges).toBe(afterInit.edges);
  68. // ...and the CLI reported the real counts, never the misleading "0 nodes".
  69. expect(out).not.toMatch(/\b0 nodes, 0 edges\b/);
  70. expect(out).toMatch(new RegExp(`\\b${afterInit.nodes} nodes\\b`));
  71. });
  72. it('is idempotent: a second index does not grow the graph', () => {
  73. runCodegraph(['init'], tempDir);
  74. runCodegraph(['index'], tempDir);
  75. const first = graphCounts(tempDir);
  76. runCodegraph(['index'], tempDir);
  77. const second = graphCounts(tempDir);
  78. // A clean rebuild each time — no duplicate (re-resolved) edges accumulating
  79. // across runs (the C# "+18 edges" symptom in the report).
  80. expect(second.nodes).toBe(first.nodes);
  81. expect(second.edges).toBe(first.edges);
  82. });
  83. it('--quiet path also rebuilds a populated graph', () => {
  84. runCodegraph(['init'], tempDir);
  85. const afterInit = graphCounts(tempDir);
  86. runCodegraph(['index', '--quiet'], tempDir);
  87. const afterIndex = graphCounts(tempDir);
  88. expect(afterIndex.nodes).toBe(afterInit.nodes);
  89. expect(afterIndex.edges).toBe(afterInit.edges);
  90. });
  91. });