mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-05 11:06:28 +00:00
3902a74d6f
Keep recovered Load Balance writes and card audit atomic, and isolate Payment Recon tests.
103 lines
4.0 KiB
JavaScript
103 lines
4.0 KiB
JavaScript
jest.mock('../../infrastructure/database/knex', () => ({ transaction: jest.fn() }));
|
|
jest.mock('../helpers/winston.logger', () => ({ logger: { error: jest.fn() } }));
|
|
|
|
const db = require('../../infrastructure/database/knex');
|
|
const { logger } = require('../helpers/winston.logger');
|
|
const model = require('../modules/loadbalance/loadbalance.model');
|
|
|
|
const header = { c_load_balance_no: 'LBL1', d_load_balance: '2026-11-24' };
|
|
const detail = {
|
|
i_reservation_balance: 7,
|
|
c_reservation_no: 'RBL1',
|
|
i_amount: 20000,
|
|
i_reservation_amount: 10000,
|
|
};
|
|
|
|
function captureTransaction(failTable, updatedReservations = 1) {
|
|
const writes = [];
|
|
const failure = Object.assign(new Error('fixture write failure'), { code: '23514' });
|
|
const trx = jest.fn((table) => {
|
|
const query = {};
|
|
|
|
for (const method of ['insert', 'update', 'del', 'returning', 'where', 'whereIn', 'whereRaw']) {
|
|
query[method] = (...args) => {
|
|
writes.push([table, method, ...args]);
|
|
return query;
|
|
};
|
|
}
|
|
|
|
query.then = (resolve, reject) => {
|
|
if (table === failTable) return Promise.reject(failure).then(resolve, reject);
|
|
const result = table === 'trx.t_d_load_balance' ? [{ i_load_balance: 10 }]
|
|
: table === 'trx.t_d_load_balance_history' ? [{ i_load_balance: 20 }]
|
|
: table === 'trx.t_d_reservation_balance' ? updatedReservations
|
|
: { rowCount: 1 };
|
|
return Promise.resolve(result).then(resolve, reject);
|
|
};
|
|
|
|
return query;
|
|
});
|
|
|
|
db.transaction.mockImplementation((callback) => callback(trx));
|
|
return { trx, writes };
|
|
}
|
|
|
|
beforeEach(() => jest.clearAllMocks());
|
|
|
|
test('simpan saldo memakai ID header dan riwayat masing-masing dalam satu transaksi', async () => {
|
|
const { trx, writes } = captureTransaction();
|
|
|
|
await expect(model.insertLoadingBalance(
|
|
header, [detail], ['RBL1'], '2026-11-24', false, 'card', 'UID', 20000, 1
|
|
)).resolves.toBe(1);
|
|
|
|
expect(db.transaction).toHaveBeenCalledTimes(1);
|
|
expect(trx.mock.calls.flat()).toEqual([
|
|
'trx.t_d_load_balance', 'trx.t_d_load_balance_detail',
|
|
'trx.t_d_load_balance_history', 'trx.t_d_load_balance_history_detail',
|
|
'trx.t_d_reservation_balance', 'trx.t_d_flag_counting', 'trx.t_d_flag_counting',
|
|
]);
|
|
expect(writes).toContainEqual([
|
|
'trx.t_d_load_balance_detail', 'insert', [expect.objectContaining({ i_load_balance: 10, i_amount: 20000 })],
|
|
]);
|
|
expect(writes).toContainEqual([
|
|
'trx.t_d_load_balance_history_detail', 'insert', [expect.objectContaining({ i_load_balance: 20, i_amount: 20000 })],
|
|
]);
|
|
});
|
|
|
|
test('kegagalan detail menggagalkan callback transaksi sebelum status reservasi berubah', async () => {
|
|
const { trx } = captureTransaction('trx.t_d_load_balance_history_detail');
|
|
|
|
await expect(model.updateReservationAndLoadingBalance(
|
|
['RBL1'], header, [detail], '2026-11-24', 1
|
|
)).resolves.toBe(false);
|
|
|
|
expect(trx).not.toHaveBeenCalledWith('trx.t_d_reservation_balance');
|
|
await expect(db.transaction.mock.results[0].value).rejects.toMatchObject({ code: '23514' });
|
|
expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('sqlstate=23514'));
|
|
});
|
|
|
|
test('pemulihan saldo memakai nominal reservasi dan mengembalikan jumlah reservasi diperbarui', async () => {
|
|
const { writes } = captureTransaction();
|
|
|
|
await expect(model.updateReservationAndInsertLoadingBalance(
|
|
['RBL1'], header, [detail], '2026-11-24', 1
|
|
)).resolves.toBe(1);
|
|
|
|
expect(writes).toContainEqual([
|
|
'trx.t_d_load_balance_detail', 'insert', [expect.objectContaining({ i_amount: 10000 })],
|
|
]);
|
|
expect(writes).toContainEqual([
|
|
'trx.t_d_reservation_balance', 'update', expect.objectContaining({ c_status: 'A', c_status_name: 'LOADED' }),
|
|
]);
|
|
});
|
|
|
|
test('pembuatan saldo pemulihan dibatalkan jika tidak ada reservasi yang diperbarui', async () => {
|
|
captureTransaction(undefined, 0);
|
|
|
|
await expect(model.updateReservationAndInsertLoadingBalance(
|
|
['RBL1'], header, [detail], '2026-11-24', 1
|
|
)).resolves.toBe('No reservations found for load balance recovery');
|
|
await expect(db.transaction.mock.results[0].value).rejects.toThrow('No reservations found');
|
|
});
|