Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
Seroxdesign committed Jun 26, 2024
1 parent 4e32ab5 commit bfac707
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 32 deletions.
1 change: 1 addition & 0 deletions wallets/metamask/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@synthetixio/synpress-tsconfig": "0.0.1-alpha.7",
"@types/fs-extra": "11.0.4",
"@types/node": "20.11.17",
"@types/unzipper": "0.10.9",
"@vitest/coverage-v8": "1.2.2",
"rimraf": "5.0.5",
"tsup": "8.0.2",
Expand Down
9 changes: 9 additions & 0 deletions wallets/metamask/src/fixture-actions/importForFixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Page } from '@playwright/test'
import { MetaMask } from '..'

export async function importForFixtures(page: Page, seedPhrase: string, password: string, extensionId: string) {
const metamask = new MetaMask(page.context(), page, password, extensionId)

await metamask.importWallet(seedPhrase)

}
7 changes: 4 additions & 3 deletions wallets/metamask/src/fixtures/metaMaskFixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { type Anvil, type CreateAnvilOptions, createPool } from '@viem/anvil'
import fs from 'fs-extra'
import { cachelessSetupMetaMask } from './noCacheMetaMaskFixtures'
import { persistLocalStorage } from '../fixture-actions/persistLocalStorage'
import { importForFixtures } from '../fixture-actions/importForFixtures'
import { SEED_PHRASE } from '../../test/wallet-setup/basic.setup'

const USECACHE = false;
Expand Down Expand Up @@ -87,7 +88,7 @@ export const metaMaskFixtures = (walletSetup: ReturnType<typeof defineWalletSetu

}
if (!USECACHE) {
context = await cachelessSetupMetaMask(SEED_PHRASE, walletSetup.walletPassword, '11.9.1')
context = await cachelessSetupMetaMask()
}
if (!context) return
// TODO: This should be stored in a store to speed up the tests.
Expand All @@ -98,8 +99,8 @@ export const metaMaskFixtures = (walletSetup: ReturnType<typeof defineWalletSetu
_metamaskPage = context.pages()[0] as Page

await _metamaskPage.goto(`chrome-extension://${extensionId}/home.html`)

await unlockForFixture(_metamaskPage, walletSetup.walletPassword)
if (!USECACHE) await importForFixtures(_metamaskPage, SEED_PHRASE, walletSetup.walletPassword, extensionId)
if (USECACHE) await unlockForFixture(_metamaskPage, walletSetup.walletPassword)

await use(context)

Expand Down
42 changes: 13 additions & 29 deletions wallets/metamask/src/fixtures/noCacheMetaMaskFixtures.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { chromium, type BrowserContext, type Page } from '@playwright/test';
import { MetaMask } from '../MetaMask';
import { unlockForFixture } from '../fixture-actions/unlockForFixture';
import { chromium, type BrowserContext } from '@playwright/test';
import fs from 'fs-extra';
import path from 'node:path';
import axios from 'axios';
import unzipper from 'unzipper';
import { getExtensionId } from '../fixture-actions';

// Define constants for MetaMask
const DEFAULT_METAMASK_VERSION = '11.9.1';
const EXTENSION_DOWNLOAD_URL = `https://github.com/MetaMask/metamask-extension/releases/download/v${DEFAULT_METAMASK_VERSION}/metamask-chrome-${DEFAULT_METAMASK_VERSION}.zip`;

Expand All @@ -27,7 +23,6 @@ async function prepareMetaMask(version: string = DEFAULT_METAMASK_VERSION): Prom
return outputPath;
}

// Function to download and unzip the MetaMask extension
async function downloadAndExtract(url: string, destination: string): Promise<void> {
const response = await axios.get(url, { responseType: 'stream' });
const writer = fs.createWriteStream(destination);
Expand All @@ -37,61 +32,50 @@ async function downloadAndExtract(url: string, destination: string): Promise<voi
await unzipArchive(destination);
}


async function unzipArchive(archivePath: string): Promise<void> {
const archiveFileExtension = path.extname(archivePath);
const outputPath = archivePath.replace(archiveFileExtension, '');

await fs.ensureDir(outputPath);

try {
await new Promise((resolve, reject) => {
await new Promise<void>((resolve, reject) => {
fs.createReadStream(archivePath)
.pipe(unzipper.Parse())
.on('entry', (entry: any) => {
const fileName = entry.path;
const type = entry.type as 'Directory' | 'File';

if (type === 'Directory') {
fs.mkdirSync(path.join(outputPath, fileName), { recursive: true });
return;
}

if (type === 'File') {
entry.pipe(fs.createWriteStream(path.join(outputPath, fileName)));
}
})
.on('finish', () => resolve())
.on('finish', () => resolve(void 0))
.on('error', (error: Error) => reject(error));
});
} catch (error: any) {
// Handle errors here
console.error(`[unzipArchive] Error unzipping archive: ${error.message}`);
// You might want to implement retries or other error handling logic
throw error; // Re-throw the error to stop execution
throw error;
}
}
// The main function to set up MetaMask without caching

export async function cachelessSetupMetaMask(
// seedPhrase: string,
// password: string,
metamaskVersion?: string
): Promise<BrowserContext> {
const metamaskPath = await prepareMetaMask(metamaskVersion || DEFAULT_METAMASK_VERSION);
const browserArgs = [`--load-extension=${metamaskPath}`, `--disable-extensions-except=${metamaskPath}`];
console.log(metamaskPath)
if (process.env.HEADLESS) {
browserArgs.push('--headless=new')
}
const context = await chromium.launchPersistentContext('', {
args: [`--load-extension=${metamaskPath}`, `--disable-extensions-except=${metamaskPath}`]
headless: false,
args: browserArgs,
});

// const extensionId = await getExtensionId(context, 'MetaMask');

// console.log(`[MetaMask] Extension ID: ${extensionId}`);
// const metamaskPage = await context.newPage();
// await metamaskPage.goto(`chrome-extension://${extensionId}/home.html`, { waitUntil: 'networkidle' });

// const metamask = new MetaMask(context, metamaskPage, password);
// await metamask.importWallet(seedPhrase);
// await unlockForFixture(metamaskPage, password);

return context;
}

0 comments on commit bfac707

Please sign in to comment.