-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgate.test.ts
More file actions
120 lines (103 loc) · 3.91 KB
/
Copy pathgate.test.ts
File metadata and controls
120 lines (103 loc) · 3.91 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
import { describe, it, expect } from 'vitest';
import { evaluateGate, parseGateConditions, ALL_GATE_CONDITIONS } from '../gate.js';
import type { ChangeReport, CVEEntry } from '../types.js';
const emptyReport = (): ChangeReport => ({
added: [],
removed: [],
upgraded: [],
newCVEs: [],
fixedCVEs: [],
summary: {
totalAdded: 0,
totalRemoved: 0,
totalUpgraded: 0,
totalNewCVEs: 0,
totalFixedCVEs: 0,
},
});
const cve = (id: string, severity: CVEEntry['severity']): CVEEntry => ({
id,
affects: 'pkg:npm/example@1.0.0',
severity,
});
describe('parseGateConditions', () => {
it('parses a comma-separated list', () => {
expect(parseGateConditions('critical,major')).toEqual(['critical', 'major']);
});
it('is case-insensitive and trims whitespace', () => {
expect(parseGateConditions(' New-CVEs , HIGH ')).toEqual(['new-cves', 'high']);
});
it('ignores empty tokens', () => {
expect(parseGateConditions('any,,')).toEqual(['any']);
});
it('throws on unknown conditions', () => {
expect(() => parseGateConditions('bogus')).toThrow(/Unknown --fail-on condition/);
});
it('accepts every documented condition', () => {
expect(parseGateConditions(ALL_GATE_CONDITIONS.join(','))).toEqual(ALL_GATE_CONDITIONS);
});
});
describe('evaluateGate', () => {
it('does not fail an empty report', () => {
const result = evaluateGate(emptyReport(), ['any', 'critical', 'major', 'new-cves']);
expect(result.shouldFail).toBe(false);
expect(result.reasons).toEqual([]);
});
it('fails on "any" when there are changes', () => {
const r = emptyReport();
r.summary.totalAdded = 2;
const result = evaluateGate(r, ['any']);
expect(result.shouldFail).toBe(true);
expect(result.reasons[0]).toMatch(/2 change/);
});
it('fails on added/removed/upgraded counts', () => {
const r = emptyReport();
r.summary.totalRemoved = 1;
expect(evaluateGate(r, ['added']).shouldFail).toBe(false);
expect(evaluateGate(r, ['removed']).shouldFail).toBe(true);
});
it('fails on major bumps only when a major bump exists', () => {
const r = emptyReport();
r.upgraded = [
{ component: { name: 'a' }, from: '1.0.0', to: '1.2.0', isMajorBump: false },
];
r.summary.totalUpgraded = 1;
expect(evaluateGate(r, ['major']).shouldFail).toBe(false);
expect(evaluateGate(r, ['upgraded']).shouldFail).toBe(true);
r.upgraded.push({ component: { name: 'b' }, from: '1.0.0', to: '2.0.0', isMajorBump: true });
r.summary.totalUpgraded = 2;
expect(evaluateGate(r, ['major']).shouldFail).toBe(true);
});
it('fails on new CVEs', () => {
const r = emptyReport();
r.newCVEs = [cve('CVE-1', 'low')];
r.summary.totalNewCVEs = 1;
expect(evaluateGate(r, ['new-cves']).shouldFail).toBe(true);
});
it('applies severity thresholds (at or above)', () => {
const r = emptyReport();
r.newCVEs = [cve('CVE-low', 'low'), cve('CVE-high', 'high')];
r.summary.totalNewCVEs = 2;
expect(evaluateGate(r, ['critical']).shouldFail).toBe(false);
expect(evaluateGate(r, ['high']).shouldFail).toBe(true);
expect(evaluateGate(r, ['medium']).shouldFail).toBe(true);
expect(evaluateGate(r, ['low']).shouldFail).toBe(true);
});
it('ignores CVEs with unknown severity for severity thresholds', () => {
const r = emptyReport();
r.newCVEs = [cve('CVE-x', undefined)];
r.summary.totalNewCVEs = 1;
expect(evaluateGate(r, ['low']).shouldFail).toBe(false);
// ...but still counts under the generic new-cves condition
expect(evaluateGate(r, ['new-cves']).shouldFail).toBe(true);
});
it('reports a reason for each triggered condition', () => {
const r = emptyReport();
r.summary.totalAdded = 1;
r.newCVEs = [cve('CVE-1', 'critical')];
r.summary.totalNewCVEs = 1;
const result = evaluateGate(r, ['added', 'critical']);
expect(result.shouldFail).toBe(true);
expect(result.reasons).toHaveLength(2);
});
});