test(tune): cover live dashboard in browsers
a5adf07849196f8dc9b72c567f494ac041f68c15
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
- Parent 1: 7f1e0bf4727c
bugabingas pi extensions
a5adf07849196f8dc9b72c567f494ac041f68c15
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
+187 / −0 text lines
new file mode 100644+78 / −0 text lines
import { appendFile, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";import { tmpdir } from "node:os";import { join } from "node:path";import { expect, test } from "@playwright/test";import { startTuneDashboard, type TuneDashboard } from "../dashboard.ts";let dashboard: TuneDashboard | undefined;let dir = "";test.afterEach(async () => { dashboard?.close(); dashboard = undefined; if (dir) await rm(dir, { recursive: true, force: true }); dir = "";});test("loads runs, switches theme, and receives live updates", async ({ page,}) => { const pageErrors: string[] = []; page.on("pageerror", (error) => pageErrors.push(error.message)); dir = await mkdtemp(join(tmpdir(), "tune-e2e-")); const jsonlPath = join(dir, "session.jsonl"); const template = await readFile( join(import.meta.dirname, "..", "assets", "template.html"), "utf8", ); const html = template.replace("__TUNE_TITLE__", "Latency"); await writeFile( jsonlPath, [ JSON.stringify({ type: "config", name: "Latency", metricName: "latency", metricUnit: "ms", bestDirection: "lower", }), JSON.stringify({ run: 1, metric: 100, status: "keep", description: "baseline", }), ].join("\n") + "\n", ); dashboard = await startTuneDashboard(html, jsonlPath); const eventsReady = page.waitForResponse( (response) => response.url().endsWith("/events") && response.status() === 200, ); await page.goto(dashboard.url); await eventsReady; await expect(page.locator("#title")).toHaveText("Latency"); await expect(page.locator("#summary")).toContainText("latency (ms)"); await expect(page.locator("#runs")).toHaveText("1"); await expect(page.locator("#rows")).toContainText("baseline"); await page.locator("#theme-mode").selectOption("dark"); await expect(page.locator("html")).toHaveAttribute("data-theme", "dark"); await appendFile( jsonlPath, JSON.stringify({ run: 2, metric: 80, status: "keep", description: "faster", }) + "\n", ); dashboard.broadcast(); await expect(page.locator("#runs")).toHaveText("2"); await expect(page.locator("#rows")).toContainText("faster"); await expect(page.locator("#baseline-best")).toHaveText("100ms → 80ms"); expect(pageErrors).toEqual([]);});new file mode 100644+98 / −0 text lines
import { readFile } from "node:fs";import { createServer, type Server, type ServerResponse } from "node:http";export type TuneDashboard = { url: string; update(html: string, jsonlPath: string): void; broadcast(): void; close(): void;};export async function startTuneDashboard( html: string, jsonlPath: string,): Promise<TuneDashboard> { let currentHtml = html; let currentJsonlPath = jsonlPath; let closed = false; const clients = new Set<ServerResponse>(); const server = createServer((req, res) => { const url = new URL(req.url ?? "/", "http://127.0.0.1"); if (url.pathname === "/events") { res.writeHead(200, { "Content-Type": "text/event-stream", "Cache-Control": "no-cache", Connection: "keep-alive", }); res.write("retry: 1000\n\n"); clients.add(res); res.on("close", () => clients.delete(res)); return; } if (url.pathname === "/") { res.writeHead(200, { "Content-Type": "text/html; charset=utf-8", "Cache-Control": "no-store", }); res.end(currentHtml); return; } if (url.pathname === "/session.jsonl") { readFile(currentJsonlPath, (error, data) => { if (error) { res.writeHead(404); res.end(); return; } res.writeHead(200, { "Content-Type": "application/x-ndjson; charset=utf-8", "Cache-Control": "no-store", }); res.end(data); }); return; } res.writeHead(404); res.end(); }); const port = await listen(server); return { url: `http://127.0.0.1:${port}`, update(nextHtml, nextJsonlPath) { currentHtml = nextHtml; currentJsonlPath = nextJsonlPath; }, broadcast() { for (const client of clients) { try { client.write("event: jsonl-updated\n"); client.write(`data: ${Date.now()}\n\n`); } catch { clients.delete(client); } } }, close() { if (closed) return; closed = true; for (const client of clients) client.end(); clients.clear(); server.close(); }, };}function listen(server: Server): Promise<number> { return new Promise((resolve, reject) => { server.once("error", reject); server.listen(0, "127.0.0.1", () => { server.off("error", reject); const address = server.address(); if (!address || typeof address === "string") { reject(new Error("Failed to bind tune dashboard server")); return; } resolve(address.port); }); });}+1 / −0 text lines
new file mode 100644+9 / −0 text lines
+1 / −0 text lines