66 lines
1.8 KiB
TypeScript
66 lines
1.8 KiB
TypeScript
import { randomUUID } from 'node:crypto';
|
|
import {
|
|
recordPartnerPaymentSchema,
|
|
reversePartnerPaymentSchema,
|
|
} from '../src/b2b/partner-payment.schemas';
|
|
import {
|
|
assertPartnerPaymentFits,
|
|
partnerPaymentBalance,
|
|
} from '../src/b2b/partner-payment-balance';
|
|
|
|
const valid = () => ({
|
|
idempotencyKey: randomUUID(),
|
|
reference: ' bank/123 ',
|
|
method: 'BANK_TRANSFER',
|
|
amount: '100.25',
|
|
receivedOn: '2026-10-03',
|
|
});
|
|
|
|
describe('partner payment validation and exact balances', () => {
|
|
it('normalizes references and preserves exact cents', () => {
|
|
expect(recordPartnerPaymentSchema.parse(valid())).toMatchObject({
|
|
reference: 'BANK/123',
|
|
amount: '100.25',
|
|
});
|
|
expect(partnerPaymentBalance('3303.00', '100.25')).toEqual({
|
|
paidTotal: '100.25',
|
|
outstandingTotal: '3202.75',
|
|
});
|
|
expect(() =>
|
|
assertPartnerPaymentFits('3303.00', '3202.75', '100.25'),
|
|
).not.toThrow();
|
|
});
|
|
|
|
it('rejects malformed, negative, zero, and over-limit amounts', () => {
|
|
for (const amount of [
|
|
'0.00',
|
|
'-1.00',
|
|
'1',
|
|
'1.001',
|
|
'01.00',
|
|
'99999999999999999.00',
|
|
])
|
|
expect(
|
|
recordPartnerPaymentSchema.safeParse({ ...valid(), amount }).success,
|
|
).toBe(false);
|
|
expect(() =>
|
|
assertPartnerPaymentFits('3303.00', '3202.75', '100.26'),
|
|
).toThrow('Payment exceeds the partner order balance');
|
|
expect(() => partnerPaymentBalance('10.00', '10.01')).toThrow(
|
|
'Partner order balance cannot be calculated',
|
|
);
|
|
});
|
|
|
|
it('rejects unknown fields and short reversal reasons', () => {
|
|
expect(
|
|
recordPartnerPaymentSchema.safeParse({ ...valid(), other: 'x' }).success,
|
|
).toBe(false);
|
|
expect(
|
|
reversePartnerPaymentSchema.safeParse({
|
|
reason: 'x',
|
|
idempotencyKey: randomUUID(),
|
|
}).success,
|
|
).toBe(false);
|
|
});
|
|
});
|