-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsubscriptionExpiryChecker.test.js
More file actions
86 lines (74 loc) · 2.53 KB
/
subscriptionExpiryChecker.test.js
File metadata and controls
86 lines (74 loc) · 2.53 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
const {
SubscriptionExpiryChecker,
DAY_IN_MS,
RISK_THRESHOLD_DAYS,
} = require('./src/services/subscriptionExpiryChecker');
describe('SubscriptionExpiryChecker', () => {
test('marks users as At Risk and triggers low balance email when run-out is below threshold', async () => {
const db = {
listSubscriptionsForRiskCheck: jest.fn(() => [
{
creatorId: 'creator-1',
walletAddress: 'wallet-1',
balance: 2,
dailySpend: 1,
userEmail: 'user@example.com',
},
]),
updateSubscriptionRiskAssessment: jest.fn(),
};
const lowBalanceEmailService = {
sendLowBalanceEmail: jest.fn().mockResolvedValue(undefined),
};
const checker = new SubscriptionExpiryChecker({
database: db,
lowBalanceEmailService,
});
const now = new Date('2026-03-25T00:00:00.000Z');
const result = await checker.runDailyCheck({ now });
expect(result).toEqual({ processed: 1, atRisk: 1 });
expect(db.updateSubscriptionRiskAssessment).toHaveBeenCalledWith(
expect.objectContaining({
creatorId: 'creator-1',
walletAddress: 'wallet-1',
riskStatus: 'At Risk',
}),
);
expect(lowBalanceEmailService.sendLowBalanceEmail).toHaveBeenCalledWith(
expect.objectContaining({
creatorId: 'creator-1',
walletAddress: 'wallet-1',
userEmail: 'user@example.com',
}),
);
});
test('estimates run-out date and does not trigger low-balance email above threshold', async () => {
const db = {
listSubscriptionsForRiskCheck: jest.fn(() => [
{
creatorId: 'creator-2',
walletAddress: 'wallet-2',
balance: 10,
dailySpend: 1,
userEmail: 'healthy@example.com',
},
]),
updateSubscriptionRiskAssessment: jest.fn(),
};
const lowBalanceEmailService = {
sendLowBalanceEmail: jest.fn().mockResolvedValue(undefined),
};
const checker = new SubscriptionExpiryChecker({
database: db,
lowBalanceEmailService,
});
const now = new Date('2026-03-25T00:00:00.000Z');
await checker.runDailyCheck({ now });
const call = db.updateSubscriptionRiskAssessment.mock.calls[0][0];
const expectedRunOut = new Date(now.getTime() + 10 * DAY_IN_MS).toISOString();
expect(call.estimatedRunOutAt).toBe(expectedRunOut);
expect(call.riskStatus).toBeUndefined();
expect(lowBalanceEmailService.sendLowBalanceEmail).not.toHaveBeenCalled();
expect(RISK_THRESHOLD_DAYS).toBe(3);
});
});