{"version":3,"file":"algorand-fixture.mjs","names":[],"sources":["../../../src/testing/fixtures/algorand-fixture.ts"],"sourcesContent":["import { algos, Config } from '../../'\nimport { AlgorandClient } from '../../types/algorand-client'\nimport { ClientManager } from '../../types/client-manager'\nimport { AlgoConfig } from '../../types/network-client'\nimport { AlgorandFixture, AlgorandFixtureConfig, AlgorandTestAutomationContext, GetTestAccountParams } from '../../types/testing'\nimport { getTestAccount } from '../account'\nimport { runWhenIndexerCaughtUp } from '../indexer'\nimport { TransactionLogger } from '../transaction-logger'\n\n/**\n * Creates a test fixture for automated testing against Algorand.\n * By default it tests against an environment variable specified client\n *  if the standard environment variables are specified, otherwise against\n *  a default LocalNet instance, but you can pass in an algod, indexer\n *  and/or kmd (or their respective config) if you want to test against\n * an explicitly defined network.\n *\n * @example No config (per-test isolation)\n * ```typescript\n * const fixture = algorandFixture()\n *\n * beforeEach(fixture.newScope)\n *\n * test('My test', async () => {\n *     const {algod, indexer, testAccount, ...} = fixture.context\n *     // test things...\n * })\n * ```\n *\n * @example No config (test suite isolation)\n * ```typescript\n * const fixture = algorandFixture()\n *\n * beforeAll(fixture.newScope)\n *\n * test('My test', async () => {\n *     const {algod, indexer, testAccount, ...} = fixture.context\n *     // test things...\n * })\n * ```\n *\n * @example With config\n * ```typescript\n * const fixture = algorandFixture({\n *  algod: new Algodv2('localhost', 12345, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'),\n *  // ...\n * })\n *\n * beforeEach(fixture.newScope)\n *\n * test('My test', async () => {\n *     const {algod, indexer, testAccount, ...} = fixture.context\n *     // test things...\n * })\n * ```\n *\n * @param fixtureConfig The fixture configuration\n * @returns The fixture\n */\nexport function algorandFixture(fixtureConfig?: AlgorandFixtureConfig): AlgorandFixture\n\n/**\n * @deprecated Config can be passed in directly to fixture config now.\n *\n * Creates a test fixture for automated testing against Algorand.\n * By default it tests against an environment variable specified client\n *  if the standard environment variables are specified, otherwise against\n *  a default LocalNet instance, but you can pass in an algod, indexer\n *  and/or kmd if you want to test against an explicitly defined network.\n *\n * @param fixtureConfig The fixture configuration\n * @param config The fixture configuration\n * @returns The fixture\n */\nexport function algorandFixture(fixtureConfig: AlgorandFixtureConfig | undefined, config: AlgoConfig): AlgorandFixture\n\nexport function algorandFixture(fixtureConfig?: AlgorandFixtureConfig, config?: AlgoConfig): AlgorandFixture {\n  fixtureConfig = { ...fixtureConfig, ...config }\n  if (!fixtureConfig.algod || !fixtureConfig.indexer || !fixtureConfig.kmd) {\n    fixtureConfig = { ...ClientManager.getConfigFromEnvironmentOrLocalNet(), ...fixtureConfig }\n  }\n\n  const algod = fixtureConfig.algod ?? ClientManager.getAlgodClient(fixtureConfig.algodConfig!)\n  const indexer = fixtureConfig.indexer ?? ClientManager.getIndexerClient(fixtureConfig.indexerConfig!)\n  const kmd = fixtureConfig.kmd ?? ClientManager.getKmdClient(fixtureConfig.kmdConfig!)\n  let context: AlgorandTestAutomationContext\n  let algorand: AlgorandClient\n\n  const newScope = async () => {\n    Config.configure({ debug: true })\n    const transactionLogger = new TransactionLogger()\n    const transactionLoggerAlgod = transactionLogger.capture(algod)\n\n    algorand = AlgorandClient.fromClients({ algod: transactionLoggerAlgod, indexer, kmd }).setSuggestedParamsCacheTimeout(0)\n\n    const testAccount = await getTestAccount({ initialFunds: fixtureConfig?.testAccountFunding ?? algos(10), suppressLog: true }, algorand)\n    algorand.setSignerFromAccount(testAccount)\n\n    // If running against LocalNet we are likely in dev mode and we need to set a much higher validity window\n    //  otherwise we are more likely to get invalid transactions.\n    if (await algorand.client.isLocalNet()) {\n      algorand.setDefaultValidityWindow(1000)\n    }\n    context = {\n      algorand,\n      algod: transactionLoggerAlgod,\n      indexer: indexer,\n      kmd: kmd,\n      testAccount,\n      generateAccount: async (params: GetTestAccountParams) => {\n        const account = await getTestAccount(params, algorand)\n        algorand.setSignerFromAccount(account)\n        return account\n      },\n      transactionLogger: transactionLogger,\n      waitForIndexer: () => transactionLogger.waitForIndexer(indexer),\n      waitForIndexerTransaction: (transactionId: string) => runWhenIndexerCaughtUp(() => indexer.lookupTransactionByID(transactionId).do()),\n    }\n  }\n\n  return {\n    get context() {\n      if (!context) throw new Error('Context not initialised; make sure to call fixture.newScope() before accessing context.')\n      return context\n    },\n    get algorand() {\n      return algorand\n    },\n    beforeEach: newScope,\n    newScope,\n  }\n}\n"],"mappings":";;;;;;;;AA4EA,SAAgB,gBAAgB,eAAuC,QAAsC;CAC3G,gBAAgB;EAAE,GAAG;EAAe,GAAG;CAAO;CAC9C,IAAI,CAAC,cAAc,SAAS,CAAC,cAAc,WAAW,CAAC,cAAc,KACnE,gBAAgB;EAAE,GAAG,cAAc,mCAAmC;EAAG,GAAG;CAAc;CAG5F,MAAM,QAAQ,cAAc,SAAS,cAAc,eAAe,cAAc,WAAY;CAC5F,MAAM,UAAU,cAAc,WAAW,cAAc,iBAAiB,cAAc,aAAc;CACpG,MAAM,MAAM,cAAc,OAAO,cAAc,aAAa,cAAc,SAAU;CACpF,IAAI;CACJ,IAAI;CAEJ,MAAM,WAAW,YAAY;EAC3B,OAAO,UAAU,EAAE,OAAO,KAAK,CAAC;EAChC,MAAM,oBAAoB,IAAI,kBAAkB;EAChD,MAAM,yBAAyB,kBAAkB,QAAQ,KAAK;EAE9D,WAAW,eAAe,YAAY;GAAE,OAAO;GAAwB;GAAS;EAAI,CAAC,CAAC,CAAC,+BAA+B,CAAC;EAEvH,MAAM,cAAc,MAAM,eAAe;GAAE,cAAc,eAAe,sBAAsB,MAAM,EAAE;GAAG,aAAa;EAAK,GAAG,QAAQ;EACtI,SAAS,qBAAqB,WAAW;EAIzC,IAAI,MAAM,SAAS,OAAO,WAAW,GACnC,SAAS,yBAAyB,GAAI;EAExC,UAAU;GACR;GACA,OAAO;GACE;GACJ;GACL;GACA,iBAAiB,OAAO,WAAiC;IACvD,MAAM,UAAU,MAAM,eAAe,QAAQ,QAAQ;IACrD,SAAS,qBAAqB,OAAO;IACrC,OAAO;GACT;GACmB;GACnB,sBAAsB,kBAAkB,eAAe,OAAO;GAC9D,4BAA4B,kBAA0B,6BAA6B,QAAQ,sBAAsB,aAAa,CAAC,CAAC,GAAG,CAAC;EACtI;CACF;CAEA,OAAO;EACL,IAAI,UAAU;GACZ,IAAI,CAAC,SAAS,MAAM,IAAI,MAAM,yFAAyF;GACvH,OAAO;EACT;EACA,IAAI,WAAW;GACb,OAAO;EACT;EACA,YAAY;EACZ;CACF;AACF"}