|
| 1 | +import should from 'should'; |
| 2 | +import { |
| 3 | + TxIntentMismatchError, |
| 4 | + TxIntentMismatchRecipientError, |
| 5 | + TxIntentMismatchContractError, |
| 6 | + TxIntentMismatchApprovalError, |
| 7 | + MismatchedRecipient, |
| 8 | + ContractDataPayload, |
| 9 | + TokenApproval, |
| 10 | +} from '../../../src/bitgo/errors'; |
| 11 | + |
| 12 | +describe('Transaction Intent Mismatch Errors', () => { |
| 13 | + const mockTransactionId = '0x1234567890abcdef'; |
| 14 | + const mockTxParams: any[] = [ |
| 15 | + { address: '0xrecipient1', amount: '1000000000000000000' }, |
| 16 | + { address: '0xrecipient2', amount: '2000000000000000000' }, |
| 17 | + ]; |
| 18 | + const mockTxHex = '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'; |
| 19 | + |
| 20 | + describe('TxIntentMismatchError', () => { |
| 21 | + it('should create base transaction intent mismatch error with all required properties', () => { |
| 22 | + const message = 'Transaction does not match user intent'; |
| 23 | + const error = new TxIntentMismatchError(message, mockTransactionId, mockTxParams, mockTxHex); |
| 24 | + |
| 25 | + should.exist(error); |
| 26 | + should.equal(error.message, message); |
| 27 | + should.equal(error.name, 'TxIntentMismatchError'); |
| 28 | + should.equal(error.id, mockTransactionId); |
| 29 | + should.deepEqual(error.txParams, mockTxParams); |
| 30 | + should.equal(error.txHex, mockTxHex); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should be an instance of Error', () => { |
| 34 | + const error = new TxIntentMismatchError('Test message', mockTransactionId, mockTxParams, mockTxHex); |
| 35 | + |
| 36 | + should(error).be.instanceOf(Error); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('TxIntentMismatchRecipientError', () => { |
| 41 | + it('should create recipient intent mismatch error with mismatched recipients', () => { |
| 42 | + const message = 'Transaction recipients do not match user intent'; |
| 43 | + const mismatchedRecipients: MismatchedRecipient[] = [ |
| 44 | + { address: '0xexpected1', amount: '1000' }, |
| 45 | + { address: '0xexpected2', amount: '2000' }, |
| 46 | + ]; |
| 47 | + |
| 48 | + const error = new TxIntentMismatchRecipientError( |
| 49 | + message, |
| 50 | + mockTransactionId, |
| 51 | + mockTxParams, |
| 52 | + mockTxHex, |
| 53 | + mismatchedRecipients |
| 54 | + ); |
| 55 | + |
| 56 | + should.exist(error); |
| 57 | + should.equal(error.message, message); |
| 58 | + should.equal(error.name, 'TxIntentMismatchRecipientError'); |
| 59 | + should.equal(error.id, mockTransactionId); |
| 60 | + should.deepEqual(error.txParams, mockTxParams); |
| 61 | + should.equal(error.txHex, mockTxHex); |
| 62 | + should.deepEqual(error.mismatchedRecipients, mismatchedRecipients); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should be an instance of TxIntentMismatchError', () => { |
| 66 | + const error = new TxIntentMismatchRecipientError('Test message', mockTransactionId, mockTxParams, mockTxHex, []); |
| 67 | + |
| 68 | + should(error).be.instanceOf(TxIntentMismatchError); |
| 69 | + should(error).be.instanceOf(Error); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe('TxIntentMismatchContractError', () => { |
| 74 | + it('should create contract intent mismatch error with mismatched data payload', () => { |
| 75 | + const message = 'Contract interaction does not match user intent'; |
| 76 | + const mismatchedDataPayload: ContractDataPayload = { |
| 77 | + address: '0xcontract123', |
| 78 | + rawContractPayload: '0xabcdef', |
| 79 | + decodedContractPayload: { method: 'transfer', params: ['0xrecipient', '1000'] }, |
| 80 | + }; |
| 81 | + |
| 82 | + const error = new TxIntentMismatchContractError( |
| 83 | + message, |
| 84 | + mockTransactionId, |
| 85 | + mockTxParams, |
| 86 | + mockTxHex, |
| 87 | + mismatchedDataPayload |
| 88 | + ); |
| 89 | + |
| 90 | + should.exist(error); |
| 91 | + should.equal(error.message, message); |
| 92 | + should.equal(error.name, 'TxIntentMismatchContractError'); |
| 93 | + should.equal(error.id, mockTransactionId); |
| 94 | + should.deepEqual(error.txParams, mockTxParams); |
| 95 | + should.equal(error.txHex, mockTxHex); |
| 96 | + should.deepEqual(error.mismatchedDataPayload, mismatchedDataPayload); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should be an instance of TxIntentMismatchError', () => { |
| 100 | + const error = new TxIntentMismatchContractError('Test message', mockTransactionId, mockTxParams, mockTxHex, { |
| 101 | + address: '0xtest', |
| 102 | + rawContractPayload: '0x', |
| 103 | + decodedContractPayload: {}, |
| 104 | + }); |
| 105 | + |
| 106 | + should(error).be.instanceOf(TxIntentMismatchError); |
| 107 | + should(error).be.instanceOf(Error); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('TxIntentMismatchApprovalError', () => { |
| 112 | + it('should create approval intent mismatch error with token approval details', () => { |
| 113 | + const message = 'Token approval does not match user intent'; |
| 114 | + const tokenApproval: TokenApproval = { |
| 115 | + tokenName: 'TestToken', |
| 116 | + tokenAddress: '0xtoken123', |
| 117 | + authorizingAmount: 'unlimited', |
| 118 | + authorizingAddress: '0xspender456', |
| 119 | + }; |
| 120 | + |
| 121 | + const error = new TxIntentMismatchApprovalError( |
| 122 | + message, |
| 123 | + mockTransactionId, |
| 124 | + mockTxParams, |
| 125 | + mockTxHex, |
| 126 | + tokenApproval |
| 127 | + ); |
| 128 | + |
| 129 | + should.exist(error); |
| 130 | + should.equal(error.message, message); |
| 131 | + should.equal(error.name, 'TxIntentMismatchApprovalError'); |
| 132 | + should.equal(error.id, mockTransactionId); |
| 133 | + should.deepEqual(error.txParams, mockTxParams); |
| 134 | + should.equal(error.txHex, mockTxHex); |
| 135 | + should.deepEqual(error.tokenApproval, tokenApproval); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should be an instance of TxIntentMismatchError', () => { |
| 139 | + const error = new TxIntentMismatchApprovalError('Test message', mockTransactionId, mockTxParams, mockTxHex, { |
| 140 | + tokenAddress: '0xtoken', |
| 141 | + authorizingAmount: 1000, |
| 142 | + authorizingAddress: '0xspender', |
| 143 | + }); |
| 144 | + |
| 145 | + should(error).be.instanceOf(TxIntentMismatchError); |
| 146 | + should(error).be.instanceOf(Error); |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + describe('Error inheritance and properties', () => { |
| 151 | + it('should maintain proper inheritance chain', () => { |
| 152 | + const baseError = new TxIntentMismatchError('Base error', mockTransactionId, mockTxParams, mockTxHex); |
| 153 | + const recipientError = new TxIntentMismatchRecipientError( |
| 154 | + 'Recipient error', |
| 155 | + mockTransactionId, |
| 156 | + mockTxParams, |
| 157 | + mockTxHex, |
| 158 | + [] |
| 159 | + ); |
| 160 | + const contractError = new TxIntentMismatchContractError( |
| 161 | + 'Contract error', |
| 162 | + mockTransactionId, |
| 163 | + mockTxParams, |
| 164 | + mockTxHex, |
| 165 | + { address: '0xtest', rawContractPayload: '0x', decodedContractPayload: {} } |
| 166 | + ); |
| 167 | + const approvalError = new TxIntentMismatchApprovalError( |
| 168 | + 'Approval error', |
| 169 | + mockTransactionId, |
| 170 | + mockTxParams, |
| 171 | + mockTxHex, |
| 172 | + { tokenAddress: '0xtoken', authorizingAmount: 1000, authorizingAddress: '0xspender' } |
| 173 | + ); |
| 174 | + |
| 175 | + // All should be instances of Error |
| 176 | + should(baseError).be.instanceOf(Error); |
| 177 | + should(recipientError).be.instanceOf(Error); |
| 178 | + should(contractError).be.instanceOf(Error); |
| 179 | + should(approvalError).be.instanceOf(Error); |
| 180 | + |
| 181 | + // All should be instances of TxIntentMismatchError |
| 182 | + should(recipientError).be.instanceOf(TxIntentMismatchError); |
| 183 | + should(contractError).be.instanceOf(TxIntentMismatchError); |
| 184 | + should(approvalError).be.instanceOf(TxIntentMismatchError); |
| 185 | + }); |
| 186 | + |
| 187 | + it('should preserve stack trace', () => { |
| 188 | + const error = new TxIntentMismatchError('Test error', mockTransactionId, mockTxParams, mockTxHex); |
| 189 | + |
| 190 | + should.exist(error.stack); |
| 191 | + should(error.stack).be.a.String(); |
| 192 | + should(error.stack).containEql('TxIntentMismatchError'); |
| 193 | + }); |
| 194 | + }); |
| 195 | +}); |
0 commit comments