|
| 1 | +import 'package:bb_mobile/core/swaps/domain/entity/auto_swap.dart'; |
| 2 | +import 'package:flutter_test/flutter_test.dart'; |
| 3 | + |
| 4 | +/// Settings that break no rule, for tests to bend one field at a time. |
| 5 | +const _valid = AutoSwap( |
| 6 | + enabled: true, |
| 7 | + balanceThresholdSats: 100000, |
| 8 | + triggerBalanceSats: 200000, |
| 9 | + feeThresholdPercent: 3, |
| 10 | + recipientWalletId: 'wallet-id', |
| 11 | +); |
| 12 | + |
| 13 | +void main() { |
| 14 | + group('AutoSwap.violation', () { |
| 15 | + test('accepts settings that break no rule', () { |
| 16 | + expect(_valid.violation, isNull); |
| 17 | + }); |
| 18 | + |
| 19 | + test('requires a recipient wallet only while enabled', () { |
| 20 | + expect( |
| 21 | + _valid.copyWith(recipientWalletId: null).violation, |
| 22 | + AutoSwapSettingsViolation.recipientWalletMissing, |
| 23 | + ); |
| 24 | + |
| 25 | + // Switching auto swap off must not demand a recipient, or turning the |
| 26 | + // feature off would be impossible. |
| 27 | + expect( |
| 28 | + _valid.copyWith(enabled: false, recipientWalletId: null).violation, |
| 29 | + isNull, |
| 30 | + ); |
| 31 | + }); |
| 32 | + |
| 33 | + test('rejects a target balance below the minimum', () { |
| 34 | + expect( |
| 35 | + _valid.copyWith(balanceThresholdSats: 49999).violation, |
| 36 | + AutoSwapSettingsViolation.balanceThresholdTooLow, |
| 37 | + ); |
| 38 | + expect( |
| 39 | + _valid |
| 40 | + .copyWith(balanceThresholdSats: 50000, triggerBalanceSats: 100000) |
| 41 | + .violation, |
| 42 | + isNull, |
| 43 | + ); |
| 44 | + }); |
| 45 | + |
| 46 | + test('rejects a trigger balance below twice the target', () { |
| 47 | + expect( |
| 48 | + _valid.copyWith(triggerBalanceSats: 199999).violation, |
| 49 | + AutoSwapSettingsViolation.triggerBalanceTooLow, |
| 50 | + ); |
| 51 | + expect(_valid.copyWith(triggerBalanceSats: 200000).violation, isNull); |
| 52 | + }); |
| 53 | + |
| 54 | + test('rejects a fee ceiling above the maximum', () { |
| 55 | + expect( |
| 56 | + _valid.copyWith(feeThresholdPercent: 10.1).violation, |
| 57 | + AutoSwapSettingsViolation.feeThresholdTooHigh, |
| 58 | + ); |
| 59 | + expect(_valid.copyWith(feeThresholdPercent: 10).violation, isNull); |
| 60 | + }); |
| 61 | + |
| 62 | + test('reports the recipient rule before the amount rules', () { |
| 63 | + final broken = _valid.copyWith( |
| 64 | + recipientWalletId: null, |
| 65 | + balanceThresholdSats: 1, |
| 66 | + triggerBalanceSats: 1, |
| 67 | + feeThresholdPercent: 99, |
| 68 | + ); |
| 69 | + |
| 70 | + expect( |
| 71 | + broken.violation, |
| 72 | + AutoSwapSettingsViolation.recipientWalletMissing, |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + test('reports the balance rule before the trigger rule', () { |
| 77 | + // Both broken; the target is the field the user must fix first. |
| 78 | + expect( |
| 79 | + _valid |
| 80 | + .copyWith(balanceThresholdSats: 1000, triggerBalanceSats: 1) |
| 81 | + .violation, |
| 82 | + AutoSwapSettingsViolation.balanceThresholdTooLow, |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + test('a persisted row that breaks a rule still deserializes', () { |
| 87 | + // The rules are not constructor-enforced on purpose: settings written by |
| 88 | + // an older version must still load, or the feature becomes unopenable. |
| 89 | + final legacy = AutoSwap.fromJson(const { |
| 90 | + 'enabled': true, |
| 91 | + 'balanceThresholdSats': 10000, |
| 92 | + 'triggerBalanceSats': 15000, |
| 93 | + 'feeThresholdPercent': 50.0, |
| 94 | + 'blockTillNextExecution': false, |
| 95 | + 'alwaysBlock': false, |
| 96 | + 'recipientWalletId': null, |
| 97 | + 'showWarning': true, |
| 98 | + }); |
| 99 | + |
| 100 | + expect(legacy.balanceThresholdSats, 10000); |
| 101 | + expect(legacy.violation, isNotNull); |
| 102 | + }); |
| 103 | + }); |
| 104 | +} |
0 commit comments