-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbilling.init.unit.tests.js
More file actions
173 lines (134 loc) · 5.97 KB
/
Copy pathbilling.init.unit.tests.js
File metadata and controls
173 lines (134 loc) · 5.97 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
/**
* Module dependencies.
*/
import { jest, describe, test, beforeEach, afterEach, expect } from '@jest/globals';
/**
* Unit tests for billing.init — boot validators.
*/
describe('billing.init unit tests:', () => {
let billingInit;
let mockBillingUsageRepository;
let mockConfig;
let mockDistinct;
let mockMongoose;
let mockLogger;
let mockInvitationEvents;
const mockApp = {};
beforeEach(async () => {
jest.resetModules();
mockConfig = {
billing: {
meterMode: false,
packs: [],
},
};
mockBillingUsageRepository = {
countLegacyConsumedHistoryIds: jest.fn().mockResolvedValue(0),
};
mockDistinct = jest.fn().mockResolvedValue([]);
mockMongoose = {
model: jest.fn().mockReturnValue({ distinct: mockDistinct }),
};
mockLogger = { info: jest.fn(), error: jest.fn(), warn: jest.fn() };
jest.unstable_mockModule('../../../config/index.js', () => ({
default: mockConfig,
}));
jest.unstable_mockModule('../repositories/billing.usage.repository.js', () => ({
default: mockBillingUsageRepository,
}));
// Stub analytics, logger and events to avoid side effects
jest.unstable_mockModule('../../../lib/services/analytics.js', () => ({
default: { groupIdentify: jest.fn() },
}));
jest.unstable_mockModule('../../../lib/services/logger.js', () => ({
default: mockLogger,
}));
jest.unstable_mockModule('../lib/events.js', () => ({
default: { on: jest.fn(), emit: jest.fn() },
}));
// P8a: billing is an optional consumer of the invitations `invitation.accepted`
// event — stub the singleton so the unit test asserts the listener wiring in isolation.
mockInvitationEvents = { on: jest.fn(), emit: jest.fn() };
jest.unstable_mockModule('../../invitations/lib/events.js', () => ({
default: mockInvitationEvents,
}));
// Stub billing.email so boot validator tests don't wire real email listeners
jest.unstable_mockModule('../billing.email.js', () => ({
setupBillingEmails: jest.fn(),
}));
jest.unstable_mockModule('mongoose', () => ({
default: mockMongoose,
}));
const mod = await import('../billing.init.js');
billingInit = mod.default;
});
afterEach(() => {
jest.restoreAllMocks();
});
test('resolves without error when meterMode=false', async () => {
await expect(billingInit(mockApp)).resolves.toBeUndefined();
});
test('P8a: wires a (no-op) invitation.accepted listener that does not throw on emit', async () => {
await billingInit(mockApp);
// The seam is proven by the listener being registered on the invitations emitter.
const acceptedCall = mockInvitationEvents.on.mock.calls.find(([evt]) => evt === 'invitation.accepted');
expect(acceptedCall).toBeDefined();
const handler = acceptedCall[1];
expect(typeof handler).toBe('function');
// No-op: invoking it with a payload must not throw (and returns nothing).
expect(() => handler({ invitationId: 'i1', email: 'a@b.co', invitedBy: 'x', acceptedUserId: 'u1' })).not.toThrow();
});
test('resolves without error when meterMode=true and no legacy docs', async () => {
mockConfig.billing.meterMode = true;
mockConfig.billing.plans = ['free', 'growth', 'pro'];
await expect(billingInit(mockApp)).resolves.toBeUndefined();
});
test('boot validator warns on orphaned Subscription.plan values when meterMode=true', async () => {
mockConfig.billing.meterMode = true;
mockConfig.billing.plans = ['free', 'starter', 'pro'];
mockDistinct.mockResolvedValue(['free', 'legacy_plan']);
await billingInit(mockApp);
expect(mockDistinct).toHaveBeenCalledWith('plan');
// After Batch 4 item 6: uses logger.warn instead of console.warn
const warnCalls = mockLogger.warn.mock.calls.map((c) => c[0]);
expect(warnCalls.some((w) => w.includes('"legacy_plan" not in planDefinitions'))).toBe(true);
expect(warnCalls.some((w) => w.includes('"free"'))).toBe(false);
});
test('meterMode=true aborts boot when legacy consumedHistoryIds fields remain', async () => {
mockConfig.billing.meterMode = true;
mockBillingUsageRepository.countLegacyConsumedHistoryIds.mockResolvedValue(2);
await expect(billingInit(mockApp)).rejects.toThrow('legacy consumedHistoryIds field still present');
});
test('warns at boot when thresholdPercents contains unsupported value (not 80/100)', async () => {
mockConfig.billing.meterMode = true;
mockConfig.billing.alerts = { thresholdPercents: [75] };
mockConfig.billing.plans = ['free'];
await billingInit(mockApp);
// After Batch 4 item 6: uses logger.warn instead of console.warn
const warnCalls = mockLogger.warn.mock.calls.map((c) => c[0]);
expect(warnCalls.some((w) => w.includes('75%') && w.includes('silently skipped'))).toBe(true);
});
test('does not warn at boot when thresholdPercents contains only supported values', async () => {
mockConfig.billing.meterMode = true;
mockConfig.billing.alerts = { thresholdPercents: [80, 100] };
mockConfig.billing.plans = ['free'];
await billingInit(mockApp);
const warnCalls = mockLogger.warn.mock.calls.map((c) => c[0]);
expect(warnCalls.some((w) => typeof w === 'string' && w.includes('silently skipped'))).toBe(false);
});
test('does not warn at boot for threshold validation when meterMode=false', async () => {
mockConfig.billing.meterMode = false;
mockConfig.billing.alerts = { thresholdPercents: [75] };
await billingInit(mockApp);
const warnCalls = mockLogger.warn.mock.calls.map((c) => c[0]);
expect(warnCalls.some((w) => typeof w === 'string' && w.includes('silently skipped'))).toBe(false);
});
test('boot validator failure does not crash boot', async () => {
mockConfig.billing.meterMode = true;
mockConfig.billing.plans = ['free'];
mockMongoose.model.mockImplementation(() => {
throw new Error('model not registered');
});
await expect(billingInit(mockApp)).resolves.toBeUndefined();
});
});