|
| 1 | +package stellar |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + hProtocol "github.com/stellar/go/protocols/horizon" |
| 7 | +) |
| 8 | + |
| 9 | +func TestIsTerminalSubmitError(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + codes *hProtocol.TransactionResultCodes |
| 13 | + want bool |
| 14 | + }{ |
| 15 | + { |
| 16 | + name: "nil codes are not terminal", |
| 17 | + codes: nil, |
| 18 | + want: false, |
| 19 | + }, |
| 20 | + { |
| 21 | + name: "op_no_trust is terminal (target removed trustline)", |
| 22 | + codes: &hProtocol.TransactionResultCodes{ |
| 23 | + TransactionCode: "tx_failed", |
| 24 | + OperationCodes: []string{"op_no_trust"}, |
| 25 | + }, |
| 26 | + want: true, |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "op_no_destination is terminal (target account gone)", |
| 30 | + codes: &hProtocol.TransactionResultCodes{ |
| 31 | + TransactionCode: "tx_failed", |
| 32 | + OperationCodes: []string{"op_no_destination"}, |
| 33 | + }, |
| 34 | + want: true, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "op_not_authorized is terminal (target not authorized to hold the asset)", |
| 38 | + codes: &hProtocol.TransactionResultCodes{ |
| 39 | + TransactionCode: "tx_failed", |
| 40 | + OperationCodes: []string{"op_not_authorized"}, |
| 41 | + }, |
| 42 | + want: true, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "op_line_full is NOT terminal (target trustline momentarily full; may succeed on retry - postpone, do not forfeit)", |
| 46 | + codes: &hProtocol.TransactionResultCodes{ |
| 47 | + TransactionCode: "tx_failed", |
| 48 | + OperationCodes: []string{"op_line_full"}, |
| 49 | + }, |
| 50 | + want: false, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "op_underfunded is NOT terminal (bridge wallet out of funds - operational, must alert not forfeit)", |
| 54 | + codes: &hProtocol.TransactionResultCodes{ |
| 55 | + TransactionCode: "tx_failed", |
| 56 | + OperationCodes: []string{"op_underfunded"}, |
| 57 | + }, |
| 58 | + want: false, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "op_src_no_trust is NOT terminal (bridge-side trustline missing, not the target's fault)", |
| 62 | + codes: &hProtocol.TransactionResultCodes{ |
| 63 | + TransactionCode: "tx_failed", |
| 64 | + OperationCodes: []string{"op_src_no_trust"}, |
| 65 | + }, |
| 66 | + want: false, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "tx_bad_seq is NOT terminal (transient, resolved by sequence reset)", |
| 70 | + codes: &hProtocol.TransactionResultCodes{ |
| 71 | + TransactionCode: "tx_bad_seq", |
| 72 | + OperationCodes: nil, |
| 73 | + }, |
| 74 | + want: false, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "terminal code mixed with others is still terminal", |
| 78 | + codes: &hProtocol.TransactionResultCodes{ |
| 79 | + TransactionCode: "tx_failed", |
| 80 | + OperationCodes: []string{"op_success", "op_no_trust"}, |
| 81 | + }, |
| 82 | + want: true, |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + for _, tt := range tests { |
| 87 | + t.Run(tt.name, func(t *testing.T) { |
| 88 | + if got := isTerminalSubmitError(tt.codes); got != tt.want { |
| 89 | + t.Errorf("isTerminalSubmitError() = %v, want %v", got, tt.want) |
| 90 | + } |
| 91 | + }) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestIsInsufficientFundsError(t *testing.T) { |
| 96 | + tests := []struct { |
| 97 | + name string |
| 98 | + codes *hProtocol.TransactionResultCodes |
| 99 | + want bool |
| 100 | + }{ |
| 101 | + { |
| 102 | + name: "nil codes are not an underfunded condition", |
| 103 | + codes: nil, |
| 104 | + want: false, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "op_underfunded means the bridge wallet is out of funds", |
| 108 | + codes: &hProtocol.TransactionResultCodes{ |
| 109 | + TransactionCode: "tx_failed", |
| 110 | + OperationCodes: []string{"op_underfunded"}, |
| 111 | + }, |
| 112 | + want: true, |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "op_no_trust is a target-side failure, not underfunded", |
| 116 | + codes: &hProtocol.TransactionResultCodes{ |
| 117 | + TransactionCode: "tx_failed", |
| 118 | + OperationCodes: []string{"op_no_trust"}, |
| 119 | + }, |
| 120 | + want: false, |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "op_underfunded mixed with others is still underfunded", |
| 124 | + codes: &hProtocol.TransactionResultCodes{ |
| 125 | + TransactionCode: "tx_failed", |
| 126 | + OperationCodes: []string{"op_success", "op_underfunded"}, |
| 127 | + }, |
| 128 | + want: true, |
| 129 | + }, |
| 130 | + } |
| 131 | + |
| 132 | + for _, tt := range tests { |
| 133 | + t.Run(tt.name, func(t *testing.T) { |
| 134 | + if got := isInsufficientFundsError(tt.codes); got != tt.want { |
| 135 | + t.Errorf("isInsufficientFundsError() = %v, want %v", got, tt.want) |
| 136 | + } |
| 137 | + }) |
| 138 | + } |
| 139 | +} |
0 commit comments