forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointTransactionRepository.test.js
More file actions
429 lines (375 loc) · 16.5 KB
/
Copy pathpointTransactionRepository.test.js
File metadata and controls
429 lines (375 loc) · 16.5 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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
'use strict';
/**
* pointTransactionRepository.test.js
*
* Unit tests for pointTransactionRepository covering:
* 1. getUserBalance, getUserTotalPoints, getUserReferralPoints, getUserPointTransactions
* 2. recordPointTransaction — happy path, validation errors, insufficient balance
* 3. Rollback on DB error
* 4. Concurrency — 50 parallel inserts for the same user; final balance = sum of amounts
* 5. Refund (negative delta) atomicity — balance decremented correctly under lock
*
* All DB I/O is mocked; no real PostgreSQL connection is required.
*/
jest.mock('../db/index', () => ({
query: jest.fn(),
pool: { connect: jest.fn() },
}));
const { query, pool } = require('../db/index');
const repo = require('../db/pointTransactionRepository');
// ── Helpers ───────────────────────────────────────────────────────────────────
/**
* Builds a mock pg client whose query() method dispatches on SQL content.
* Responses are consumed in the order provided via the `map` object:
* key: substring of SQL, value: response to return
*
* @param {object} sqlResponseMap { sqlSubstring: resolvedValue | Error }
*/
function buildClient(sqlResponseMap = {}) {
const client = {
query: jest.fn(async (sql) => {
for (const [substr, resp] of Object.entries(sqlResponseMap)) {
if (sql.includes(substr)) {
if (resp instanceof Error) throw resp;
return resp;
}
}
// Default: return empty rows (for BEGIN / COMMIT / ROLLBACK)
return { rows: [], rowCount: 0 };
}),
release: jest.fn(),
};
pool.connect.mockResolvedValue(client);
return client;
}
beforeEach(() => jest.clearAllMocks());
// =============================================================================
// 1. getUserBalance
// =============================================================================
describe('getUserBalance', () => {
test('returns balance from user_balance table', async () => {
query.mockResolvedValue({ rows: [{ balance: 250 }] });
const balance = await repo.getUserBalance(1);
expect(balance).toBe(250);
expect(query).toHaveBeenCalledWith(
'SELECT balance FROM user_balance WHERE user_id = $1',
[1]
);
});
test('returns 0 when no row exists', async () => {
query.mockResolvedValue({ rows: [] });
const balance = await repo.getUserBalance(99);
expect(balance).toBe(0);
});
});
// =============================================================================
// 2. getUserTotalPoints
// =============================================================================
describe('getUserTotalPoints', () => {
test('returns sum of earned, referral, bonus points as string', async () => {
query.mockResolvedValue({ rows: [{ total: '350' }] });
const total = await repo.getUserTotalPoints(1);
expect(total).toBe('350');
});
});
// =============================================================================
// 3. getUserReferralPoints
// =============================================================================
describe('getUserReferralPoints', () => {
test('returns sum of referral points as string', async () => {
query.mockResolvedValue({ rows: [{ total: '100' }] });
const total = await repo.getUserReferralPoints(1);
expect(total).toBe('100');
});
});
// =============================================================================
// 4. getUserPointTransactions
// =============================================================================
describe('getUserPointTransactions', () => {
test('returns paginated transactions with total', async () => {
query
.mockResolvedValueOnce({ rows: [{ total: '5' }] })
.mockResolvedValueOnce({ rows: [{ id: 1, type: 'earned', amount: 100 }] });
const result = await repo.getUserPointTransactions(1, { page: 1, limit: 20 });
expect(result.total).toBe(5);
expect(result.data).toHaveLength(1);
expect(result.page).toBe(1);
expect(result.limit).toBe(20);
});
});
// =============================================================================
// 5. recordPointTransaction — validation errors
// =============================================================================
describe('recordPointTransaction — validation', () => {
test('throws 400 on zero amount', async () => {
await expect(
repo.recordPointTransaction({ userId: 1, type: 'earned', amount: 0 })
).rejects.toMatchObject({ status: 400 });
});
test('throws 400 on non-numeric amount', async () => {
await expect(
repo.recordPointTransaction({ userId: 1, type: 'earned', amount: NaN })
).rejects.toMatchObject({ status: 400 });
});
test('throws 400 on fractional amount (rounds to 0 when near-zero)', async () => {
// Math.round(0.4) = 0 → should throw
await expect(
repo.recordPointTransaction({ userId: 1, type: 'earned', amount: 0.4 })
).rejects.toMatchObject({ status: 400 });
});
});
// =============================================================================
// 6. recordPointTransaction — happy path
// =============================================================================
describe('recordPointTransaction — happy path', () => {
test('inserts transaction and returns row for earned type', async () => {
const txRow = {
id: 1, user_id: 1, type: 'earned', amount: 100,
balance_before: 0, balance_after: 100,
};
buildClient({
'INSERT INTO user_balance': { rows: [], rowCount: 0 },
'SELECT balance FROM user_balance': { rows: [{ balance: 0 }] },
'INSERT INTO point_transactions': { rows: [txRow] },
});
const result = await repo.recordPointTransaction({
userId: 1, type: 'earned', amount: 100, description: 'Test earn',
});
expect(result).toEqual(txRow);
});
test('rounds non-integer amounts before inserting', async () => {
const txRow = { id: 2, user_id: 1, type: 'earned', amount: 10, balance_before: 0, balance_after: 10 };
const client = buildClient({
'INSERT INTO user_balance': { rows: [], rowCount: 0 },
'SELECT balance FROM user_balance': { rows: [{ balance: 0 }] },
'INSERT INTO point_transactions': { rows: [txRow] },
});
await repo.recordPointTransaction({ userId: 1, type: 'earned', amount: 9.7 });
// The INSERT params should contain 10 (rounded), not 9.7
const insertCall = client.query.mock.calls.find(([sql]) =>
sql.includes('INSERT INTO point_transactions')
);
expect(insertCall).toBeDefined();
expect(insertCall[1][2]).toBe(10); // amount param
});
test('client.release() is always called on success', async () => {
const txRow = { id: 3, user_id: 1, type: 'earned', amount: 50, balance_before: 100, balance_after: 150 };
const client = buildClient({
'INSERT INTO user_balance': { rows: [] },
'SELECT balance FROM user_balance': { rows: [{ balance: 100 }] },
'INSERT INTO point_transactions': { rows: [txRow] },
});
await repo.recordPointTransaction({ userId: 1, type: 'earned', amount: 50 });
expect(client.release).toHaveBeenCalledTimes(1);
});
});
// =============================================================================
// 7. recordPointTransaction — insufficient balance
// =============================================================================
describe('recordPointTransaction — insufficient balance', () => {
test('throws 422 when balance would go negative', async () => {
buildClient({
'INSERT INTO user_balance': { rows: [] },
'SELECT balance FROM user_balance': { rows: [{ balance: 10 }] },
});
await expect(
repo.recordPointTransaction({ userId: 1, type: 'redeemed', amount: 50 })
).rejects.toMatchObject({ status: 422 });
});
test('client.release() is called even when balance check fails', async () => {
const client = buildClient({
'INSERT INTO user_balance': { rows: [] },
'SELECT balance FROM user_balance': { rows: [{ balance: 5 }] },
});
await expect(
repo.recordPointTransaction({ userId: 1, type: 'redeemed', amount: 100 })
).rejects.toMatchObject({ status: 422 });
expect(client.release).toHaveBeenCalledTimes(1);
});
});
// =============================================================================
// 8. recordPointTransaction — DB error triggers rollback
// =============================================================================
describe('recordPointTransaction — DB error rollback', () => {
test('rolls back and rethrows on DB error during user_balance insert', async () => {
const client = buildClient({
'INSERT INTO user_balance': new Error('DB error'),
});
await expect(
repo.recordPointTransaction({ userId: 1, type: 'earned', amount: 50 })
).rejects.toThrow('DB error');
// ROLLBACK must have been issued
const rollbackCall = client.query.mock.calls.find(([sql]) => sql === 'ROLLBACK');
expect(rollbackCall).toBeDefined();
expect(client.release).toHaveBeenCalledTimes(1);
});
test('rolls back and rethrows on DB error during point_transactions insert', async () => {
const client = buildClient({
'INSERT INTO user_balance': { rows: [] },
'SELECT balance FROM user_balance': { rows: [{ balance: 200 }] },
'INSERT INTO point_transactions': new Error('insert failed'),
});
await expect(
repo.recordPointTransaction({ userId: 1, type: 'earned', amount: 50 })
).rejects.toThrow('insert failed');
const rollbackCall = client.query.mock.calls.find(([sql]) => sql === 'ROLLBACK');
expect(rollbackCall).toBeDefined();
expect(client.release).toHaveBeenCalledTimes(1);
});
});
// =============================================================================
// 9. Concurrency — 50 parallel inserts for the same user
//
// Requirement: the final balance must equal the exact sum of all inserted
// amounts, with no lost updates.
//
// How we simulate MVCC serialisation in the mock:
// - We maintain an in-memory "balance" variable that is updated atomically
// using JavaScript's single-threaded event loop.
// - Each mock call reads the current balance, computes balance_after, and
// returns it — exactly what the DB does when SELECT ... FOR UPDATE
// serialises concurrent transactions.
// - Because Promise resolution in Node.js is interleaved on the microtask
// queue (not truly parallel), this faithfully models the behaviour of
// PostgreSQL's row-level lock: all 50 workers "queue up", each sees the
// balance left by the previous committer, and each adds its amount.
// =============================================================================
describe('concurrent inserts — 50 parallel earned transactions', () => {
test('final balance equals exact sum of all 50 inserted amounts', async () => {
// Shared mutable state representing the DB-persisted balance
let persistedBalance = 0;
// Each worker gets a unique amount
const amounts = Array.from({ length: 50 }, (_, i) => (i + 1) * 10); // 10, 20, … 500
const expectedTotal = amounts.reduce((a, b) => a + b, 0); // 12750
// Per-call mock: each call reads `persistedBalance`, computes balance_after,
// "commits" it, and returns the row.
pool.connect.mockImplementation(async () => {
const client = {
query: jest.fn(async (sql, params) => {
if (sql === 'BEGIN' || sql === 'COMMIT' || sql === 'ROLLBACK') {
return { rows: [] };
}
if (sql.includes('INSERT INTO user_balance')) {
return { rows: [], rowCount: 0 };
}
if (sql.includes('SELECT balance FROM user_balance')) {
// Return current balance (simulating SELECT ... FOR UPDATE)
return { rows: [{ balance: persistedBalance }] };
}
if (sql.includes('INSERT INTO point_transactions')) {
// params[4] = balance_after
const balanceAfter = params[4];
// "Commit" — advance the persisted balance
persistedBalance = balanceAfter;
return {
rows: [{
id: Math.random(),
user_id: params[0],
type: params[1],
amount: params[2],
balance_before: params[3],
balance_after: params[4],
}],
};
}
return { rows: [] };
}),
release: jest.fn(),
};
return client;
});
// Fire all 50 inserts concurrently
const results = await Promise.all(
amounts.map(amount =>
repo.recordPointTransaction({ userId: 42, type: 'earned', amount })
)
);
expect(results).toHaveLength(50);
// Every call must have returned a valid row
for (const row of results) {
expect(row).toBeDefined();
expect(typeof row.balance_after).toBe('number');
}
// Final persisted balance must equal the sum of all amounts
expect(persistedBalance).toBe(expectedTotal);
});
});
// =============================================================================
// 10. Refund atomicity — negative delta updates balance correctly
// =============================================================================
describe('refund (negative delta) atomicity', () => {
test('a redeemed transaction correctly decrements the balance', async () => {
const startingBalance = 500;
const redeemAmount = 150;
const expectedAfter = startingBalance - redeemAmount; // 350
const txRow = {
id: 99, user_id: 7, type: 'redeemed', amount: redeemAmount,
balance_before: startingBalance, balance_after: expectedAfter,
};
buildClient({
'INSERT INTO user_balance': { rows: [] },
'SELECT balance FROM user_balance': { rows: [{ balance: startingBalance }] },
'INSERT INTO point_transactions': { rows: [txRow] },
});
const result = await repo.recordPointTransaction({
userId: 7, type: 'redeemed', amount: redeemAmount,
});
expect(result.balance_after).toBe(expectedAfter);
expect(result.balance_before).toBe(startingBalance);
expect(result.amount).toBe(redeemAmount);
});
test('a refund that would go negative is rejected with 422', async () => {
buildClient({
'INSERT INTO user_balance': { rows: [] },
'SELECT balance FROM user_balance': { rows: [{ balance: 100 }] },
});
await expect(
repo.recordPointTransaction({ userId: 7, type: 'redeemed', amount: 200 })
).rejects.toMatchObject({ status: 422 });
});
test('a refund to exactly zero balance is accepted', async () => {
const txRow = {
id: 100, user_id: 7, type: 'redeemed', amount: 100,
balance_before: 100, balance_after: 0,
};
buildClient({
'INSERT INTO user_balance': { rows: [] },
'SELECT balance FROM user_balance': { rows: [{ balance: 100 }] },
'INSERT INTO point_transactions': { rows: [txRow] },
});
const result = await repo.recordPointTransaction({
userId: 7, type: 'redeemed', amount: 100,
});
expect(result.balance_after).toBe(0);
});
test('client.release() is called after a successful refund', async () => {
const txRow = { id: 101, user_id: 7, type: 'redeemed', amount: 50, balance_before: 200, balance_after: 150 };
const client = buildClient({
'INSERT INTO user_balance': { rows: [] },
'SELECT balance FROM user_balance': { rows: [{ balance: 200 }] },
'INSERT INTO point_transactions': { rows: [txRow] },
});
await repo.recordPointTransaction({ userId: 7, type: 'redeemed', amount: 50 });
expect(client.release).toHaveBeenCalledTimes(1);
});
});
// =============================================================================
// 11. Bonus / referral types are treated as credits (positive delta)
// =============================================================================
describe('bonus and referral types', () => {
for (const type of ['bonus', 'referral']) {
test(`"${type}" increases balance`, async () => {
const startingBalance = 100;
const amount = 50;
const expectedAfter = 150;
const txRow = { id: 200, user_id: 1, type, amount, balance_before: startingBalance, balance_after: expectedAfter };
buildClient({
'INSERT INTO user_balance': { rows: [] },
'SELECT balance FROM user_balance': { rows: [{ balance: startingBalance }] },
'INSERT INTO point_transactions': { rows: [txRow] },
});
const result = await repo.recordPointTransaction({ userId: 1, type, amount });
expect(result.balance_after).toBe(expectedAfter);
});
}
});