|
| 1 | +import { |
| 2 | + EscrowContractError, |
| 3 | + ChainTimeoutError, |
| 4 | + SimulationError, |
| 5 | + StellarIntegrationError, |
| 6 | + SubmissionError, |
| 7 | +} from '../../stellar/errors/stellar-integration.errors'; |
| 8 | +import { EscrowErrorCode } from '../../stellar/errors/escrow-error-code.enum'; |
| 9 | +import { |
| 10 | + EscrowContractRejectedError, |
| 11 | + EscrowSimulationFailedError, |
| 12 | + EscrowSubmissionFailedError, |
| 13 | + PaymentFlowError, |
| 14 | +} from './payment-flow.errors'; |
| 15 | +import { mapStellarFundingError } from './stellar-error-mapper'; |
| 16 | + |
| 17 | +/** |
| 18 | + * Every EscrowError variant the on-chain contract can return (mirrors |
| 19 | + * contracts/escrow/src/lib.rs exactly). If the contract adds a new |
| 20 | + * variant and someone forgets to update ESCROW_ERROR_MESSAGES, this |
| 21 | + * test will catch it because the Record type becomes incomplete. |
| 22 | + */ |
| 23 | +const ALL_ESCROW_ERROR_CODES: EscrowErrorCode[] = [ |
| 24 | + EscrowErrorCode.NotInitialized, |
| 25 | + EscrowErrorCode.AlreadyInitialized, |
| 26 | + EscrowErrorCode.NotFound, |
| 27 | + EscrowErrorCode.AlreadyFunded, |
| 28 | + EscrowErrorCode.NotFunded, |
| 29 | + EscrowErrorCode.InvalidStatus, |
| 30 | + EscrowErrorCode.Unauthorized, |
| 31 | + EscrowErrorCode.InvalidAmount, |
| 32 | + EscrowErrorCode.InsufficientBalance, |
| 33 | +]; |
| 34 | + |
| 35 | +describe('mapStellarFundingError', () => { |
| 36 | + // ── EscrowContractError (9 variants) ────────────────────────────────── |
| 37 | + |
| 38 | + describe('EscrowContractError → EscrowContractRejectedError', () => { |
| 39 | + it.each(ALL_ESCROW_ERROR_CODES)( |
| 40 | + 'maps EscrowErrorCode.%s to an EscrowContractRejectedError', |
| 41 | + (code) => { |
| 42 | + const rawError = `HostError: Error(Contract, #${code})`; |
| 43 | + const input = new EscrowContractError(code, rawError); |
| 44 | + |
| 45 | + const result = mapStellarFundingError(input); |
| 46 | + |
| 47 | + expect(result).toBeInstanceOf(EscrowContractRejectedError); |
| 48 | + expect(result.code).toBe('ESCROW_CONTRACT_REJECTED'); |
| 49 | + expect((result as EscrowContractRejectedError).escrowErrorCode).toBe( |
| 50 | + code, |
| 51 | + ); |
| 52 | + }, |
| 53 | + ); |
| 54 | + |
| 55 | + it.each(ALL_ESCROW_ERROR_CODES)( |
| 56 | + 'provides a distinct, actionable message for EscrowErrorCode.%s', |
| 57 | + (code) => { |
| 58 | + const rawError = `HostError: Error(Contract, #${code})`; |
| 59 | + const input = new EscrowContractError(code, rawError); |
| 60 | + |
| 61 | + const result = mapStellarFundingError(input); |
| 62 | + const message = (result as EscrowContractRejectedError).message; |
| 63 | + |
| 64 | + // Every variant must produce a non-empty message that does NOT |
| 65 | + // look like the old generic catch-all (which just forwarded the |
| 66 | + // raw Soroban error string). |
| 67 | + expect(message.length).toBeGreaterThan(0); |
| 68 | + expect(message).not.toBe(rawError); |
| 69 | + // Actionable messages contain a verb / instruction for the caller. |
| 70 | + expect(message).not.toMatch(/^Escrow contract rejected the call/); |
| 71 | + }, |
| 72 | + ); |
| 73 | + |
| 74 | + it.each(ALL_ESCROW_ERROR_CODES)( |
| 75 | + 'maps EscrowErrorCode.%s to a 422 (UNPROCESSABLE_ENTITY)', |
| 76 | + (code) => { |
| 77 | + const input = new EscrowContractError(code, 'HostError'); |
| 78 | + |
| 79 | + const result = mapStellarFundingError(input); |
| 80 | + |
| 81 | + expect(result.getStatus()).toBe(422); |
| 82 | + }, |
| 83 | + ); |
| 84 | + |
| 85 | + it('each EscrowErrorCode produces a unique error message', () => { |
| 86 | + const messages = ALL_ESCROW_ERROR_CODES.map((code) => { |
| 87 | + const input = new EscrowContractError(code, `HostError: #${code}`); |
| 88 | + return (mapStellarFundingError(input) as EscrowContractRejectedError) |
| 89 | + .message; |
| 90 | + }); |
| 91 | + |
| 92 | + const unique = new Set(messages); |
| 93 | + expect(unique.size).toBe(ALL_ESCROW_ERROR_CODES.length); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + // ── Non-escrow Stellar errors ───────────────────────────────────────── |
| 98 | + |
| 99 | + describe('SimulationError → EscrowSimulationFailedError', () => { |
| 100 | + it('maps a simulation error to ESCROW_SIMULATION_FAILED', () => { |
| 101 | + const input = new SimulationError( |
| 102 | + 'simulation failed', |
| 103 | + 'HostError: insufficient balance', |
| 104 | + ); |
| 105 | + |
| 106 | + const result = mapStellarFundingError(input); |
| 107 | + |
| 108 | + expect(result).toBeInstanceOf(EscrowSimulationFailedError); |
| 109 | + expect(result.code).toBe('ESCROW_SIMULATION_FAILED'); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe('SubmissionError → EscrowSubmissionFailedError', () => { |
| 114 | + it('maps a submission error to ESCROW_SUBMISSION_FAILED', () => { |
| 115 | + const input = new SubmissionError('tx rejected', { status: 'ERROR' }); |
| 116 | + |
| 117 | + const result = mapStellarFundingError(input); |
| 118 | + |
| 119 | + expect(result).toBeInstanceOf(EscrowSubmissionFailedError); |
| 120 | + expect(result.code).toBe('ESCROW_SUBMISSION_FAILED'); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + describe('ChainTimeoutError → EscrowSubmissionFailedError', () => { |
| 125 | + it('maps a chain timeout to ESCROW_SUBMISSION_FAILED', () => { |
| 126 | + const input = new ChainTimeoutError('network never confirmed'); |
| 127 | + |
| 128 | + const result = mapStellarFundingError(input); |
| 129 | + |
| 130 | + expect(result).toBeInstanceOf(EscrowSubmissionFailedError); |
| 131 | + expect(result.code).toBe('ESCROW_SUBMISSION_FAILED'); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + describe('generic StellarIntegrationError → EscrowSubmissionFailedError', () => { |
| 136 | + it('maps a non-specific Stellar integration error to ESCROW_SUBMISSION_FAILED', () => { |
| 137 | + class OtherStellarError extends StellarIntegrationError { |
| 138 | + constructor() { |
| 139 | + super('some other stellar error'); |
| 140 | + } |
| 141 | + } |
| 142 | + const input = new OtherStellarError(); |
| 143 | + |
| 144 | + const result = mapStellarFundingError(input); |
| 145 | + |
| 146 | + expect(result).toBeInstanceOf(EscrowSubmissionFailedError); |
| 147 | + expect(result.code).toBe('ESCROW_SUBMISSION_FAILED'); |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + // ── Unknown / non-Stellar errors ────────────────────────────────────── |
| 152 | + |
| 153 | + describe('unknown error → EscrowSubmissionFailedError', () => { |
| 154 | + it('maps a plain Error to ESCROW_SUBMISSION_FAILED', () => { |
| 155 | + const input = new Error('something broke'); |
| 156 | + |
| 157 | + const result = mapStellarFundingError(input); |
| 158 | + |
| 159 | + expect(result).toBeInstanceOf(EscrowSubmissionFailedError); |
| 160 | + expect(result.code).toBe('ESCROW_SUBMISSION_FAILED'); |
| 161 | + }); |
| 162 | + |
| 163 | + it('maps a non-Error value to ESCROW_SUBMISSION_FAILED', () => { |
| 164 | + const result = mapStellarFundingError('string error'); |
| 165 | + |
| 166 | + expect(result).toBeInstanceOf(EscrowSubmissionFailedError); |
| 167 | + expect(result.code).toBe('ESCROW_SUBMISSION_FAILED'); |
| 168 | + }); |
| 169 | + }); |
| 170 | + |
| 171 | + // ── Regression guard: never returns a bare generic 500 ──────────────── |
| 172 | + |
| 173 | + describe('none of the 9 escrow variants produce a generic fallback', () => { |
| 174 | + it.each(ALL_ESCROW_ERROR_CODES)( |
| 175 | + 'EscrowErrorCode.%s is NOT mapped to EscrowSubmissionFailedError', |
| 176 | + (code) => { |
| 177 | + const input = new EscrowContractError(code, `HostError: #${code}`); |
| 178 | + const result = mapStellarFundingError(input); |
| 179 | + |
| 180 | + expect(result).not.toBeInstanceOf(EscrowSubmissionFailedError); |
| 181 | + expect(result).not.toBeInstanceOf(EscrowSimulationFailedError); |
| 182 | + }, |
| 183 | + ); |
| 184 | + }); |
| 185 | +}); |
0 commit comments