/** * Pass 2 Session A - mobile layout audit. * * Walks every frontend route at 390x844 against a running dev server and * classifies issues into pattern classes: * overflow horizontal scroll / elements crossing the viewport edge * dialog dialogs/panels taller than the viewport * grid multi-track grids that are cramped at mobile width * hoverOnly affordances revealed only via (group-)hover * hiddenMobile content gated behind md:/lg: breakpoints (invisible on mobile) * tapTarget interactive elements under 24px (WCAG 2.5.8 violation) * tapTargetMarginal interactive elements 24-39px (review tier) * * Usage: bun run tests/audit/mobile-audit.ts * Env: AUDIT_BASE_URL (default http://localhost:3000) * AUDIT_USER / AUDIT_PASS (default dev / Test123) * AUDIT_ROUTE_FILTER (substring match, or "=/route" for an exact match) * E2E_BROWSER_PATH (browser executable override) * * Outputs tests/audit/mobile-audit-report.json (regression gate artifact for * Sessions B-D) and mobile-audit-report.md (human issue matrix). */ import { chromium, type BrowserContext, type Page } from "@playwright/test"; import { mkdirSync, writeFileSync } from "node:fs"; import { dirname, join } from "node:path"; const BASE_URL = process.env.AUDIT_BASE_URL ?? "http://localhost:3000"; const USER = process.env.AUDIT_USER ?? "dev"; const PASS = process.env.AUDIT_PASS ?? "Test123"; const VIEWPORT = { width: 390, height: 844 }; const OUT_DIR = join(__dirname); // Optional substring filter for targeted re-audits (Sessions B-D). When set, // reports go to mobile-audit-filtered.{json,md} so the baseline is untouched. const ROUTE_FILTER = process.env.AUDIT_ROUTE_FILTER ?? ""; let authToken: string | null = null; type Issue = { kind: string; detail: string; selector?: string; size?: string }; type RouteResult = { route: string; url: string; status: "ok" | "skipped" | "error"; httpStatus?: number; note?: string; issues: Issue[]; }; /** Static routes, straight from the filesystem. */ const STATIC_ROUTES = [ "/", "/account", "/awards", "/awards/design", "/awards/review", "/contributions", "/events", "/helpdesk", "/intelligence", "/intelligence/campaigns", "/intelligence/factions", "/intelligence/missions", "/intelligence/missions/calendar", "/journals", "/locker", "/logistics", "/logistics/banking", "/logistics/game-vehicles", "/logistics/market", "/logistics/resources", "/logistics/shipments", "/logistics/supply-boxes", "/logistics/vehicles", "/map", "/notifications", "/operations", "/operations/ledger", "/operations/reservations", "/personnel/contacts", "/personnel/promotions", "/personnel/statistics", "/projects", "/projects/search", "/roster", "/story", "/surveys", "/transfers", "/voiceover", "/wiki", "/wiki/new", "/minigames/blackjack", "/minigames/call-for-fire", "/minigames/call-for-fire-versus", "/minigames/eod", "/minigames/eod/custom", "/minigames/flappy", "/minigames/leaderboards", "/minigames/opord", "/minigames/poker", "/minigames/qualification", "/minigames/radio", "/minigames/radio/custom", ]; /** * Dynamic routes: template -> REST collection to resolve a real doc from the * dev DB, plus the field substituted into the path. `null` collection means * the route is skipped if no candidate resolves. */ const DYNAMIC_ROUTES: { template: string; collection: string | null; field: string; fallback?: string }[] = [ { template: "/helpdesk/[id]", collection: "tickets", field: "id" }, { template: "/journals/[id]", collection: "journals", field: "id" }, { template: "/surveys/[id]", collection: "forms", field: "id" }, { template: "/profile/[username]", collection: null, field: "", fallback: USER }, { template: "/operations/[operationId]", collection: "operations", field: "id" }, { template: "/operations/reservations/[id]", collection: "operation-reservations", field: "id" }, { template: "/intelligence/factions/[id]", collection: "factions", field: "id" }, { template: "/intelligence/campaigns/[id]", collection: "campaigns", field: "id" }, { template: "/intelligence/missions/[id]", collection: "missions", field: "id" }, { template: "/projects/[key]", collection: "projects", field: "key", fallback: "" }, { template: "/voiceover/[id]", collection: "voice-scripts", field: "id" }, { template: "/map/[id]", collection: "maps", field: "id" }, { template: "/personnel/contacts/[id]", collection: "contacts", field: "id" }, { template: "/logistics/supply-boxes/[id]", collection: "supply-boxes", field: "id" }, { template: "/logistics/structures/[id]", collection: "game-structures", field: "id" }, { template: "/logistics/banking/[id]", collection: "bank-accounts", field: "id" }, { template: "/logistics/resources/[id]", collection: "resources", field: "id" }, { template: "/logistics/shipments/[id]", collection: "shipments", field: "id" }, { template: "/logistics/game-vehicles/[id]", collection: "game-vehicles", field: "id" }, { template: "/logistics/vehicles/[id]", collection: "vehicles", field: "id" }, { template: "/minigames/eod/custom/[id]", collection: "custom-minefields", field: "id" }, { template: "/minigames/radio/custom/[id]", collection: "custom-radio-tests", field: "id" }, ]; /** Wiki routes need a slug/category instead of an id. */ const WIKI_ROUTES = [ { template: "/wiki/[slug]", suffix: "" }, { template: "/wiki/[slug]", suffix: "/history" }, { template: "/wiki/[slug]", suffix: "/edit" }, ]; async function resolveDoc(collection: string): Promise | null> { try { const res = await fetch(`${BASE_URL}/api/${collection}?limit=1&sort=-createdAt`, { headers: authToken ? { Cookie: `payload-token=${authToken}` } : {}, }); if (!res.ok) return null; const body = (await res.json()) as { docs?: Record[] }; return body.docs?.[0] ?? null; } catch { return null; } } /** Runs inside the page; returns raw issue records. */ async function collectIssues(): Promise { const issues: Issue[] = []; const vw = window.innerWidth; const vh = window.innerHeight; const path = (el: Element): string => { const parts: string[] = [el.tagName.toLowerCase()]; if (el.id) parts.push("#" + el.id); const cls = typeof el.className === "string" ? el.className.split(/\s+/).filter(Boolean).slice(0, 3) : []; if (cls.length) parts.push("." + cls.join(".")); const parent = el.parentElement; return (parent ? path(parent).split(" > ").pop() + " > " : "") + parts.join(""); }; // 1. Horizontal overflow const docW = document.documentElement.scrollWidth; if (docW > vw + 1) { issues.push({ kind: "overflow", detail: `document scrollWidth ${docW}px > ${vw}px` }); const offenders: { el: Element; over: number }[] = []; for (const el of Array.from(document.querySelectorAll("body *"))) { const rect = el.getBoundingClientRect(); if (rect.width < 8 || rect.height < 2) continue; const style = getComputedStyle(el); if (style.position === "fixed") continue; const over = Math.max(rect.right - vw, -rect.left); if (over > 4) offenders.push({ el, over }); } offenders.sort((a, b) => b.over - a.over); for (const o of offenders.slice(0, 4)) { issues.push({ kind: "overflow", detail: `crosses edge by ${Math.round(o.over)}px`, selector: path(o.el), }); } } // 2. Dialogs taller than viewport for (const dlg of Array.from(document.querySelectorAll('[role="dialog"],[role="alertdialog"]'))) { const rect = dlg.getBoundingClientRect(); if (rect.height > vh + 1 || rect.bottom > vh + 1) { issues.push({ kind: "dialog", detail: `height ${Math.round(rect.height)}px > viewport ${vh}px`, selector: path(dlg), }); } } // 3. Grids that overflow a container which cannot scroll. Wide-by-design // layouts (min-w-max boards) and grids inside scroll containers are // intentional, and narrow icon tracks are not cramped layouts. const hasScrollAncestor = (el: Element): boolean => { let node = el.parentElement; while (node && node !== document.body) { if (/(auto|scroll)/.test(getComputedStyle(node).overflowX)) return true; node = node.parentElement; } return false; }; for (const el of Array.from(document.querySelectorAll("*"))) { const style = getComputedStyle(el); if (style.display !== "grid") continue; const tracks = style.gridTemplateColumns.split(" ").filter(Boolean); if (tracks.length < 2) continue; const rect = el.getBoundingClientRect(); if (rect.width < 40) continue; if (style.minWidth === "max-content" || hasScrollAncestor(el)) continue; const overflowing = el.scrollWidth > el.clientWidth + 1 || rect.right > vw + 4; if (overflowing) { issues.push({ kind: "grid", detail: `${tracks.length}-track grid overflows its container (${Math.round(rect.width)}px wide)`, selector: path(el), }); } } // 4. Hover-only affordances (invisible until hover/group-hover) for (const el of Array.from(document.querySelectorAll('[class*="group-hover:"],[class*="hover:opacity"],[class*="hover:visible"],[class*="hover:block"],[class*="hover:flex"]'))) { const style = getComputedStyle(el); const cls: string = typeof el.className === "string" ? el.className : ""; const revealsOpacity = /(?:group-)?hover:opacity-\d/.test(cls) && parseFloat(style.opacity) < 0.05; const revealsDisplay = /(?:group-)?hover:(block|flex)/.test(cls) && style.display === "none"; if (revealsOpacity || revealsDisplay) { issues.push({ kind: "hoverOnly", detail: "affordance only visible on hover", selector: path(el) }); } } // 5. Content hidden at mobile behind md:/lg: reveal utilities. shadcn's // Sidebar ships desktop-only scaffolding (peer spacer + fixed container) // and Tailwind `peer` helpers are styling aids, never user content. for (const el of Array.from(document.querySelectorAll('[class*="md:block"],[class*="md:flex"],[class*="lg:block"],[class*="lg:flex"],[class*="md:inline"],[class*="lg:inline"]'))) { const cls: string = typeof el.className === "string" ? el.className : ""; if (!/(?:^|\s)hidden(?=\s|$)/.test(cls)) continue; if (el.closest(".peer") || el.closest("aside")) continue; const style = getComputedStyle(el); if (style.display !== "none") continue; issues.push({ kind: "hiddenMobile", detail: "hidden on mobile (md:/lg: reveal)", selector: path(el) }); } // 6. Tap targets: WCAG 2.5.8 floor is 24px; 24-39px is a marginal tier for // review (shadcn defaults live there). The measured box is the EFFECTIVE // target: a control wrapped in (or labelled by) a