Files
Strata/terminal/tools/webtest/lib.mjs
T
Niko1221 4058aa782b Layout move for 0.2.16: the top level becomes app/, engine/, terminal/, docs/, tools/ and .github/
Made by tools/layout/move_0216.py (the commit before this one) on db8e0be9c, the final code head of 0.2.16:
- 75 git mv (renames, so `git log --follow` and blame keep working; packaging/acceptance_matrix.md shows as delete + add because its links
  changed too) and 3 removals: SETUP.bat, UPDATE.bat and update.sh (setup.py --update pulls the code now);
- 1,425 path edits in 307 files: root-relative paths in CI, Docker, .gitignore and the release scripts; Markdown links; Python and
  Node joins from a root that moved with its folder; explicit edits for code that computes a root or names a moved path; tables of
  repository paths inside tests.

engine/ holds what CMake builds (src, include, cmake, metal, sycl, spark, third_party, the C++ tests and benches). app/ is the desktop
app and its host (was apps/). terminal/ is the Terminal in the layout of the installed one (strata.py, setup.py, strata_cli,
strata_control, serve, bin, data, ref, crates, the Python tools and tests). docs/ has the documentation, docs/lang/ the translated
READMEs, docs/history and docs/bench-results. tools/ has the repository's own checks, packaging, docker, research and nix.
tests/core/test_tqp_ref.py is a pytest file and stays with the Python tests (terminal/tests/core/).
AGENTS.md, README.md, LICENSE, setup.sh and START-HERE.bat stay at the root.

No behaviour change: the same command lines, the same sources. For this head a replay of the script gave the same numbers and the path
scan (tools/layout/checks/path_scan.py) reports no hit; the suites and the builds run on this commit next.
2026-11-17 13:19:21 +01:00

78 lines
4.0 KiB
JavaScript

// Shared bits for the browser tests and the screenshot script of the served web page (serve/web): a headless Chromium,
// a page against the mock server, small helpers. puppeteer-core is not a dependency of the repo: install it anywhere and
// point WEBTEST_MODULES at that folder (the one that holds node_modules); CHROME_BIN is the Chrome or Edge to drive.
import { createRequire } from "node:module";
import path from "node:path";
import fs from "node:fs";
const req = createRequire(path.join(process.env.WEBTEST_MODULES || process.cwd(), "noop.js"));
export const puppeteer = req("puppeteer-core");
const CANDIDATES = [process.env.CHROME_BIN, "C:/Program Files/Google/Chrome/Application/chrome.exe",
"C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe", "/usr/bin/google-chrome", "/usr/bin/chromium", "/usr/bin/chromium-browser",
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"].filter(Boolean);
export const chrome = () => {
const found = CANDIDATES.find((p) => fs.existsSync(p));
if (!found) throw new Error("no Chrome or Edge found; set CHROME_BIN");
return found;
};
export const launch = () => puppeteer.launch({ executablePath: chrome(), headless: true, args: ["--no-sandbox", "--disable-gpu"] });
// a page with a seeded browser store (localStorage values are JSON, as the page's own store writes them), console errors collected
export async function openPage(browser, mock, { width = 1280, height = 800, seed = {}, query = "", dark = false, hash = "", dialogs = "accept" } = {}) {
const context = await browser.createBrowserContext();
const page = await context.newPage();
page.errors = [];
page.on("pageerror", (e) => page.errors.push("pageerror: " + e.message));
page.on("console", (m) => { if (m.type() === "error") page.errors.push("console: " + m.text()); });
page.on("dialog", async (d) => { page.lastDialog = d.message(); if (dialogs === "accept") await d.accept(); else await d.dismiss(); });
await page.setViewport({ width, height });
await page.emulateMediaFeatures([{ name: "prefers-color-scheme", value: dark ? "dark" : "light" }]);
await page.evaluateOnNewDocument((s) => {
if (window !== window.top) return; // not in the preview frames: they have no storage
if (sessionStorage.getItem("seeded")) return;
sessionStorage.setItem("seeded", "1");
for (const [k, v] of Object.entries(s)) localStorage.setItem("strata." + k, JSON.stringify(v));
}, seed);
await page.goto(mock.url + "/" + query + hash, { waitUntil: "networkidle0" });
return page;
}
export const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
// the page's own `busy` is a top-level let of app.js: reachable by name from page.evaluate
export const idle = (page, timeout = 15000) => page.waitForFunction(() => busy === null, { timeout });
export async function say(page, text) {
await page.click("#input");
await page.type("#input", text);
await page.keyboard.press("Enter");
}
export const assistantText = (page, i = -1) => page.evaluate((n) => { const a = messages.filter((m) => m.role === "assistant"); return a.at(n).text; }, i);
export const shot = async (page, dir, name, opts = {}) => {
fs.mkdirSync(dir, { recursive: true });
const file = path.join(dir, name + ".png");
await page.screenshot({ path: file, ...opts });
return file;
};
// tiny test runner: node file.mjs [name-substring]
export function runner(title) {
const tests = [];
const only = process.argv[2];
return {
test: (name, fn) => tests.push([name, fn]),
async run(setup) {
const ctx = await setup();
let failed = 0;
for (const [name, fn] of tests) {
if (only && !name.includes(only)) continue;
const t = Date.now();
try { await fn(ctx); console.log(`ok ${name} (${Date.now() - t} ms)`); }
catch (e) { failed++; console.log(`FAIL ${name}\n ${String(e.stack || e).split("\n").slice(0, 6).join("\n ")}`); }
}
await ctx.close();
console.log(failed ? `${title}: ${failed} failed` : `${title}: all passed`);
process.exitCode = failed ? 1 : 0;
},
};
}