|
| 1 | +import { execSync, spawn } from 'child_process' |
| 2 | +import * as fs from 'node:fs' |
| 3 | +import * as path from 'node:path' |
| 4 | +import * as os from 'node:os' |
| 5 | + |
| 6 | +const BOOT_TIMEOUT_MS = 300_000 |
| 7 | +const BOOT_POLL_INTERVAL_MS = 2_000 |
| 8 | + |
| 9 | +const DEV_SERVER_PORT = 8080 |
| 10 | +// Port range matching httpServers.ts |
| 11 | +const PORT_RANGE_START = 9200 |
| 12 | +const PORT_RANGE_END = 9400 |
| 13 | + |
| 14 | +// eslint-disable-next-line import/no-default-export |
| 15 | +export default async function globalSetup() { |
| 16 | + console.log('Starting Android emulator...') |
| 17 | + |
| 18 | + const emulatorProcess = spawn( |
| 19 | + 'emulator', |
| 20 | + ['-avd', 'test_device', '-no-window', '-no-audio', '-no-snapshot', '-no-boot-anim', '-gpu', 'auto'], |
| 21 | + { |
| 22 | + stdio: ['ignore', 'ignore', 'ignore'], |
| 23 | + detached: true, |
| 24 | + } |
| 25 | + ) |
| 26 | + emulatorProcess.unref() |
| 27 | + |
| 28 | + await waitForBoot() |
| 29 | + setupAdbReverse() |
| 30 | + await installChromium() |
| 31 | + |
| 32 | + process.env.ANDROID_E2E = 'true' |
| 33 | +} |
| 34 | + |
| 35 | +async function waitForBoot() { |
| 36 | + const startTime = Date.now() |
| 37 | + |
| 38 | + while (Date.now() - startTime < BOOT_TIMEOUT_MS) { |
| 39 | + try { |
| 40 | + const result = execSync('adb shell getprop sys.boot_completed', { encoding: 'utf-8', timeout: 5_000 }).trim() |
| 41 | + if (result === '1') { |
| 42 | + console.log(`Emulator booted in ${Math.round((Date.now() - startTime) / 1000)}s`) |
| 43 | + return |
| 44 | + } |
| 45 | + } catch { |
| 46 | + // adb not yet connected, keep polling |
| 47 | + } |
| 48 | + await new Promise((resolve) => setTimeout(resolve, BOOT_POLL_INTERVAL_MS)) |
| 49 | + } |
| 50 | + |
| 51 | + throw new Error(`Emulator failed to boot within ${BOOT_TIMEOUT_MS / 1000}s`) |
| 52 | +} |
| 53 | + |
| 54 | +function setupAdbReverse() { |
| 55 | + console.log('Setting up adb reverse port forwarding...') |
| 56 | + |
| 57 | + // Forward dev server port |
| 58 | + execSync(`adb reverse tcp:${DEV_SERVER_PORT} tcp:${DEV_SERVER_PORT}`) |
| 59 | + |
| 60 | + // Forward test server port range |
| 61 | + for (let port = PORT_RANGE_START; port <= PORT_RANGE_END; port++) { |
| 62 | + execSync(`adb reverse tcp:${port} tcp:${port}`) |
| 63 | + } |
| 64 | + |
| 65 | + console.log(`Forwarded ports: ${DEV_SERVER_PORT}, ${PORT_RANGE_START}-${PORT_RANGE_END}`) |
| 66 | +} |
| 67 | + |
| 68 | +async function installChromium() { |
| 69 | + // The system Chrome on the emulator is v113, which is too old for many web APIs |
| 70 | + // (LoAf, modern resource timing, etc.). Install a recent Chromium from snapshots. |
| 71 | + console.log('Installing Chromium on emulator...') |
| 72 | + |
| 73 | + try { |
| 74 | + // Get the latest Chromium snapshot revision for Android ARM64 |
| 75 | + const revisionResponse = await fetch( |
| 76 | + 'https://storage.googleapis.com/chromium-browser-snapshots/Android_Arm64/LAST_CHANGE' |
| 77 | + ) |
| 78 | + const revision = (await revisionResponse.text()).trim() |
| 79 | + console.log(`Latest Chromium ARM64 snapshot revision: ${revision}`) |
| 80 | + |
| 81 | + // Download the Chromium Android ARM64 build |
| 82 | + const downloadUrl = `https://storage.googleapis.com/chromium-browser-snapshots/Android_Arm64/${revision}/chrome-android.zip` |
| 83 | + console.log(`Downloading from ${downloadUrl}`) |
| 84 | + |
| 85 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'chromium-android-')) |
| 86 | + const zipPath = path.join(tmpDir, 'chrome-android.zip') |
| 87 | + |
| 88 | + const downloadResponse = await fetch(downloadUrl) |
| 89 | + if (!downloadResponse.ok) { |
| 90 | + console.log(`Download failed: ${downloadResponse.status}`) |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + const buffer = Buffer.from(await downloadResponse.arrayBuffer()) |
| 95 | + fs.writeFileSync(zipPath, buffer) |
| 96 | + console.log(`Downloaded ${(buffer.length / 1024 / 1024).toFixed(1)}MB`) |
| 97 | + |
| 98 | + // Extract the zip |
| 99 | + execSync(`unzip -o "${zipPath}" -d "${tmpDir}"`, { timeout: 60_000 }) |
| 100 | + |
| 101 | + // Find APKs |
| 102 | + const apks = execSync(`find "${tmpDir}" -name "*.apk"`, { encoding: 'utf-8', timeout: 5_000 }) |
| 103 | + .trim() |
| 104 | + .split('\n') |
| 105 | + .filter(Boolean) |
| 106 | + |
| 107 | + console.log(`Found APKs: ${apks.map((a) => path.basename(a)).join(', ')}`) |
| 108 | + |
| 109 | + // Install ChromePublic.apk (the main Chromium browser) |
| 110 | + const chromeApk = apks.find((a) => path.basename(a) === 'ChromePublic.apk') |
| 111 | + if (!chromeApk) { |
| 112 | + console.log('ChromePublic.apk not found in download') |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + const result = execSync(`adb install -r -d "${chromeApk}"`, { encoding: 'utf-8', timeout: 120_000 }) |
| 117 | + console.log(`Install result: ${result.trim()}`) |
| 118 | + |
| 119 | + // Log the installed Chromium version |
| 120 | + const versionInfo = execSync('adb shell dumpsys package org.chromium.chrome | grep versionName', { |
| 121 | + encoding: 'utf-8', |
| 122 | + timeout: 5_000, |
| 123 | + }).trim() |
| 124 | + console.log(`Chromium installed: ${versionInfo}`) |
| 125 | + |
| 126 | + // Cleanup |
| 127 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 128 | + } catch (error) { |
| 129 | + console.log('Chromium install failed (non-fatal):', error) |
| 130 | + } |
| 131 | +} |
0 commit comments