-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathlouvain.test.ts
More file actions
48 lines (43 loc) · 1.65 KB
/
Copy pathlouvain.test.ts
File metadata and controls
48 lines (43 loc) · 1.65 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
import { describe, expect, it } from 'vitest';
import { louvainCommunities } from '../../../src/graph/algorithms/louvain.js';
import { CodeGraph } from '../../../src/graph/model.js';
describe('louvainCommunities', () => {
it('returns empty for empty graph', () => {
const g = new CodeGraph();
const { assignments, modularity } = louvainCommunities(g);
expect(assignments.size).toBe(0);
expect(modularity).toBe(0);
});
it('detects communities in a two-cluster graph', () => {
const g = new CodeGraph();
// Cluster 1: a-b-c tightly connected
g.addEdge('a', 'b');
g.addEdge('b', 'c');
g.addEdge('c', 'a');
// Cluster 2: x-y-z tightly connected
g.addEdge('x', 'y');
g.addEdge('y', 'z');
g.addEdge('z', 'x');
// Weak bridge
g.addEdge('c', 'x');
const { assignments, modularity } = louvainCommunities(g);
expect(assignments.size).toBe(6);
expect(typeof modularity).toBe('number');
// a, b, c should be in the same community
expect(assignments.get('a')).toBe(assignments.get('b'));
expect(assignments.get('b')).toBe(assignments.get('c'));
// x, y, z should be in the same community
expect(assignments.get('x')).toBe(assignments.get('y'));
expect(assignments.get('y')).toBe(assignments.get('z'));
// The two clusters should differ
expect(assignments.get('a')).not.toBe(assignments.get('x'));
});
it('returns assignments for nodes-only graph (no edges)', () => {
const g = new CodeGraph();
g.addNode('a');
g.addNode('b');
const { assignments, modularity } = louvainCommunities(g);
expect(assignments.size).toBe(0);
expect(modularity).toBe(0);
});
});