Something went wrong. Try again.
Tools for building Offline-first Web Apps
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146#!/usr/bin/env -S deno run -A/** * @module Component behaviour suite * * ``` * deno task test:behaviour # all cases * deno task test:behaviour ui-search-field # cases whose specimen matches * ``` * * The third check over `packages/ui`, and the one that covers what the other * two structurally cannot: * * | Check | Answers | * | --------------------- | ---------------------------------------- | * | `test:classes` | does it emit a class nobody defines? | * | `test:shapes` | did its rendered DOM change? | * | `test:behaviour` | does it still *do* the thing? | * * It exists because a component shipped whose whole reason to exist was a * stale-response guard, and the guard did nothing: it sequenced state the * component owned while the *runner* wrote the results. Both other checks * passed, the shape was right, the class names were right, and the one thing * it was for was missing. Nothing in the repo was looking. * * Lit only runs in a browser, so this drives one — the same gallery * `test:shapes` uses, which already renders every component from local source * and signals readiness through `documentElement.dataset.ready`. * * Cases live in `cases.ts` and run **inside the page**; see the notes there. */import { launch } from '@astral/astral'import { serveDir } from '@std/http/file-server'import { fromFileUrl } from '@std/path'import { type BehaviourCase, CASES } from './cases.ts'
const GALLERY_DIST = fromFileUrl( new URL('../../packages/ui/gallery/dist', import.meta.url),)
interface Failure { case: BehaviourCase reason: string}
/** Drive one case: show its specimen, run it in the page, collect the verdict. */async function runCase( // deno-lint-ignore no-explicit-any page: any, c: BehaviourCase,): Promise<string | null> { const route = `/${c.specimen}` // Set the hash but do *not* clear `ready` first: between cases the page is // reloaded, which lands on the previous case's hash and renders it. Two // cases on the same specimen would then set an unchanged hash, fire no // `hashchange`, and wait forever for a flag only the router sets. Reading // `ready === route` covers both — already there, or about to be. await page.evaluate( (hash: string) => { location.hash = `#!${hash}` }, { args: [route] }, )
const deadline = Date.now() + 10_000 let ready = false while (Date.now() < deadline) { ready = await page.evaluate( (r: string) => document.documentElement.dataset.ready === r, { args: [route] }, ) if (ready) break await page.waitForTimeout(25) } if (!ready) return `specimen ${route} never rendered`
try { // Serialised, so the case cannot capture anything from this file — which // is why `cases.ts` insists every case be self-contained. return await page.evaluate(`(${c.run})()`) as string | null } catch (err) { return `threw: ${err instanceof Error ? err.message : String(err)}` }}
const filter = Deno.args.find((a) => !a.startsWith('-'))const cases = filter ? CASES.filter((c) => c.specimen.includes(filter)) : CASES
if (cases.length === 0) { console.error(`No cases match ${JSON.stringify(filter)}.`) Deno.exit(1)}
const server = Deno.serve( { port: 0, hostname: '127.0.0.1', onListen: () => {} }, (req) => serveDir(req, { fsRoot: GALLERY_DIST, quiet: true }),)const browser = await launch()const failures: Failure[] = []
try { const page = await browser.newPage() await page.goto( `http://127.0.0.1:${server.addr.port}/#!/`, { waitUntil: 'networkidle2' }, ) const routes = await page.evaluate(() => (globalThis as { __ROUTES__?: string[] }).__ROUTES__ ?? [] ) as string[] if (routes.length === 0) { throw new Error('gallery published no __ROUTES__; is it built?') }
for (const c of cases) { if (!routes.includes(`/${c.specimen}`)) { failures.push({ case: c, reason: `no such specimen: /${c.specimen}` }) continue } const reason = await runCase(page, c) if (reason) failures.push({ case: c, reason }) // Each case mutates its specimen (setting `runner`, emptying `links`), so // reload rather than hand the next one a poked-at fixture. await page.reload({ waitUntil: 'networkidle2' }) } await page.close()} finally { await browser.close() await server.shutdown()}
if (failures.length > 0) { console.error( `\n${failures.length} of ${cases.length} behaviour case(s) failed.\n`, ) for (const f of failures) { console.error(` ${f.case.specimen}`) console.error(` expected: ${f.case.name}`) console.error(` but: ${f.reason}\n`) } Deno.exit(1)}
console.log( `${cases.length} behaviour case(s) hold across ` + `${new Set(cases.map((c) => c.specimen)).size} specimen(s).`,)