-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmodel.test.ts
More file actions
170 lines (146 loc) · 4.76 KB
/
Copy pathmodel.test.ts
File metadata and controls
170 lines (146 loc) · 4.76 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { describe, expect, it } from 'vitest';
import { CodeGraph } from '../../src/graph/model.js';
describe('CodeGraph — directed', () => {
it('starts empty', () => {
const g = new CodeGraph();
expect(g.nodeCount).toBe(0);
expect(g.edgeCount).toBe(0);
expect(g.directed).toBe(true);
});
it('adds nodes and edges', () => {
const g = new CodeGraph();
g.addNode('a', { label: 'A' });
g.addNode('b', { label: 'B' });
g.addEdge('a', 'b', { kind: 'calls' });
expect(g.nodeCount).toBe(2);
expect(g.edgeCount).toBe(1);
expect(g.hasNode('a')).toBe(true);
expect(g.hasNode('c')).toBe(false);
expect(g.hasEdge('a', 'b')).toBe(true);
expect(g.hasEdge('b', 'a')).toBe(false);
});
it('auto-adds nodes when adding edges', () => {
const g = new CodeGraph();
g.addEdge('x', 'y');
expect(g.hasNode('x')).toBe(true);
expect(g.hasNode('y')).toBe(true);
expect(g.nodeCount).toBe(2);
});
it('stringifies numeric IDs', () => {
const g = new CodeGraph();
g.addNode(42, { name: 'foo' });
expect(g.hasNode('42')).toBe(true);
expect(g.getNodeAttrs('42')).toEqual({ name: 'foo' });
});
it('reports correct adjacency', () => {
const g = new CodeGraph();
g.addEdge('a', 'b');
g.addEdge('a', 'c');
g.addEdge('d', 'a');
expect(g.successors('a').sort()).toEqual(['b', 'c']);
expect(g.predecessors('a')).toEqual(['d']);
expect(g.neighbors('a').sort()).toEqual(['b', 'c', 'd']);
expect(g.outDegree('a')).toBe(2);
expect(g.inDegree('a')).toBe(1);
});
it('iterates nodes and edges', () => {
const g = new CodeGraph();
g.addEdge('a', 'b', { w: 1 });
g.addEdge('b', 'c', { w: 2 });
const nodeIds = [...g.nodes()].map(([id]) => id);
expect(nodeIds.sort()).toEqual(['a', 'b', 'c']);
const edges = [...g.edges()];
expect(edges).toHaveLength(2);
expect(edges[0]).toEqual(['a', 'b', { w: 1 }]);
});
it('nodeIds returns array of keys', () => {
const g = new CodeGraph();
g.addNode('x');
g.addNode('y');
expect(g.nodeIds().sort()).toEqual(['x', 'y']);
});
it('getEdgeAttrs returns attributes', () => {
const g = new CodeGraph();
g.addEdge('a', 'b', { kind: 'imports' });
expect(g.getEdgeAttrs('a', 'b')).toEqual({ kind: 'imports' });
expect(g.getEdgeAttrs('b', 'a')).toBeUndefined();
});
});
describe('CodeGraph — undirected', () => {
it('mirrors edges', () => {
const g = new CodeGraph({ directed: false });
g.addEdge('a', 'b');
expect(g.hasEdge('a', 'b')).toBe(true);
expect(g.hasEdge('b', 'a')).toBe(true);
expect(g.successors('a')).toEqual(['b']);
expect(g.successors('b')).toEqual(['a']);
});
it('counts each undirected edge once', () => {
const g = new CodeGraph({ directed: false });
g.addEdge('a', 'b');
g.addEdge('b', 'c');
expect(g.edgeCount).toBe(2);
expect([...g.edges()]).toHaveLength(2);
});
});
describe('CodeGraph — subgraph', () => {
it('filters nodes and preserves internal edges', () => {
const g = new CodeGraph();
g.addNode('a', { keep: true });
g.addNode('b', { keep: true });
g.addNode('c', { keep: false });
g.addEdge('a', 'b');
g.addEdge('a', 'c');
g.addEdge('b', 'c');
const sub = g.subgraph((_, attrs) => attrs.keep);
expect(sub.nodeCount).toBe(2);
expect(sub.edgeCount).toBe(1);
expect(sub.hasEdge('a', 'b')).toBe(true);
expect(sub.hasEdge('a', 'c')).toBe(false);
});
});
describe('CodeGraph — filterEdges', () => {
it('keeps all nodes, filters edges', () => {
const g = new CodeGraph();
g.addEdge('a', 'b', { kind: 'calls' });
g.addEdge('a', 'c', { kind: 'imports' });
const filtered = g.filterEdges((_, __, attrs) => attrs.kind === 'calls');
expect(filtered.nodeCount).toBe(3);
expect(filtered.edgeCount).toBe(1);
expect(filtered.hasEdge('a', 'b')).toBe(true);
expect(filtered.hasEdge('a', 'c')).toBe(false);
});
});
describe('CodeGraph — toEdgeArray', () => {
it('returns {source, target} objects', () => {
const g = new CodeGraph();
g.addEdge('a', 'b');
g.addEdge('b', 'c');
const arr = g.toEdgeArray();
expect(arr).toEqual([
{ source: 'a', target: 'b' },
{ source: 'b', target: 'c' },
]);
});
});
describe('CodeGraph — clone', () => {
it('produces an independent copy', () => {
const g = new CodeGraph();
g.addEdge('a', 'b');
const c = g.clone();
c.addEdge('b', 'c');
expect(g.edgeCount).toBe(1);
expect(c.edgeCount).toBe(2);
});
});
describe('CodeGraph — merge', () => {
it('combines two graphs', () => {
const g1 = new CodeGraph();
g1.addEdge('a', 'b');
const g2 = new CodeGraph();
g2.addEdge('b', 'c');
g1.merge(g2);
expect(g1.nodeCount).toBe(3);
expect(g1.edgeCount).toBe(2);
});
});