// Browser capture helpers: launch Chromium, walk a page, assert it is healthy. // // This module deliberately touches neither the kernel nor OPFS. It takes // already-projected mesh data and already-written files, so the kernel selftest // can stay browser-free while the guide checker reuses the same primitives. // // The browser dependency (playwright) lives in web/node_modules, so the require // is resolved from there rather than from the host's global module path. import fs from "node:fs"; import path from "node:path"; import { createRequire } from "node:module"; import { fileURLToPath } from "node:url"; const here = path.dirname(fileURLToPath(import.meta.url)); const repoRoot = path.resolve(here, "../.."); const require = createRequire(path.join(repoRoot, "web", "package.json")); export const DEFAULT_CHROME = process.env.FREECAD_CHROME || "/usr/bin/google-chrome"; /** Launch Chromium with the flags this project already needs. */ export async function launchBrowser({ headed = false, executablePath = DEFAULT_CHROME } = {}) { const { chromium } = require("playwright"); return chromium.launch({ executablePath, headless: !headed, args: ["--no-sandbox", "--disable-dev-shm-usage"], }); } /** * Open a page and start collecting everything a check needs to judge it. * Returns { page, errors, consoleErrors, failedRequests }. */ export async function openPage(browser, url, { viewport = { width: 1440, height: 900 } } = {}) { const page = await browser.newPage({ viewport }); const errors = []; const consoleErrors = []; const failedRequests = []; page.on("pageerror", error => errors.push(String(error))); page.on("console", message => { if (message.type() === "error") consoleErrors.push(message.text()); }); page.on("requestfailed", request => failedRequests.push(`${request.url()} ${request.failure()?.errorText ?? ""}`)); const response = await page.goto(url, { waitUntil: "networkidle" }); return { page, response, errors, consoleErrors, failedRequests }; } export async function screenshot(page, { file, fullPage = false, locator } = {}) { fs.mkdirSync(path.dirname(file), { recursive: true }); if (locator) await page.locator(locator).screenshot({ path: file }); else await page.screenshot({ path: file, fullPage }); return { file, bytes: fs.statSync(file).size }; } /** * Structural health check of a served page at one or more viewports. * * `expect.chapters` is the number of content files the page should have * rendered; `expect.anchorIds` are ids that must resolve. Everything is * measured in the page, so a missing element or a broken image is reported * rather than thrown. */ export async function pageCheck(browser, url, { viewports = [{ width: 1440, height: 900 }], expect = {} } = {}) { const reports = []; for (const viewport of viewports) { const { page, response, errors, consoleErrors, failedRequests } = await openPage(browser, url, { viewport }); try { const dom = await page.evaluate(() => { // An with no src is a placeholder (the shared manual template // ships one as its lightbox target), not a broken image. const brokenImages = [...document.images] .filter(image => image.getAttribute("src")) .filter(image => image.complete && image.naturalWidth === 0) .map(image => image.currentSrc || image.src); const doc = document.documentElement; const overflow = Math.max(0, doc.scrollWidth - doc.clientWidth); const ids = [...document.querySelectorAll("[id]")].map(node => node.id); return { brokenImages, overflow, ids, title: document.title, chapterCount: document.querySelectorAll("main section, main article").length, headings: [...document.querySelectorAll("h1, h2")].map(node => node.id || node.textContent.trim().slice(0, 40)), }; }); const missingIds = (expect.anchorIds ?? []).filter(id => !dom.ids.includes(id)); reports.push({ viewport, status: response?.status() ?? null, ...dom, missingIds, pageErrors: errors, consoleErrors, failedRequests, ok: (response?.status() ?? 0) === 200 && errors.length === 0 && failedRequests.length === 0 && dom.brokenImages.length === 0 && dom.overflow === 0 && missingIds.length === 0 && (expect.chapters == null || dom.chapterCount >= expect.chapters), }); } finally { await page.close(); } } return { url, viewports: reports, ok: reports.every(item => item.ok) }; } /** Mesh projection shared with the viewer: already-rounded positions + normals. */ export function projectMesh(objects) { return (objects ?? []).map(item => ({ name: item.name, label: item.label ?? item.name, volume: item.volume ?? null, positions: item.positions ?? [], normals: item.normals ?? [], triangleCount: item.triangleCount ?? (item.positions ?? []).length / 9, })); } export function writeArtifact(dir, name, value) { fs.mkdirSync(dir, { recursive: true }); const file = path.join(dir, name); const text = typeof value === "string" ? value : JSON.stringify(value, null, 2); fs.writeFileSync(file, text); return { file, bytes: fs.statSync(file).size }; }