From 452bb95441fb5bf803d293202e38c0746f2885d5 Mon Sep 17 00:00:00 2001 From: Daniele Ricci Date: Tue, 24 Sep 2024 19:48:31 +0200 Subject: [PATCH] test(e2e): reduce test flakiness by awaiting wallet to settle tx confirmation check result may be dependent on which provider completes earlier --- .../PersonalWallet/byron.test.ts | 15 +----- .../PersonalWallet/metadata.test.ts | 14 ++---- .../PersonalWallet/mint.test.ts | 14 +----- .../PersonalWallet/multiAddress.test.ts | 26 ++--------- .../PersonalWallet/phase2validation.test.ts | 26 ++--------- .../PersonalWallet/plutusTest.test.ts | 46 +++---------------- .../PersonalWallet/txChainHistory.test.ts | 4 +- .../PersonalWallet/unspendableUtxos.test.ts | 26 ++--------- .../SharedWallet/simpleTx.test.ts | 3 +- 9 files changed, 29 insertions(+), 145 deletions(-) diff --git a/packages/e2e/test/wallet_epoch_0/PersonalWallet/byron.test.ts b/packages/e2e/test/wallet_epoch_0/PersonalWallet/byron.test.ts index 78b1ebbad4d..46a6626b6b8 100644 --- a/packages/e2e/test/wallet_epoch_0/PersonalWallet/byron.test.ts +++ b/packages/e2e/test/wallet_epoch_0/PersonalWallet/byron.test.ts @@ -2,10 +2,8 @@ import { BaseWallet } from '@cardano-sdk/wallet'; import { Cardano } from '@cardano-sdk/core'; import { createLogger } from '@cardano-sdk/util-dev'; -import { filter, firstValueFrom, map, take } from 'rxjs'; import { getEnv, walletVariables } from '../../../src/environment'; -import { getWallet, normalizeTxBody, walletReady } from '../../../src'; -import { isNotNil } from '@cardano-sdk/util'; +import { getWallet, normalizeTxBody, submitAndConfirm, walletReady } from '../../../src'; const env = getEnv(walletVariables); const logger = createLogger(); @@ -33,16 +31,7 @@ describe('PersonalWallet/byron', () => { .build(); const { tx: signedTx } = await txBuilder.addOutput(txOutput).build().sign(); - await wallet.submitTx(signedTx); - - // Search chain history to see if the transaction is there. - const txFoundInHistory = await firstValueFrom( - wallet.transactions.history$.pipe( - map((txs) => txs.find((tx) => tx.id === signedTx.id)), - filter(isNotNil), - take(1) - ) - ); + const [, txFoundInHistory] = await submitAndConfirm(wallet, signedTx, 1); // Assert expect(txFoundInHistory).toBeDefined(); diff --git a/packages/e2e/test/wallet_epoch_0/PersonalWallet/metadata.test.ts b/packages/e2e/test/wallet_epoch_0/PersonalWallet/metadata.test.ts index cedc2a14ded..45d460ab558 100644 --- a/packages/e2e/test/wallet_epoch_0/PersonalWallet/metadata.test.ts +++ b/packages/e2e/test/wallet_epoch_0/PersonalWallet/metadata.test.ts @@ -1,8 +1,7 @@ import { BaseWallet, createWalletUtil } from '@cardano-sdk/wallet'; import { Cardano } from '@cardano-sdk/core'; -import { filter, firstValueFrom, map } from 'rxjs'; -import { getEnv, getWallet, walletReady, walletVariables } from '../../../src'; -import { isNotNil } from '@cardano-sdk/util'; +import { firstValueFrom } from 'rxjs'; +import { getEnv, getWallet, submitAndConfirm, walletReady, walletVariables } from '../../../src'; import { logger } from '@cardano-sdk/util-dev'; const env = getEnv(walletVariables); @@ -35,15 +34,8 @@ describe('PersonalWallet/metadata', () => { .build() .sign(); - const outgoingTx = signedTx; - await wallet.submitTx(signedTx); + const [, loadedTx] = await submitAndConfirm(wallet, signedTx, 1); - const loadedTx = await firstValueFrom( - wallet.transactions.history$.pipe( - map((txs) => txs.find((tx) => tx.id === outgoingTx.id)), - filter(isNotNil) - ) - ); expect(loadedTx.auxiliaryData?.blob).toEqual(metadata); }); }); diff --git a/packages/e2e/test/wallet_epoch_0/PersonalWallet/mint.test.ts b/packages/e2e/test/wallet_epoch_0/PersonalWallet/mint.test.ts index 17834da408d..882fccb7026 100644 --- a/packages/e2e/test/wallet_epoch_0/PersonalWallet/mint.test.ts +++ b/packages/e2e/test/wallet_epoch_0/PersonalWallet/mint.test.ts @@ -13,8 +13,7 @@ import { walletVariables } from '../../../src'; import { createLogger } from '@cardano-sdk/util-dev'; -import { filter, firstValueFrom, map, take } from 'rxjs'; -import { isNotNil } from '@cardano-sdk/util'; +import { filter, firstValueFrom } from 'rxjs'; const env = getEnv(walletVariables); const logger = createLogger(); @@ -99,16 +98,7 @@ describe('PersonalWallet/mint', () => { }; const signedTx = await wallet.finalizeTx(finalizeProps); - await submitAndConfirm(wallet, signedTx); - - // Search chain history to see if the transaction is there. - const txFoundInHistory = await firstValueFrom( - wallet.transactions.history$.pipe( - map((txs) => txs.find((tx) => tx.id === signedTx.id)), - filter(isNotNil), - take(1) - ) - ); + const [, txFoundInHistory] = await submitAndConfirm(wallet, signedTx, 1); expect(txFoundInHistory.id).toEqual(signedTx.id); diff --git a/packages/e2e/test/wallet_epoch_0/PersonalWallet/multiAddress.test.ts b/packages/e2e/test/wallet_epoch_0/PersonalWallet/multiAddress.test.ts index d6aac295a7b..ebe9dc1a2d5 100644 --- a/packages/e2e/test/wallet_epoch_0/PersonalWallet/multiAddress.test.ts +++ b/packages/e2e/test/wallet_epoch_0/PersonalWallet/multiAddress.test.ts @@ -9,12 +9,11 @@ import { firstValueFromTimed, getWallet, normalizeTxBody, + submitAndConfirm, walletReady } from '../../../src'; import { createLogger } from '@cardano-sdk/util-dev'; -import { filter, map, take } from 'rxjs'; import { getEnv, walletVariables } from '../../../src/environment'; -import { isNotNil } from '@cardano-sdk/util'; const env = getEnv(walletVariables); const logger = createLogger(); @@ -77,16 +76,7 @@ describe('PersonalWallet/multiAddress', () => { const { tx: signedTx } = await txBuilder.build().sign(); - await wallet.submitTx(signedTx); - - // Search chain history to see if the transaction is there. - const txFoundInHistory = await firstValueFromTimed( - wallet.transactions.history$.pipe( - map((txs) => txs.find((tx) => tx.id === signedTx.id)), - filter(isNotNil), - take(1) - ) - ); + const [, txFoundInHistory] = await submitAndConfirm(wallet, signedTx, 1); expect(txFoundInHistory.id).toEqual(signedTx.id); expect(normalizeTxBody(txFoundInHistory.body)).toEqual(normalizeTxBody(signedTx.body)); @@ -143,16 +133,8 @@ describe('PersonalWallet/multiAddress', () => { ) .build() .sign(); - await newWallet.wallet.submitTx(returnAdaSignedTx); - - // Search chain history to see if the transaction is there. - const returnAdaTxFoundInHistory = await firstValueFromTimed( - newWallet.wallet.transactions.history$.pipe( - map((txs) => txs.find((tx) => tx.id === returnAdaSignedTx.id)), - filter(isNotNil), - take(1) - ) - ); + + const [, returnAdaTxFoundInHistory] = await submitAndConfirm(newWallet.wallet, returnAdaSignedTx, 1); expect(returnAdaTxFoundInHistory.id).toEqual(returnAdaSignedTx.id); expect(normalizeTxBody(returnAdaTxFoundInHistory.body)).toEqual(normalizeTxBody(returnAdaSignedTx.body)); diff --git a/packages/e2e/test/wallet_epoch_0/PersonalWallet/phase2validation.test.ts b/packages/e2e/test/wallet_epoch_0/PersonalWallet/phase2validation.test.ts index 35c56c2d4cd..1d73ed048f1 100644 --- a/packages/e2e/test/wallet_epoch_0/PersonalWallet/phase2validation.test.ts +++ b/packages/e2e/test/wallet_epoch_0/PersonalWallet/phase2validation.test.ts @@ -4,7 +4,7 @@ import { HexBlob, isNotNil } from '@cardano-sdk/util'; import { InitializeTxProps, computeScriptDataHash } from '@cardano-sdk/tx-construction'; import { createLogger } from '@cardano-sdk/util-dev'; import { filter, firstValueFrom, map, take } from 'rxjs'; -import { firstValueFromTimed, getEnv, getWallet, walletReady, walletVariables } from '../../../src'; +import { firstValueFromTimed, getEnv, getWallet, submitAndConfirm, walletReady, walletVariables } from '../../../src'; const env = getEnv(walletVariables); const logger = createLogger(); @@ -37,16 +37,7 @@ const createCollateral = async ( const txOutput = await txBuilder.buildOutput().address(address).coin(5_000_000n).build(); const { tx: signedTx } = await txBuilder.addOutput(txOutput).build().sign(); - await wallet.submitTx(signedTx); - - // Wait for transaction to be on chain. - await firstValueFrom( - wallet.transactions.history$.pipe( - map((txs) => txs.find((tx) => tx.id === signedTx.id)), - filter(isNotNil), - take(1) - ) - ); + await submitAndConfirm(wallet, signedTx, 1); // Find the collateral UTxO in the UTxO set. const utxo = await firstValueFrom( @@ -151,17 +142,8 @@ describe('PersonalWallet/phase2validation', () => { const signedTx = await wallet.finalizeTx(finalizeProps); - const [, failedTx, txFoundInHistory] = await Promise.all([ - wallet.submitTx(signedTx), - firstValueFromTimed(wallet.transactions.outgoing.failed$), - firstValueFromTimed( - wallet.transactions.history$.pipe( - map((txs) => txs.find((tx) => tx.id === signedTx.id)), - filter(isNotNil), - take(1) - ) - ) - ]); + const [, txFoundInHistory] = await submitAndConfirm(wallet, signedTx, 1); + const failedTx = await firstValueFromTimed(wallet.transactions.outgoing.failed$); // Transaction should be part of the history expect(txFoundInHistory).toBeTruthy(); diff --git a/packages/e2e/test/wallet_epoch_0/PersonalWallet/plutusTest.test.ts b/packages/e2e/test/wallet_epoch_0/PersonalWallet/plutusTest.test.ts index 12093b1505d..914eb979ff2 100644 --- a/packages/e2e/test/wallet_epoch_0/PersonalWallet/plutusTest.test.ts +++ b/packages/e2e/test/wallet_epoch_0/PersonalWallet/plutusTest.test.ts @@ -1,9 +1,9 @@ import { BaseWallet } from '@cardano-sdk/wallet'; import { Cardano, Serialization, UtxoProvider } from '@cardano-sdk/core'; -import { HexBlob, isNotNil } from '@cardano-sdk/util'; -import { Observable, filter, firstValueFrom, interval, map, switchMap, take } from 'rxjs'; +import { HexBlob } from '@cardano-sdk/util'; +import { Observable, filter, firstValueFrom, interval, switchMap } from 'rxjs'; import { createLogger } from '@cardano-sdk/util-dev'; -import { getEnv, getWallet, utxoProviderFactory, walletReady, walletVariables } from '../../../src'; +import { getEnv, getWallet, submitAndConfirm, utxoProviderFactory, walletReady, walletVariables } from '../../../src'; const env = getEnv(walletVariables); const logger = createLogger(); @@ -85,7 +85,7 @@ const fundScript = async (wallet: BaseWallet, receivingAddress: Cardano.PaymentA const txBuilder = wallet.createTxBuilder(); const txOutput = await txBuilder.buildOutput().address(receivingAddress).coin(tAdaToSend).datum(datum).build(); const signedTx = (await txBuilder.addOutput(txOutput).build().sign()).tx; - await wallet.submitTx(signedTx); + const [, txFoundInHistory] = await submitAndConfirm(wallet, signedTx, 1); logger.info( `Submitted transaction id: ${signedTx.id}, inputs: ${JSON.stringify( @@ -95,14 +95,6 @@ const fundScript = async (wallet: BaseWallet, receivingAddress: Cardano.PaymentA )}.` ); - const txFoundInHistory = await firstValueFrom( - wallet.transactions.history$.pipe( - map((txs) => txs.find((tx) => tx.id === signedTx.id)), - filter(isNotNil), - take(1) - ) - ); - logger.info(`Found transaction id in chain history: ${txFoundInHistory.id}`); // Assert @@ -135,7 +127,7 @@ const createScriptRefInput = async (wallet: BaseWallet, script: Cardano.Script): .build(); const signedTx = (await txBuilder.addOutput(txOutput).build().sign()).tx; - await wallet.submitTx(signedTx); + const [, txFoundInHistory] = await submitAndConfirm(wallet, signedTx, 1); logger.info( `Submitted transaction id: ${signedTx.id}, inputs: ${JSON.stringify( @@ -145,14 +137,6 @@ const createScriptRefInput = async (wallet: BaseWallet, script: Cardano.Script): )}.` ); - const txFoundInHistory = await firstValueFrom( - wallet.transactions.history$.pipe( - map((txs) => txs.find((tx) => tx.id === signedTx.id)), - filter(isNotNil), - take(1) - ) - ); - logger.info(`Found transaction id in chain history: ${txFoundInHistory.id}`); // Assert @@ -243,7 +227,7 @@ describe.skip('PersonalWallet/plutus', () => { .sign() ).tx; - await wallet.submitTx(signedTx); + const [, txFoundInHistory] = await submitAndConfirm(wallet, signedTx, 1); logger.info( `Submitted transaction id: ${signedTx.id}, inputs: ${JSON.stringify( @@ -253,14 +237,6 @@ describe.skip('PersonalWallet/plutus', () => { )}.` ); - const txFoundInHistory = await firstValueFrom( - wallet.transactions.history$.pipe( - map((txs) => txs.find((tx) => tx.id === signedTx.id)), - filter(isNotNil), - take(1) - ) - ); - logger.info(`Found transaction id in chain history: ${txFoundInHistory.id}`); // Assert @@ -327,7 +303,7 @@ describe.skip('PersonalWallet/plutus', () => { .sign() ).tx; - await wallet.submitTx(signedTx); + const [, txFoundInHistory] = await submitAndConfirm(wallet, signedTx, 1); logger.info( `Submitted transaction id: ${signedTx.id}, inputs: ${JSON.stringify( @@ -337,14 +313,6 @@ describe.skip('PersonalWallet/plutus', () => { )}.` ); - const txFoundInHistory = await firstValueFrom( - wallet.transactions.history$.pipe( - map((txs) => txs.find((tx) => tx.id === signedTx.id)), - filter(isNotNil), - take(1) - ) - ); - logger.info(`Found transaction id in chain history: ${txFoundInHistory.id}`); // Assert diff --git a/packages/e2e/test/wallet_epoch_0/PersonalWallet/txChainHistory.test.ts b/packages/e2e/test/wallet_epoch_0/PersonalWallet/txChainHistory.test.ts index 051938e1fcb..5263f10fe26 100644 --- a/packages/e2e/test/wallet_epoch_0/PersonalWallet/txChainHistory.test.ts +++ b/packages/e2e/test/wallet_epoch_0/PersonalWallet/txChainHistory.test.ts @@ -1,7 +1,7 @@ import { BaseWallet } from '@cardano-sdk/wallet'; import { Cardano, CardanoNodeUtil } from '@cardano-sdk/core'; import { filter, firstValueFrom, map, take } from 'rxjs'; -import { getEnv, getWallet, normalizeTxBody, walletReady, walletVariables } from '../../../src'; +import { getEnv, getWallet, normalizeTxBody, submitAndConfirm, walletReady, walletVariables } from '../../../src'; import { isNotNil } from '@cardano-sdk/util'; import { logger } from '@cardano-sdk/util-dev'; @@ -28,7 +28,7 @@ describe('PersonalWallet/txChainHistory', () => { const txBuilder = wallet.createTxBuilder(); const txOutput = await txBuilder.buildOutput().address(receivingAddress).coin(tAdaToSend).build(); signedTx = (await txBuilder.addOutput(txOutput).build().sign()).tx; - await wallet.submitTx(signedTx); + await submitAndConfirm(wallet, signedTx, 1); logger.info( `Submitted transaction id: ${signedTx.id}, inputs: ${JSON.stringify( diff --git a/packages/e2e/test/wallet_epoch_0/PersonalWallet/unspendableUtxos.test.ts b/packages/e2e/test/wallet_epoch_0/PersonalWallet/unspendableUtxos.test.ts index bb7a6dc5568..563a81dea3d 100644 --- a/packages/e2e/test/wallet_epoch_0/PersonalWallet/unspendableUtxos.test.ts +++ b/packages/e2e/test/wallet_epoch_0/PersonalWallet/unspendableUtxos.test.ts @@ -2,7 +2,7 @@ import { BaseWallet, utxoEquals } from '@cardano-sdk/wallet'; import { createLogger } from '@cardano-sdk/util-dev'; import { filter, firstValueFrom, map, take } from 'rxjs'; -import { firstValueFromTimed, getEnv, getWallet, walletReady, walletVariables } from '../../../src'; +import { firstValueFromTimed, getEnv, getWallet, submitAndConfirm, walletReady, walletVariables } from '../../../src'; import { isNotNil } from '@cardano-sdk/util'; const env = getEnv(walletVariables); @@ -37,17 +37,7 @@ describe('PersonalWallet/unspendableUtxos', () => { const txOutput = await txBuilder1.buildOutput().address(address).coin(5_000_000n).build(); const { tx: signedTx } = await txBuilder1.addOutput(txOutput).build().sign(); - await wallet1.submitTx(signedTx); - - // Search chain history to see if the transaction is there. - let txFoundInHistory = await firstValueFromTimed( - wallet1.transactions.history$.pipe( - map((txs) => txs.find((tx) => tx.id === signedTx.id)), - filter(isNotNil), - take(1) - ), - `Failed to find transaction ${signedTx.id} in src wallet history` - ); + let [, txFoundInHistory] = await submitAndConfirm(wallet1, signedTx, 1); // Find the UTxO in the UTxO set. const utxo = await firstValueFromTimed( @@ -97,17 +87,7 @@ describe('PersonalWallet/unspendableUtxos', () => { .addOutput(await txBuilder2.buildOutput().address(address).value(totalBalance).build()) .build() .sign(); - await wallet2.submitTx(signedMoveAdaTx); - - // Search chain history to see if the transaction is there. - txFoundInHistory = await firstValueFromTimed( - wallet1.transactions.history$.pipe( - map((txs) => txs.find((tx) => tx.id === signedMoveAdaTx.id)), - filter(isNotNil), - take(1) - ), - `Failed to find second transaction ${signedMoveAdaTx.id} in dest wallet history` - ); + [, txFoundInHistory] = await submitAndConfirm(wallet2, signedMoveAdaTx, 1); expect(txFoundInHistory.id).toEqual(signedMoveAdaTx.id); diff --git a/packages/e2e/test/wallet_epoch_0/SharedWallet/simpleTx.test.ts b/packages/e2e/test/wallet_epoch_0/SharedWallet/simpleTx.test.ts index 463228f0be7..f55a57cfe61 100644 --- a/packages/e2e/test/wallet_epoch_0/SharedWallet/simpleTx.test.ts +++ b/packages/e2e/test/wallet_epoch_0/SharedWallet/simpleTx.test.ts @@ -6,6 +6,7 @@ import { getEnv, getWallet, normalizeTxBody, + submitAndConfirm, waitForWalletStateSettle, walletReady, walletVariables @@ -52,7 +53,7 @@ describe('SharedWallet/simpleTx', () => { const txBuilder = faucetWallet.createTxBuilder(); const txOutput = await txBuilder.buildOutput().address(receivingAddress).coin(initialFunds).build(); fundingTx = (await txBuilder.addOutput(txOutput).build().sign()).tx; - await faucetWallet.submitTx(fundingTx); + await submitAndConfirm(faucetWallet, fundingTx, 1); logger.info( `Submitted transaction id: ${fundingTx.id}, inputs: ${JSON.stringify(