-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbilling.config-knobs.unit.tests.js
More file actions
140 lines (122 loc) · 4.83 KB
/
Copy pathbilling.config-knobs.unit.tests.js
File metadata and controls
140 lines (122 loc) · 4.83 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
/**
* Module dependencies.
*/
import { jest, describe, test, beforeEach, afterEach, expect } from '@jest/globals';
describe('billing config knob helpers:', () => {
let mockConfig;
let constants;
beforeEach(async () => {
jest.resetModules();
mockConfig = {
billing: {
plans: ['free', 'pro'],
meter: {
runBase: 9,
runBaseUnits: 3,
fallbackPlanId: 'enterprise',
},
outbox: {
maxRetryAttempts: 7,
retryIntervalSec: 42,
},
crons: {
jitterMaxMs: 1234,
},
planChange: {
preserveUsageDefault: false,
},
alerts: {
thresholdPercents: [95, 50],
},
events: {
extrasExhausted: 'billing.custom.exhausted',
},
},
};
jest.unstable_mockModule('../../../config/index.js', () => ({
default: mockConfig,
}));
constants = await import('../lib/billing.constants.js');
});
afterEach(() => {
jest.restoreAllMocks();
});
test('reads explicit billing hardening config overrides', () => {
expect(constants.getMeterRunBase()).toBe(9);
expect(constants.getMeterFallbackPlanId()).toBe('enterprise');
expect(constants.getOutboxMaxRetryAttempts()).toBe(7);
expect(constants.getOutboxRetryIntervalMs()).toBe(42_000);
expect(constants.getCronJitterMaxMs()).toBe(1234);
expect(constants.getPlanChangePreserveUsageDefault()).toBe(false);
expect(constants.getAlertThresholdPercents()).toEqual([95, 50]);
expect(constants.getExtrasExhaustedEventName()).toBe('billing.custom.exhausted');
});
test('getDollarsToUnitRatio reads from config', () => {
mockConfig.billing.meter.dollarsToUnitRatio = 500;
expect(constants.getDollarsToUnitRatio()).toBe(500);
});
test('getDollarsToUnitRatio defaults to 1000', async () => {
mockConfig.billing = {};
expect(constants.getDollarsToUnitRatio()).toBe(1000);
});
test('getDollarsToUnitRatio falls back to 1000 on invalid config (0, negative, NaN)', () => {
mockConfig.billing.meter.dollarsToUnitRatio = 0;
expect(constants.getDollarsToUnitRatio()).toBe(1000);
mockConfig.billing.meter.dollarsToUnitRatio = -5;
expect(constants.getDollarsToUnitRatio()).toBe(1000);
mockConfig.billing.meter.dollarsToUnitRatio = NaN;
expect(constants.getDollarsToUnitRatio()).toBe(1000);
});
test('getMaxUnitsPerOperation reads from config', () => {
mockConfig.billing.meter.maxUnitsPerOperation = 25000;
expect(constants.getMaxUnitsPerOperation()).toBe(25000);
});
test('getMaxUnitsPerOperation defaults to Infinity', async () => {
mockConfig.billing = {};
expect(constants.getMaxUnitsPerOperation()).toBe(Infinity);
});
test('getMaxUnitsPerOperation falls back to Infinity on invalid config (0, negative, NaN)', () => {
mockConfig.billing.meter.maxUnitsPerOperation = 0;
expect(constants.getMaxUnitsPerOperation()).toBe(Infinity);
mockConfig.billing.meter.maxUnitsPerOperation = -100;
expect(constants.getMaxUnitsPerOperation()).toBe(Infinity);
});
test('getDefaultPlanId reads from config', () => {
mockConfig.billing.defaultPlan = 'starter';
expect(constants.getDefaultPlanId()).toBe('starter');
});
test('getDefaultPlanId defaults to free', async () => {
mockConfig.billing = {};
expect(constants.getDefaultPlanId()).toBe('free');
});
test('getDefaultPlanId falls back to free on empty/non-string config', () => {
mockConfig.billing.defaultPlan = '';
expect(constants.getDefaultPlanId()).toBe('free');
mockConfig.billing.defaultPlan = ' ';
expect(constants.getDefaultPlanId()).toBe('free');
});
test('getAlertThresholdPercents coerces string env value to number (env-override guard)', () => {
// env vars deliver a string like '80' — should not throw, should return [80]
mockConfig.billing.alerts = { thresholdPercents: '80' };
expect(constants.getAlertThresholdPercents()).toEqual([80]);
});
test('keeps backward-compatible defaults and runBaseUnits alias', async () => {
mockConfig.billing = {
plans: ['free'],
meter: {
runBaseUnits: 4,
},
};
expect(constants.getMeterRunBase()).toBe(4);
expect(constants.getMeterFallbackPlanId()).toBe('free');
expect(constants.getOutboxMaxRetryAttempts()).toBe(5);
expect(constants.getOutboxRetryIntervalMs()).toBe(300_000);
expect(constants.getCronJitterMaxMs()).toBe(60_000);
expect(constants.getPlanChangePreserveUsageDefault()).toBe(true);
expect(constants.getAlertThresholdPercents()).toEqual([100, 80]);
expect(constants.getExtrasExhaustedEventName()).toBe('billing.extras_debit.exhausted');
expect(constants.getDollarsToUnitRatio()).toBe(1000);
expect(constants.getMaxUnitsPerOperation()).toBe(Infinity);
expect(constants.getDefaultPlanId()).toBe('free');
});
});