forked from SmartDropLabs/smartdrop-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathairdropExpiry.test.js
More file actions
221 lines (181 loc) · 7.91 KB
/
Copy pathairdropExpiry.test.js
File metadata and controls
221 lines (181 loc) · 7.91 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
'use strict';
const mockLogger = {
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
debug: jest.fn(),
};
const mockAirdropsService = {
getCurrentLedger: jest.fn(),
scanIds: jest.fn(),
get: jest.fn(),
markExpired: jest.fn(),
TERMINAL_STATUSES: new Set(['completed', 'failed', 'cancelled', 'expired']),
};
const mockDispatch = jest.fn();
jest.mock('../src/logger', () => mockLogger);
jest.mock('../src/services/airdrops', () => mockAirdropsService);
jest.mock('../src/services/webhookDispatcher', () => ({ dispatch: mockDispatch }));
jest.mock('../src/config', () => ({
airdrops: {
expiryCheckIntervalSeconds: 60,
ledgerCacheTtlMs: 5000,
expiryScanBatchSize: 100,
},
}));
// scanIds() is an async generator in the real service; this mock accepts a
// plain array of batches and yields them the same way.
function mockScanIdsReturning(batches) {
mockAirdropsService.scanIds.mockReturnValue(
(async function* () {
for (const batch of batches) yield batch;
})()
);
}
function draftAirdrop(overrides = {}) {
return {
id: 'drop_1',
status: 'draft',
expiry_ledger: 100,
...overrides,
};
}
const { tick } = require('../src/jobs/airdropExpiry');
beforeEach(() => {
jest.clearAllMocks();
mockAirdropsService.TERMINAL_STATUSES = new Set(['completed', 'failed', 'cancelled', 'expired']);
});
describe('airdropExpiry job tick (#88)', () => {
test('expires an airdrop past its expiry_ledger and dispatches airdrop.failed exactly once', async () => {
mockAirdropsService.getCurrentLedger.mockResolvedValue(150);
mockScanIdsReturning([['drop_1']]);
mockAirdropsService.get.mockResolvedValue(draftAirdrop({ expiry_ledger: 100 }));
mockAirdropsService.markExpired.mockResolvedValue(
draftAirdrop({ status: 'expired', expiry_ledger: 100 })
);
await tick();
expect(mockAirdropsService.markExpired).toHaveBeenCalledWith('drop_1', 150);
expect(mockDispatch).toHaveBeenCalledTimes(1);
expect(mockDispatch).toHaveBeenCalledWith(
expect.objectContaining({
event_type: 'airdrop.failed',
data: expect.objectContaining({ airdrop_id: 'drop_1', reason: 'expired' }),
})
);
});
test('leaves a non-expired airdrop untouched', async () => {
mockAirdropsService.getCurrentLedger.mockResolvedValue(50);
mockScanIdsReturning([['drop_1']]);
mockAirdropsService.get.mockResolvedValue(draftAirdrop({ expiry_ledger: 100 }));
await tick();
expect(mockAirdropsService.markExpired).not.toHaveBeenCalled();
expect(mockDispatch).not.toHaveBeenCalled();
});
test('skips an airdrop already in a terminal status without attempting a transition', async () => {
mockAirdropsService.getCurrentLedger.mockResolvedValue(150);
mockScanIdsReturning([['drop_1']]);
mockAirdropsService.get.mockResolvedValue(draftAirdrop({ status: 'cancelled', expiry_ledger: 100 }));
await tick();
expect(mockAirdropsService.markExpired).not.toHaveBeenCalled();
expect(mockDispatch).not.toHaveBeenCalled();
});
test('does not dispatch when markExpired reports no transition happened (lost a race)', async () => {
mockAirdropsService.getCurrentLedger.mockResolvedValue(150);
mockScanIdsReturning([['drop_1']]);
mockAirdropsService.get.mockResolvedValue(draftAirdrop({ expiry_ledger: 100 }));
mockAirdropsService.markExpired.mockResolvedValue(null);
await tick();
expect(mockAirdropsService.markExpired).toHaveBeenCalled();
expect(mockDispatch).not.toHaveBeenCalled();
});
test('is idempotent across two ticks: the webhook fires exactly once total', async () => {
mockAirdropsService.getCurrentLedger.mockResolvedValue(150);
mockScanIdsReturning([['drop_1']]);
mockAirdropsService.get.mockResolvedValue(draftAirdrop({ expiry_ledger: 100 }));
mockAirdropsService.markExpired.mockResolvedValueOnce(
draftAirdrop({ status: 'expired', expiry_ledger: 100 })
);
await tick();
// Second tick: the airdrop is now expired (terminal), matching what a
// real second scan would see after the first tick's transition landed.
mockScanIdsReturning([['drop_1']]);
mockAirdropsService.get.mockResolvedValue(draftAirdrop({ status: 'expired', expiry_ledger: 100 }));
await tick();
expect(mockDispatch).toHaveBeenCalledTimes(1);
});
test('retries Horizon call with backoff on failure before skipping cycle', async () => {
mockAirdropsService.getCurrentLedger.mockRejectedValue(new Error('Horizon unreachable'));
await expect(tick()).resolves.toBeUndefined();
expect(mockAirdropsService.getCurrentLedger).toHaveBeenCalledTimes(3);
expect(mockLogger.warn).toHaveBeenCalledWith(
expect.stringContaining('Horizon call failed, retrying'),
expect.objectContaining({ attempt: 1, maxRetries: 3 })
);
expect(mockLogger.warn).toHaveBeenCalledWith(
expect.stringContaining('Horizon unreachable'),
expect.objectContaining({ error: 'Horizon unreachable' })
);
expect(mockAirdropsService.scanIds).not.toHaveBeenCalled();
expect(mockAirdropsService.markExpired).not.toHaveBeenCalled();
expect(mockDispatch).not.toHaveBeenCalled();
});
test('succeeds if Horizon call succeeds on a retry attempt', async () => {
mockAirdropsService.getCurrentLedger
.mockRejectedValueOnce(new Error('Horizon temporary timeout'))
.mockResolvedValueOnce(150);
mockScanIdsReturning([['drop_1']]);
mockAirdropsService.get.mockResolvedValue(draftAirdrop({ expiry_ledger: 100 }));
mockAirdropsService.markExpired.mockResolvedValue(
draftAirdrop({ status: 'expired', expiry_ledger: 100 })
);
await tick();
expect(mockAirdropsService.getCurrentLedger).toHaveBeenCalledTimes(2);
expect(mockAirdropsService.markExpired).toHaveBeenCalledWith('drop_1', 150);
expect(mockDispatch).toHaveBeenCalledTimes(1);
});
test('a dispatch failure is logged and does not throw out of the tick', async () => {
mockAirdropsService.getCurrentLedger.mockResolvedValue(150);
mockScanIdsReturning([['drop_1']]);
mockAirdropsService.get.mockResolvedValue(draftAirdrop({ expiry_ledger: 100 }));
mockAirdropsService.markExpired.mockResolvedValue(
draftAirdrop({ status: 'expired', expiry_ledger: 100 })
);
mockDispatch.mockRejectedValue(new Error('webhook target unreachable'));
await expect(tick()).resolves.toBeUndefined();
expect(mockLogger.error).toHaveBeenCalledWith(
'Airdrop expiry webhook dispatch failed',
expect.objectContaining({ airdrop_id: 'drop_1' })
);
});
test('handles multiple airdrops across multiple scan batches', async () => {
mockAirdropsService.getCurrentLedger.mockResolvedValue(150);
mockScanIdsReturning([['drop_1'], ['drop_2']]);
mockAirdropsService.get.mockImplementation(async (id) =>
draftAirdrop({ id, expiry_ledger: 100 })
);
mockAirdropsService.markExpired.mockImplementation(async (id) =>
draftAirdrop({ id, status: 'expired', expiry_ledger: 100 })
);
await tick();
expect(mockAirdropsService.markExpired).toHaveBeenCalledTimes(2);
expect(mockDispatch).toHaveBeenCalledTimes(2);
});
test('a per-airdrop read error is logged and does not stop the rest of the scan', async () => {
mockAirdropsService.getCurrentLedger.mockResolvedValue(150);
mockScanIdsReturning([['drop_1', 'drop_2']]);
mockAirdropsService.get.mockImplementation(async (id) => {
if (id === 'drop_1') throw new Error('redis timeout');
return draftAirdrop({ id, expiry_ledger: 100 });
});
mockAirdropsService.markExpired.mockResolvedValue(
draftAirdrop({ id: 'drop_2', status: 'expired', expiry_ledger: 100 })
);
await tick();
expect(mockLogger.error).toHaveBeenCalledWith(
expect.stringContaining('failed to read airdrop'),
expect.objectContaining({ airdrop_id: 'drop_1' })
);
expect(mockAirdropsService.markExpired).toHaveBeenCalledWith('drop_2', 150);
expect(mockDispatch).toHaveBeenCalledTimes(1);
});
});