-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathbfs.test.ts
More file actions
69 lines (58 loc) · 1.81 KB
/
Copy pathbfs.test.ts
File metadata and controls
69 lines (58 loc) · 1.81 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
import { describe, expect, it } from 'vitest';
import { bfs } from '../../../src/graph/algorithms/bfs.js';
import { CodeGraph } from '../../../src/graph/model.js';
describe('bfs', () => {
it('traverses forward from a single start', () => {
const g = new CodeGraph();
g.addEdge('a', 'b');
g.addEdge('b', 'c');
g.addEdge('a', 'd');
const depths = bfs(g, 'a');
expect(depths.get('a')).toBe(0);
expect(depths.get('b')).toBe(1);
expect(depths.get('c')).toBe(2);
expect(depths.get('d')).toBe(1);
});
it('respects maxDepth', () => {
const g = new CodeGraph();
g.addEdge('a', 'b');
g.addEdge('b', 'c');
g.addEdge('c', 'd');
const depths = bfs(g, 'a', { maxDepth: 1 });
expect(depths.has('a')).toBe(true);
expect(depths.has('b')).toBe(true);
expect(depths.has('c')).toBe(false);
});
it('traverses backward', () => {
const g = new CodeGraph();
g.addEdge('a', 'b');
g.addEdge('b', 'c');
const depths = bfs(g, 'c', { direction: 'backward' });
expect(depths.get('c')).toBe(0);
expect(depths.get('b')).toBe(1);
expect(depths.get('a')).toBe(2);
});
it('traverses both directions', () => {
const g = new CodeGraph();
g.addEdge('a', 'b');
g.addEdge('c', 'b');
const depths = bfs(g, 'b', { direction: 'both' });
expect(depths.size).toBe(3);
});
it('handles multiple start nodes', () => {
const g = new CodeGraph();
g.addEdge('a', 'c');
g.addEdge('b', 'c');
g.addEdge('c', 'd');
const depths = bfs(g, ['a', 'b']);
expect(depths.get('a')).toBe(0);
expect(depths.get('b')).toBe(0);
expect(depths.get('c')).toBe(1);
expect(depths.get('d')).toBe(2);
});
it('handles empty graph', () => {
const g = new CodeGraph();
const depths = bfs(g, 'a');
expect(depths.size).toBe(0);
});
});