Skip to content

Commit

Permalink
✨ feat(core): Add importWalletSetupFile function
Browse files Browse the repository at this point in the history
  • Loading branch information
duckception committed Oct 24, 2023
1 parent 4c83abf commit 1bd65d2
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"axios": "^1.4.0",
"esbuild": "^0.19.5",
"fs-extra": "^11.1.1",
"unzipper": "^0.10.14"
"unzipper": "^0.10.14",
"zod": "^3.22.4"
},
"devDependencies": {
"@types/archiver": "^5.3.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utils/getWalletSetupFuncHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import esbuild from 'esbuild'
export const WALLET_SETUP_FUNC_HASH_LENGTH = 10

// biome-ignore lint/suspicious/noExplicitAny: any type here is intentional
type AnyFunction = (...args: any) => any
type AnyFunction = (...args: any) => Promise<any>

export function getWalletSetupFuncHash(walletSetupFunc: AnyFunction) {
// This transformation is necessary because a user could end up using a different execution engine than Playwright.
Expand Down
32 changes: 32 additions & 0 deletions packages/core/src/utils/importWalletSetupFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { z } from 'zod'
import type { WalletSetupFunction } from '../defineWalletSetup'

// TODO: Add hash length validation.
const WalletSetupModule = z.object({
default: z.object({
hash: z.string(),
fn: z.function().returns(z.promise(z.void()))
})
})

export async function importWalletSetupFile(walletSetupFilePath: string) {
const walletSetupModule = await import(walletSetupFilePath)

const result = WalletSetupModule.safeParse(walletSetupModule)
if (!result.success) {
throw new Error(
[
`[ImportWalletSetupFile] Invalid wallet setup function at ${walletSetupFilePath}`,
'Remember that all wallet setup files must export the wallet setup function as a default export!'
].join('\n')
)
}

const { hash, fn } = result.data.default

// TODO: Can we somehow validate this function type with Zod?
return {
hash,
fn: fn as WalletSetupFunction
}
}
52 changes: 52 additions & 0 deletions packages/core/test/utils/importWalletSetupFile.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { afterAll, describe, expect, it, vi } from 'vitest'
import { importWalletSetupFile } from '../../src/utils/importWalletSetupFile'

vi.mock('./valid.setup.ts', async () => {
return {
default: {
hash: 'bca3f22838f317ed9e87',
fn: async () => {
return
}
}
}
})

vi.mock('./invalid.setup.ts', async () => {
return {
default: {
duck: 'Quack! 🦆'
}
}
})

describe('importWalletSetupFile', () => {
afterAll(() => {
vi.resetAllMocks()
})

// This error is from `await import`.
it('throws if the target file does not exist', async () => {
const nonExistentFilePath = './non-existent-file.ts'
await expect(importWalletSetupFile(nonExistentFilePath)).rejects.toThrowError(
`Failed to load url ${nonExistentFilePath}`
)
})

it('throws if the target file is not a valid wallet setup file', async () => {
const invalidFilePath = './invalid.setup.ts'
await expect(importWalletSetupFile(invalidFilePath)).rejects.toThrowError(
`[ImportWalletSetupFile] Invalid wallet setup function at ${invalidFilePath}`
)
})

it('returns the hash and function of a valid wallet setup file', async () => {
const validFilePath = './valid.setup.ts'
const result = await importWalletSetupFile(validFilePath)

expect(result).toEqual({
hash: 'bca3f22838f317ed9e87',
fn: expect.any(Function)
})
})
})
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1bd65d2

Please sign in to comment.