From c647be701e139ced428675c8872999b29e5d9179 Mon Sep 17 00:00:00 2001 From: Mayukha Vadari Date: Wed, 31 May 2023 12:46:00 -0400 Subject: [PATCH] test: fix Escrow integration tests (#2305) * improve runtime of TrustSet integ test * fix EscrowFinish test * finish EscrowCancel test * add comment * fix linter issues * fix browser tests --------- Co-authored-by: Caleb Kniffen Co-authored-by: justinr1234 --- packages/xrpl/.eslintignore | 2 + packages/xrpl/karma.config.js | 10 ++++ packages/xrpl/test/integration/index.ts | 9 +++- .../transactions/escrowCancel.test.ts | 54 ++++++++----------- .../transactions/escrowFinish.test.ts | 3 ++ .../integration/transactions/trustSet.test.ts | 19 ++++--- packages/xrpl/test/integration/utils.ts | 3 +- 7 files changed, 57 insertions(+), 43 deletions(-) diff --git a/packages/xrpl/.eslintignore b/packages/xrpl/.eslintignore index 1d39d000..b24141f3 100644 --- a/packages/xrpl/.eslintignore +++ b/packages/xrpl/.eslintignore @@ -2,3 +2,5 @@ dist node_modules .github .vscode +karma.config.js +karma-setup.js diff --git a/packages/xrpl/karma.config.js b/packages/xrpl/karma.config.js index b5b6aba6..838fcbb5 100644 --- a/packages/xrpl/karma.config.js +++ b/packages/xrpl/karma.config.js @@ -30,5 +30,15 @@ module.exports = function (config) { }, browsers: ['ChromeHeadless'], + // runs only one browser at a time + concurrency: 1, + // CI mode + singleRun: true, + client: { + jasmine: { + // ensures that tests are run in order instead of a random order + random: false, + }, + }, }) } diff --git a/packages/xrpl/test/integration/index.ts b/packages/xrpl/test/integration/index.ts index 10e0c294..de4b212d 100644 --- a/packages/xrpl/test/integration/index.ts +++ b/packages/xrpl/test/integration/index.ts @@ -1,12 +1,16 @@ /* eslint-disable import/export -- Tells webpack which files exist. */ + +// These go first because they're affected by the `ledger_accept`s +export * from './transactions/escrowFinish.test' +export * from './transactions/escrowCancel.test' + +// Transactions export * from './transactions/accountSet.test' export * from './transactions/checkCancel.test' export * from './transactions/checkCash.test' export * from './transactions/checkCreate.test' export * from './transactions/depositPreauth.test' -export * from './transactions/escrowCancel.test' export * from './transactions/escrowCreate.test' -export * from './transactions/escrowFinish.test' export * from './transactions/offerCancel.test' export * from './transactions/offerCreate.test' export * from './transactions/payment.test' @@ -16,6 +20,7 @@ export * from './transactions/paymentChannelFund.test' export * from './transactions/signerListSet.test' export * from './transactions/trustSet.test' +// Requests export * from './requests/accountChannels.test' export * from './requests/accountCurrencies.test' export * from './requests/accountInfo.test' diff --git a/packages/xrpl/test/integration/transactions/escrowCancel.test.ts b/packages/xrpl/test/integration/transactions/escrowCancel.test.ts index 13b2854b..2a99a8ac 100644 --- a/packages/xrpl/test/integration/transactions/escrowCancel.test.ts +++ b/packages/xrpl/test/integration/transactions/escrowCancel.test.ts @@ -8,21 +8,13 @@ import { type XrplIntegrationTestContext, } from '../setup' import { - // calculateWaitTimeForTransaction, + calculateWaitTimeForTransaction, generateFundedWallet, - // getXRPBalance, + getXRPBalance, testTransaction, - submitTransaction, + sendLedgerAccept, } from '../utils' -// TODO: Fix these tests -// NOTE: Because ledger accept is called among multiple tests, the actual ledger close time is not -// accurate. It can end up very far into the future. This means that the CancelAfter timer can potentially -// need to wait for several minutes to be able to properly complete. Since we are not testing the functionaity -// of rippled in this library, only that we are submitting commands properly, we can just test that the EscrowCancel -// command was successfully received. If in the future we isolate tests to run on their own rippled instance, -// we can uncomment the code in this file to test that the escrow was actually cancelled. - // how long before each test case times out const TIMEOUT = 50000 @@ -48,7 +40,7 @@ describe('EscrowCancel', function () { }) ).result.ledger.close_time - // const waitTimeInMs = calculateWaitTimeForTransaction(CLOSE_TIME) + const waitTimeInMs = calculateWaitTimeForTransaction(CLOSE_TIME) const createTx: EscrowCreate = { Account: testContext.wallet.classicAddress, @@ -61,10 +53,10 @@ describe('EscrowCancel', function () { await testTransaction(testContext.client, createTx, testContext.wallet) - // const initialBalanceWallet1 = await getXRPBalance( - // testContext.client, - // wallet1, - // ) + const initialBalanceWallet1 = await getXRPBalance( + testContext.client, + wallet1, + ) // check that the object was actually created const accountObjects = ( @@ -96,29 +88,25 @@ describe('EscrowCancel', function () { // We set the CancelAfter timer to be 3 seconds after the last ledger close_time. We need to wait this long // before we can cancel the escrow. - // const cancelAfterTimerPromise = new Promise((resolve) => { - // setTimeout(resolve, waitTimeInMs) - // }) + const cancelAfterTimerPromise = new Promise((resolve) => { + setTimeout(resolve, waitTimeInMs) + }) // Make sure we wait long enough before canceling the escrow. - // await cancelAfterTimerPromise + await cancelAfterTimerPromise - // await testTransaction(testContext.client, cancelTx, testContext.wallet, { - // count: 20, - // delayMs: 2000, - // }) - - await submitTransaction({ - client: testContext.client, - transaction: cancelTx, - wallet: testContext.wallet, + // rippled uses the close time of the previous ledger + await sendLedgerAccept(testContext.client) + await testTransaction(testContext.client, cancelTx, testContext.wallet, { + count: 20, + delayMs: 2000, }) // Make sure the Destination wallet did not receive any XRP. - // assert.equal( - // await getXRPBalance(testContext.client, wallet1), - // initialBalanceWallet1, - // ) + assert.equal( + await getXRPBalance(testContext.client, wallet1), + initialBalanceWallet1, + ) }, TIMEOUT, ) diff --git a/packages/xrpl/test/integration/transactions/escrowFinish.test.ts b/packages/xrpl/test/integration/transactions/escrowFinish.test.ts index f93319f2..f086fbd7 100644 --- a/packages/xrpl/test/integration/transactions/escrowFinish.test.ts +++ b/packages/xrpl/test/integration/transactions/escrowFinish.test.ts @@ -11,6 +11,7 @@ import { calculateWaitTimeForTransaction, generateFundedWallet, getXRPBalance, + sendLedgerAccept, testTransaction, } from '../utils' @@ -84,6 +85,8 @@ describe('EscrowFinish', function () { await finishAfterPromise + // rippled uses the close time of the previous ledger + await sendLedgerAccept(testContext.client) await testTransaction(testContext.client, finishTx, testContext.wallet) const expectedBalance = String(Number(initialBalance) + Number(AMOUNT)) diff --git a/packages/xrpl/test/integration/transactions/trustSet.test.ts b/packages/xrpl/test/integration/transactions/trustSet.test.ts index b31b38c6..7736d0d1 100644 --- a/packages/xrpl/test/integration/transactions/trustSet.test.ts +++ b/packages/xrpl/test/integration/transactions/trustSet.test.ts @@ -1,4 +1,6 @@ -import { TrustSet, percentToQuality } from '../../../src' +import { assert } from 'chai' + +import { TrustSet, percentToQuality, Wallet } from '../../../src' import serverUrl from '../serverUrl' import { setupClient, @@ -12,16 +14,21 @@ const TIMEOUT = 20000 describe('TrustSet', function () { let testContext: XrplIntegrationTestContext + let wallet2: Wallet | undefined beforeEach(async () => { testContext = await setupClient(serverUrl) + if (!wallet2) { + // eslint-disable-next-line require-atomic-updates -- race condition doesn't really matter + wallet2 = await generateFundedWallet(testContext.client) + } }) afterEach(async () => teardownClient(testContext)) it( 'base', async () => { - const wallet2 = await generateFundedWallet(testContext.client) + assert(wallet2 != null) const tx: TrustSet = { TransactionType: 'TrustSet', Account: testContext.wallet.classicAddress, @@ -40,14 +47,14 @@ describe('TrustSet', function () { it( 'Quality < 1', async () => { - const wallet2 = await generateFundedWallet(testContext.client) + assert(wallet2 != null) const tx: TrustSet = { TransactionType: 'TrustSet', Account: testContext.wallet.address, QualityIn: percentToQuality('99%'), QualityOut: percentToQuality('99%'), LimitAmount: { - currency: 'USD', + currency: 'BTC', issuer: wallet2.address, value: '100', }, @@ -61,14 +68,14 @@ describe('TrustSet', function () { it( 'Quality > 1', async () => { - const wallet2 = await generateFundedWallet(testContext.client) + assert(wallet2 != null) const tx: TrustSet = { TransactionType: 'TrustSet', QualityIn: percentToQuality('101%'), QualityOut: percentToQuality('101%'), Account: testContext.wallet.address, LimitAmount: { - currency: 'USD', + currency: 'ETH', issuer: wallet2.address, value: '100', }, diff --git a/packages/xrpl/test/integration/utils.ts b/packages/xrpl/test/integration/utils.ts index b87a5530..7cdc80af 100644 --- a/packages/xrpl/test/integration/utils.ts +++ b/packages/xrpl/test/integration/utils.ts @@ -18,7 +18,7 @@ import { hashSignedTx } from '../../src/utils/hashes' const masterAccount = 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh' const masterSecret = 'snoPBrXtMeMyMHUVTgbuqAfg1SUTb' -async function sendLedgerAccept(client: Client): Promise { +export async function sendLedgerAccept(client: Client): Promise { return client.connection.request({ command: 'ledger_accept' }) } @@ -253,7 +253,6 @@ export async function testTransaction( }, ): Promise { // Accept any un-validated changes. - await ledgerAccept(client) // sign/submit the transaction const response = await submitTransaction({