-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathrisk.test.ts
More file actions
58 lines (51 loc) · 2.1 KB
/
Copy pathrisk.test.ts
File metadata and controls
58 lines (51 loc) · 2.1 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
import { describe, expect, it } from 'vitest';
import { minMaxNormalize, ROLE_WEIGHTS, scoreRisk } from '../../../src/graph/classifiers/risk.js';
describe('minMaxNormalize', () => {
it('normalizes to [0, 1] range', () => {
expect(minMaxNormalize([0, 5, 10])).toEqual([0, 0.5, 1]);
});
it('returns all zeros for equal values', () => {
expect(minMaxNormalize([3, 3, 3])).toEqual([0, 0, 0]);
});
it('handles single element', () => {
expect(minMaxNormalize([42])).toEqual([0]);
});
});
describe('scoreRisk', () => {
it('scores items with all signals', () => {
const items = [
{ fan_in: 10, cognitive: 20, churn: 5, mi: 80, role: 'core' },
{ fan_in: 1, cognitive: 2, churn: 0, mi: 100, role: 'leaf' },
];
const scores = scoreRisk(items);
expect(scores).toHaveLength(2);
expect(scores[0].riskScore).toBeGreaterThan(scores[1].riskScore);
expect(scores[0].roleWeight).toBe(ROLE_WEIGHTS.core);
expect(scores[1].roleWeight).toBe(ROLE_WEIGHTS.leaf);
});
it('respects custom weights', () => {
const items = [
{ fan_in: 10, cognitive: 0, churn: 0, mi: 100, role: 'core' },
{ fan_in: 0, cognitive: 10, churn: 0, mi: 100, role: 'core' },
];
const fanWeighted = scoreRisk(items, { fanIn: 1, complexity: 0, churn: 0, role: 0, mi: 0 });
expect(fanWeighted[0].riskScore).toBeGreaterThan(fanWeighted[1].riskScore);
});
it('returns all zeros when all signals are equal', () => {
const items = [
{ fan_in: 5, cognitive: 5, churn: 5, mi: 50, role: 'leaf' },
{ fan_in: 5, cognitive: 5, churn: 5, mi: 50, role: 'leaf' },
];
const scores = scoreRisk(items);
// normFanIn, normComplexity, normChurn should be 0 (all equal)
expect(scores[0].normFanIn).toBe(0);
expect(scores[0].normComplexity).toBe(0);
// But roleWeight and normMI contribute
expect(scores[0].riskScore).toBeGreaterThan(0);
});
it('uses default role weight for unknown roles', () => {
const items = [{ fan_in: 0, cognitive: 0, churn: 0, mi: 0, role: 'unknown' }];
const scores = scoreRisk(items);
expect(scores[0].roleWeight).toBe(0.5);
});
});