|
| 1 | +import { _electron as electron } from '@playwright/test'; |
| 2 | +import fs from 'node:fs/promises'; |
| 3 | +import os from 'node:os'; |
| 4 | +import path from 'node:path'; |
| 5 | +import type { ElectronApplication, Page } from '@playwright/test'; |
| 6 | +import type { CameraConfig } from '../../../electron/types'; |
| 7 | +import type { TestFixtures } from '../../../electron/testing/fixtures'; |
| 8 | + |
| 9 | +const projectRoot = path.resolve(process.cwd()); |
| 10 | + |
| 11 | +export interface LaunchElectronAppOptions { |
| 12 | + fixtures: TestFixtures; |
| 13 | + cameras?: CameraConfig[]; |
| 14 | +} |
| 15 | + |
| 16 | +export interface LaunchedElectronApp { |
| 17 | + app: ElectronApplication; |
| 18 | + window: Page; |
| 19 | + userDataDir: string; |
| 20 | +} |
| 21 | + |
| 22 | +async function readTail(filePath: string, maxBytes: number): Promise<string | null> { |
| 23 | + try { |
| 24 | + const content = await fs.readFile(filePath, 'utf8'); |
| 25 | + if (content.length <= maxBytes) return content; |
| 26 | + return content.slice(-maxBytes); |
| 27 | + } catch { |
| 28 | + return null; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +function formatLaunchFailure(error: unknown, userDataDir: string): string { |
| 33 | + const baseError = error instanceof Error ? error.stack || error.message : String(error); |
| 34 | + const runtimeLogPath = path.join(userDataDir, 'vigilatus.log'); |
| 35 | + const bootstrapLogPath = path.join(userDataDir, 'vigilatus-bootstrap.log'); |
| 36 | + const logHeader = [ |
| 37 | + `Electron launch failed`, |
| 38 | + `userDataDir: ${userDataDir}`, |
| 39 | + `logPath: ${runtimeLogPath}`, |
| 40 | + `bootstrapLogPath: ${bootstrapLogPath}`, |
| 41 | + ].join('\n'); |
| 42 | + |
| 43 | + return `${logHeader}\n\n${baseError}`; |
| 44 | +} |
| 45 | + |
| 46 | +export async function launchElectronApp(options: LaunchElectronAppOptions): Promise<LaunchedElectronApp> { |
| 47 | + const userDataDir = await fs.mkdtemp(path.join(os.tmpdir(), 'vigilatus-e2e-')); |
| 48 | + const fixturePath = path.join(userDataDir, 'fixtures.json'); |
| 49 | + |
| 50 | + await fs.writeFile(fixturePath, JSON.stringify(options.fixtures, null, 2), 'utf8'); |
| 51 | + |
| 52 | + if (options.cameras) { |
| 53 | + await fs.writeFile( |
| 54 | + path.join(userDataDir, 'cameras.json'), |
| 55 | + JSON.stringify( |
| 56 | + { |
| 57 | + cameras: options.cameras, |
| 58 | + uiDisplay: { |
| 59 | + previews: true, |
| 60 | + timeline: true, |
| 61 | + previewPosition: 'right', |
| 62 | + }, |
| 63 | + }, |
| 64 | + null, |
| 65 | + 2, |
| 66 | + ), |
| 67 | + 'utf8', |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + const isCI = Boolean(process.env.CI); |
| 72 | + const isLinux = process.platform === 'linux'; |
| 73 | + |
| 74 | + const launchArgs = [projectRoot]; |
| 75 | + if (isLinux) { |
| 76 | + launchArgs.push('--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage'); |
| 77 | + } |
| 78 | + |
| 79 | + try { |
| 80 | + const app = await electron.launch({ |
| 81 | + args: launchArgs, |
| 82 | + env: { |
| 83 | + ...process.env, |
| 84 | + VIGILATUS_USER_DATA_DIR: userDataDir, |
| 85 | + VIGILATUS_TEST_FIXTURES: fixturePath, |
| 86 | + VIGILATUS_OPEN_DEVTOOLS: '0', |
| 87 | + ...(isCI ? { ELECTRON_ENABLE_LOGGING: '1', ELECTRON_ENABLE_STACK_DUMPING: '1' } : {}), |
| 88 | + }, |
| 89 | + timeout: isCI ? 30_000 : 10_000, |
| 90 | + }); |
| 91 | + |
| 92 | + const proc = app.process(); |
| 93 | + proc.stdout?.on('data', (d: Buffer) => { |
| 94 | + const line = d.toString().trim(); |
| 95 | + if (line) console.info(`[electron] ${line}`); |
| 96 | + }); |
| 97 | + proc.stderr?.on('data', (d: Buffer) => { |
| 98 | + const line = d.toString().trim(); |
| 99 | + if (line) console.info(`[electron:err] ${line}`); |
| 100 | + }); |
| 101 | + |
| 102 | + const window = await app.firstWindow(); |
| 103 | + await window.waitForLoadState('domcontentloaded'); |
| 104 | + |
| 105 | + return { app, window, userDataDir }; |
| 106 | + } catch (error) { |
| 107 | + const launchDetails = formatLaunchFailure(error, userDataDir); |
| 108 | + const runtimeLogTail = await readTail(path.join(userDataDir, 'vigilatus.log'), 20_000); |
| 109 | + const bootstrapLogTail = await readTail(path.join(userDataDir, 'vigilatus-bootstrap.log'), 20_000); |
| 110 | + const details = [launchDetails]; |
| 111 | + if (bootstrapLogTail) { |
| 112 | + details.push(`--- vigilatus-bootstrap.log tail ---\n${bootstrapLogTail}`); |
| 113 | + } |
| 114 | + if (runtimeLogTail) { |
| 115 | + details.push(`--- vigilatus.log tail ---\n${runtimeLogTail}`); |
| 116 | + } |
| 117 | + throw new Error(details.join('\n\n')); |
| 118 | + } |
| 119 | +} |
0 commit comments