-
-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(metamask): Add
prepareExtension
function (#910)
- Loading branch information
1 parent
815dc96
commit da70e59
Showing
7 changed files
with
140 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"extends": "tsconfig/base.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "types", | ||
"declaration": true, | ||
"sourceMap": true, | ||
"declarationMap": true | ||
}, | ||
"include": ["src"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
] | ||
} |