Treasury & Bank Transfer Management System — سامانه مدیریت خزانهداری و حوالههای بانکی
feature/reconciliation-guard-20261013
ci/public-windows-bridge-probe-20261017
ci/windows-dispatch-bootstrap-20261016
ci/windows-dual-app-lab-20261016
dev/v7-production-failclosed-20261021
feature/reconciliation-guard-20261013
main
phase-1-foundation
phase-1-implementation
phase-2-treasury-core
phase-3-banking-reconciliation
phase-4-operations-enterprise-ux
phase-5-hardening-release
phase-6-production-platform
phase-7-production-infrastructure-bank-golive
phase-7-sql-readonly-ux-20261015
transfer-exe-to-drive-20261006
| 1 | import test from 'node:test'; |
| 2 | import assert from 'node:assert/strict'; |
| 3 | import { |
| 4 | applyVerifiedBankEvidence as apply, mayAutomaticallyDispatch, |
| 5 | hasFinalBankOutcome, ReconciliationError, |
| 6 | } from '../src/reconciliation.mjs'; |
| 7 | |
| 8 | const payment = (status = 'UNKNOWN') => ({ |
| 9 | id: 'transfer-1', status, amountMinor: '1200000', currency: 'IRR', |
| 10 | version: 5, bankReference: null, appliedProofs: [], |
| 11 | }); |
| 12 | const proof = (kind = 'SETTLED') => ({ |
| 13 | evidenceId: 'bank-event-1', kind, paymentId: 'transfer-1', |
| 14 | amountMinor: '1200000', currency: 'IRR', bankReference: 'bank-ref-1', |
| 15 | verifiedBy: 'trusted-bank-adapter', observedAt: '2026-10-13T10:00:00Z', |
| 16 | definitive: kind === 'REJECTED', |
| 17 | }); |
| 18 | function fails(fn, code) { |
| 19 | assert.throws(fn, (err) => err instanceof ReconciliationError && err.code === code); |
| 20 | } |
| 21 | |
| 22 | test('bank acknowledgment is not final settlement', () => { |
| 23 | const next = apply(payment(), proof('ACCEPTED')).payment; |
| 24 | assert.equal(next.status, 'BANK_ACCEPTED'); |
| 25 | assert.equal(next.version, 6); |
| 26 | assert.equal(hasFinalBankOutcome(next.status), false); |
| 27 | }); |
| 28 | test('verified settlement resolves UNKNOWN without another transfer', () => { |
| 29 | const next = apply(payment(), proof()).payment; |
| 30 | assert.equal(next.status, 'SETTLED'); |
| 31 | assert.equal(hasFinalBankOutcome(next.status), true); |
| 32 | }); |
| 33 | test('definitive rejection resolves UNKNOWN', () => { |
| 34 | const next = apply(payment(), proof('REJECTED')).payment; |
| 35 | assert.equal(next.status, 'BANK_REJECTED'); |
| 36 | assert.equal(hasFinalBankOutcome(next.status), true); |
| 37 | }); |
| 38 | test('non-definitive bank failure cannot be treated as rejection', () => { |
| 39 | fails(() => apply(payment(), {...proof('REJECTED'), definitive: false}), 'NON_DEFINITIVE_REJECTION'); |
| 40 | }); |
| 41 | test('UNKNOWN and all post-send statuses cannot auto-dispatch', () => { |
| 42 | assert.equal(mayAutomaticallyDispatch('READY'), true); |
| 43 | for (const s of ['DISPATCHING', 'UNKNOWN', 'BANK_ACCEPTED', 'SETTLED', 'BANK_REJECTED']) { |
| 44 | assert.equal(mayAutomaticallyDispatch(s), false); |
| 45 | } |
| 46 | }); |
| 47 | test('same verified event is idempotent and returns snapshot identity', () => { |
| 48 | const first = apply(payment(), proof()).payment; |
| 49 | const next = apply(first, proof()); |
| 50 | assert.equal(next.changed, false); |
| 51 | assert.strictEqual(next.payment, first); |
| 52 | }); |
| 53 | test('same event ID with changed contents is rejected', () => { |
| 54 | const first = apply(payment(), proof()).payment; |
| 55 | fails(() => apply(first, {...proof(), observedAt: '2026-10-13T10:00:01Z'}), 'EVIDENCE_ID_COLLISION'); |
| 56 | }); |
| 57 | test('mismatched recipient payment, amount and currency are rejected', () => { |
| 58 | fails(() => apply(payment(), {...proof(), paymentId: 'other'}), 'PAYMENT_MISMATCH'); |
| 59 | fails(() => apply(payment(), {...proof(), amountMinor: '1200001'}), 'AMOUNT_MISMATCH'); |
| 60 | fails(() => apply(payment(), {...proof(), currency: 'USD'}), 'CURRENCY_MISMATCH'); |
| 61 | }); |
| 62 | test('settlement and acknowledgment require a bank reference', () => { |
| 63 | fails(() => apply(payment(), {...proof(), bankReference: ''}), 'MISSING_BANK_REFERENCE'); |
| 64 | fails(() => apply(payment(), {...proof('ACCEPTED'), bankReference: ''}), 'MISSING_BANK_REFERENCE'); |
| 65 | }); |
| 66 | test('evidence cannot affect a READY non-dispatched payment', () => { |
| 67 | fails(() => apply(payment('READY'), proof()), 'NOT_DISPATCHED'); |
| 68 | }); |
| 69 | test('conflicting terminal outcomes are rejected', () => { |
| 70 | const settled = apply(payment(), proof()).payment; |
| 71 | fails(() => apply(settled, {...proof('REJECTED'), evidenceId: 'evt-2'}), 'TERMINAL_CONFLICT'); |
| 72 | const rejected = apply(payment(), proof('REJECTED')).payment; |
| 73 | fails(() => apply(rejected, {...proof('SETTLED'), evidenceId: 'evt-2'}), 'TERMINAL_CONFLICT'); |
| 74 | }); |
| 75 | test('new evidence cannot change bank reference', () => { |
| 76 | const accepted = apply(payment(), proof('ACCEPTED')).payment; |
| 77 | fails(() => apply(accepted, {...proof(), evidenceId: 'evt-2', bankReference: 'another-bank-ref'}), 'BANK_REFERENCE_CONFLICT'); |
| 78 | }); |
| 79 | test('matching acknowledgment followed by settlement is allowed', () => { |
| 80 | const acknowledged = apply(payment(), proof('ACCEPTED')).payment; |
| 81 | const settled = apply(acknowledged, {...proof(), evidenceId: 'evt-2'}).payment; |
| 82 | assert.equal(settled.status, 'SETTLED'); |
| 83 | assert.equal(settled.appliedProofs.length, 2); |
| 84 | assert.equal(settled.version, 7); |
| 85 | }); |
| 86 | test('unverified, malformed amounts and dates fail closed', () => { |
| 87 | fails(() => apply(payment(), {...proof(), verifiedBy: ''}), 'UNVERIFIED_EVIDENCE'); |
| 88 | fails(() => apply(payment(), {...proof(), amountMinor: '1.20'}), 'INVALID_PROOF'); |
| 89 | fails(() => apply(payment(), {...proof(), observedAt: 'not-a-date'}), 'INVALID_PROOF'); |
| 90 | fails(() => apply({...payment(), amountMinor: 1200000}, proof()), 'INVALID_PAYMENT'); |
| 91 | }); |
| 92 | test('applied event digest does not store the bank reference in proof history', () => { |
| 93 | const applied = apply(payment(), proof()).payment.appliedProofs[0]; |
| 94 | assert.deepEqual(Object.keys(applied).sort(), ['digest', 'evidenceId']); |
| 95 | assert.equal(applied.digest.length, 64); |
| 96 | }); |
| 97 | test('input snapshot is unchanged by apply', () => { |
| 98 | const original = payment(); |
| 99 | const snapshot = JSON.stringify(original); |
| 100 | apply(original, proof()); |
| 101 | assert.equal(JSON.stringify(original), snapshot); |
| 102 | }); |