Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» feat(core): Throw errors during cache creation (#1040)
Browse files Browse the repository at this point in the history
  • Loading branch information
duckception authored Dec 14, 2023
1 parent 1102170 commit bc82498
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 17 deletions.
13 changes: 11 additions & 2 deletions packages/core/src/utils/createCacheForWalletSetupFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`]
Expand All @@ -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.
Expand Down
10 changes: 8 additions & 2 deletions packages/core/src/utils/triggerCacheCreation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> = createCacheForWalletSetupFunction(extensionPath, cachePath, setupFunction)
const createCachePromise: Promise<void> = createCacheForWalletSetupFunction(
extensionPath,
cachePath,
setupFunction,
fileNameWithCorrectExtension
)
cacheCreationPromises.push(createCachePromise)
}

Expand Down
16 changes: 14 additions & 2 deletions packages/core/test/utils/createCacheForWalletSetupFunction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down
29 changes: 18 additions & 11 deletions packages/core/test/utils/triggerCacheCreation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,25 @@ describe('triggerCacheCreation', () => {
const setupFunctions = new Map<string, { fileName: string; setupFunction: WalletSetupFunction }>()

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<typeof prepareSetupFunctions>,
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
)
}

Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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')
})
})

Expand All @@ -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')
})
})
})

0 comments on commit bc82498

Please sign in to comment.