node-sqlite-backend.test.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * node:sqlite backend (issue #238 follow-up).
  3. *
  4. * node:sqlite (Node's built-in real SQLite) is now the sole backend. This drives
  5. * a real index + queries through it, so WAL, FTS5 search, and @named-param
  6. * writes are all exercised end-to-end.
  7. *
  8. * Skipped on Node < 22.5 where node:sqlite doesn't exist.
  9. */
  10. import { describe, it, expect, beforeAll, afterAll } from 'vitest';
  11. import * as fs from 'fs';
  12. import * as path from 'path';
  13. import * as os from 'os';
  14. import CodeGraph from '../src';
  15. let nodeSqliteAvailable = false;
  16. try {
  17. // eslint-disable-next-line @typescript-eslint/no-require-imports
  18. require('node:sqlite');
  19. nodeSqliteAvailable = true;
  20. } catch {
  21. nodeSqliteAvailable = false;
  22. }
  23. describe.skipIf(!nodeSqliteAvailable)('node:sqlite backend — real index + queries', () => {
  24. let dir: string;
  25. let cg: CodeGraph;
  26. beforeAll(async () => {
  27. dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-nodesqlite-'));
  28. fs.writeFileSync(path.join(dir, 'a.ts'), 'export function helper(): number { return 1; }\n');
  29. fs.writeFileSync(
  30. path.join(dir, 'b.ts'),
  31. "import { helper } from './a';\nexport function main(): number { return helper(); }\n"
  32. );
  33. cg = await CodeGraph.init(dir, { index: true });
  34. });
  35. afterAll(() => {
  36. cg?.close();
  37. fs.rmSync(dir, { recursive: true, force: true });
  38. });
  39. it('uses the node:sqlite backend', () => {
  40. expect(cg.getBackend()).toBe('node-sqlite');
  41. });
  42. it('runs in WAL mode — the whole reason it beats the wasm fallback', () => {
  43. expect(cg.getJournalMode()).toBe('wal');
  44. });
  45. it('indexed the project (write path: @named-param INSERTs via node:sqlite)', () => {
  46. const stats = cg.getStats();
  47. expect(stats.fileCount).toBe(2);
  48. expect(stats.nodeCount).toBeGreaterThan(0);
  49. });
  50. it('FTS5 search returns the indexed symbol (read path)', () => {
  51. const results = cg.searchNodes('helper');
  52. const names = results.map(r => r.node.name);
  53. expect(names).toContain('helper');
  54. });
  55. it('graph traversal resolves the cross-file caller', () => {
  56. const helper = cg.searchNodes('helper').find(r => r.node.name === 'helper');
  57. expect(helper).toBeTruthy();
  58. const callers = cg.getCallers(helper!.node.id);
  59. expect(callers.map(c => c.node.name)).toContain('main');
  60. });
  61. });