Skip to content

Commit

Permalink
test(e2e): reduce test flakiness by awaiting wallet to settle
Browse files Browse the repository at this point in the history
tx confirmation check result may be dependent on which provider completes earlier
  • Loading branch information
iccicci committed Sep 24, 2024
1 parent 297958a commit 452bb95
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 145 deletions.
15 changes: 2 additions & 13 deletions packages/e2e/test/wallet_epoch_0/PersonalWallet/byron.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
14 changes: 3 additions & 11 deletions packages/e2e/test/wallet_epoch_0/PersonalWallet/metadata.test.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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);
});
});
14 changes: 2 additions & 12 deletions packages/e2e/test/wallet_epoch_0/PersonalWallet/mint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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();
Expand Down
46 changes: 7 additions & 39 deletions packages/e2e/test/wallet_epoch_0/PersonalWallet/plutusTest.test.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getEnv,
getWallet,
normalizeTxBody,
submitAndConfirm,
waitForWalletStateSettle,
walletReady,
walletVariables
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 452bb95

Please sign in to comment.