diff --git a/webui/.gitignore b/webui/.gitignore index a547bf3..e7fe242 100644 --- a/webui/.gitignore +++ b/webui/.gitignore @@ -22,3 +22,7 @@ dist-ssr *.njsproj *.sln *.sw? + +# Playwright visual harness output +.screenshots/ +test-results/ diff --git a/webui/e2e/checks.ts b/webui/e2e/checks.ts new file mode 100644 index 0000000..1ffdf9c --- /dev/null +++ b/webui/e2e/checks.ts @@ -0,0 +1,127 @@ +import AxeBuilder from '@axe-core/playwright' +import type { Page } from '@playwright/test' + +export interface CheckResult { + name: CheckName + ok: boolean + detail: string +} + +export const CHECK_NAMES = [ + 'noConsoleErrors', + 'noHorizontalOverflow', + 'headerAlignsWithBody', + 'numbersRightAligned', + 'focusRingVisible', + 'axeNoSeriousOrCritical', + 'no-literal-NaN-undefined', +] as const +export type CheckName = (typeof CHECK_NAMES)[number] + +const ALIGN_TOLERANCE_PX = 1 +const FOCUS_TRANSITION_MS = 300 +const LITERAL_RE = /\bNaN\b|undefined/ + +/** Console errors and uncaught page errors seen since the collector was attached. */ +export function collectConsoleErrors(page: Page, ignore: RegExp[] = []): () => string[] { + const errors: string[] = [] + page.on('console', (m) => { + if (m.type() === 'error' && !ignore.some((re) => re.test(m.text()))) errors.push(m.text()) + }) + page.on('pageerror', (e) => errors.push(`pageerror: ${e.message}`)) + return () => errors +} + +export function noConsoleErrors(errors: string[]): CheckResult { + return { name: 'noConsoleErrors', ok: errors.length === 0, detail: errors.slice(0, 3).join(' | ') } +} + +export async function noHorizontalOverflow(page: Page): Promise { + const { sw, iw } = await page.evaluate(() => ({ sw: document.documentElement.scrollWidth, iw: window.innerWidth })) + return { name: 'noHorizontalOverflow', ok: sw <= iw, detail: `scrollWidth ${sw} > innerWidth ${iw}` } +} + +export async function headerAlignsWithBody(page: Page): Promise { + const bad = await page.evaluate((tol) => { + const out: string[] = [] + document.querySelectorAll('table').forEach((table, ti) => { + const ths = Array.from(table.querySelectorAll('thead th')) + const row = table.querySelector('tbody tr') + if (!ths.length || !row) return + const cells = Array.from(row.children) + ths.forEach((th, i) => { + const td = cells[i] + const a = th.getBoundingClientRect() + const b = td?.getBoundingClientRect() + if (!b || a.width === 0 || b.width === 0) return + if (Math.abs(a.left - b.left) > tol) out.push(`table ${ti} col ${i} header ${a.left.toFixed(1)} body ${b.left.toFixed(1)}`) + }) + }) + return out + }, ALIGN_TOLERANCE_PX) + return { name: 'headerAlignsWithBody', ok: bad.length === 0, detail: bad.slice(0, 3).join(' | ') } +} + +export async function numbersRightAligned(page: Page): Promise { + const { total, bad } = await page.evaluate(() => { + const cells = Array.from(document.querySelectorAll('[data-numeric]')) + const wrong = cells.filter((el) => !['right', 'end'].includes(getComputedStyle(el).textAlign)) + return { total: cells.length, bad: wrong.length } + }) + return { name: 'numbersRightAligned', ok: bad === 0, detail: `${bad} of ${total} [data-numeric] cells not right aligned` } +} + +// perfect-design draws its focus ring as a box-shadow spread with `outline-style: none`; a ring is +// a layer with zero offset and blur, a spread above 0 and a non-transparent colour. +const RING_LAYER_RE = /(rgba?\([^)]*\)|oklab\([^)]*\)|oklch\([^)]*\)|color\([^)]*\)|#[0-9a-f]+)\s+0px\s+0px\s+0px\s+([\d.]+)px/g + +export async function focusRingVisible(page: Page): Promise { + await page.evaluate(() => (document.activeElement as HTMLElement | null)?.blur()) + await page.keyboard.press('Tab') + await page.waitForTimeout(FOCUS_TRANSITION_MS) + const info = await page.evaluate(() => { + const el = document.activeElement + if (!el || el === document.body) return null + const cs = getComputedStyle(el) + return { tag: el.tagName.toLowerCase(), width: parseFloat(cs.outlineWidth) || 0, style: cs.outlineStyle, shadow: cs.boxShadow } + }) + if (!info) return { name: 'focusRingVisible', ok: false, detail: 'Tab focused nothing' } + const outline = info.width > 0 && info.style !== 'none' + const ring = Array.from(info.shadow.matchAll(RING_LAYER_RE)).some( + (m) => parseFloat(m[2]) > 0 && !/rgba\(0, 0, 0, 0\)|\/ 0\)/.test(m[1]), + ) + return { name: 'focusRingVisible', ok: outline || ring, detail: `${info.tag} outline ${info.width}px ${info.style}, ring ${ring}` } +} + +export async function axeNoSeriousOrCritical(page: Page): Promise { + const { violations } = await new AxeBuilder({ page }).analyze() + const bad = violations.filter((v) => v.impact === 'serious' || v.impact === 'critical') + return { name: 'axeNoSeriousOrCritical', ok: bad.length === 0, detail: bad.map((v) => `${v.id}(${v.nodes.length})`).join(', ') } +} + +export async function noLiteralNaNUndefined(page: Page): Promise { + const text = await page.evaluate(() => document.body.innerText) + const m = LITERAL_RE.exec(text) + const ctx = m ? text.slice(Math.max(0, m.index - 30), m.index + 30).replace(/\s+/g, ' ') : '' + return { name: 'no-literal-NaN-undefined', ok: !m, detail: ctx } +} + +async function guarded(name: CheckName, run: () => Promise): Promise { + try { + return await run() + } catch (e) { + return { name, ok: false, detail: `check threw: ${(e as Error).message.split('\n')[0]}` } + } +} + +export async function runChecks(page: Page, consoleErrors: string[]): Promise { + return [ + noConsoleErrors(consoleErrors), + await guarded('noHorizontalOverflow', () => noHorizontalOverflow(page)), + await guarded('headerAlignsWithBody', () => headerAlignsWithBody(page)), + await guarded('numbersRightAligned', () => numbersRightAligned(page)), + await guarded('axeNoSeriousOrCritical', () => axeNoSeriousOrCritical(page)), + await guarded('no-literal-NaN-undefined', () => noLiteralNaNUndefined(page)), + await guarded('focusRingVisible', () => focusRingVisible(page)), + ] +} diff --git a/webui/e2e/visual.spec.ts b/webui/e2e/visual.spec.ts new file mode 100644 index 0000000..ff85e3f --- /dev/null +++ b/webui/e2e/visual.spec.ts @@ -0,0 +1,90 @@ +import { mkdirSync, writeFileSync } from 'node:fs' +import { join } from 'node:path' + +import { expect, test } from '@playwright/test' +import type { Page } from '@playwright/test' + +import { PAGE_IDS } from '../src/lib/pages' +import { collectConsoleErrors, runChecks } from './checks' + +const SCENARIOS = ['live', 'empty', 'limits-hot', 'breaker-open', 'redis-down', 'auth-401', 'huge'] as const +const VIEWPORTS = { + desktop: { width: 1440, height: 900 }, + phone: { width: 390, height: 844 }, +} as const +const THEMES = ['light', 'dark'] as const +const KEY_STORAGE = 'llmgwAdminKey' +const OUT = join(import.meta.dirname, '..', '.screenshots') +const SETTLE_MS = 600 +// Chromium logs every 401 fetch as a console error; auth-401 forces one by design. +const AUTH_401_NOISE = /Failed to load resource.*status of 40[13]/ + +async function sampleIds(page: Page): Promise> { + const key = { 'x-api-key': 'e2e-dummy-key' } + const get = async (path: string) => (await page.request.get(`/admin/api/${path}`, { headers: key })).json() + const [usage, catalog, targets] = await Promise.all([get('usage'), get('catalog'), get('targets')]) + return { + user: `user:${encodeURIComponent(usage.users[0].id)}`, + model: `model:${encodeURIComponent(catalog.providers[0].models[0].id)}`, + router: `router:${encodeURIComponent(catalog.routers[0].name)}`, + target: `target:${encodeURIComponent((targets.mcpServers[0] ?? targets.agents[0]).name)}`, + } +} + +const PANEL_PAGES: Array<{ page: (typeof PAGE_IDS)[number]; kind: string }> = [ + { page: 'users', kind: 'user' }, + { page: 'models', kind: 'model' }, + { page: 'routers', kind: 'router' }, + { page: 'targets', kind: 'target' }, +] + +async function visit(page: Page, hash: string, scenario: string, theme: string): Promise { + await page.addInitScript( + ([k, t]) => { + sessionStorage.setItem(k, 'e2e-dummy-key') + localStorage.setItem('pd-theme', JSON.stringify({ mode: t })) + }, + [KEY_STORAGE, theme], + ) + await page.emulateMedia({ colorScheme: theme as 'light' | 'dark' }) + await page.goto(`/admin/?mock=${scenario}${hash}`) + await page.waitForLoadState('networkidle').catch(() => undefined) + await page.waitForTimeout(SETTLE_MS) +} + +async function exercise(page: Page, label: string, hash: string, scenario: string, viewport: keyof typeof VIEWPORTS, theme: string): Promise { + const errors = collectConsoleErrors(page, scenario === 'auth-401' ? [AUTH_401_NOISE] : []) + await visit(page, hash, scenario, theme) + mkdirSync(OUT, { recursive: true }) + const name = `${label}-${scenario}-${viewport}-${theme}` + await page.screenshot({ path: join(OUT, `${name}.png`), fullPage: true }) + const results = await runChecks(page, errors()) + mkdirSync(join(OUT, 'results'), { recursive: true }) + writeFileSync(join(OUT, 'results', `${name}.json`), JSON.stringify({ label, scenario, viewport, theme, results })) + for (const r of results) expect.soft(r.ok, `${r.name}: ${r.detail}`).toBe(true) +} + +for (const id of PAGE_IDS) { + for (const scenario of SCENARIOS) { + for (const [vp, size] of Object.entries(VIEWPORTS)) { + for (const theme of THEMES) { + test(`${id} ${scenario} ${vp} ${theme}`, async ({ page }) => { + await page.setViewportSize(size) + await exercise(page, id, `#${id}`, scenario, vp as keyof typeof VIEWPORTS, theme) + }) + } + } + } +} + +for (const { page: pageId, kind } of PANEL_PAGES) { + for (const [vp, size] of Object.entries(VIEWPORTS)) { + for (const theme of THEMES) { + test(`${pageId} panel ${kind} live ${vp} ${theme}`, async ({ page }) => { + await page.setViewportSize(size) + const sel = (await sampleIds(page))[kind] + await exercise(page, `${pageId}-panel-${kind}`, `#${pageId}?sel=${encodeURIComponent(sel)}`, 'live', vp as keyof typeof VIEWPORTS, theme) + }) + } + } +} diff --git a/webui/package-lock.json b/webui/package-lock.json index 935fc41..824e808 100644 --- a/webui/package-lock.json +++ b/webui/package-lock.json @@ -27,6 +27,8 @@ "vue-chartjs": "^5.3.4" }, "devDependencies": { + "@axe-core/playwright": "^4.14.1", + "@playwright/test": "^1.65.1", "@tailwindcss/vite": "^4.3.3", "@types/node": "^24.13.3", "@vitejs/plugin-vue": "^6.0.8", @@ -38,6 +40,22 @@ "vue-tsc": "^3.3.8" } }, + "node_modules/@axe-core/playwright": { + "version": "4.14.1", + "resolved": "http://gohoarder.h.raczylo.com/npm/@axe-core/playwright/-/playwright-4.14.1.tgz", + "integrity": "sha512-cSN1ijrWLnlOof22sPN4RtWGSoPpt7GzOVyOKomyhnPsy1fPNoRdBiJAUziuCRs8KoK50kKufiMUFt7itAuwOg==", + "dev": true, + "license": "MPL-2.0", + "dependencies": { + "axe-core": "~4.14.1" + }, + "engines": { + "node": ">=22.12.0" + }, + "peerDependencies": { + "playwright-core": ">= 1.0.0" + } + }, "node_modules/@babel/code-frame": { "version": "7.29.7", "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.29.7.tgz", @@ -979,6 +997,22 @@ "vue": "^3.5.0" } }, + "node_modules/@playwright/test": { + "version": "1.65.1", + "resolved": "http://gohoarder.h.raczylo.com/npm/@playwright/test/-/test-1.65.1.tgz", + "integrity": "sha512-y+NCIRm2LvY4slUVWvGh1nujJ8RF/hEisIsq2mtUPgdQC4L701bq5hZGMhrpeXjACvwvO9tj5dJqhvM6TP9lbw==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "playwright": "1.65.1" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=20" + } + }, "node_modules/@rolldown/binding-android-arm-eabi": { "version": "1.2.5", "resolved": "https://registry.npmjs.org/@rolldown/binding-android-arm-eabi/-/binding-android-arm-eabi-1.2.5.tgz", @@ -2565,6 +2599,16 @@ "node": ">= 4.5.0" } }, + "node_modules/axe-core": { + "version": "4.14.1", + "resolved": "http://gohoarder.h.raczylo.com/npm/axe-core/-/axe-core-4.14.1.tgz", + "integrity": "sha512-FHfa+lmNjQZfJz1q8wKqIDwC6DExcHt/pkjNQb09a3NPjCKg2rhu5qb2S4r76ASyFNNtuFooJpZe37OgudVGtQ==", + "dev": true, + "license": "MPL-2.0", + "engines": { + "node": ">=4" + } + }, "node_modules/balanced-match": { "version": "4.0.4", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-4.0.4.tgz", @@ -5666,6 +5710,35 @@ "pathe": "^2.0.3" } }, + "node_modules/playwright": { + "version": "1.65.1", + "resolved": "http://gohoarder.h.raczylo.com/npm/playwright/-/playwright-1.65.1.tgz", + "integrity": "sha512-vJ1FuoOrNwOKHUIVEvSt3jT1L+wjjDpwhwIstKtfvTh8XIG4FG4bIFuvNdMxi81LGfAhUgUZleVFaOLY4RIUUw==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "playwright-core": "1.65.1" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=20" + } + }, + "node_modules/playwright-core": { + "version": "1.65.1", + "resolved": "http://gohoarder.h.raczylo.com/npm/playwright-core/-/playwright-core-1.65.1.tgz", + "integrity": "sha512-hqPpz7blSMPxWVdoeE1yiBSzXsKiVOd2k+kjCvmUywD2GPO2Dd9HfcWnq9NN0GHbNZDFgZ9JfBFr+X5/BLo0qw==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "playwright-core": "cli.js" + }, + "engines": { + "node": ">=20" + } + }, "node_modules/postcss": { "version": "8.5.26", "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.26.tgz", diff --git a/webui/package.json b/webui/package.json index 2c27492..b28e2ee 100644 --- a/webui/package.json +++ b/webui/package.json @@ -8,7 +8,8 @@ "dev:mock": "MOCK=1 vite", "build": "vue-tsc -b && vite build", "preview": "vite preview", - "test": "vitest run" + "test": "vitest run", + "test:visual": "playwright test" }, "dependencies": { "@fortawesome/fontawesome-svg-core": "^7.3.1", @@ -30,6 +31,8 @@ "vue-chartjs": "^5.3.4" }, "devDependencies": { + "@axe-core/playwright": "^4.14.1", + "@playwright/test": "^1.65.1", "@tailwindcss/vite": "^4.3.3", "@types/node": "^24.13.3", "@vitejs/plugin-vue": "^6.0.8", diff --git a/webui/playwright.config.ts b/webui/playwright.config.ts new file mode 100644 index 0000000..6f2ef68 --- /dev/null +++ b/webui/playwright.config.ts @@ -0,0 +1,20 @@ +import { defineConfig } from '@playwright/test' + +const PORT = 5199 + +export default defineConfig({ + testDir: './e2e', + testMatch: '*.spec.ts', + outputDir: '.screenshots/.pw-output', + fullyParallel: true, + workers: 4, + timeout: 120_000, + reporter: [['list']], + use: { baseURL: `http://localhost:${PORT}`, browserName: 'chromium' }, + webServer: { + command: `npm run dev:mock -- --port ${PORT} --strictPort`, + url: `http://localhost:${PORT}/admin/`, + reuseExistingServer: !process.env.CI, + timeout: 60_000, + }, +}) diff --git a/webui/src/assets/main.css b/webui/src/assets/main.css index 1842bca..140ff9c 100644 --- a/webui/src/assets/main.css +++ b/webui/src/assets/main.css @@ -197,3 +197,13 @@ radial-gradient(farthest-side at 0 50%, oklch(0 0 0 / 0.22), transparent) 0 0 / 0.6rem 100% no-repeat scroll, radial-gradient(farthest-side at 100% 50%, oklch(0 0 0 / 0.22), transparent) 100% 0 / 0.6rem 100% no-repeat scroll; } + +/* Workbench table: 8px grid, 12px cell inset, 32px body rows, 30px header row. */ +.wb-table table { table-layout: auto; } +.wb-table :is(th, td) { padding-inline: 12px; } +.wb-table thead :is(th) { height: 30px; padding-block: 0; } +.wb-table tbody :is(td, th) { height: 32px; padding-block: 0; } +.wb-table [data-numeric] { display: block; text-align: right; font-family: var(--font-mono); font-variant-numeric: tabular-nums; line-height: 20px; } +.wb-table :is(th, td).wb-num { text-align: right; } +.wb-table th.wb-num > * { justify-content: flex-end; } +.wb-table .wb-share-bar { min-width: 96px; } diff --git a/webui/src/layout/AppHeader.vue b/webui/src/layout/AppHeader.vue index b494f9a..5b75d2d 100644 --- a/webui/src/layout/AppHeader.vue +++ b/webui/src/layout/AppHeader.vue @@ -95,7 +95,7 @@ function onRange(value: string): void { /> refresh failed -