// SPDX-FileCopyrightText: © 2024 LEDGER SAS // SPDX-License-Identifier: Apache-2.0 import type { StakingOperation } from '../api/index' import { createAssetInfoMockData, createSendTransactionIntentMockData, createStakingTransactionIntentMockData, } from './helpers' describe('createSendTransactionIntentMockData', () => { it.each([ [ 'minimal defaults', { type: 'SendTransaction' }, { intentType: 'transaction', type: 'SendTransaction', sender: 'sender-address', recipient: 'recipient-address', amount: 0n, asset: { type: 'native' }, }, ], [ 'sender override wins', { type: 'SendTransaction', sender: 'alice' }, { intentType: 'transaction', type: 'SendTransaction', sender: 'alice', recipient: 'recipient-address', amount: 0n, asset: { type: 'native' }, }, ], [ 'recipient override wins', { type: 'SendTransaction', recipient: 'bob' }, { intentType: 'transaction', type: 'SendTransaction', sender: 'sender-address', recipient: 'bob', amount: 0n, asset: { type: 'native' }, }, ], [ 'amount override wins', { type: 'SendTransaction', amount: 1000000n }, { intentType: 'transaction', type: 'SendTransaction', sender: 'sender-address', recipient: 'recipient-address', amount: 1000000n, asset: { type: 'native' }, }, ], [ 'amount 0n is preserved (nullish coalescing, not falsy)', { type: 'SendTransaction', amount: 0n }, { intentType: 'transaction', type: 'SendTransaction', sender: 'sender-address', recipient: 'recipient-address', amount: 0n, asset: { type: 'native' }, }, ], [ 'asset native with name override wins', { type: 'SendTransaction', asset: { type: 'native', name: 'CoinA' } }, { intentType: 'transaction', type: 'SendTransaction', sender: 'sender-address', recipient: 'recipient-address', amount: 0n, asset: { type: 'native', name: 'CoinA' }, }, ], [ 'asset token override wins', { type: 'SendTransaction', asset: { type: 'token', assetReference: '0xcafe' } }, { intentType: 'transaction', type: 'SendTransaction', sender: 'sender-address', recipient: 'recipient-address', amount: 0n, asset: { type: 'token', assetReference: '0xcafe' }, }, ], [ 'all fields overridden (no defaults used)', { type: 'SendToken', sender: 'alice', recipient: 'bob', amount: 50000000n, asset: { type: 'native' }, }, { intentType: 'transaction', type: 'SendToken', sender: 'alice', recipient: 'bob', amount: 50000000n, asset: { type: 'native' }, }, ], ])('%s', (_label, partial, expected) => { expect(createSendTransactionIntentMockData(partial)).toEqual(expected) }) it('attaches memo when provided', () => { expect( createSendTransactionIntentMockData({ type: 'SendTransaction', memo: { type: 'text', text: 'hello' }, }) ).toEqual({ intentType: 'transaction', type: 'SendTransaction', sender: 'sender-address', recipient: 'recipient-address', amount: 0n, asset: { type: 'native' }, memo: { type: 'text', text: 'hello' }, }) }) it('attaches data when provided', () => { expect( createSendTransactionIntentMockData({ type: 'SendTransaction', data: { type: 'calldata', hex: '0xcafe' }, }) ).toEqual({ intentType: 'transaction', type: 'SendTransaction', sender: 'sender-address', recipient: 'recipient-address', amount: 0n, asset: { type: 'native' }, data: { type: 'calldata', hex: '0xcafe' }, }) }) it('attaches both memo and data when provided', () => { expect( createSendTransactionIntentMockData({ type: 'SendTransaction', memo: { type: 'text', text: 'hello' }, data: { type: 'calldata', hex: '0xcafe' }, }) ).toEqual({ intentType: 'transaction', type: 'SendTransaction', sender: 'sender-address', recipient: 'recipient-address', amount: 0n, asset: { type: 'native' }, memo: { type: 'text', text: 'hello' }, data: { type: 'calldata', hex: '0xcafe' }, }) }) }) describe('createStakingTransactionIntentMockData', () => { it.each([ [ 'minimal defaults', { type: 'StakeDelegate', mode: 'delegate' }, { intentType: 'staking', type: 'StakeDelegate', mode: 'delegate', sender: 'sender-address', recipient: 'recipient-address', amount: 0n, asset: { type: 'native' }, valAddress: 'validator-address', }, ], [ 'sender override wins', { type: 'StakeDelegate', mode: 'delegate', sender: 'alice' }, { intentType: 'staking', type: 'StakeDelegate', mode: 'delegate', sender: 'alice', recipient: 'recipient-address', amount: 0n, asset: { type: 'native' }, valAddress: 'validator-address', }, ], [ 'recipient override wins', { type: 'StakeDelegate', mode: 'delegate', recipient: 'bob' }, { intentType: 'staking', type: 'StakeDelegate', mode: 'delegate', sender: 'sender-address', recipient: 'bob', amount: 0n, asset: { type: 'native' }, valAddress: 'validator-address', }, ], [ 'amount override wins', { type: 'StakeDelegate', mode: 'delegate', amount: 5000000n }, { intentType: 'staking', type: 'StakeDelegate', mode: 'delegate', sender: 'sender-address', recipient: 'recipient-address', amount: 5000000n, asset: { type: 'native' }, valAddress: 'validator-address', }, ], [ 'valAddress override wins', { type: 'StakeDelegate', mode: 'delegate', valAddress: 'custom-validator' }, { intentType: 'staking', type: 'StakeDelegate', mode: 'delegate', sender: 'sender-address', recipient: 'recipient-address', amount: 0n, asset: { type: 'native' }, valAddress: 'custom-validator', }, ], [ 'dstValAddress passes through', { type: 'StakeRedelegate', mode: 'redelegate', dstValAddress: 'dst-validator' }, { intentType: 'staking', type: 'StakeRedelegate', mode: 'redelegate', sender: 'sender-address', recipient: 'recipient-address', amount: 0n, asset: { type: 'native' }, valAddress: 'validator-address', dstValAddress: 'dst-validator', }, ], [ 'asset override wins', { type: 'StakeDelegate', mode: 'delegate', asset: { type: 'native', name: 'CoinA' } }, { intentType: 'staking', type: 'StakeDelegate', mode: 'delegate', sender: 'sender-address', recipient: 'recipient-address', amount: 0n, asset: { type: 'native', name: 'CoinA' }, valAddress: 'validator-address', }, ], ] as const)('%s', (_label, partial, expected) => { expect(createStakingTransactionIntentMockData(partial)).toEqual(expected) }) it.each([ 'delegate', 'undelegate', 'redelegate', 'claimReward', 'compoundReward', 'withdraw', ])('mode %s is preserved', (mode) => { expect(createStakingTransactionIntentMockData({ type: 'StakeOperation', mode })).toEqual({ intentType: 'staking', type: 'StakeOperation', mode, sender: 'sender-address', recipient: 'recipient-address', amount: 0n, asset: { type: 'native' }, valAddress: 'validator-address', }) }) it('attaches memo when provided', () => { expect( createStakingTransactionIntentMockData({ type: 'StakeDelegate', mode: 'delegate', memo: { type: 'text', text: 'stake' }, }) ).toEqual({ intentType: 'staking', type: 'StakeDelegate', mode: 'delegate', sender: 'sender-address', recipient: 'recipient-address', amount: 0n, asset: { type: 'native' }, valAddress: 'validator-address', memo: { type: 'text', text: 'stake' }, }) }) it('attaches data when provided', () => { expect( createStakingTransactionIntentMockData({ type: 'StakeDelegate', mode: 'delegate', data: { type: 'msg-data', encoded: '0xcafe' }, }) ).toEqual({ intentType: 'staking', type: 'StakeDelegate', mode: 'delegate', sender: 'sender-address', recipient: 'recipient-address', amount: 0n, asset: { type: 'native' }, valAddress: 'validator-address', data: { type: 'msg-data', encoded: '0xcafe' }, }) }) it('attaches both memo and data when provided', () => { expect( createStakingTransactionIntentMockData({ type: 'StakeDelegate', mode: 'delegate', memo: { type: 'text', text: 'stake' }, data: { type: 'msg-data', encoded: '0xcafe' }, }) ).toEqual({ intentType: 'staking', type: 'StakeDelegate', mode: 'delegate', sender: 'sender-address', recipient: 'recipient-address', amount: 0n, asset: { type: 'native' }, valAddress: 'validator-address', memo: { type: 'text', text: 'stake' }, data: { type: 'msg-data', encoded: '0xcafe' }, }) }) }) describe('createAssetInfoMockData', () => { it.each([ ['native minimal', { type: 'native' }, { type: 'native' }], ['native with name', { type: 'native', name: 'CoinA' }, { type: 'native', name: 'CoinA' }], [ 'fungible token', { type: 'token', assetReference: '0xcafe' }, { type: 'token', assetReference: '0xcafe' }, ], [ 'nft with owner', { type: 'nft', assetReference: '0xcafe', assetOwner: '0xcafe' }, { type: 'nft', assetReference: '0xcafe', assetOwner: '0xcafe' }, ], [ 'token with name', { type: 'fungible-token', name: 'TokenA' }, { type: 'fungible-token', name: 'TokenA' }, ], ])('%s', (_label, partial, expected) => { expect(createAssetInfoMockData(partial)).toEqual(expected) }) })