// Shared stack for the client e2e (TESTING 10.3): the real client build served by the TLS/HTTP2 // test nginx in front of the real server binary. Never a fixture, never a mocked protocol. import assert from 'node:assert/strict' import fs from 'node:fs' import path from 'node:path' import { chromium } from 'playwright-core' import { createPlayer, freePort, httpsBaseUrl, namePlayer, projectRoot, sleep } from '../../tools/lib.mjs' import { gmKeyOf, makeStateDir, openAs, startServer, stopServer } from '../../tools/withserver.mjs' import { startTestNginx, stopTestNginx } from '../../tools/nginx.mjs' export const CHROMIUM = process.env.CHROMIUM ?? 'chromium' export const LAUNCH_ARGS = [ '--no-sandbox', '--use-angle=swiftshader', '--enable-unsafe-swiftshader', '--ignore-gpu-blocklist', ] export const CLIENT_DIST = path.join(projectRoot, 'client', 'dist') export function assertClientBuild() { assert.ok( fs.existsSync(path.join(CLIENT_DIST, 'index.html')), `client build missing at ${CLIENT_DIST}: run \`npm run build\` in client/ (client/e2e/run.sh does)`, ) } export async function withStack(fn) { assertClientBuild() const stateDir = makeStateDir() const gmKey = gmKeyOf(stateDir) const nginxPort = await freePort() const server = await startServer({ stateDir, publicOrigin: httpsBaseUrl(nginxPort) }) let nginx try { nginx = await startTestNginx({ backendPort: server.port, stateDir, port: nginxPort, distDir: CLIENT_DIST, }) return await fn({ stateDir, server, nginx, origin: nginx.origin, gmKey }) } finally { if (nginx) await stopTestNginx(nginx) await stopServer(server) fs.rmSync(stateDir, { recursive: true, force: true }) } } export async function launchBrowser() { return chromium.launch({ executablePath: CHROMIUM, args: LAUNCH_ARGS }) } export async function newPage(browser) { // The certificate is generated per run by tools/tlsdev.mjs; trusting it in the browser is the // one concession, the rest of the page is the real https/h2 path. const context = await browser.newContext({ ignoreHTTPSErrors: true, viewport: { width: 1000, height: 700 } }) return { context, page: await context.newPage() } } /** A named player created the way the product creates one: a GM key.create, then player.setname. */ export async function makePlayer(origin, gmKey, name) { const gm = await openAs(origin, gmKey) await gm.ws.waitText('hello') const created = await createPlayer(gm.ws) await namePlayer(origin, created.key, name) gm.ws.close() await gm.ws.closed return { id: created.id, key: created.key, name } } export async function signIn(page, key) { await page.fill('#key', key) await page.click('#signin-submit') } export async function waitForVisible(page, selector, timeout = 20000) { await page.locator(selector).waitFor({ state: 'visible', timeout }) } export async function waitForHidden(page, selector, timeout = 20000) { await page.locator(selector).waitFor({ state: 'hidden', timeout }) } export async function waitForText(page, selector, pattern, timeout = 20000) { const deadline = Date.now() + timeout let last = '' while (Date.now() < deadline) { last = (await page.locator(selector).textContent()) ?? '' if (pattern.test(last)) return last await sleep(50) } throw new Error(`${selector} never matched ${pattern}: last was ${JSON.stringify(last)}`) } export { sleep }