import { test as base, expect, chromium, type BrowserContext, type Page } from "@playwright/test"; import fs from "node:fs"; import path from "node:path"; import { DEPLOYED_ORIGIN, DEPLOYED_VIEWPORT, browserArgs, chromePath, profileDir } from "./browser-options"; import { REPO_ROOT, normalizeApp } from "./deployed"; /** * Test entry point for the deployed suite. * * Loading the deployed bundle costs ~3 minutes per browser context: a 300 MB * wasm module is downloaded and compiled for every fresh context. Re-running * that for each of ~60 cases is what made the suite impractically slow, so the * default mode launches ONE persistent context per worker (Chrome profile on * disk, so repeat runs only revalidate the wasm with a 304) and every test * shares its page. Cases reset their own state by opening the project they need. * * `DEPLOYED_ISOLATE=1` restores per-test contexts and video capture for the * rare case where a full recording is worth the reload cost. */ const isolated = process.env.DEPLOYED_ISOLATE === "1"; interface Session { context: BrowserContext; page: Page; bootMs: number; loads: number; pageErrors: string[]; /** Spec file whose case currently owns the page; a new file reloads it. */ lastFile: string | null; reloads: number[]; } /** Worker-scoped: the whole file (and run, with workers=1) reuses one page. */ const sharedSession = base.extend<{ session: Session }, { session: Session }>({ session: [async ({}, use) => { const context = await chromium.launchPersistentContext(profileDir(REPO_ROOT), { headless: false, executablePath: chromePath(), args: browserArgs(), viewport: DEPLOYED_VIEWPORT, baseURL: DEPLOYED_ORIGIN, acceptDownloads: true, ignoreHTTPSErrors: process.env.DEPLOYED_IGNORE_HTTPS === "1", locale: "zh-CN", }); const page = context.pages()[0] ?? await context.newPage(); const started = Date.now(); await page.goto("/?ui=web", { waitUntil: "domcontentloaded", timeout: 20_000 }); await page.locator("main.blender-app").waitFor({ timeout: 20_000 }); await page.waitForFunction(() => document.body.innerText.includes("WASM ABI: ready"), undefined, { timeout: 30_000 }); const bootMs = Date.now() - started; /* A crash in a shared page otherwise shows up as a mysterious missing * element; keep the browser's own errors next to the case. */ const pageErrors: string[] = []; page.on("pageerror", (error) => { if (pageErrors.length < 20) pageErrors.push(`pageerror: ${error.message}`); }); page.on("console", (message) => { if (message.type() === "error" && pageErrors.length < 20) pageErrors.push(`console: ${message.text()}`); }); const session: Session = { context, page, bootMs, loads: 1, pageErrors, lastFile: null, reloads: [] }; /* The runner reads this to report how much the reuse saved. */ const reportDir = process.env.DEPLOYED_REPORT_DIR; if (reportDir) { try { fs.mkdirSync(reportDir, { recursive: true }); fs.writeFileSync(path.join(reportDir, "session.json"), JSON.stringify({ schemaVersion: 1, mode: "shared-persistent", origin: DEPLOYED_ORIGIN, bootMs, loads: 1, profile: profileDir(REPO_ROOT), startedAt: new Date().toISOString(), }, null, 2)); } catch { /* the report directory is the runner's business */ } } await use(session); await context.close(); }, { scope: "worker" }], /* Same context, same page: no reload, no recompile between cases. */ page: async ({ session }, use) => { await use(session.page); }, context: async ({ session }, use) => { await use(session.context); }, /* A case inherits layout/preferences from the previous one; put the shell back * into a known state before it runs. * * The first case of each spec file also reloads the page. Sharing one page * for the whole run is what keeps the suite fast, but area DOM state carries * across files: in a full run ch12/ch15 waited forever for `treeitem * BasicCube` even though the same files passed when run alone. A warm reload * (~2 s, wasm served from the disk profile) restores file-boundary * conditions without paying for a new context. */ normalize: [async ({ session }, use, testInfo) => { if (session.lastFile !== testInfo.file) { const started = Date.now(); await session.page.goto("/?ui=web", { waitUntil: "domcontentloaded", timeout: 30_000 }); await session.page.locator("main.blender-app").waitFor({ timeout: 20_000 }); await session.page.waitForFunction(() => document.body.innerText.includes("WASM ABI: ready"), undefined, { timeout: 30_000 }); session.reloads.push(Date.now() - started); session.lastFile = testInfo.file; } await normalizeApp(session.page); await use(); }, { auto: true }], /* Playwright's built-in `trace` fixture already records this context per test * and keeps the archive only on failure (config: retain-on-failure). Starting * a second trace here fails with "Tracing has been already started". */ /* Failure screenshot from the shared page, which the runner cannot take itself. */ evidence: [async ({ session }, use, testInfo) => { await use(); if (testInfo.status !== testInfo.expectedStatus) { const file = testInfo.outputPath("failure.png"); await session.page.screenshot({ path: file }).catch(() => undefined); await testInfo.attach("failure-screenshot", { path: file, contentType: "image/png" }).catch(() => undefined); if (session.pageErrors.length) { const log = testInfo.outputPath("page-errors.txt"); fs.writeFileSync(log, session.pageErrors.join("\n")); await testInfo.attach("page-errors", { path: log, contentType: "text/plain" }).catch(() => undefined); } } }, { auto: true }], }); export const test = isolated ? base : sharedSession; export { expect };