#!/usr/bin/env node /** * Fleet audit: rendered defects that no unit test, type check or lint rule * can see, because nothing is wrong until the page is painted — and, since * 2026-09-21, painted at more than one width. * * 1. INVISIBLE ACTIONS — an interactive label below its WCAG AA contrast * floor. On loki/control the only route to a feedback report's * screenshot and history rendered at 3.13:1; the operator's report was not * "low contrast", it was "i dont see it". An action nobody can find is a * feature that does not exist. * * 2. RAGGED STACKS — sibling lines in one vertical stack that start at * slightly different x, and wrapped lines whose second line does not align * with the first. Both come from the same habit: putting an icon INLINE at * the head of a line, so it shoves that one line sideways by its own width * and gives a wrapped paragraph no hanging indent. On loki's fleet * card four stacked rows started at three different x (369 / 383 / 385) * with the hint's second line snapping back to 369. The operator's words * were "this area looks bad. not aligned." * * Central auditor, same doctrine as verify-floor-audit.sh: ONE script that * reads every SITE remotely, never a check copied into each repo. A copied * check drifts; this one cannot. * * The site list is DISCOVERED, not hardcoded: loki's public footer is the * fleet's own SSOT for "sites we run" (config/fleet-sites.ts renders there * precisely so each has a crawlable anchor). Reading it live means a new site * is audited the day it is linked, and a retired one stops being audited, * without anyone editing this file. * * 3. CONTROLS OFF THE SCREEN — a nav or header control with part of itself * past the viewport edge, and a page that scrolls sideways. Both only * exist at a width, and for its first year this audit rendered exactly * one (1440). substrata's mobile menu opened 22rem off the LEFT of a * 390px screen for months with every other rule green; the first * three-width sweep found the same class live on surf-your-life (93px), * vitareba (19px) and kivvi (15px, plus 15px of sideways scroll), every * one of them clean at 1440. * * SCOPE — what this does NOT prove. It renders each site's PUBLIC entry page * only, unauthenticated. Defects behind a login are invisible here; for those, * run the per-repo authed audits (loki: `npm run audit:contrast`). It * also judges only text it can attribute to a background — an element painted * over an image reports the image's absence, not its colour, so those are * skipped rather than guessed at. * * Usage: * node scripts/ci/ui-defect-audit.mjs # audit, exit 1 on defects * node scripts/ci/ui-defect-audit.mjs --warn-only # report, always exit 0 * SITES="https://a.example,https://b.example" node scripts/ci/ui-defect-audit.mjs * VIEWPORTS="390x844,1440x1000" node scripts/ci/ui-defect-audit.mjs * * Needs playwright. dotfiles has no package.json on purpose, so the browser is * resolved from a fleet repo that already installs it (override with * PLAYWRIGHT_FROM=/path/to/repo). */ import { createRequire } from "node:module"; import { existsSync } from "node:fs"; import { join } from "node:path"; import { homedir } from "node:os"; const WARN_ONLY = process.argv.includes("--warn-only"); const AA_SMALL = 4.5; const AA_LARGE = 3.0; /** A stack whose rows differ by more than this is deliberate indentation, not * an icon accidentally shoving one line sideways. Icons in this fleet are * 10–16px plus a gap; 24 covers them with headroom and excludes real nesting. */ const MAX_ACCIDENTAL_INDENT_PX = 24; const DISCOVERY_URL = process.env.DISCOVERY_URL ?? "https://loki.orangecat.ch/"; /** * The widths every site is rendered at. * * This audit ran at 1440 only until 2026-09-21, and that was the hole it could * not see through. substrata's header shipped a mobile menu whose panel was * positioned against a button in the middle of the bar, so at 390px it opened * 22rem to the LEFT of that button — the search field and every link label * painted off the side of the screen. Every rule here passed: the links had * boxes, they cleared 44px, the current page carried aria-current. They were * simply not on the screen, and nothing rendered the screen they were missing * from. The same header hid its whole nav between 768 and 1099px. * * Three widths, because each is a different layout branch in this fleet's CSS: * a phone below every breakpoint, a tablet in the gap where `md:` has fired and * `lg:` has not (where both of substrata's defects lived), and the desktop this * audit already covered. */ const DEFAULT_VIEWPORTS = "390x844,834x1112,1440x1000"; export function parseViewports(spec = process.env.VIEWPORTS) { return (spec?.trim() || DEFAULT_VIEWPORTS) .split(",") .map((pair) => pair.trim()) .filter(Boolean) .map((pair) => { const [w, h] = pair.split("x").map((n) => Number.parseInt(n, 10)); if (!Number.isFinite(w) || !Number.isFinite(h) || w < 1 || h < 1) { throw new Error(`bad viewport "${pair}" — expected WIDTHxHEIGHT, e.g. 390x844`); } return { width: w, height: h, label: String(w) }; }); } export function loadPlaywright() { const candidates = [ process.env.PLAYWRIGHT_FROM, join(homedir(), "dev", "loki"), join(homedir(), "dev", "orangecat"), join(homedir(), "dev", "vitareba"), ].filter(Boolean); for (const root of candidates) { const entry = join(root, "node_modules", "playwright", "index.js"); if (!existsSync(entry)) continue; return createRequire(join(root, "package.json"))("playwright"); } console.error( "✗ playwright not found. Set PLAYWRIGHT_FROM=/path/to/a/repo that installs it." ); process.exit(2); } /** The fleet's own list of the sites it runs, read from where it is published. */ async function discoverSites() { const explicit = process.env.SITES?.trim(); if (explicit) return explicit.split(",").map((s) => s.trim()).filter(Boolean); const res = await fetch(DISCOVERY_URL, { headers: { "user-agent": "fleet-ui-audit" } }); if (!res.ok) throw new Error(`site discovery failed: ${DISCOVERY_URL} → ${res.status}`); const html = await res.text(); const found = new Set(); for (const m of html.matchAll(/https:\/\/[a-z0-9.-]*orangecat\.ch(?=["'/\s<])/g)) { found.add(m[0]); } // The audit runs FROM loki's own page; auditing the page we discovered // from is still worth doing, so it stays in the list. found.add("https://loki.orangecat.ch"); return [...found].sort(); } /** * Open what the nav hides, before measuring it. * * A menu panel is closed on arrival, so a panel that paints off the side of the * screen is invisible to a detector that only measures what is already open — * which is exactly how substrata's shipped for months. `
` is opened by * setting the property rather than clicking it: no event fires, so nothing can * navigate, and there is no state for a click handler to get wrong. * * Buttons are NOT touched here; `main()` clicks those with a URL guard, * because a `button[aria-expanded]` in this fleet may be anything at all. * * Returns how many it opened, so a run can say whether it saw any panel. */ export const DISCLOSE = String.raw`(() => { var opened = 0; var roots = document.querySelectorAll('nav, [role="navigation"], header'); for (var i = 0; i < roots.length; i++) { var ds = roots[i].querySelectorAll("details"); for (var j = 0; j < ds.length; j++) { if (!ds[j].open) { ds[j].open = true; opened++; } } } return opened; })()`; /** * Passed to the page as a STRING, not a function: a bundler that injects a * `__name` helper into arrow functions makes page.evaluate throw * "ReferenceError: __name is not defined" inside the browser. */ export const MEASURE = String.raw`(() => { // ── colour ──────────────────────────────────────────────────────────────── // Resolve ANY CSS colour syntax (oklch, lab, color-mix, var) to straight // RGBA. One paint is not enough: a translucent colour composites against // whatever the canvas already holds, silently discarding alpha and turning a // 4%-white overlay into SOLID WHITE. Paint twice over known backdrops and // solve: white - black = 255*(1-a). var resolve = function (color) { var c = document.createElement("canvas"); c.width = c.height = 1; var x = c.getContext("2d", { willReadFrequently: true }); var paint = function (bd) { x.globalCompositeOperation = "copy"; x.fillStyle = bd; x.fillRect(0, 0, 1, 1); x.globalCompositeOperation = "source-over"; x.fillStyle = color; x.fillRect(0, 0, 1, 1); return Array.prototype.slice.call(x.getImageData(0, 0, 1, 1).data, 0, 3); }; var b = paint("#000"), w = paint("#fff"); var a = 1 - (w[0] - b[0]) / 255; if (a <= 0.0001) return [0, 0, 0, 0]; return [b[0] / a, b[1] / a, b[2] / a, a]; }; var over = function (t, bo) { var a = t[3]; return [t[0]*a + bo[0]*(1-a), t[1]*a + bo[1]*(1-a), t[2]*a + bo[2]*(1-a), 1]; }; var lum = function (r) { var f = function (v) { var s = v/255; return s <= 0.03928 ? s/12.92 : Math.pow((s+0.055)/1.055, 2.4); }; return 0.2126*f(r[0]) + 0.7152*f(r[1]) + 0.0722*f(r[2]); }; // Composite every translucent layer down to the first opaque ancestor — // that stack is what the eye actually sees behind the glyphs. Returns null // when an image is in the way: an unknown backdrop must be skipped, not // guessed at, or the audit invents failures. var bgOf = function (el) { var st = [], n = el; while (n) { var cs = getComputedStyle(n); if (cs.backgroundImage && cs.backgroundImage !== "none") return null; var c = resolve(cs.backgroundColor); if (c[3] > 0) st.push(c); if (c[3] >= 0.999) break; n = n.parentElement; } if (!st.length) return [0, 0, 0]; var base = st[st.length - 1]; if (base[3] < 1) base = over(base, [0, 0, 0, 1]); for (var i = st.length - 2; i >= 0; i--) base = over(st[i], base); return [base[0], base[1], base[2]]; }; var ratio = function (fgRgba, bg) { var fg = fgRgba[3] < 1 ? over(fgRgba, bg.concat([1])) : fgRgba; var a = lum(fg), b = lum(bg); var hi = Math.max(a, b), lo = Math.min(a, b); return Math.round(((hi + 0.05) / (lo + 0.05)) * 100) / 100; }; var contrast = []; var actions = document.querySelectorAll('a, button, summary, [role="button"], [role="link"], [role="tab"]'); for (var i = 0; i < actions.length; i++) { var el = actions[i]; var text = (el.innerText || "").trim(); if (!text) continue; // A wrapper whose visible label is painted by a nested action measures the // wrapper's own never-painted ink: surf-your-life's bare around a // styled