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([]); });