From 3e394f98e8a00feb870ec1ab31d28b3e08c963db Mon Sep 17 00:00:00 2001 From: Muzaffar Ahmad Bhat Date: Mon, 7 Oct 2024 13:42:44 +0530 Subject: [PATCH] feat: added xrp sign-txn tests --- .../tests/03.signTxn/__fixtures__/error.ts | 105 ++++++++ .../tests/03.signTxn/__fixtures__/index.ts | 14 + .../03.signTxn/__fixtures__/invalidArgs.ts | 104 ++++++++ .../03.signTxn/__fixtures__/invalidData.ts | 129 ++++++++++ .../tests/03.signTxn/__fixtures__/types.ts | 28 ++ .../tests/03.signTxn/__fixtures__/valid.ts | 239 ++++++++++++++++++ .../tests/03.signTxn/__helpers__/index.ts | 23 ++ packages/app-xrp/tests/03.signTxn/index.ts | 102 ++++++++ 8 files changed, 744 insertions(+) create mode 100644 packages/app-xrp/tests/03.signTxn/__fixtures__/error.ts create mode 100644 packages/app-xrp/tests/03.signTxn/__fixtures__/index.ts create mode 100644 packages/app-xrp/tests/03.signTxn/__fixtures__/invalidArgs.ts create mode 100644 packages/app-xrp/tests/03.signTxn/__fixtures__/invalidData.ts create mode 100644 packages/app-xrp/tests/03.signTxn/__fixtures__/types.ts create mode 100644 packages/app-xrp/tests/03.signTxn/__fixtures__/valid.ts create mode 100644 packages/app-xrp/tests/03.signTxn/__helpers__/index.ts create mode 100644 packages/app-xrp/tests/03.signTxn/index.ts diff --git a/packages/app-xrp/tests/03.signTxn/__fixtures__/error.ts b/packages/app-xrp/tests/03.signTxn/__fixtures__/error.ts new file mode 100644 index 000000000..fb8157f19 --- /dev/null +++ b/packages/app-xrp/tests/03.signTxn/__fixtures__/error.ts @@ -0,0 +1,105 @@ +import { + DeviceAppError, + DeviceAppErrorType, + deviceAppErrorTypeDetails, +} from '@cypherock/sdk-interfaces'; +import { ISignTxnTestCase } from './types'; +import { queryToUint8Array, resultToUint8Array } from '../__helpers__'; +import { ISignTxnParams } from '../../../src'; + +const commonParams: { + params: ISignTxnParams; + queries: { + name: string; + data: Uint8Array; + }[]; +} = { + params: { + walletId: new Uint8Array([ + 199, 89, 252, 26, 32, 135, 183, 211, 90, 220, 38, 17, 160, 103, 233, 62, + 110, 172, 92, 20, 35, 250, 190, 146, 62, 8, 53, 86, 128, 26, 3, 187, 121, + 64, + ]), + derivationPath: [0x80000000 + 44, 0x80000000 + 144, 0x80000000, 0, 0], + txn: { + rawTxn: { + TransactionType: 'Payment', + Account: 'rQGDkQchoJxMSLZR7q9GwvY3iKtDqTUYNQ', + Amount: '5000000', + Destination: 'rEgfV7YeyG4YayQQufxaqDx4aUA93SidLb', + Flags: 0, + NetworkID: undefined, + Sequence: 676674, + Fee: '12', + LastLedgerSequence: 1238396, + SigningPubKey: + '027497533006d024ffb612a2110eb327ccfeed2b752d787c96ab2d3cca425a40e8', + }, + txnHex: + '53545800120000220000000024000A5342201B0012E57C6140000000004C4B4068400000000000000C7321027497533006D024FFB612A2110EB327CCFEED2B752D787C96AB2D3CCA425A40E88114FF2BC637244009494C6203505254126638AAD7CD8314A0F766DFCC0B5DDC91E7679C7539590983A41D9F', + }, + }, + queries: [ + { + name: 'Initate query', + data: queryToUint8Array({ + signTxn: { + initiate: { + walletId: new Uint8Array([ + 199, 89, 252, 26, 32, 135, 183, 211, 90, 220, 38, 17, 160, 103, + 233, 62, 110, 172, 92, 20, 35, 250, 190, 146, 62, 8, 53, 86, 128, + 26, 3, 187, 121, 64, + ]), + derivationPath: [ + 0x80000000 + 44, + 0x80000000 + 144, + 0x80000000, + 0, + 0, + ], + transactionSize: 120, + }, + }, + }), + }, + ], +}; + +const withUnknownError: ISignTxnTestCase = { + name: 'With unknown error', + ...commonParams, + results: [ + { + name: 'error', + data: resultToUint8Array({ + signTxn: { + commonError: { + unknownError: 1, + }, + }, + }), + }, + ], + errorInstance: DeviceAppError, + errorMessage: + deviceAppErrorTypeDetails[DeviceAppErrorType.UNKNOWN_ERROR].message, +}; + +const withInvalidResult: ISignTxnTestCase = { + name: 'With invalid result', + ...commonParams, + results: [ + { + name: 'error', + data: new Uint8Array([18, 4, 26, 2, 24, 1]), + }, + ], + errorInstance: DeviceAppError, + errorMessage: + deviceAppErrorTypeDetails[DeviceAppErrorType.INVALID_MSG_FROM_DEVICE] + .message, +}; + +const error: ISignTxnTestCase[] = [withUnknownError, withInvalidResult]; + +export default error; diff --git a/packages/app-xrp/tests/03.signTxn/__fixtures__/index.ts b/packages/app-xrp/tests/03.signTxn/__fixtures__/index.ts new file mode 100644 index 000000000..4347cf94d --- /dev/null +++ b/packages/app-xrp/tests/03.signTxn/__fixtures__/index.ts @@ -0,0 +1,14 @@ +import { IFixtures } from './types'; +import error from './error'; +import invalidData from './invalidData'; +import invalidArgs from './invalidArgs'; +import valid from './valid'; + +const fixtures: IFixtures = { + valid, + invalidArgs, + error, + invalidData, +}; + +export default fixtures; diff --git a/packages/app-xrp/tests/03.signTxn/__fixtures__/invalidArgs.ts b/packages/app-xrp/tests/03.signTxn/__fixtures__/invalidArgs.ts new file mode 100644 index 000000000..03b2da5ae --- /dev/null +++ b/packages/app-xrp/tests/03.signTxn/__fixtures__/invalidArgs.ts @@ -0,0 +1,104 @@ +import { ISignTxnTestCase } from './types'; + +const commonParams = { + queries: [{ name: 'empty', data: new Uint8Array([]) }], + results: [{ name: 'empty', data: new Uint8Array([]) }], + errorInstance: Error, + errorMessage: /AssertionError/, +}; + +const validParams = { + walletId: new Uint8Array([ + 199, 89, 252, 26, 32, 135, 183, 211, 90, 220, 38, 17, 160, 103, 233, 62, + 110, 172, 92, 20, 35, 250, 190, 146, 62, 8, 53, 86, 128, 26, 3, 187, 121, + 64, + ]), + derivationPath: [0x80000000 + 44, 0x80000000 + 144, 0x80000000, 0, 0], + txn: { + rawTxn: { + TransactionType: 'Payment', + Account: 'rQGDkQchoJxMSLZR7q9GwvY3iKtDqTUYNQ', + Amount: '5000000', + Destination: 'rEgfV7YeyG4YayQQufxaqDx4aUA93SidLb', + Flags: 0, + NetworkID: undefined, + Sequence: 676674, + Fee: '12', + LastLedgerSequence: 1238396, + SigningPubKey: + '027497533006d024ffb612a2110eb327ccfeed2b752d787c96ab2d3cca425a40e8', + }, + txnHex: + '53545800120000220000000024000A5342201B0012E57C6140000000004C4B4068400000000000000C7321027497533006D024FFB612A2110EB327CCFEED2B752D787C96AB2D3CCA425A40E88114FF2BC637244009494C6203505254126638AAD7CD8314A0F766DFCC0B5DDC91E7679C7539590983A41D9F', + }, +}; + +const invalidArgs: ISignTxnTestCase[] = [ + { + name: 'Null', + ...commonParams, + params: null as any, + }, + { + name: 'Undefined', + ...commonParams, + params: null as any, + }, + { + name: 'Empty Object', + ...commonParams, + params: {} as any, + }, + { + name: 'No derivation path', + ...commonParams, + params: { ...validParams, derivationPath: undefined } as any, + }, + { + name: 'No wallet id', + ...commonParams, + params: { ...validParams, walletId: undefined } as any, + }, + { + name: 'No txn', + ...commonParams, + params: { ...validParams, txn: undefined } as any, + }, + { + name: 'No raw txn', + ...commonParams, + params: { + ...validParams, + txn: { ...validParams.txn, rawTxn: undefined }, + } as any, + }, + { + name: 'No txn hex', + ...commonParams, + params: { + ...validParams, + txn: { ...validParams.txn, txnHex: undefined }, + } as any, + }, + { + name: 'Empty derivation path', + ...commonParams, + params: { + ...validParams, + derivationPath: [], + } as any, + }, + { + name: 'invalid derivation path in array (depth:2)', + ...commonParams, + params: { + ...validParams, + derivationPath: [ + { index: 44, isHardened: true }, + { index: 0, isHardened: true }, + ], + }, + }, +]; + +export default invalidArgs; diff --git a/packages/app-xrp/tests/03.signTxn/__fixtures__/invalidData.ts b/packages/app-xrp/tests/03.signTxn/__fixtures__/invalidData.ts new file mode 100644 index 000000000..f28baa81d --- /dev/null +++ b/packages/app-xrp/tests/03.signTxn/__fixtures__/invalidData.ts @@ -0,0 +1,129 @@ +import { + DeviceAppError, + DeviceAppErrorType, + deviceAppErrorTypeDetails, +} from '@cypherock/sdk-interfaces'; +import { ISignTxnTestCase } from './types'; +import { queryToUint8Array } from '../__helpers__'; +import { ISignTxnParams } from '../../../src'; + +const commonParams: { + params: ISignTxnParams; + queries: { + name: string; + data: Uint8Array; + }[]; + errorInstance?: any; + [key: string]: any; +} = { + params: { + walletId: new Uint8Array([ + 199, 89, 252, 26, 32, 135, 183, 211, 90, 220, 38, 17, 160, 103, 233, 62, + 110, 172, 92, 20, 35, 250, 190, 146, 62, 8, 53, 86, 128, 26, 3, 187, 121, + 64, + ]), + derivationPath: [0x80000000 + 44, 0x80000000 + 144, 0x80000000, 0, 0], + txn: { + rawTxn: { + TransactionType: 'Payment', + Account: 'rQGDkQchoJxMSLZR7q9GwvY3iKtDqTUYNQ', + Amount: '5000000', + Destination: 'rEgfV7YeyG4YayQQufxaqDx4aUA93SidLb', + Flags: 0, + NetworkID: undefined, + Sequence: 676674, + Fee: '12', + LastLedgerSequence: 1238396, + SigningPubKey: + '027497533006d024ffb612a2110eb327ccfeed2b752d787c96ab2d3cca425a40e8', + }, + txnHex: + '53545800120000220000000024000A5342201B0012E57C6140000000004C4B4068400000000000000C7321027497533006D024FFB612A2110EB327CCFEED2B752D787C96AB2D3CCA425A40E88114FF2BC637244009494C6203505254126638AAD7CD8314A0F766DFCC0B5DDC91E7679C7539590983A41D9F', + }, + }, + queries: [ + { + name: 'Initate query', + data: queryToUint8Array({ + signTxn: { + initiate: { + walletId: new Uint8Array([ + 199, 89, 252, 26, 32, 135, 183, 211, 90, 220, 38, 17, 160, 103, + 233, 62, 110, 172, 92, 20, 35, 250, 190, 146, 62, 8, 53, 86, 128, + 26, 3, 187, 121, 64, + ]), + derivationPath: [ + 0x80000000 + 44, + 0x80000000 + 144, + 0x80000000, + 0, + 0, + ], + transactionSize: 120, + }, + }, + }), + }, + ], + errorInstance: DeviceAppError, + errorMessage: + deviceAppErrorTypeDetails[DeviceAppErrorType.INVALID_MSG_FROM_DEVICE] + .message, +}; + +const invalidData: ISignTxnTestCase[] = [ + { + name: 'Invalid data', + ...commonParams, + results: [ + { + name: 'error', + data: new Uint8Array([ + 109, 112, 102, 98, 72, 57, 117, 109, 75, 69, 83, 117, 117, 49, 103, + 78, 100, 105, 87, 83, 116, 106, 71, 54, 67, 110, 104, 77, 86, 49, 113, + 97, 78, 111, 50, 98, 118, 52, 67, 113, 72, 122, 120, 85, 98, 53, 86, + 68, 115, 86, 52, 77, 86, 112, 83, 70, 86, 78, 121, 121, 109, 83, 112, + 98, 74, 76, 55, 57, 75, 89, 86, 57, 75, 56, 88, 82, 100, 105, 98, 70, + 109, 118, 54, 116, 86, 54, 116, 50, 122, 52, 100, 87, 110, 111, 110, + 78, 52, 78, 77, 89, 109, + ]), + }, + ], + }, + { + name: 'Invalid data', + ...commonParams, + results: [ + { + name: 'error', + data: new Uint8Array([ + 10, 34, 10, 3, 90, 221, 135, 18, 2, 8, 1, 24, 1, 34, 11, 8, 2, 18, 7, + 8, + ]), + }, + ], + }, + { + name: 'Invalid data', + ...commonParams, + + results: [ + { + name: 'error', + data: new Uint8Array([10]), + }, + ], + }, + { + name: 'Invalid data', + ...commonParams, + results: [ + { + name: 'error', + data: new Uint8Array([]), + }, + ], + }, +]; + +export default invalidData; diff --git a/packages/app-xrp/tests/03.signTxn/__fixtures__/types.ts b/packages/app-xrp/tests/03.signTxn/__fixtures__/types.ts new file mode 100644 index 000000000..cd2c17870 --- /dev/null +++ b/packages/app-xrp/tests/03.signTxn/__fixtures__/types.ts @@ -0,0 +1,28 @@ +import { ISignTxnParams, ISignTxnResult } from '../../../src'; + +export interface ISignTxnTestCase { + name: string; + params: ISignTxnParams; + queries: { + name: string; + data: Uint8Array; + }[]; + results: { + name: string; + data: Uint8Array; + statuses?: { flowStatus: number; expectEventCalls?: number[] }[]; + }[]; + mocks?: { + eventCalls?: number[][]; + }; + output?: Partial; + errorInstance?: any; + [key: string]: any; +} + +export interface IFixtures { + valid: ISignTxnTestCase[]; + invalidArgs: ISignTxnTestCase[]; + invalidData: ISignTxnTestCase[]; + error: ISignTxnTestCase[]; +} diff --git a/packages/app-xrp/tests/03.signTxn/__fixtures__/valid.ts b/packages/app-xrp/tests/03.signTxn/__fixtures__/valid.ts new file mode 100644 index 000000000..3d729eeee --- /dev/null +++ b/packages/app-xrp/tests/03.signTxn/__fixtures__/valid.ts @@ -0,0 +1,239 @@ +import { hexToUint8Array } from '@cypherock/sdk-utils'; +import { ISignTxnTestCase } from './types'; +import { queryToUint8Array, resultToUint8Array } from '../__helpers__'; + +const xrpTransferWithSerialize: ISignTxnTestCase = { + name: 'Xrp transfer', + params: { + walletId: new Uint8Array([ + 199, 89, 252, 26, 32, 135, 183, 211, 90, 220, 38, 17, 160, 103, 233, 62, + 110, 172, 92, 20, 35, 250, 190, 146, 62, 8, 53, 86, 128, 26, 3, 187, 121, + 64, + ]), + derivationPath: [0x80000000 + 44, 0x80000000 + 144, 0x80000000, 0, 0], + serializeTxn: true, + txn: { + rawTxn: { + TransactionType: 'Payment', + Account: 'rQGDkQchoJxMSLZR7q9GwvY3iKtDqTUYNQ', + Amount: '5000000', + Destination: 'rEgfV7YeyG4YayQQufxaqDx4aUA93SidLb', + Flags: 0, + NetworkID: undefined, + Sequence: 676674, + Fee: '12', + LastLedgerSequence: 1238396, + SigningPubKey: + '027497533006d024ffb612a2110eb327ccfeed2b752d787c96ab2d3cca425a40e8', + }, + txnHex: + '53545800120000220000000024000A5342201B0012E57C6140000000004C4B4068400000000000000C7321027497533006D024FFB612A2110EB327CCFEED2B752D787C96AB2D3CCA425A40E88114FF2BC637244009494C6203505254126638AAD7CD8314A0F766DFCC0B5DDC91E7679C7539590983A41D9F', + }, + }, + queries: [ + { + name: 'Initate query', + data: queryToUint8Array({ + signTxn: { + initiate: { + walletId: new Uint8Array([ + 199, 89, 252, 26, 32, 135, 183, 211, 90, 220, 38, 17, 160, 103, + 233, 62, 110, 172, 92, 20, 35, 250, 190, 146, 62, 8, 53, 86, 128, + 26, 3, 187, 121, 64, + ]), + derivationPath: [ + 0x80000000 + 44, + 0x80000000 + 144, + 0x80000000, + 0, + 0, + ], + transactionSize: 120, + }, + }, + }), + }, + { + name: 'Txn data chunk payload', + data: queryToUint8Array({ + signTxn: { + txnData: { + chunkPayload: { + chunk: hexToUint8Array( + '53545800120000220000000024000A5342201B0012E57C6140000000004C4B4068400000000000000C7321027497533006D024FFB612A2110EB327CCFEED2B752D787C96AB2D3CCA425A40E88114FF2BC637244009494C6203505254126638AAD7CD8314A0F766DFCC0B5DDC91E7679C7539590983A41D9F', + ), + chunkIndex: 0, + totalChunks: 1, + remainingSize: 0, + }, + }, + }, + }), + }, + { + name: 'Signature request', + data: queryToUint8Array({ + signTxn: { + signature: {}, + }, + }), + }, + ], + results: [ + { + name: 'Confirmation', + data: resultToUint8Array({ + signTxn: { + confirmation: {}, + }, + }), + }, + { + name: 'Data accepted', + data: resultToUint8Array({ + signTxn: { + dataAccepted: { chunkAck: { chunkIndex: 0 } }, + }, + }), + }, + { + name: 'Signature', + data: resultToUint8Array({ + signTxn: { + signature: { + signature: hexToUint8Array( + '304402202b1341dda956b8e7843015c3246e7b909cdd00accc28954653760f5483044f9d02204bad75aa489fa162c281082e5cf07afe3f094551b9155753f2ba18937d43e3c4', + ), + }, + }, + }), + }, + ], + mocks: { eventCalls: [[0], [1], [2], [3], [4]] }, + output: { + signature: + '304402202b1341dda956b8e7843015c3246e7b909cdd00accc28954653760f5483044f9d02204bad75aa489fa162c281082e5cf07afe3f094551b9155753f2ba18937d43e3c4', + serializedTxn: + '120000220000000024000A5342201B0012E57C6140000000004C4B4068400000000000000C7321027497533006D024FFB612A2110EB327CCFEED2B752D787C96AB2D3CCA425A40E87446304402202B1341DDA956B8E7843015C3246E7B909CDD00ACCC28954653760F5483044F9D02204BAD75AA489FA162C281082E5CF07AFE3F094551B9155753F2BA18937D43E3C48114FF2BC637244009494C6203505254126638AAD7CD8314A0F766DFCC0B5DDC91E7679C7539590983A41D9F', + }, +}; + +const xrpTransferWithoutSerialize: ISignTxnTestCase = { + name: 'Xrp transfer', + params: { + walletId: new Uint8Array([ + 199, 89, 252, 26, 32, 135, 183, 211, 90, 220, 38, 17, 160, 103, 233, 62, + 110, 172, 92, 20, 35, 250, 190, 146, 62, 8, 53, 86, 128, 26, 3, 187, 121, + 64, + ]), + derivationPath: [0x80000000 + 44, 0x80000000 + 144, 0x80000000, 0, 0], + txn: { + rawTxn: { + TransactionType: 'Payment', + Account: 'rQGDkQchoJxMSLZR7q9GwvY3iKtDqTUYNQ', + Amount: '5000000', + Destination: 'rEgfV7YeyG4YayQQufxaqDx4aUA93SidLb', + Flags: 0, + NetworkID: undefined, + Sequence: 676674, + Fee: '12', + LastLedgerSequence: 1238396, + SigningPubKey: + '027497533006d024ffb612a2110eb327ccfeed2b752d787c96ab2d3cca425a40e8', + }, + txnHex: + '53545800120000220000000024000A5342201B0012E57C6140000000004C4B4068400000000000000C7321027497533006D024FFB612A2110EB327CCFEED2B752D787C96AB2D3CCA425A40E88114FF2BC637244009494C6203505254126638AAD7CD8314A0F766DFCC0B5DDC91E7679C7539590983A41D9F', + }, + }, + queries: [ + { + name: 'Initate query', + data: queryToUint8Array({ + signTxn: { + initiate: { + walletId: new Uint8Array([ + 199, 89, 252, 26, 32, 135, 183, 211, 90, 220, 38, 17, 160, 103, + 233, 62, 110, 172, 92, 20, 35, 250, 190, 146, 62, 8, 53, 86, 128, + 26, 3, 187, 121, 64, + ]), + derivationPath: [ + 0x80000000 + 44, + 0x80000000 + 144, + 0x80000000, + 0, + 0, + ], + transactionSize: 120, + }, + }, + }), + }, + { + name: 'Txn data chunk payload', + data: queryToUint8Array({ + signTxn: { + txnData: { + chunkPayload: { + chunk: hexToUint8Array( + '53545800120000220000000024000A5342201B0012E57C6140000000004C4B4068400000000000000C7321027497533006D024FFB612A2110EB327CCFEED2B752D787C96AB2D3CCA425A40E88114FF2BC637244009494C6203505254126638AAD7CD8314A0F766DFCC0B5DDC91E7679C7539590983A41D9F', + ), + chunkIndex: 0, + totalChunks: 1, + remainingSize: 0, + }, + }, + }, + }), + }, + { + name: 'Signature request', + data: queryToUint8Array({ + signTxn: { + signature: {}, + }, + }), + }, + ], + results: [ + { + name: 'Confirmation', + data: resultToUint8Array({ + signTxn: { + confirmation: {}, + }, + }), + }, + { + name: 'Data accepted', + data: resultToUint8Array({ + signTxn: { + dataAccepted: { chunkAck: { chunkIndex: 0 } }, + }, + }), + }, + { + name: 'Signature', + data: resultToUint8Array({ + signTxn: { + signature: { + signature: hexToUint8Array( + '304402202b1341dda956b8e7843015c3246e7b909cdd00accc28954653760f5483044f9d02204bad75aa489fa162c281082e5cf07afe3f094551b9155753f2ba18937d43e3c4', + ), + }, + }, + }), + }, + ], + mocks: { eventCalls: [[0], [1], [2], [3], [4]] }, + output: { + signature: + '304402202b1341dda956b8e7843015c3246e7b909cdd00accc28954653760f5483044f9d02204bad75aa489fa162c281082e5cf07afe3f094551b9155753f2ba18937d43e3c4', + }, +}; + +const valid: ISignTxnTestCase[] = [ + xrpTransferWithSerialize, + xrpTransferWithoutSerialize, +]; + +export default valid; diff --git a/packages/app-xrp/tests/03.signTxn/__helpers__/index.ts b/packages/app-xrp/tests/03.signTxn/__helpers__/index.ts new file mode 100644 index 000000000..525ad75d9 --- /dev/null +++ b/packages/app-xrp/tests/03.signTxn/__helpers__/index.ts @@ -0,0 +1,23 @@ +import * as superMocks from '../../__helpers__'; +import { ISignTxnTestCase } from '../__fixtures__/types'; +import { Query, Result } from '../../../src/proto/generated/xrp/core'; + +export function setupMocks(testCase: ISignTxnTestCase) { + return superMocks.setupMocks(testCase); +} + +export function clearMocks() { + superMocks.clearMocks(); +} + +export function expectMockCalls(testCase: ISignTxnTestCase) { + superMocks.expectMockCalls(testCase); +} + +export function queryToUint8Array(queryData: Query): Uint8Array { + return Uint8Array.from(Query.encode(Query.create(queryData)).finish()); +} + +export function resultToUint8Array(resultData: Result): Uint8Array { + return Uint8Array.from(Result.encode(Result.create(resultData)).finish()); +} diff --git a/packages/app-xrp/tests/03.signTxn/index.ts b/packages/app-xrp/tests/03.signTxn/index.ts new file mode 100644 index 000000000..32bb9327b --- /dev/null +++ b/packages/app-xrp/tests/03.signTxn/index.ts @@ -0,0 +1,102 @@ +import { MockDeviceConnection } from '@cypherock/sdk-interfaces'; +import { afterEach, beforeEach, describe, expect, test } from '@jest/globals'; + +import * as xrpl from 'xrpl'; +import { clearMocks, expectMockCalls, setupMocks } from './__helpers__'; +import fixtures from './__fixtures__'; + +import { setXrpLib, XrpApp } from '../../src'; + +// eslint-disable-next-line @typescript-eslint/no-var-requires + +describe('xrpApp.signTxn', () => { + let connection: MockDeviceConnection; + let xrpApp: XrpApp; + + beforeEach(async () => { + clearMocks(); + + setXrpLib(xrpl); + connection = await MockDeviceConnection.create(); + xrpApp = await XrpApp.create(connection); + }); + + afterEach(async () => { + await xrpApp.destroy(); + }); + + describe('should be able to get signature', () => { + fixtures.valid.forEach(testCase => { + test(testCase.name, async () => { + const onEvent = setupMocks(testCase); + + const output = await xrpApp.signTxn({ + ...testCase.params, + onEvent, + }); + expect(output).toEqual(testCase.output); + + expectMockCalls(testCase); + }); + }); + }); + + describe('should throw error with invalid arguments', () => { + fixtures.invalidArgs.forEach(testCase => { + test(testCase.name, async () => { + setupMocks(testCase); + + const rejectedPromise = xrpApp.signTxn(testCase.params); + + await expect(rejectedPromise).rejects.toThrow(testCase.errorInstance); + if (testCase.errorMessage) { + try { + await rejectedPromise; + } catch (error: any) { + expect(error.message).toMatch(testCase.errorMessage); + } + } + }); + }); + }); + + describe('should throw error when device returns invalid data', () => { + fixtures.invalidData.forEach(testCase => { + test(testCase.name, async () => { + setupMocks(testCase); + + const rejectedPromise = xrpApp.signTxn(testCase.params); + + await expect(rejectedPromise).rejects.toThrow(testCase.errorInstance); + if (testCase.errorMessage) { + try { + await rejectedPromise; + } catch (error: any) { + expect(error.message).toMatch(testCase.errorMessage); + } + } + + expectMockCalls(testCase); + }); + }); + }); + + describe('should throw error when device returns error', () => { + fixtures.error.forEach(testCase => { + test(testCase.name, async () => { + setupMocks(testCase); + + const rejectedPromise = xrpApp.signTxn(testCase.params); + + await expect(rejectedPromise).rejects.toThrow(testCase.errorInstance); + if (testCase.errorMessage) { + await expect(rejectedPromise).rejects.toThrowError( + testCase.errorMessage, + ); + } + + expectMockCalls(testCase); + }); + }); + }); +});