Files
Online-Topup-V1/application/tests/paymentrecon-concurrency.test.js
T
Mohamad Ihsan 3902a74d6f Align transaction and log timestamps with Jakarta time
Keep recovered Load Balance writes and card audit atomic, and isolate Payment Recon tests.
2026-11-24 22:57:32 +07:00

96 lines
3.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const express = require('express');
const request = require('supertest');
const model = require('../modules/paymentrecon/paymentrecon.model');
jest.mock('../middlewares/jwt.middleware', () => ({
authenticateToken(req, res, next) {
req.partner_id_token = '1';
next();
},
}));
jest.mock('../helpers/helper', () => ({ getUniqueCode: () => 'fixture', randomNumberInt: () => 0 }));
jest.mock('../helpers/winston.logger', () => ({ logger: {
debug: jest.fn(), info: jest.fn(), warn: jest.fn(), error: jest.fn(),
} }));
jest.mock('../modules/paymentrecon/paymentrecon.model', () => ({
getPartnerByID: jest.fn(),
checkReservation: jest.fn(),
checkReservationNumberAndAmountAlreadyRecon: jest.fn(),
getLastCounterPaymentReconNo: jest.fn(),
insertPaymentReconNumber: jest.fn(),
checkReservationCanceledStatus: jest.fn(),
checkReservationNumberAndAmount: jest.fn(),
completePaymentRecon: jest.fn(),
insertPaymentReconTransaction: jest.fn(),
}));
const app = express();
app.use(express.json());
app.use('/api', require('../routes/paymentrecon.routes'));
const body = { date: '2026-11-23', reservation_number: 'RBL1', bank_ref: 'BANK1', amount: 1000 };
beforeEach(() => {
jest.resetAllMocks();
model.getPartnerByID.mockResolvedValue({ b_payment_recon: true, partner_code: 'TST' });
model.checkReservation.mockResolvedValue({ i_partner: 1 });
model.insertPaymentReconNumber.mockResolvedValue({ rowCount: 1 });
model.checkReservationNumberAndAmount.mockResolvedValue({ total_transaction: 1, total_amount: '1000' });
});
test('Payment Recon berhasil menyimpan transaksi dan mengembalikan nomor rekonsiliasi', async () => {
model.completePaymentRecon.mockResolvedValue(true);
const response = await request(app).post('/api/payment-recon').send(body);
expect(response.status).toBe(200);
expect(response.body).toMatchObject({
code: '00',
data: { payment_recon_no: expect.stringMatching(/^RCTST\d{12}$/) },
});
expect(model.completePaymentRecon).toHaveBeenCalledWith(expect.objectContaining({
paymentReconNumber: response.body.data.payment_recon_no,
time: response.body.data.recon_date,
}));
});
test('the loser of a concurrent reconciliation returns the winner’s number and code 04', async () => {
const winner = { c_payment_recon_no: 'RC-WINNER', d_payment_recon: '2026-11-23 10:00:00' };
model.checkReservationNumberAndAmountAlreadyRecon
.mockResolvedValueOnce(undefined)
.mockResolvedValueOnce(winner);
model.completePaymentRecon.mockResolvedValue(false);
const response = await request(app).post('/api/payment-recon').send(body);
expect(response.status).toBe(200);
expect(response.body).toMatchObject({ code: '04', data: { payment_recon_no: 'RC-WINNER' } });
expect(model.completePaymentRecon).toHaveBeenCalledWith(expect.objectContaining({
reservationNumber: 'RBL1', expectedAmount: '1000',
}));
expect(model.insertPaymentReconTransaction).not.toHaveBeenCalled();
});
test('a failed atomic commit returns an error instead of reporting reconciliation success', async () => {
model.completePaymentRecon.mockRejectedValue(new Error('transaction failed'));
const response = await request(app).post('/api/payment-recon').send(body);
expect(response.status).toBe(200);
expect(response.body.code).toBe('500');
expect(model.insertPaymentReconTransaction).not.toHaveBeenCalled();
});
test('konflik nomor rekonsiliasi berhenti setelah lima percobaan', async () => {
model.insertPaymentReconNumber.mockResolvedValue({ rowCount: 0 });
const response = await request(app).post('/api/payment-recon').send(body);
expect(response.status).toBe(200);
expect(response.body).toMatchObject({
code: '500',
message: 'server is busy. please try again.',
});
expect(model.insertPaymentReconNumber).toHaveBeenCalledTimes(5);
expect(model.completePaymentRecon).not.toHaveBeenCalled();
});