-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolicy.test.ts
More file actions
252 lines (222 loc) · 8.46 KB
/
Copy pathpolicy.test.ts
File metadata and controls
252 lines (222 loc) · 8.46 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import { describe, it } from 'node:test';
import * as assert from 'node:assert/strict';
import { writeFileSync, unlinkSync, mkdirSync } from 'node:fs';
import { resolve } from 'node:path';
import { loadPolicy, evaluatePolicy } from './policy';
import type { PlanAnalysisResult } from './types';
function makeMockResult(overrides: Partial<PlanAnalysisResult['totals']> = {}): PlanAnalysisResult {
return {
analysedAt: '2026-03-27T00:00:00Z',
ledgerVersion: '1.2.0',
planFile: 'plan.json',
resources: [],
skipped: [],
providers: ['aws' as const],
unsupportedTypes: [],
totals: {
currentCo2eGramsPerMonth: 5000,
currentEmbodiedCo2eGramsPerMonth: 1041.7,
currentLifecycleCo2eGramsPerMonth: 6041.7,
currentWaterLitresPerMonth: 2.3,
currentCostUsdPerMonth: 200,
potentialCo2eSavingGramsPerMonth: 1000,
potentialCostSavingUsdPerMonth: 20,
...overrides,
},
};
}
const TMP_DIR = resolve('/tmp', `greenops-policy-test-${Date.now()}`);
describe('Policy Engine', () => {
it('returns null when no .greenops.yml exists', () => {
mkdirSync(TMP_DIR, { recursive: true });
const policy = loadPolicy(TMP_DIR);
assert.equal(policy, null, 'Should return null when no policy file present');
});
it('loads a valid .greenops.yml', () => {
mkdirSync(TMP_DIR, { recursive: true });
const policyPath = resolve(TMP_DIR, '.greenops.yml');
writeFileSync(policyPath, [
'version: 1',
'budgets:',
' max_pr_co2e_increase_kg: 10',
' max_pr_cost_increase_usd: 500',
'fail_on_violation: true',
].join('\n'));
const policy = loadPolicy(TMP_DIR);
assert.ok(policy !== null);
assert.equal(policy!.version, 1);
assert.equal(policy!.budgets?.max_pr_co2e_increase_kg, 10);
assert.equal(policy!.budgets?.max_pr_cost_increase_usd, 500);
assert.equal(policy!.fail_on_violation, true);
unlinkSync(policyPath);
});
it('is compliant when no policy file exists', () => {
const result = makeMockResult();
const evaluation = evaluatePolicy(result, null);
assert.equal(evaluation.isCompliant, true);
assert.equal(evaluation.violations.length, 0);
assert.equal(evaluation.shouldBlock, false);
});
it('is compliant when all budgets are within limits', () => {
const result = makeMockResult({
currentCo2eGramsPerMonth: 5000, // 5kg
currentCostUsdPerMonth: 200,
});
const policy = {
version: 1,
budgets: {
max_pr_co2e_increase_kg: 10, // 5kg < 10kg ✓
max_pr_cost_increase_usd: 500, // $200 < $500 ✓
},
fail_on_violation: false,
};
const evaluation = evaluatePolicy(result, policy);
assert.equal(evaluation.isCompliant, true);
assert.equal(evaluation.violations.length, 0);
});
it('detects max_pr_co2e_increase_kg violation', () => {
const result = makeMockResult({
currentCo2eGramsPerMonth: 15000, // 15kg — exceeds 10kg limit
});
const policy = {
version: 1,
budgets: { max_pr_co2e_increase_kg: 10 },
fail_on_violation: false,
};
const evaluation = evaluatePolicy(result, policy);
assert.equal(evaluation.isCompliant, false);
assert.equal(evaluation.violations.length, 1);
assert.equal(evaluation.violations[0].constraint, 'max_pr_co2e_increase_kg');
assert.equal(evaluation.violations[0].actual, 15);
assert.equal(evaluation.violations[0].limit, 10);
});
it('detects max_pr_cost_increase_usd violation', () => {
const result = makeMockResult({
currentCostUsdPerMonth: 600, // $600 — exceeds $500 limit
});
const policy = {
version: 1,
budgets: { max_pr_cost_increase_usd: 500 },
fail_on_violation: false,
};
const evaluation = evaluatePolicy(result, policy);
assert.equal(evaluation.isCompliant, false);
assert.equal(evaluation.violations[0].constraint, 'max_pr_cost_increase_usd');
});
it('collects multiple violations in a single pass', () => {
const result = makeMockResult({
currentCo2eGramsPerMonth: 20000, // 20kg
currentCostUsdPerMonth: 1000,
});
const policy = {
version: 1,
budgets: {
max_pr_co2e_increase_kg: 10,
max_pr_cost_increase_usd: 500,
max_total_co2e_kg: 15,
},
fail_on_violation: true,
};
const evaluation = evaluatePolicy(result, policy);
assert.equal(evaluation.isCompliant, false);
assert.equal(evaluation.violations.length, 3, 'Should collect all 3 violations');
assert.equal(evaluation.shouldBlock, true, 'Should block when fail_on_violation is true');
});
it('shouldBlock is false when fail_on_violation is not set', () => {
const result = makeMockResult({ currentCo2eGramsPerMonth: 20000 });
const policy = {
version: 1,
budgets: { max_pr_co2e_increase_kg: 10 },
// fail_on_violation not set — defaults to false
};
const evaluation = evaluatePolicy(result, policy);
assert.equal(evaluation.isCompliant, false);
assert.equal(evaluation.shouldBlock, false, 'Should not block when fail_on_violation is unset');
});
it('detects max_lifecycle_co2e_kg violation (Scope 2 + Scope 3)', () => {
// lifecycle = Scope2 + Scope3 = 5000 + 1041.7 = 6041.7g = 6.04kg
const result = makeMockResult({
currentCo2eGramsPerMonth: 5000,
currentEmbodiedCo2eGramsPerMonth: 1041.7,
currentLifecycleCo2eGramsPerMonth: 6041.7,
});
const policy = {
version: 1,
budgets: { max_lifecycle_co2e_kg: 5 }, // 6.04kg > 5kg — violation
fail_on_violation: false,
};
const evaluation = evaluatePolicy(result, policy);
assert.equal(evaluation.isCompliant, false);
assert.equal(evaluation.violations.length, 1);
assert.equal(evaluation.violations[0].constraint, 'max_lifecycle_co2e_kg');
assert.ok(evaluation.violations[0].actual > 5, 'Actual should exceed limit');
assert.ok(evaluation.violations[0].message.includes('Scope 2 + Scope 3'), 'Message should mention both scopes');
});
it('max_lifecycle_co2e_kg is compliant when within limit', () => {
const result = makeMockResult({
currentLifecycleCo2eGramsPerMonth: 4000, // 4kg lifecycle
});
const policy = {
version: 1,
budgets: { max_lifecycle_co2e_kg: 10 }, // 4kg < 10kg — pass
fail_on_violation: true,
};
const evaluation = evaluatePolicy(result, policy);
assert.equal(evaluation.isCompliant, true);
assert.equal(evaluation.violations.length, 0);
assert.equal(evaluation.shouldBlock, false);
});
it('max_lifecycle_co2e_kg can trigger independently of Scope 2 constraints', () => {
// Scope 2 alone passes, but lifecycle (Scope 2 + Scope 3) fails
const result = makeMockResult({
currentCo2eGramsPerMonth: 3000, // 3kg Scope 2 — under limit
currentEmbodiedCo2eGramsPerMonth: 4000, // 4kg Scope 3
currentLifecycleCo2eGramsPerMonth: 7000, // 7kg lifecycle — over limit
});
const policy = {
version: 1,
budgets: {
max_pr_co2e_increase_kg: 5, // 3kg < 5kg — passes
max_lifecycle_co2e_kg: 6, // 7kg > 6kg — fails
},
fail_on_violation: true,
};
const evaluation = evaluatePolicy(result, policy);
assert.equal(evaluation.isCompliant, false);
assert.equal(evaluation.violations.length, 1);
assert.equal(evaluation.violations[0].constraint, 'max_lifecycle_co2e_kg');
assert.equal(evaluation.shouldBlock, true);
});
it('loads max_lifecycle_co2e_kg from .greenops.yml', () => {
mkdirSync(TMP_DIR, { recursive: true });
const policyPath = resolve(TMP_DIR, '.greenops.yml');
writeFileSync(policyPath, [
'version: 1',
'budgets:',
' max_lifecycle_co2e_kg: 20',
'fail_on_violation: true',
].join('\n'));
const policy = loadPolicy(TMP_DIR);
assert.ok(policy !== null);
assert.equal(policy!.budgets?.max_lifecycle_co2e_kg, 20);
unlinkSync(policyPath);
});
it('throws a descriptive error for malformed budgets values', () => {
mkdirSync(TMP_DIR, { recursive: true });
const policyPath = resolve(TMP_DIR, '.greenops.yml');
writeFileSync(policyPath, [
'version: 1',
'budgets:',
' max_pr_co2e_increase_kg: -5', // negative — invalid
].join('\n'));
assert.throws(
() => loadPolicy(TMP_DIR),
(err: unknown) => {
assert.ok(err instanceof Error);
assert.ok(err.message.includes('max_pr_co2e_increase_kg'));
return true;
}
);
unlinkSync(policyPath);
});
});