From 7943bf9399c185b0e2339e5b800c4b3e48e10091 Mon Sep 17 00:00:00 2001 From: Joe Polny Date: Fri, 13 Dec 2024 15:56:07 -0500 Subject: [PATCH 1/2] fix: make algorand client persistent for test fixture --- src/testing/fixtures/algorand-fixture.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/testing/fixtures/algorand-fixture.ts b/src/testing/fixtures/algorand-fixture.ts index b57bacba..c82297e5 100644 --- a/src/testing/fixtures/algorand-fixture.ts +++ b/src/testing/fixtures/algorand-fixture.ts @@ -90,7 +90,7 @@ export function algorandFixture(fixtureConfig?: AlgorandFixtureConfig, config?: Config.configure({ debug: true }) const transactionLogger = new TransactionLogger() const transactionLoggerAlgod = transactionLogger.capture(algod) - algorand = AlgorandClient.fromClients({ algod: transactionLoggerAlgod, indexer, kmd }) + algorand = algorand ?? AlgorandClient.fromClients({ algod: transactionLoggerAlgod, indexer, kmd }) const testAccount = await getTestAccount({ initialFunds: fixtureConfig?.testAccountFunding ?? algos(10), suppressLog: true }, algorand) algorand.setSignerFromAccount(testAccount).setSuggestedParamsCacheTimeout(0) // If running against LocalNet we are likely in dev mode and we need to set a much higher validity window From 5918017d56261870c5c66c7b1b6bd56d31e792db Mon Sep 17 00:00:00 2001 From: Joe Polny Date: Sat, 14 Dec 2024 10:57:29 -0500 Subject: [PATCH 2/2] reduce repeated logic in algorandFixture beforeEach --- src/testing/fixtures/algorand-fixture.ts | 59 ++++++++++++++++-------- 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/src/testing/fixtures/algorand-fixture.ts b/src/testing/fixtures/algorand-fixture.ts index c82297e5..d83d928c 100644 --- a/src/testing/fixtures/algorand-fixture.ts +++ b/src/testing/fixtures/algorand-fixture.ts @@ -75,6 +75,8 @@ export function algorandFixture(fixtureConfig?: AlgorandFixtureConfig): Algorand export function algorandFixture(fixtureConfig: AlgorandFixtureConfig | undefined, config: AlgoConfig): AlgorandFixture export function algorandFixture(fixtureConfig?: AlgorandFixtureConfig, config?: AlgoConfig): AlgorandFixture { + Config.configure({ debug: true }) + fixtureConfig = { ...fixtureConfig, ...config } if (!fixtureConfig.algod || !fixtureConfig.indexer || !fixtureConfig.kmd) { fixtureConfig = { ...ClientManager.getConfigFromEnvironmentOrLocalNet(), ...fixtureConfig } @@ -83,21 +85,44 @@ export function algorandFixture(fixtureConfig?: AlgorandFixtureConfig, config?: const algod = fixtureConfig.algod ?? ClientManager.getAlgodClient(fixtureConfig.algodConfig!) const indexer = fixtureConfig.indexer ?? ClientManager.getIndexerClient(fixtureConfig.indexerConfig!) const kmd = fixtureConfig.kmd ?? ClientManager.getKmdClient(fixtureConfig.kmdConfig!) + const transactionLogger = new TransactionLogger() + const transactionLoggerAlgod = transactionLogger.capture(algod) + + const algorand = AlgorandClient.fromClients({ algod: transactionLoggerAlgod, indexer, kmd }) + + const generateAccount = async (params: GetTestAccountParams) => { + const account = await getTestAccount(params, algorand) + algorand.setSignerFromAccount(account) + return account + } + + // TODO: Figure out why waitForIndexer is not working + // The tests pass if we just wait for 15 seconds: + // const waitForIndexer = () => new Promise((resolve) => setTimeout(resolve, 15_000)) + // DO NOT MERGE UNTIL WE FIX THIS + const waitForIndexer = () => transactionLogger.waitForIndexer(indexer) + const waitForIndexerTransaction = (transactionId: string) => + runWhenIndexerCaughtUp(() => indexer.lookupTransactionByID(transactionId).do()) + + // If running against LocalNet we are likely in dev mode and we need to set a much higher validity window + // otherwise we are more likely to get invalid transactions. + const algorandClientSetup = new Promise((resolve) => { + algorand.client.isLocalNet().then((isLocalNet) => { + if (isLocalNet) { + algorand.setDefaultValidityWindow(1000) + algorand.setSuggestedParamsCacheTimeout(0) + resolve() + } + }) + }) + let context: AlgorandTestAutomationContext - let algorand: AlgorandClient const beforeEach = async () => { - Config.configure({ debug: true }) - const transactionLogger = new TransactionLogger() - const transactionLoggerAlgod = transactionLogger.capture(algod) - algorand = algorand ?? AlgorandClient.fromClients({ algod: transactionLoggerAlgod, indexer, kmd }) const testAccount = await getTestAccount({ initialFunds: fixtureConfig?.testAccountFunding ?? algos(10), suppressLog: true }, algorand) - algorand.setSignerFromAccount(testAccount).setSuggestedParamsCacheTimeout(0) - // If running against LocalNet we are likely in dev mode and we need to set a much higher validity window - // otherwise we are more likely to get invalid transactions. - if (await algorand.client.isLocalNet()) { - algorand.setDefaultValidityWindow(1000) - } + algorand.setSignerFromAccount(testAccount) + + await algorandClientSetup algorand.account.setSignerFromAccount(testAccount) context = { algorand, @@ -105,14 +130,10 @@ export function algorandFixture(fixtureConfig?: AlgorandFixtureConfig, config?: indexer: indexer, kmd: kmd, testAccount, - generateAccount: async (params: GetTestAccountParams) => { - const account = await getTestAccount(params, algorand) - algorand.setSignerFromAccount(account) - return account - }, - transactionLogger: transactionLogger, - waitForIndexer: () => transactionLogger.waitForIndexer(indexer), - waitForIndexerTransaction: (transactionId: string) => runWhenIndexerCaughtUp(() => indexer.lookupTransactionByID(transactionId).do()), + generateAccount, + transactionLogger, + waitForIndexer, + waitForIndexerTransaction, } }