|
| 1 | +import 'package:bb_mobile/core/exchange/domain/entity/order.dart'; |
| 2 | +import 'package:bb_mobile/core/exchange/domain/repositories/exchange_order_repository.dart'; |
| 3 | +import 'package:bb_mobile/core/settings/domain/repositories/settings_repository.dart'; |
| 4 | +import 'package:bb_mobile/core/settings/domain/settings_entity.dart'; |
| 5 | +import 'package:bb_mobile/core/swaps/domain/repositories/swap_history_repository.dart'; |
| 6 | +import 'package:bb_mobile/core/wallet/domain/repositories/wallet_transaction_repository.dart'; |
| 7 | +import 'package:bb_mobile/features/transactions/application/usecases/get_transaction_order_swaps_usecase.dart'; |
| 8 | +import 'package:bb_mobile/features/transactions/application/usecases/get_transactions_by_tx_id_usecase.dart'; |
| 9 | +import 'package:bull_payjoin/bull_payjoin.dart'; |
| 10 | +import 'package:flutter_test/flutter_test.dart'; |
| 11 | +import 'package:mocktail/mocktail.dart'; |
| 12 | +import 'package:primitives/primitives.dart'; |
| 13 | + |
| 14 | +class _MockSettingsRepository extends Mock implements SettingsRepository {} |
| 15 | + |
| 16 | +class _MockWalletTransactionRepository extends Mock |
| 17 | + implements WalletTransactionRepository {} |
| 18 | + |
| 19 | +class _MockSwapHistoryRepository extends Mock |
| 20 | + implements SwapHistoryRepository {} |
| 21 | + |
| 22 | +class _MockPayjoinSessions extends Mock implements PayjoinSessions {} |
| 23 | + |
| 24 | +class _MockExchangeOrderRepository extends Mock |
| 25 | + implements ExchangeOrderRepository {} |
| 26 | + |
| 27 | +class _MockGetTransactionOrderSwapsUsecase extends Mock |
| 28 | + implements GetTransactionOrderSwapsUsecase {} |
| 29 | + |
| 30 | +const _txId = 'tx-1'; |
| 31 | + |
| 32 | +void main() { |
| 33 | + late _MockSettingsRepository settingsRepository; |
| 34 | + late _MockWalletTransactionRepository walletTransactionRepository; |
| 35 | + late _MockSwapHistoryRepository swapHistoryRepository; |
| 36 | + late _MockPayjoinSessions payjoinSessions; |
| 37 | + late _MockExchangeOrderRepository mainnetOrderRepository; |
| 38 | + late _MockExchangeOrderRepository testnetOrderRepository; |
| 39 | + late _MockGetTransactionOrderSwapsUsecase getTransactionOrderSwapsUsecase; |
| 40 | + late GetTransactionsByTxIdUsecase usecase; |
| 41 | + |
| 42 | + setUp(() { |
| 43 | + settingsRepository = _MockSettingsRepository(); |
| 44 | + walletTransactionRepository = _MockWalletTransactionRepository(); |
| 45 | + swapHistoryRepository = _MockSwapHistoryRepository(); |
| 46 | + payjoinSessions = _MockPayjoinSessions(); |
| 47 | + mainnetOrderRepository = _MockExchangeOrderRepository(); |
| 48 | + testnetOrderRepository = _MockExchangeOrderRepository(); |
| 49 | + getTransactionOrderSwapsUsecase = _MockGetTransactionOrderSwapsUsecase(); |
| 50 | + usecase = GetTransactionsByTxIdUsecase( |
| 51 | + settingsRepository: settingsRepository, |
| 52 | + walletTransactionRepository: walletTransactionRepository, |
| 53 | + boltzSwapRepository: swapHistoryRepository, |
| 54 | + payjoinSessions: payjoinSessions, |
| 55 | + mainnetExchangeOrderRepository: mainnetOrderRepository, |
| 56 | + testnetExchangeOrderRepository: testnetOrderRepository, |
| 57 | + getTransactionOrderSwapsUsecase: getTransactionOrderSwapsUsecase, |
| 58 | + ); |
| 59 | + |
| 60 | + when(() => settingsRepository.fetch()).thenAnswer((_) async => _settings()); |
| 61 | + when( |
| 62 | + () => walletTransactionRepository.getWalletTransactions( |
| 63 | + txId: any(named: 'txId'), |
| 64 | + ), |
| 65 | + ).thenAnswer((_) async => []); |
| 66 | + when( |
| 67 | + () => swapHistoryRepository.getSwapByTxId(any()), |
| 68 | + ).thenAnswer((_) async => null); |
| 69 | + when( |
| 70 | + () => getTransactionOrderSwapsUsecase.execute(), |
| 71 | + ).thenAnswer((_) async => []); |
| 72 | + }); |
| 73 | + |
| 74 | + test( |
| 75 | + 'keeps the order when only a payjoin session is found for the txid', |
| 76 | + () async { |
| 77 | + final payjoin = _payjoinSession(); |
| 78 | + final order = _buyOrder(); |
| 79 | + when( |
| 80 | + () => payjoinSessions.byTransactionId(_txId), |
| 81 | + ).thenAnswer((_) async => Ok([payjoin])); |
| 82 | + when( |
| 83 | + () => mainnetOrderRepository.getOrderByTxId(_txId), |
| 84 | + ).thenAnswer((_) async => order); |
| 85 | + |
| 86 | + final transactions = await usecase.execute(_txId); |
| 87 | + |
| 88 | + expect(transactions, hasLength(1)); |
| 89 | + expect(transactions.single.payjoin, payjoin); |
| 90 | + // Without this the order-anchored details screen renders as a payjoin |
| 91 | + // session until the wallet indexes the broadcast. |
| 92 | + expect(transactions.single.order, order); |
| 93 | + }, |
| 94 | + ); |
| 95 | + |
| 96 | + test('still returns the payjoin when no order matches the txid', () async { |
| 97 | + final payjoin = _payjoinSession(); |
| 98 | + when( |
| 99 | + () => payjoinSessions.byTransactionId(_txId), |
| 100 | + ).thenAnswer((_) async => Ok([payjoin])); |
| 101 | + when( |
| 102 | + () => mainnetOrderRepository.getOrderByTxId(_txId), |
| 103 | + ).thenAnswer((_) async => null); |
| 104 | + |
| 105 | + final transactions = await usecase.execute(_txId); |
| 106 | + |
| 107 | + expect(transactions, hasLength(1)); |
| 108 | + expect(transactions.single.payjoin, payjoin); |
| 109 | + expect(transactions.single.order, isNull); |
| 110 | + }); |
| 111 | +} |
| 112 | + |
| 113 | +SettingsEntity _settings() => const SettingsEntity( |
| 114 | + environment: Environment.mainnet, |
| 115 | + bitcoinUnit: BitcoinUnit.btc, |
| 116 | + currencyCode: 'CAD', |
| 117 | +); |
| 118 | + |
| 119 | +PayjoinSession _payjoinSession() => PayjoinReceiverSession( |
| 120 | + status: PayjoinStatus.completed, |
| 121 | + id: 'payjoin-1', |
| 122 | + network: BitcoinNetwork.mainnet, |
| 123 | + walletId: 'wallet-1', |
| 124 | + createdAt: DateTime.utc(2026, 8, 19), |
| 125 | + expiresAt: DateTime.utc(2026, 8, 20), |
| 126 | + payjoinUri: 'bitcoin:address?pj=https://payjoin.example', |
| 127 | + transactionId: _txId, |
| 128 | +); |
| 129 | + |
| 130 | +Order _buyOrder() => Order.buy( |
| 131 | + orderId: 'order-1', |
| 132 | + orderType: OrderType.buy, |
| 133 | + message: OrderMessage(code: '', message: ''), |
| 134 | + orderNumber: 1, |
| 135 | + payinAmount: 100, |
| 136 | + payinCurrency: 'CAD', |
| 137 | + payoutAmount: 0.001, |
| 138 | + payoutCurrency: 'BTC', |
| 139 | + payinMethod: OrderPaymentMethod.eTransfer, |
| 140 | + payoutMethod: OrderPaymentMethod.bitcoin, |
| 141 | + orderStatus: OrderStatus.inProgress, |
| 142 | + payinStatus: OrderPayinStatus.completed, |
| 143 | + payoutStatus: OrderPayoutStatus.completed, |
| 144 | + createdAt: DateTime.utc(2026, 8, 19), |
| 145 | + payjoinDetails: OrderPayjoinDetails(txid: _txId), |
| 146 | + isTestnet: false, |
| 147 | +); |
0 commit comments