Skip to content

Commit

Permalink
chore: improve test for composeTx so that random number generotor can…
Browse files Browse the repository at this point in the history
… be mocked per-case in fixure independently
  • Loading branch information
peter-sanderson committed Oct 22, 2024
1 parent a78ed47 commit 131b6aa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
7 changes: 6 additions & 1 deletion packages/utxo-lib/tests/__fixtures__/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Fixture = {
changeAddress: AnyComposeRequest['changeAddress'] & { path?: number[] | string };
};
result: AnyComposeResult;
randomIntSequence?: number[];
};

export const composeTxFixture: Fixture[] = [
Expand Down Expand Up @@ -528,7 +529,11 @@ export const composeTxFixture: Fixture[] = [
description:
'sorts the inputs randomly and puts change at random place between user-defined outputs' +
'when sortingStrategy=random',
// This test relies on the `fakeRandom` generator in the test file.
randomIntSequence: [
1, // For outputs (so change output is inserted at index 1 after the first output)
1, // Shuffling inputs (Fisher-Yates swap last, the 3rd with second)
0, // Shuffling inputs (Fisher-Yates swap second with the first)
],
request: {
changeAddress: { address: '1CrwjoKxvdbAnPcGzYjpvZ4no4S71neKXT' },
dustThreshold: 546,
Expand Down
38 changes: 16 additions & 22 deletions packages/utxo-lib/tests/compose.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,11 @@ import { verifyTxBytes } from './compose.utils';
import { composeTxFixture } from './__fixtures__/compose';
import { fixturesCrossCheck } from './__fixtures__/compose.crosscheck';

jest.mock('@trezor/utils', () => {
const actual = jest.requireActual('@trezor/utils');

let fakeRandomIndex = 0;
const fakeRandom = [
1, // Called from `sortingStrategy=random` test, for outputs (so change output is inserted at index 1 after the first output)
1, // Called from `sortingStrategy=random` test, when shuffling inputs
0, // Called from `sortingStrategy=random` test, when shuffling inputs
];

return {
...actual,
getRandomInt: () => {
if (fakeRandomIndex >= fakeRandom.length) {
throw new Error(
`getRandomInt called too many times, add more values to fakeRandom`,
);
}
import { getRandomInt, arrayShuffle } from '@trezor/utils';

return fakeRandom[fakeRandomIndex++];
},
};
});
jest.mock('@trezor/utils');

(arrayShuffle as jest.Mock).mockImplementation(jest.requireActual('@trezor/utils').arrayShuffle);

describe(composeTx.name, () => {
composeTxFixture.forEach(f => {
Expand All @@ -36,6 +18,18 @@ describe(composeTx.name, () => {
const result = { ...f.result };

it(f.description, () => {
let fakeRandomIndex = 0;
(getRandomInt as jest.Mock).mockImplementation(() => {
if (
f.randomIntSequence === undefined ||
fakeRandomIndex >= f.randomIntSequence.length
) {
throw new Error(`Not enough random numbers provided (i: ${fakeRandomIndex})`);
}

return f.randomIntSequence?.[fakeRandomIndex++];
});

const tx = composeTx(request);
expect(tx).toEqual(result);

Expand Down

0 comments on commit 131b6aa

Please sign in to comment.