From 1bd65d29d06d06059d1dd426f26b16b016129ae1 Mon Sep 17 00:00:00 2001 From: Daniel Izdebski Date: Tue, 24 Oct 2023 23:59:13 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(core):=20Add=20`importWalletSe?= =?UTF-8?q?tupFile`=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/package.json | 3 +- .../core/src/utils/getWalletSetupFuncHash.ts | 2 +- .../core/src/utils/importWalletSetupFile.ts | 32 ++++++++++++ .../test/utils/importWalletSetupFile.test.ts | 52 +++++++++++++++++++ pnpm-lock.yaml | 3 ++ 5 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 packages/core/src/utils/importWalletSetupFile.ts create mode 100644 packages/core/test/utils/importWalletSetupFile.test.ts diff --git a/packages/core/package.json b/packages/core/package.json index 78563ecfe..27ac74973 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -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", diff --git a/packages/core/src/utils/getWalletSetupFuncHash.ts b/packages/core/src/utils/getWalletSetupFuncHash.ts index 521d42618..3a89ceab9 100644 --- a/packages/core/src/utils/getWalletSetupFuncHash.ts +++ b/packages/core/src/utils/getWalletSetupFuncHash.ts @@ -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 export function getWalletSetupFuncHash(walletSetupFunc: AnyFunction) { // This transformation is necessary because a user could end up using a different execution engine than Playwright. diff --git a/packages/core/src/utils/importWalletSetupFile.ts b/packages/core/src/utils/importWalletSetupFile.ts new file mode 100644 index 000000000..498e86e65 --- /dev/null +++ b/packages/core/src/utils/importWalletSetupFile.ts @@ -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 + } +} diff --git a/packages/core/test/utils/importWalletSetupFile.test.ts b/packages/core/test/utils/importWalletSetupFile.test.ts new file mode 100644 index 000000000..aac0eb2f0 --- /dev/null +++ b/packages/core/test/utils/importWalletSetupFile.test.ts @@ -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) + }) + }) +}) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 90e333dd2..ee6fbd99e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -64,6 +64,9 @@ importers: unzipper: specifier: ^0.10.14 version: 0.10.14 + zod: + specifier: ^3.22.4 + version: 3.22.4 devDependencies: '@types/archiver': specifier: ^5.3.3