node-sqlite-backend.test.ts 2.7 KB

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