-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcache.test.ts
More file actions
83 lines (66 loc) · 2.61 KB
/
Copy pathcache.test.ts
File metadata and controls
83 lines (66 loc) · 2.61 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
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Unit tests for the native ParseTreeCache (incremental parsing).
*
* Skipped when the native engine is not available.
*/
import { beforeEach, describe, expect, it } from 'vitest';
import { isNativeAvailable, loadNative } from '../../src/infrastructure/native.js';
const hasNative = isNativeAvailable();
describe.skipIf(!hasNative)('ParseTreeCache', () => {
let cache: any;
beforeEach(() => {
const native = loadNative();
cache = new native.ParseTreeCache();
});
it('parses a JS file and caches the tree', () => {
const source = 'function hello() { return 1; }';
const result = cache.parseFile('test.js', source);
expect(result).not.toBeNull();
expect(result.definitions.length).toBeGreaterThanOrEqual(1);
expect(result.definitions[0].name).toBe('hello');
expect(cache.contains('test.js')).toBe(true);
expect(cache.size()).toBe(1);
});
// Known native engine limitation: incremental re-parse does not pick up
// newly added definitions. Tracked for fix in the Rust crate.
it.skip('incrementally re-parses when source changes', () => {
const source1 = 'function hello() { return 1; }';
cache.parseFile('test.js', source1);
const source2 = 'function hello() { return 1; }\nfunction world() { return 2; }';
const result = cache.parseFile('test.js', source2);
expect(result).not.toBeNull();
const names = result.definitions.map((d) => d.name);
expect(names).toContain('hello');
expect(names).toContain('world');
expect(cache.size()).toBe(1);
});
it('returns null for unsupported extensions', () => {
const result = cache.parseFile('readme.md', '# Hello');
expect(result).toBeNull();
expect(cache.size()).toBe(0);
});
it('remove() evicts a file from the cache', () => {
cache.parseFile('test.js', 'function a() {}');
expect(cache.contains('test.js')).toBe(true);
cache.remove('test.js');
expect(cache.contains('test.js')).toBe(false);
expect(cache.size()).toBe(0);
});
it('clear() removes all entries', () => {
cache.parseFile('a.js', 'function a() {}');
cache.parseFile('b.js', 'function b() {}');
expect(cache.size()).toBe(2);
cache.clear();
expect(cache.size()).toBe(0);
});
it('contains() returns false for unknown files', () => {
expect(cache.contains('nope.js')).toBe(false);
});
it('handles multiple languages', () => {
const jsResult = cache.parseFile('app.js', 'function run() {}');
const pyResult = cache.parseFile('app.py', 'def run():\n pass');
expect(jsResult).not.toBeNull();
expect(pyResult).not.toBeNull();
expect(cache.size()).toBe(2);
});
});