import 'package:pshared/models/payment/payment.dart'; import 'package:pshared/models/payment/state.dart'; import 'package:test/test.dart'; void main() { group('PaymentOrchestrationState parser', () { test('maps v2 orchestration states', () { expect( paymentOrchestrationStateFromValue('orchestration_state_created'), PaymentOrchestrationState.created, ); expect( paymentOrchestrationStateFromValue('ORCHESTRATION_STATE_EXECUTING'), PaymentOrchestrationState.executing, ); expect( paymentOrchestrationStateFromValue( 'orchestration_state_needs_attention', ), PaymentOrchestrationState.needsAttention, ); expect( paymentOrchestrationStateFromValue('orchestration_state_settled'), PaymentOrchestrationState.settled, ); expect( paymentOrchestrationStateFromValue('orchestration_state_failed'), PaymentOrchestrationState.failed, ); }); test('maps legacy payment states for compatibility', () { expect( paymentOrchestrationStateFromValue('payment_state_accepted'), PaymentOrchestrationState.created, ); expect( paymentOrchestrationStateFromValue('payment_state_submitted'), PaymentOrchestrationState.executing, ); expect( paymentOrchestrationStateFromValue('payment_state_settled'), PaymentOrchestrationState.settled, ); expect( paymentOrchestrationStateFromValue('payment_state_cancelled'), PaymentOrchestrationState.failed, ); }); test('unknown state maps to unspecified', () { expect( paymentOrchestrationStateFromValue('something_else'), PaymentOrchestrationState.unspecified, ); expect( paymentOrchestrationStateFromValue(null), PaymentOrchestrationState.unspecified, ); }); }); group('Payment model state helpers', () { test('isPending and isTerminal are derived from typed state', () { const created = Payment( paymentRef: 'p-1', idempotencyKey: 'idem-1', state: 'orchestration_state_created', orchestrationState: PaymentOrchestrationState.created, failureCode: null, failureReason: null, operations: [], lastQuote: null, metadata: null, createdAt: null, ); const settled = Payment( paymentRef: 'p-2', idempotencyKey: 'idem-2', state: 'orchestration_state_settled', orchestrationState: PaymentOrchestrationState.settled, failureCode: null, failureReason: null, operations: [], lastQuote: null, metadata: null, createdAt: null, ); expect(created.isPending, isTrue); expect(created.isTerminal, isFalse); expect(settled.isPending, isFalse); expect(settled.isTerminal, isTrue); }); test('isFailure handles both explicit code and failed state', () { const withFailureCode = Payment( paymentRef: 'p-3', idempotencyKey: 'idem-3', state: 'orchestration_state_executing', orchestrationState: PaymentOrchestrationState.executing, failureCode: 'failure_ledger', failureReason: 'ledger failed', operations: [], lastQuote: null, metadata: null, createdAt: null, ); const failedState = Payment( paymentRef: 'p-4', idempotencyKey: 'idem-4', state: 'orchestration_state_failed', orchestrationState: PaymentOrchestrationState.failed, failureCode: null, failureReason: null, operations: [], lastQuote: null, metadata: null, createdAt: null, ); expect(withFailureCode.isFailure, isTrue); expect(failedState.isFailure, isTrue); }); }); }