diff --git a/package.json b/package.json index 5efbbedbd..c77ecbf97 100644 --- a/package.json +++ b/package.json @@ -9,8 +9,8 @@ "lint:check": "biome check . --verbose", "lint:unsafe": "biome check . --apply-unsafe", "prepare": "husky install", - "sort-package-json": "sort-package-json 'package.json' 'packages/*/package.json' 'examples/*/package.json'", - "sort-package-json:check": "sort-package-json 'package.json' 'packages/*/package.json' 'examples/*/package.json' --check", + "sort-package-json": "sort-package-json 'package.json' '{packages,wallets,examples}/*/package.json'", + "sort-package-json:check": "sort-package-json 'package.json' '{packages,wallets,examples}/*/package.json' --check", "test": "turbo test" }, "lint-staged": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cfb7249cb..f1a8d6ab1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -124,7 +124,14 @@ importers: packages/tsconfig: {} wallets/metamask: + dependencies: + core: + specifier: workspace:* + version: link:../../packages/core devDependencies: + '@vitest/coverage-v8': + specifier: 1.0.0-beta.0 + version: 1.0.0-beta.0(vitest@0.34.6) rimraf: specifier: ^5.0.1 version: 5.0.1 @@ -137,6 +144,9 @@ importers: typescript: specifier: ^5.2.2 version: 5.2.2 + vitest: + specifier: ^0.34.6 + version: 0.34.6 wallets/metamask-legacy: dependencies: diff --git a/wallets/metamask/package.json b/wallets/metamask/package.json index c7f77636c..435476556 100644 --- a/wallets/metamask/package.json +++ b/wallets/metamask/package.json @@ -18,15 +18,23 @@ ], "scripts": { "build": "pnpm run clean && pnpm run build:dist && pnpm run build:types", - "build:dist": "tsup", - "build:types": "tsc --emitDeclarationOnly", + "build:dist": "tsup --tsconfig tsconfig.build.json", + "build:types": "tsc --emitDeclarationOnly --project tsconfig.build.json", "clean": "rimraf dist types", + "test": "vitest run", + "test:coverage": "vitest run --coverage", + "test:watch": "vitest watch", "types:check": "tsc --noEmit" }, + "dependencies": { + "core": "workspace:*" + }, "devDependencies": { + "@vitest/coverage-v8": "1.0.0-beta.0", "rimraf": "^5.0.1", "tsconfig": "workspace:*", "tsup": "^7.2.0", - "typescript": "^5.2.2" + "typescript": "^5.2.2", + "vitest": "^0.34.6" } } diff --git a/wallets/metamask/src/prepareExtension.ts b/wallets/metamask/src/prepareExtension.ts new file mode 100644 index 000000000..98f2ec8fd --- /dev/null +++ b/wallets/metamask/src/prepareExtension.ts @@ -0,0 +1,20 @@ +import { downloadFile, ensureCacheDirExists, unzipArchive } from 'core' + +export const DEFAULT_METAMASK_VERSION = '10.25.0' +export const EXTENSION_DOWNLOAD_URL = `https://github.com/MetaMask/metamask-extension/releases/download/v${DEFAULT_METAMASK_VERSION}/metamask-chrome-${DEFAULT_METAMASK_VERSION}.zip` + +export async function prepareExtension() { + const cacheDirPath = ensureCacheDirExists() + + const downloadResult = await downloadFile({ + url: EXTENSION_DOWNLOAD_URL, + outputDir: cacheDirPath, + fileName: `metamask-chrome-${DEFAULT_METAMASK_VERSION}.zip` + }) + + const unzipResult = await unzipArchive({ + archivePath: downloadResult.filePath + }) + + return unzipResult.outputPath +} diff --git a/wallets/metamask/test/prepareExtension.test.ts b/wallets/metamask/test/prepareExtension.test.ts new file mode 100644 index 000000000..584f2d1e9 --- /dev/null +++ b/wallets/metamask/test/prepareExtension.test.ts @@ -0,0 +1,82 @@ +import * as core from 'core' +import { afterAll, afterEach, describe, expect, it, vi } from 'vitest' +import { + DEFAULT_METAMASK_VERSION, + EXTENSION_DOWNLOAD_URL, + prepareExtension +} from '../src/prepareExtension' + +const MOCK_CACHE_DIR_PATH = 'mockCacheDirPath' +const MOCK_EXTENSION_ARCHIVE_PATH = 'mockExtensionArchivePath' +const MOCK_EXTENSION_FINAL_PATH = 'mockExtensionFinalPath' + +vi.mock('core', async () => { + return { + default: vi.fn(), + ensureCacheDirExists: vi.fn().mockImplementation(() => MOCK_CACHE_DIR_PATH), + downloadFile: vi.fn().mockImplementation(() => { + return { + filePath: 'mockExtensionArchivePath' + } + }), + unzipArchive: vi.fn().mockImplementation(() => { + return { + outputPath: MOCK_EXTENSION_FINAL_PATH + } + }) + } +}) + +describe('prepareExtension', () => { + afterEach(() => { + vi.clearAllMocks() + }) + + afterAll(() => { + vi.resetAllMocks() + }) + + it('creates cache directory', async () => { + const ensureCacheDirExistsSpy = vi.spyOn(core, 'ensureCacheDirExists') + + await prepareExtension() + + expect(ensureCacheDirExistsSpy).toHaveBeenCalledOnce() + expect(ensureCacheDirExistsSpy).toReturnWith(MOCK_CACHE_DIR_PATH) + }) + + it('downloads MetaMask extension archive', async () => { + const downloadFileSpy = vi.spyOn(core, 'downloadFile') + + await prepareExtension() + + expect(downloadFileSpy).toHaveBeenCalledOnce() + expect(downloadFileSpy).toHaveBeenCalledWith({ + url: EXTENSION_DOWNLOAD_URL, + outputDir: MOCK_CACHE_DIR_PATH, + fileName: `metamask-chrome-${DEFAULT_METAMASK_VERSION}.zip` + }) + expect(downloadFileSpy).toReturnWith({ + filePath: MOCK_EXTENSION_ARCHIVE_PATH + }) + }) + + it('unzips MetaMask extension archive', async () => { + const unzipArchiveSpy = vi.spyOn(core, 'unzipArchive') + + await prepareExtension() + + expect(unzipArchiveSpy).toHaveBeenCalledOnce() + expect(unzipArchiveSpy).toHaveBeenCalledWith({ + archivePath: MOCK_EXTENSION_ARCHIVE_PATH + }) + expect(unzipArchiveSpy).toReturnWith({ + outputPath: MOCK_EXTENSION_FINAL_PATH + }) + }) + + it('returns correct unzipped extension path', async () => { + const extensionPath = await prepareExtension() + expect(extensionPath).toEqual(MOCK_EXTENSION_FINAL_PATH) + }) +}) diff --git a/wallets/metamask/tsconfig.build.json b/wallets/metamask/tsconfig.build.json new file mode 100644 index 000000000..177eb2938 --- /dev/null +++ b/wallets/metamask/tsconfig.build.json @@ -0,0 +1,11 @@ +{ + "extends": "tsconfig/base.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "types", + "declaration": true, + "sourceMap": true, + "declarationMap": true + }, + "include": ["src"] +} diff --git a/wallets/metamask/tsconfig.json b/wallets/metamask/tsconfig.json index 8d8c8bf96..f210b2a2f 100644 --- a/wallets/metamask/tsconfig.json +++ b/wallets/metamask/tsconfig.json @@ -1,13 +1,10 @@ { - "extends": "tsconfig/base.json", + "extends": "./tsconfig.build.json", "compilerOptions": { - "rootDir": "src", - "outDir": "types", - "declaration": true, - "sourceMap": true, - "declarationMap": true + "rootDir": "." }, "include": [ - "src" + "src", + "test" ] }