Files
client/scripts/lib/electron-app.mjs
T

83 lines
4.1 KiB
JavaScript

// Deterministic Playwright launch of TeamsPeach for the smoke scripts.
//
// Dev tree: no executablePath, so Playwright starts its bundled Electron with its own `-r loader.js`, which
// holds the app's `ready` until Playwright has attached.
// Packaged build (--exe): Playwright cannot inject that loader, and when it attaches while the first page is
// committing its utility world is lost and `electron.launch` hangs. The app is therefore started with
// TEAMSPEACH_TEST_HOLD_WINDOW=1 and the window is created through the main-process hook only after attach.
import { _electron as electron } from 'playwright-core';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { readFileSync } from 'node:fs';
import { createRequire } from 'node:module';
import { setTimeout as delay } from 'node:timers/promises';
export const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..');
export async function assertOffscreen(app) {
try {
await app.evaluate(({ BrowserWindow }) => {
const windows = BrowserWindow.getAllWindows();
if (windows.length !== 1) throw new Error('Offscreen QA requires exactly one owned window.');
const win = windows[0];
win.webContents.setAudioMuted(true);
if (win.isVisible() || win.isFocused() || win.isFocusable() || !win.webContents.isOffscreen()) {
throw new Error('Unsafe QA window: hidden, unfocused, nonfocusable and offscreen are mandatory.');
}
for (const method of ['show', 'showInactive', 'focus', 'maximize', 'minimize', 'restore', 'setFullScreen', 'setSimpleFullScreen']) {
win[method] = () => { throw new Error(`Native window operation forbidden in offscreen QA: ${method}`); };
}
});
return app;
} catch (error) {
await app.close().catch(() => {});
throw error;
}
}
/** Leaves Settings the way the product offers it: the in-panel back control exists only below 900 px; wider layouts toggle with #btn-settings. */
export async function closeSettings(page) {
if (await page.locator('#settings-back').isVisible()) await page.click('#settings-back');
else await page.click('#btn-settings');
await page.waitForSelector('#settings-view', { state: 'hidden' });
}
/**
* @param {{ exe?: string, profile: string, args?: string[], env?: object, colorScheme?: null | 'light' | 'dark', timeout?: number }} o
*/
export async function launchApp({ exe, profile, args = [], env = {}, colorScheme = null, timeout }) {
const baseEnv = { ...process.env, ...env, TEAMSPEACH_PROFILE_DIR: profile, TEAMSPEACH_TEST_OFFSCREEN: '1' };
if (!exe) {
const app = await electron.launch({ args: [root, ...args], env: baseEnv, colorScheme, timeout });
await app.firstWindow();
return assertOffscreen(app);
}
// An environment flag alone cannot make an older package safe. Verify the packaged main code
// is byte-identical to the current offscreen-aware main before starting its executable.
const require = createRequire(import.meta.url);
const archive = path.join(path.dirname(path.resolve(exe)), 'resources', 'app.asar');
const packagedMain = require('@electron/asar').extractFile(archive, path.join('app', 'main', 'main.js'));
const currentMain = readFileSync(path.join(root, 'app/main/main.js'));
if (!packagedMain.equals(currentMain)) throw new Error('Packaged main is not the verified offscreen-safe source; launch refused.');
const app = await electron.launch({
executablePath: path.resolve(exe),
args,
env: { ...baseEnv, TEAMSPEACH_TEST_HOLD_WINDOW: '1' },
colorScheme,
timeout,
});
await app.evaluate(() => globalThis.__teamspeachCreateWindow());
return assertOffscreen(app);
}
/** Playwright's in-page waitForFunction treats a Promise as truthy; await IPC/import predicates explicitly. */
export async function waitForAsync(page, predicate, argument, { timeout = 30000 } = {}) {
const deadline = Date.now() + timeout;
do {
const value = await page.evaluate(predicate, argument);
if (value) return value;
if (Date.now() >= deadline) throw new Error(`Async condition did not become true within ${timeout}ms`);
await delay(50);
} while (true);
}