diff --git a/packages/core/src/utils/createCacheForWalletSetupFunction.ts b/packages/core/src/utils/createCacheForWalletSetupFunction.ts index d73baac3c..021bbbe07 100644 --- a/packages/core/src/utils/createCacheForWalletSetupFunction.ts +++ b/packages/core/src/utils/createCacheForWalletSetupFunction.ts @@ -8,7 +8,8 @@ const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)) export async function createCacheForWalletSetupFunction( extensionPath: string, contextCacheDirPath: string, - walletSetup: WalletSetupFunction + walletSetup: WalletSetupFunction, + fileName: string ) { // TODO: Extract & Make a constant. const browserArgs = [`--disable-extensions-except=${extensionPath}`, `--load-extension=${extensionPath}`] @@ -24,7 +25,15 @@ export async function createCacheForWalletSetupFunction( const extensionPage = await waitForExtensionOnLoadPage(context) - await walletSetup(context, extensionPage) + try { + await walletSetup(context, extensionPage) + } catch (e) { + throw new Error( + `[CORE] Encountered an error while executing wallet setup function from file ${fileName}. Error message: ${ + (e as Error).message + }` + ) + } // We sleep here to give the browser enough time to save the context to the disk. await sleep(3000) // TODO: Extract & Make this timeout configurable. diff --git a/packages/core/src/utils/triggerCacheCreation.ts b/packages/core/src/utils/triggerCacheCreation.ts index 524bd459e..669115c17 100644 --- a/packages/core/src/utils/triggerCacheCreation.ts +++ b/packages/core/src/utils/triggerCacheCreation.ts @@ -26,10 +26,16 @@ export async function triggerCacheCreation( await fs.remove(cachePath) } - console.log(`Triggering cache creation for: ${funcHash} (${fileName.replace(/\.(js|ts)$/, '.{js,ts}')})`) + const fileNameWithCorrectExtension = fileName.replace(/\.(js|ts)$/, '.{js,ts}') + console.log(`Triggering cache creation for: ${funcHash} (${fileNameWithCorrectExtension})`) // We're not inferring the return type here to make sure we don't accidentally await the function. - const createCachePromise: Promise = createCacheForWalletSetupFunction(extensionPath, cachePath, setupFunction) + const createCachePromise: Promise = createCacheForWalletSetupFunction( + extensionPath, + cachePath, + setupFunction, + fileNameWithCorrectExtension + ) cacheCreationPromises.push(createCachePromise) } diff --git a/packages/core/test/utils/createCacheForWalletSetupFunction.test.ts b/packages/core/test/utils/createCacheForWalletSetupFunction.test.ts index bdf1da6ad..3c3ec1d89 100644 --- a/packages/core/test/utils/createCacheForWalletSetupFunction.test.ts +++ b/packages/core/test/utils/createCacheForWalletSetupFunction.test.ts @@ -34,9 +34,10 @@ vi.mock('../../src/utils/waitForExtensionOnLoadPage', async () => { describe('createCacheForWalletSetupFunction', () => { const launchPersistentContextSpy = vi.spyOn(chromium, 'launchPersistentContext') const walletSetup = vi.fn() + const fileName = 'test.ts' const runCreateCacheForWalletSetupFunction = async () => { - const promise = createCacheForWalletSetupFunction(EXTENSION_PATH, CONTEXT_PATH, walletSetup) + const promise = createCacheForWalletSetupFunction(EXTENSION_PATH, CONTEXT_PATH, walletSetup, fileName) await vi.runAllTimersAsync() await promise } @@ -100,6 +101,17 @@ describe('createCacheForWalletSetupFunction', () => { expect(walletSetup).toHaveBeenCalledWith(context, extensionPage) }) + it('throws an error if walletSetup throws one', async () => { + const errorMessage = 'A duck has thrown a tantrum! 🦆' + walletSetup.mockRejectedValueOnce(new Error(errorMessage)) + + const promise = createCacheForWalletSetupFunction(EXTENSION_PATH, CONTEXT_PATH, walletSetup, fileName) + + await expect(promise).rejects.toThrowError( + `[CORE] Encountered an error while executing wallet setup function from file ${fileName}. Error message: ${errorMessage}` + ) + }) + it('closes context', async () => { const closeContext = vi.fn() // biome-ignore lint/suspicious/noExplicitAny: any type here is intentional @@ -117,7 +129,7 @@ describe('createCacheForWalletSetupFunction', () => { const setTimeoutSpy = vi.spyOn(global, 'setTimeout') - const promise = createCacheForWalletSetupFunction(EXTENSION_PATH, CONTEXT_PATH, walletSetup) + const promise = createCacheForWalletSetupFunction(EXTENSION_PATH, CONTEXT_PATH, walletSetup, fileName) // Verify that nothing was run yet. expect(walletSetup).not.toHaveBeenCalled() diff --git a/packages/core/test/utils/triggerCacheCreation.test.ts b/packages/core/test/utils/triggerCacheCreation.test.ts index a54ba138d..f25ed0fec 100644 --- a/packages/core/test/utils/triggerCacheCreation.test.ts +++ b/packages/core/test/utils/triggerCacheCreation.test.ts @@ -52,18 +52,25 @@ describe('triggerCacheCreation', () => { const setupFunctions = new Map() for (const hash of hashes) { - setupFunctions.set(hash, { fileName: path.join(ROOT_DIR, hash), setupFunction: testSetupFunction }) + setupFunctions.set(hash, { fileName: path.join(ROOT_DIR, `${hash}.ts`), setupFunction: testSetupFunction }) } return setupFunctions } - function expectCreateCacheForWalletSetupFunction(n: number, contextCacheDirName: string) { + function expectCreateCacheForWalletSetupFunction( + n: number, + setupFunctions: ReturnType, + hash: string + ) { + const fileNameWithCorrectExtension = setupFunctions.get(hash)?.fileName?.replace(/\.(js|ts)$/, '.{js,ts}') + expect(createCacheForWalletSetupFunctionSpy).toHaveBeenNthCalledWith( n, EXTENSION_PATH, - path.join(ROOT_DIR, contextCacheDirName), - testSetupFunction + path.join(ROOT_DIR, hash), + testSetupFunction, + fileNameWithCorrectExtension ) } @@ -101,8 +108,8 @@ describe('triggerCacheCreation', () => { await triggerCacheCreation(setupFunctions, downloadExtension, false) expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(2) - expectCreateCacheForWalletSetupFunction(1, 'hash1') - expectCreateCacheForWalletSetupFunction(2, 'hash2') + expectCreateCacheForWalletSetupFunction(1, setupFunctions, 'hash1') + expectCreateCacheForWalletSetupFunction(2, setupFunctions, 'hash2') }) it('checks if cache already exists for each entry', async () => { @@ -136,8 +143,8 @@ describe('triggerCacheCreation', () => { expect(promises).toHaveLength(2) expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(2) - expectCreateCacheForWalletSetupFunction(1, 'hash1') - expectCreateCacheForWalletSetupFunction(2, 'hash3') + expectCreateCacheForWalletSetupFunction(1, setupFunctions, 'hash1') + expectCreateCacheForWalletSetupFunction(2, setupFunctions, 'hash3') }) }) @@ -164,9 +171,9 @@ describe('triggerCacheCreation', () => { expect(promises).toHaveLength(3) expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(3) - expectCreateCacheForWalletSetupFunction(1, 'hash1') - expectCreateCacheForWalletSetupFunction(2, 'hash2') - expectCreateCacheForWalletSetupFunction(3, 'hash3') + expectCreateCacheForWalletSetupFunction(1, setupFunctions, 'hash1') + expectCreateCacheForWalletSetupFunction(2, setupFunctions, 'hash2') + expectCreateCacheForWalletSetupFunction(3, setupFunctions, 'hash3') }) }) })