-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.test.ts
More file actions
403 lines (355 loc) · 17 KB
/
engine.test.ts
File metadata and controls
403 lines (355 loc) · 17 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import { describe, it } from 'node:test';
import * as assert from 'node:assert/strict';
import { calculateBaseline, generateRecommendation } from './engine';
describe('calculateBaseline', () => {
it('calculates the exact gCO2e value using the ledger default utilization (HIGH confidence)', () => {
// Audit Ledger Proof — v0.7.0 includes memory power draw (CCF standard: 0.392W/GB)
// Instance: m5.large (x86_64, 2 vCPU, 8GB RAM)
// Power: Idle=6.8W, Max=20.4W
// Utilisation: 50% (0.5) [LEDGER DEFAULT]
//
// CPU watts = 6.8 + (20.4 - 6.8) × 0.50 = 13.6W
// Memory = 8GB × 0.392W/GB = 3.136W
// Total = 13.6 + 3.136 = 16.736W
//
// Region: us-east-1 | Grid: 384.5 gCO2e/kWh | PUE: 1.13
//
// Energy = 16.736W × 1.13 × 730h / 1000 = 13.82 kWh/month
// CO2e = 13.82 × 384.5 = 5,308.22g CO2e/month
const expectedCo2e = 5308.2249;
const result = calculateBaseline({
resourceId: 'test',
region: 'us-east-1',
instanceType: 'm5.large',
});
assert.equal(result.confidence, 'HIGH');
assert.ok(
Math.abs(result.totalCo2eGramsPerMonth - expectedCo2e) < 0.001,
`Expected ~${expectedCo2e}, got ${result.totalCo2eGramsPerMonth}`
);
// Verify memory watts are reported in assumptionsApplied
assert.ok(
Math.abs(result.assumptionsApplied.memoryWattsApplied - 3.136) < 0.001,
`Memory watts expected 3.136W, got ${result.assumptionsApplied.memoryWattsApplied}`
);
});
it('calculates a different gCO2e value for an explicitly-supplied utilization (MEDIUM confidence)', () => {
const defaultResult = calculateBaseline({
resourceId: 'test',
region: 'us-east-1',
instanceType: 'm5.large',
});
const explicitResult = calculateBaseline({
resourceId: 'test',
region: 'us-east-1',
instanceType: 'm5.large',
avgUtilization: 0.75,
});
assert.equal(explicitResult.confidence, 'MEDIUM');
assert.ok(explicitResult.totalCo2eGramsPerMonth > defaultResult.totalCo2eGramsPerMonth);
});
it('calculates a meaningfully lower gCO2e value for the same instance in us-west-2', () => {
const eastResult = calculateBaseline({
resourceId: 'east',
region: 'us-east-1',
instanceType: 'm5.large',
});
const westResult = calculateBaseline({
resourceId: 'west',
region: 'us-west-2',
instanceType: 'm5.large',
});
// us-west-2 (240.1 gCO2e/kWh) vs us-east-1 (384.5 gCO2e/kWh)
assert.ok(
westResult.totalCo2eGramsPerMonth < eastResult.totalCo2eGramsPerMonth,
'us-west-2 should have lower carbon than us-east-1'
);
// >30% reduction (ratio is ~0.624)
assert.ok(
westResult.totalCo2eGramsPerMonth < eastResult.totalCo2eGramsPerMonth * 0.7,
'us-west-2 should provide a significant carbon reduction'
);
});
it('handles unsupported instance types gracefully', () => {
const result = calculateBaseline({
resourceId: 'unknown',
region: 'us-east-1',
instanceType: 'x99.superlarge',
});
assert.equal(result.confidence, 'LOW_ASSUMED_DEFAULT');
assert.ok(result.unsupportedReason !== undefined);
assert.equal(result.totalCo2eGramsPerMonth, 0);
assert.equal(result.totalCostUsdPerMonth, 0);
assert.equal(result.assumptionsApplied.memoryWattsApplied, 0);
});
it('returns zero cost and carbon for unsupported region', () => {
const result = calculateBaseline({
resourceId: 'test',
region: 'xx-fake-1',
instanceType: 'm5.large',
});
assert.equal(result.confidence, 'LOW_ASSUMED_DEFAULT');
assert.ok(result.unsupportedReason?.includes('xx-fake-1'));
assert.equal(result.totalCo2eGramsPerMonth, 0);
assert.equal(result.totalCostUsdPerMonth, 0);
});
it('uses 730 hours when hoursPerMonth is not supplied', () => {
const withDefault = calculateBaseline({
resourceId: 'test',
region: 'us-east-1',
instanceType: 'm5.large',
});
const explicit730 = calculateBaseline({
resourceId: 'test',
region: 'us-east-1',
instanceType: 'm5.large',
hoursPerMonth: 730,
});
assert.equal(withDefault.totalCo2eGramsPerMonth, explicit730.totalCo2eGramsPerMonth);
});
it('throws RangeError for avgUtilization > 1', () => {
assert.throws(() => {
calculateBaseline({ resourceId: 'test', region: 'us-east-1', instanceType: 'm5.large', avgUtilization: 1.5 });
}, RangeError);
});
it('throws RangeError for avgUtilization < 0', () => {
assert.throws(() => {
calculateBaseline({ resourceId: 'test', region: 'us-east-1', instanceType: 'm5.large', avgUtilization: -0.1 });
}, RangeError);
});
it('throws RangeError for hoursPerMonth = 0', () => {
assert.throws(() => {
calculateBaseline({ resourceId: 'test', region: 'us-east-1', instanceType: 'm5.large', hoursPerMonth: 0 });
}, RangeError);
});
it('throws RangeError for negative hoursPerMonth', () => {
assert.throws(() => {
calculateBaseline({ resourceId: 'test', region: 'us-east-1', instanceType: 'm5.large', hoursPerMonth: -100 });
}, RangeError);
});
it('accepts avgUtilization = 0 (idle-only carbon + full memory)', () => {
const result = calculateBaseline({
resourceId: 'test', region: 'us-east-1', instanceType: 'm5.large', avgUtilization: 0,
});
// At zero CPU utilization, memory power still contributes
assert.ok(result.totalCo2eGramsPerMonth > 0, 'Should have carbon from idle CPU + memory power');
assert.ok(result.assumptionsApplied.memoryWattsApplied > 0, 'Memory watts should be non-zero');
});
it('accepts avgUtilization = 1 (max carbon)', () => {
const idle = calculateBaseline({
resourceId: 'test', region: 'us-east-1', instanceType: 'm5.large', avgUtilization: 0,
});
const max = calculateBaseline({
resourceId: 'test', region: 'us-east-1', instanceType: 'm5.large', avgUtilization: 1,
});
assert.ok(max.totalCo2eGramsPerMonth > idle.totalCo2eGramsPerMonth);
});
it('returns scope SCOPE_2_AND_3 on all estimates', () => {
const result = calculateBaseline({
resourceId: 'test', region: 'us-east-1', instanceType: 'm5.large',
});
assert.equal(result.scope, 'SCOPE_2_AND_3');
});
// ---------------------------------------------------------------------------
// 4A: Memory power draw tests
// ---------------------------------------------------------------------------
it('4A: memory power is included in Scope 2 calculation (CPU + memory watts)', () => {
// m5.large: CPU=13.6W at 50%, Memory=8GB×0.392=3.136W, Total=16.736W
// Without memory: 4313.57g (old). With memory: 5308.22g (new).
const result = calculateBaseline({
resourceId: 'test', region: 'us-east-1', instanceType: 'm5.large',
});
const expectedCo2e = 5308.2249;
const expectedMemW = 3.136;
assert.ok(
Math.abs(result.totalCo2eGramsPerMonth - expectedCo2e) < 0.001,
`Expected ${expectedCo2e}, got ${result.totalCo2eGramsPerMonth}`
);
assert.ok(
Math.abs(result.assumptionsApplied.memoryWattsApplied - expectedMemW) < 0.001,
`Expected memoryWatts=${expectedMemW}, got ${result.assumptionsApplied.memoryWattsApplied}`
);
});
it('4A: memory-optimised instances carry higher memory power fraction than general-purpose', () => {
// r5.large: 2 vCPU, 16GB RAM — memory is 28.2% of total watts
// m5.large: 2 vCPU, 8GB RAM — memory is 18.7% of total watts
// r5.large should have meaningfully higher Scope 2 than m5.large
// even though both have similar CPU TDP profiles
const r5 = calculateBaseline({
resourceId: 'test', region: 'us-east-1', instanceType: 'r5.large',
});
const m5 = calculateBaseline({
resourceId: 'test', region: 'us-east-1', instanceType: 'm5.large',
});
// r5.large memory=16GB vs m5.large memory=8GB
assert.ok(
r5.assumptionsApplied.memoryWattsApplied > m5.assumptionsApplied.memoryWattsApplied,
'r5.large should have higher memory watts than m5.large'
);
assert.ok(
Math.abs(r5.assumptionsApplied.memoryWattsApplied - 6.272) < 0.001,
`r5.large memory watts expected 6.272W, got ${r5.assumptionsApplied.memoryWattsApplied}`
);
assert.ok(r5.totalCo2eGramsPerMonth > m5.totalCo2eGramsPerMonth,
'r5.large should have higher total CO2e than m5.large'
);
});
it('4A: memory power is constant regardless of CPU utilization', () => {
// Memory draws constant power — not affected by CPU utilisation
const at0 = calculateBaseline({
resourceId: 'test', region: 'us-east-1', instanceType: 'm5.large', avgUtilization: 0,
});
const at100 = calculateBaseline({
resourceId: 'test', region: 'us-east-1', instanceType: 'm5.large', avgUtilization: 1,
});
// Memory watts should be identical regardless of utilization
assert.equal(
at0.assumptionsApplied.memoryWattsApplied,
at100.assumptionsApplied.memoryWattsApplied,
'Memory watts should be constant across utilization levels'
);
});
// ---------------------------------------------------------------------------
// Azure engine tests
// ---------------------------------------------------------------------------
it('Azure: calculates correct Scope 2 CO2e for Standard_D2s_v3 in eastus', () => {
// Audit trace (v0.7.0 — includes memory power):
// Instance: Standard_D2s_v3 (x86_64, 2 vCPU, 8GB)
// CPU: idle=6.8W, max=20.4W → 13.6W at 50%
// Memory: 8GB × 0.392 = 3.136W
// Total: 16.736W
// Region: eastus — grid=380.0 gCO2e/kWh, PUE=1.125, WUE=0.43 L/kWh
// Scope 2: 16.736 × 1.125 × 730 / 1000 × 380.0 = 5,222.89 gCO2e/month
// Scope 3: 1041.7 gCO2e/month (unchanged)
// Water: 16.736 × 730 / 1000 × 0.43 = 5.253 L/month
// Cost: $0.096 × 730 = $70.08/month (unchanged)
const result = calculateBaseline({
resourceId: 'azurerm_linux_virtual_machine.api',
instanceType: 'Standard_D2s_v3',
region: 'eastus',
provider: 'azure',
});
assert.equal(result.confidence, 'HIGH');
assert.equal(result.scope, 'SCOPE_2_AND_3');
assert.ok(Math.abs(result.totalCo2eGramsPerMonth - 5222.8872) < 0.01,
`Scope 2 expected ~5222.89, got ${result.totalCo2eGramsPerMonth}`);
assert.ok(Math.abs(result.embodiedCo2eGramsPerMonth - 1041.7) < 0.01, 'Scope 3 unchanged');
assert.ok(Math.abs(result.waterLitresPerMonth - 5.25343) < 0.001,
`Water expected ~5.25L, got ${result.waterLitresPerMonth}`);
assert.ok(Math.abs(result.totalCostUsdPerMonth - 70.08) < 0.001, 'Cost unchanged');
});
it('Azure: ARM upgrade recommendation (Standard_D2s_v3 → Standard_D2ps_v5) produces savings', () => {
const baseline = calculateBaseline({
resourceId: 'test', instanceType: 'Standard_D2s_v3', region: 'eastus', provider: 'azure',
});
const arm = calculateBaseline({
resourceId: 'test', instanceType: 'Standard_D2ps_v5', region: 'eastus', provider: 'azure',
});
assert.ok(arm.totalCo2eGramsPerMonth < baseline.totalCo2eGramsPerMonth, 'ARM should have lower Scope 2');
assert.ok(arm.embodiedCo2eGramsPerMonth < baseline.embodiedCo2eGramsPerMonth, 'ARM lower embodied');
assert.ok(arm.totalCostUsdPerMonth < baseline.totalCostUsdPerMonth, 'ARM cheaper');
assert.ok(Math.abs(arm.embodiedCo2eGramsPerMonth - 833.3) < 0.01, 'ARM Scope 3 should be 833.3g');
});
it('Azure: returns LOW_ASSUMED_DEFAULT for unsupported instance', () => {
const result = calculateBaseline({
resourceId: 'test', instanceType: 'Standard_M96ms_v3', region: 'eastus', provider: 'azure',
});
assert.equal(result.confidence, 'LOW_ASSUMED_DEFAULT');
assert.equal(result.totalCo2eGramsPerMonth, 0);
assert.ok(result.unsupportedReason?.includes('Standard_M96ms_v3'));
});
it('Azure: returns LOW_ASSUMED_DEFAULT for unsupported region', () => {
const result = calculateBaseline({
resourceId: 'test', instanceType: 'Standard_D2s_v3', region: 'newzealandnorth', provider: 'azure',
});
assert.equal(result.confidence, 'LOW_ASSUMED_DEFAULT');
});
// ---------------------------------------------------------------------------
// GCP engine tests
// ---------------------------------------------------------------------------
it('GCP: calculates correct Scope 2 CO2e for n2-standard-2 in us-central1', () => {
// Audit trace (v0.7.0 — includes memory power):
// Instance: n2-standard-2 (x86_64, 2 vCPU, 8GB)
// CPU: idle=6.8W, max=20.4W → 13.6W at 50%
// Memory: 8GB × 0.392 = 3.136W
// Total: 16.736W
// Region: us-central1 (Iowa) — grid=340.0 gCO2e/kWh, PUE=1.10, WUE=0.40 L/kWh
// Scope 2: 16.736 × 1.10 × 730 / 1000 × 340.0 = 4,569.26 gCO2e/month
// Scope 3: 1041.7 gCO2e/month (unchanged)
// Water: 16.736 × 730 / 1000 × 0.40 = 4.887 L/month
// Cost: $0.097 × 730 = $70.81/month (unchanged)
const result = calculateBaseline({
resourceId: 'google_compute_instance.web',
instanceType: 'n2-standard-2',
region: 'us-central1',
provider: 'gcp',
});
assert.equal(result.confidence, 'HIGH');
assert.equal(result.scope, 'SCOPE_2_AND_3');
assert.ok(Math.abs(result.totalCo2eGramsPerMonth - 4569.26272) < 0.01,
`Scope 2 expected ~4569.26, got ${result.totalCo2eGramsPerMonth}`);
assert.ok(Math.abs(result.embodiedCo2eGramsPerMonth - 1041.7) < 0.01, 'Scope 3 unchanged');
assert.ok(Math.abs(result.waterLitresPerMonth - 4.88691) < 0.001,
`Water expected ~4.89L, got ${result.waterLitresPerMonth}`);
assert.ok(Math.abs(result.totalCostUsdPerMonth - 70.81) < 0.001, 'Cost unchanged');
});
it('GCP: ARM upgrade recommendation (n2-standard-2 → t2a-standard-2) produces savings', () => {
const baseline = calculateBaseline({
resourceId: 'test', instanceType: 'n2-standard-2', region: 'us-central1', provider: 'gcp',
});
const arm = calculateBaseline({
resourceId: 'test', instanceType: 't2a-standard-2', region: 'us-central1', provider: 'gcp',
});
assert.ok(arm.totalCo2eGramsPerMonth < baseline.totalCo2eGramsPerMonth, 'ARM lower Scope 2');
assert.ok(arm.embodiedCo2eGramsPerMonth < baseline.embodiedCo2eGramsPerMonth, 'ARM lower embodied');
assert.ok(arm.totalCostUsdPerMonth < baseline.totalCostUsdPerMonth, 'ARM cheaper');
assert.ok(Math.abs(arm.embodiedCo2eGramsPerMonth - 833.3) < 0.01, 'ARM Scope 3 should be 833.3g');
});
it('GCP: GCP region has lower PUE than AWS (1.10 vs 1.13) — produces lower carbon than equivalent AWS', () => {
const aws = calculateBaseline({
resourceId: 'test', instanceType: 'm5.large', region: 'us-east-1', provider: 'aws',
});
const gcp = calculateBaseline({
resourceId: 'test', instanceType: 'n2-standard-2', region: 'us-east1', provider: 'gcp',
});
assert.ok(gcp.totalCo2eGramsPerMonth < aws.totalCo2eGramsPerMonth,
'GCP us-east1 should have lower Scope 2 than AWS us-east-1 due to lower PUE'
);
});
it('GCP: returns LOW_ASSUMED_DEFAULT for unsupported instance', () => {
const result = calculateBaseline({
resourceId: 'test', instanceType: 'n1-standard-2', region: 'us-central1', provider: 'gcp',
});
assert.equal(result.confidence, 'LOW_ASSUMED_DEFAULT');
assert.ok(result.unsupportedReason?.includes('n1-standard-2'));
});
it('GCP: returns LOW_ASSUMED_DEFAULT for unsupported region', () => {
const result = calculateBaseline({
resourceId: 'test', instanceType: 'n2-standard-2', region: 'me-west1', provider: 'gcp',
});
assert.equal(result.confidence, 'LOW_ASSUMED_DEFAULT');
});
// ---------------------------------------------------------------------------
// t2 series — kunduso compatibility tests
// ---------------------------------------------------------------------------
it('t2.micro: calculates Scope 2 CO2e in us-east-1 (HIGH confidence)', () => {
const result = calculateBaseline({
resourceId: 'aws_instance.web', instanceType: 't2.micro', region: 'us-east-1', provider: 'aws',
});
assert.equal(result.confidence, 'HIGH');
assert.ok(result.totalCo2eGramsPerMonth > 0);
assert.ok(result.totalCo2eGramsPerMonth < 2000, 't2.micro should emit less than 2kg CO2e/month');
assert.equal(result.assumptionsApplied.powerModelUsed, 'LINEAR_INTERPOLATION');
});
it('t2.micro: emits less than t3.micro (lower power spec)', () => {
const t2 = calculateBaseline({ resourceId: 'x', instanceType: 't2.micro', region: 'us-east-1', provider: 'aws' });
const t3 = calculateBaseline({ resourceId: 'x', instanceType: 't3.micro', region: 'us-east-1', provider: 'aws' });
assert.ok(t2.totalCo2eGramsPerMonth < t3.totalCo2eGramsPerMonth, 't2.micro should emit less than t3.micro');
});
it('t2.micro: generates a recommendation in us-east-1', () => {
const input = { resourceId: 'x', instanceType: 't2.micro', region: 'us-east-1', provider: 'aws' as const };
const baseline = calculateBaseline(input);
const rec = generateRecommendation(input, baseline);
assert.ok(rec !== null, 'Should recommend region shift or ARM upgrade for t2.micro in us-east-1');
});
});