// Page tests for User Preferences: the saved looks (highlight and theme), the // launcher's model menus and the Cleanup tab. Run with: // node --test scripts/test-web-console-page-prefs.mjs // // Each test drives the real console in a real Chrome or Edge through // scripts/page-test-helpers.mjs: one browser for the file, a fresh console // session per test. import fs from "node:fs"; import path from "node:path"; import test from "node:test"; import assert from "node:assert/strict"; import { pageSuite, openSession, textOf, drawn } from "./page-test-helpers.mjs"; import { HOME, MODELS_DIR, backdate } from "./web-console-test-helpers.mjs"; import { formatBytes } from "../src/web-console/public/answers.js"; const suite = pageSuite(test); // A plan session on the person's turn: one text question and the agent listening. const PLAN_QUESTION = { items: [{ id: "q1", kind: "text", title: "What to call it", body: "A short name for the export." }], agent: { status: "waiting" }, }; // The console home's saved looks are shared by every session of this file, so // each test starts with none and leaves none. const LOOKS_FILE = path.join(HOME, "looks.json"); function noSavedLooks(t) { fs.rmSync(LOOKS_FILE, { force: true }); t.after(() => fs.rmSync(LOOKS_FILE, { force: true })); } const root = (s, attr) => s.page.evaluate((a) => document.documentElement.getAttribute(a), attr); // The highlight the page paints with: an inline override while it is not the default. const accentVar = (s) => s.page.evaluate(() => document.documentElement.style.getPropertyValue("--accent")); const swatch = (s, id) => s.page.locator(`#looks-swatches [data-accent="${id}"]`); // The page as the server first sends it, before any script has run: the saved // theme and highlight are in the markup so a reload never flashes the defaults. async function firstPaintAttrs(s) { const html = await (await s.context.request.get(s.url)).text(); const tag = html.match(/]*>/i)[0]; return { theme: tag.match(/data-theme="([^"]*)"/)?.[1] ?? null, accent: tag.match(/data-looks-accent="([^"]*)"/)?.[1] ?? null }; } test("the violet highlight and dark theme chosen in preferences stick across a reload, and Back to default clears them", async (t) => { noSavedLooks(t); const s = await openSession(await suite.browser(), { t, workflow: "plan", payload: PLAN_QUESTION }); assert.equal(await root(s, "data-theme"), null, "the theme starts on the system's"); await s.page.click("#btn-looks"); await s.page.locator("#looks-modal[open]").waitFor(); await swatch(s, "violet").click(); await s.page.locator('input[name="looks-theme"][value="dark"]').check(); await s.page.waitForFunction(() => document.documentElement.getAttribute("data-theme") === "dark"); assert.equal(await swatch(s, "violet").getAttribute("aria-pressed"), "true"); assert.equal(await accentVar(s), "#a785e2", "the dark violet is painted"); // Saved on the server (looks.json), not only in this tab. await s.page.waitForFunction(async () => { const saved = (await (await fetch("/looks")).json()).looks || {}; return saved.accent === "violet" && saved.theme === "dark"; }); await s.page.click("#looks-done"); // What the server sends first, and what the page settles on, both keep the choice. assert.deepEqual(await firstPaintAttrs(s), { theme: "dark", accent: "violet" }, "the first paint carries the saved looks"); await s.page.reload({ waitUntil: "domcontentloaded" }); await drawn(s.page); assert.equal(await root(s, "data-theme"), "dark", "the theme did not stick"); await s.page.waitForFunction(() => document.documentElement.style.getPropertyValue("--accent") === "#a785e2"); await s.page.click("#btn-looks"); assert.equal(await swatch(s, "violet").getAttribute("aria-pressed"), "true", "the violet swatch is not the chosen one after a reload"); assert.equal(await s.page.locator('input[name="looks-theme"][value="dark"]').isChecked(), true); // Back to default returns both, here and for the next page. await s.page.click("#looks-reset"); await s.page.waitForFunction(() => !document.documentElement.hasAttribute("data-theme")); assert.equal(await accentVar(s), "", "the chosen highlight is still painted"); assert.equal(await swatch(s, "violet").getAttribute("aria-pressed"), "false"); assert.equal(await s.page.locator('input[name="looks-theme"][value="system"]').isChecked(), true); await s.page.waitForFunction(async () => { const saved = (await (await fetch("/looks")).json()).looks || {}; return saved.theme === "system" && saved.accent !== "violet"; }); assert.deepEqual(await firstPaintAttrs(s), { theme: null, accent: null }, "the first paint still carries the old looks"); }); test("the launcher's model menus have Claude, Devin and Codex tabs, and a model id typed on the Codex tab is saved", async (t) => { noSavedLooks(t); // The file the launcher reads: put back as the suite had it (no file). t.after(() => fs.rmSync(path.join(MODELS_DIR, "models.json"), { force: true })); const s = await openSession(await suite.browser(), { t, workflow: "plan", payload: PLAN_QUESTION }); await s.page.click("#btn-looks"); const tabs = s.page.getByRole("group", { name: "Which agent" }); for (const name of ["Claude", "Devin", "Codex"]) assert.equal(await tabs.getByRole("button", { name, exact: true }).count(), 1, `a ${name} tab`); const pressed = async () => (await tabs.locator('button[aria-pressed="true"]').allInnerTexts()).map((x) => x.trim()); assert.deepEqual(await pressed(), ["Claude"], "the menus open on Claude"); await tabs.getByRole("button", { name: "Codex", exact: true }).click(); assert.deepEqual(await pressed(), ["Codex"]); await s.page.click("#models-add"); const id = s.page.locator("#models-user").getByRole("textbox", { name: "Model id" }); await id.fill("gpt-test-codex-1"); await id.press("Tab"); await s.page.locator("#models-status").filter({ hasText: /^Saved\./ }).waitFor(); const saved = await s.page.evaluate(async () => (await (await fetch("/models")).json()).user); assert.deepEqual(saved.codex.map((m) => m.id), ["gpt-test-codex-1"], "the id was not saved under Codex"); assert.deepEqual([saved.claude, saved.devin], [[], []], "the id leaked into another agent's menu"); // Back on Claude the typed id is not listed; on Codex it is, after a reload. await tabs.getByRole("button", { name: "Claude", exact: true }).click(); assert.equal(await s.page.locator("#models-user .models-row").count(), 0); await s.page.reload({ waitUntil: "domcontentloaded" }); await drawn(s.page); await s.page.click("#btn-looks"); await s.page.locator("#models-tab-codex").click(); await s.page.locator("#models-user").getByRole("textbox", { name: "Model id" }).waitFor(); assert.equal(await s.page.locator("#models-user").getByRole("textbox", { name: "Model id" }).inputValue(), "gpt-test-codex-1"); }); test("Cleanup scans the console home and lists an old session folder with its size", async (t) => { noSavedLooks(t); const s = await openSession(await suite.browser(), { t, workflow: "plan", payload: PLAN_QUESTION }); // Made after the session opens: opening sweeps old sessions away. A session // is old when nothing in it has been touched for 30 days. const sid = "20250801-101010-aaaaaa"; const dir = path.join(HOME, "sessions", sid); fs.mkdirSync(dir, { recursive: true }); const state = path.join(dir, "state.json"); fs.writeFileSync(state, "x".repeat(3000)); backdate(state); backdate(dir); t.after(() => fs.rmSync(dir, { recursive: true, force: true })); await s.page.click("#btn-looks"); const list = s.page.locator("#cleanup-list"); assert.equal(await list.locator(".cleanup-row").count(), 0, "Cleanup lists things before a scan"); assert.equal(await s.page.locator("#cleanup-delete").isHidden(), true, "Delete is offered before a scan"); await s.page.click("#cleanup-scan"); const row = list.locator(".cleanup-row").filter({ hasText: sid }); await row.waitFor(); assert.match(await textOf(row), new RegExp(`^Session ${sid}`)); const size = await textOf(row.locator(".cleanup-size")); assert.match(size, /^\d/, "the row names a size"); assert.ok(size === formatBytes(3000) || /^[\d.,]+\s*(kB|KB|MB|B)\b/.test(size), `the size reads as a size: ${size}`); assert.match(await textOf(list.locator(".cleanup-summary")), /1 old session/); assert.match(await textOf(s.page.locator("#cleanup-status")), /^Found \d+ items?\./); assert.equal(await s.page.locator("#cleanup-delete").isVisible(), true, "Delete these is offered once something is found"); // The scan only looks. assert.equal(fs.existsSync(dir), true, "a scan deleted the folder"); // Closing preferences forgets the scan: a reopened dialog starts clean. await s.page.click("#looks-done"); await s.page.click("#btn-looks"); assert.equal(await list.locator(".cleanup-row").count(), 0, "the old scan is still listed"); assert.equal(await s.page.locator("#cleanup-delete").isHidden(), true); });