okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
5d9f47922
main
1// Shared bits for the browser tests and the screenshot script of the served web page (serve/web): a headless Chromium, 2// a page against the mock server, small helpers. puppeteer-core is not a dependency of the repo: install it anywhere and 3// point WEBTEST_MODULES at that folder (the one that holds node_modules); CHROME_BIN is the Chrome or Edge to drive. 4import { createRequire } from "node:module" ; 5import path from "node:path" ; 6import fs from "node:fs" ; 7 8const req = createRequire ( path . join ( process . env . WEBTEST_MODULES || process . cwd (), "noop.js" )); 9export const puppeteer = req ( "puppeteer-core" ); 10 11const CANDIDATES = [ process . env . CHROME_BIN , "C:/Program Files/Google/Chrome/Application/chrome.exe" , 12"C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe" , "/usr/bin/google-chrome" , "/usr/bin/chromium" , "/usr/bin/chromium-browser" , 13"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" ]. filter ( Boolean ); 14export const chrome = () => { 15const found = CANDIDATES . find (( p ) => fs . existsSync ( p )); 16if ( ! found ) throw new Error ( "no Chrome or Edge found; set CHROME_BIN" ); 17return found ; 18}; 19export const launch = () => puppeteer . launch ({ executablePath :chrome (), headless :true , args :[ "--no-sandbox" , "--disable-gpu" ] }); 20 21// a page with a seeded browser store (localStorage values are JSON, as the page's own store writes them), console errors collected 22export async function openPage ( browser , mock , { width= 1280 , height= 800 , seed= {}, query= "" , dark= false , hash= "" , dialogs= "accept" } = {}) { 23const context = await browser . createBrowserContext (); 24const page = await context . newPage (); 25page . errors = []; 26page . on ( "pageerror" , ( e ) => page . errors . push ( "pageerror: " + e . message )); 27page . on ( "console" , ( m ) => { if ( m . type () === "error" ) page . errors . push ( "console: " + m . text ()); }); 28page . on ( "dialog" , async ( d ) => { page . lastDialog = d . message (); if ( dialogs === "accept" ) await d . accept (); else await d . dismiss (); }); 29await page . setViewport ({ width, height}); 30await page . emulateMediaFeatures ([{ name :"prefers-color-scheme" , value :dark ?"dark" :"light" }]); 31await page . evaluateOnNewDocument (( s ) => { 32if ( window !== window . top ) return ; // not in the preview frames: they have no storage 33if ( sessionStorage . getItem ( "seeded" )) return ; 34sessionStorage . setItem ( "seeded" , "1" ); 35for ( const [ k , v ] of Object . entries ( s )) localStorage . setItem ( "strata." + k , JSON . stringify ( v )); 36}, seed ); 37await page . goto ( mock . url + "/" + query + hash , { waitUntil :"networkidle0" }); 38return page ; 39} 40 41export const sleep = ( ms ) => new Promise (( r ) => setTimeout ( r , ms )); 42// the page's own `busy` is a top-level let of app.js: reachable by name from page.evaluate 43export const idle = ( page , timeout = 15000 ) => page . waitForFunction (() => busy === null , { timeout}); 44export async function say ( page , text ) { 45await page . click ( "#input" ); 46await page . type ( "#input" , text ); 47await page . keyboard . press ( "Enter" ); 48} 49export const assistantText = ( page , i = - 1 ) => page . evaluate (( n ) => { const a = messages . filter (( m ) => m . role === "assistant" ); return a . at ( n ). text ; }, i ); 50export const shot = async ( page , dir , name , opts = {}) => { 51fs . mkdirSync ( dir , { recursive :true }); 52const file = path . join ( dir , name + ".png" ); 53await page . screenshot ({ path :file , ...opts }); 54return file ; 55}; 56 57// tiny test runner: node file.mjs [name-substring] 58export function runner ( title ) { 59const tests = []; 60const only = process . argv [ 2 ]; 61return { 62test :( name , fn ) => tests . push ([ name , fn ]), 63async run ( setup ) { 64const ctx = await setup (); 65let failed = 0 ; 66for ( const [ name , fn ] of tests ) { 67if ( only && ! name . includes ( only )) continue ; 68const t = Date . now (); 69try { await fn ( ctx ); console . log ( `ok ${ name } ( ${ Date . now () - t } ms)` ); } 70catch ( e ) { failed ++ ; console . log ( `FAIL ${ name } \n ${ String ( e . stack || e ). split ( "\n" ). slice ( 0 , 6 ). join ( "\n " )} ` ); } 71} 72await ctx . close (); 73console . log ( failed ?` ${ title } : ${ failed } failed` :` ${ title } : all passed` ); 74process . exitCode = failed ?1 :0 ; 75}, 76}; 77}