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