Files
2026-11-16 04:21:21 -07:00

117 lines
8.1 KiB
JavaScript

// Theme picker cards and the theme editor tab: keyboard selection, the check mark, a live preview, contrast badges, Save, Discard, Duplicate, and narrow windows.
import assert from "node:assert/strict";
import path from "node:path";
import { mockTauri, openPage, outputDir, runSmoke, smokeUrl } from "../_harness/index.mjs";
await runSmoke("appearance/themeEditor", async ({ browser }) => {
const output = await outputDir("appearance/themeEditor");
const context = await browser.newContext({ viewport: { width: 1200, height: 820 } });
const commands = test => ({
...test.workspace({ root: "C:\\theme-editor", files: { "a.md": "# Note\n\nSome text with a [link](https://example.com).\n" } }),
plugins_snapshot: () => ({ root: "C:/plugins", plugins: [] }),
spelling_menu_install: () => null,
"plugin:window|is_maximized": () => false,
});
// The Settings window label names the window that opened it (src-tauri/src/settings.rs).
const main = await openPage(context);
await mockTauri(main, { label: "main", commands });
const settings = await openPage(context);
await mockTauri(settings, { label: "settings-main", commands });
await main.goto(smokeUrl);
await main.locator('[data-tree-rel="a.md"]').waitFor();
await settings.goto(smokeUrl + "/?settingsWindow=true");
await settings.getByRole("button", { name: "Appearance", exact: true }).click();
const token = (page, role) => page.evaluate(role => getComputedStyle(document.documentElement).getPropertyValue(`--rs-${role}`).trim(), role);
// Picker cards: one radio group with a check mark on the current theme.
const cards = settings.getByRole("radiogroup", { name: "Color theme", exact: true });
const card = name => cards.getByRole("radio", { name, exact: true });
assert.equal(await card("RuneStone Dark").getAttribute("aria-checked"), "true");
assert.equal(await card("RuneStone Dark").locator(".card-name svg").count(), 1, "The current card shows a check mark");
assert.equal(await card("Claude Light").locator(".card-name svg").count(), 0);
await card("RuneStone Dark").focus();
await settings.keyboard.press("ArrowRight");
await settings.waitForFunction(() => document.documentElement.dataset.palette === "claude" && document.documentElement.dataset.theme === "light");
assert.equal(await card("Claude Light").evaluate(node => node === document.activeElement && node.getAttribute("aria-checked") === "true"), true);
await settings.keyboard.press("ArrowLeft");
await settings.waitForFunction(() => document.documentElement.dataset.palette === "default" && document.documentElement.dataset.theme === "dark");
await main.waitForFunction(() => document.documentElement.dataset.palette === "default");
await settings.screenshot({ path: path.join(output, "picker-cards.png") });
// Edit theme opens the editor tab in the window that opened Settings.
await settings.getByRole("button", { name: "Edit theme", exact: true }).click();
const editor = main.getByRole("region", { name: "Theme editor", exact: true });
await editor.waitFor();
await main.getByRole("tab", { name: /Theme editor/ }).waitFor();
assert.equal(await editor.getByRole("textbox", { name: "Theme name", exact: true }).inputValue(), "RuneStone Dark copy");
assert.match(await editor.getByRole("status").textContent(), /All readability checks pass/);
// A change shows at once in this window, and not in Settings before a save.
const hex = role => editor.getByRole("textbox", { name: `Hex value of ${role}`, exact: true });
await hex("accent").fill("#ff7a1a");
await hex("accent").press("Tab");
await main.waitForFunction(() => getComputedStyle(document.documentElement).getPropertyValue("--rs-accent").trim() === "#ff7a1a");
assert.equal(await token(settings, "accent"), "#55dcf5");
await main.getByRole("tab", { name: "• Theme editor", exact: true }).waitFor();
// A color that fails a rule shows the failure on its row and in the summary. Reset takes the base color again.
await hex("text-dim").fill("#3a3a3a");
await hex("text-dim").press("Tab");
await editor.getByRole("status").filter({ hasText: /readability checks fail/ }).waitFor();
const dimRow = hex("text-dim").locator("xpath=..");
assert.equal(await dimRow.locator(".badge.fail").count(), 1);
await main.screenshot({ path: path.join(output, "editor-failing-role.png") });
await editor.getByRole("button", { name: "Reset text-dim", exact: true }).click();
assert.equal(await dimRow.locator(".badge.fail").count(), 0);
assert.equal(await token(main, "text-dim"), "#ababab");
// Save copies the built-in theme to a custom theme with a base, and every window selects it.
await editor.getByRole("button", { name: "Save", exact: true }).click();
await editor.getByRole("status").filter({ hasText: "Saved RuneStone Dark copy." }).waitFor();
await settings.waitForFunction(() => document.documentElement.dataset.palette === "custom:runestone-dark-copy");
assert.equal(await token(settings, "accent"), "#ff7a1a");
const saved = await main.evaluate(() => window.__TEST__.settings("customThemes"));
assert.deepEqual(saved.map(theme => [theme.id, theme.base, theme.mode, theme.colors]), [["runestone-dark-copy", "default", "dark", { accent: "#ff7a1a" }]]);
assert.equal(await card("RuneStone Dark copy").getAttribute("aria-checked"), "true");
await main.getByRole("tab", { name: "Theme editor", exact: true }).waitFor();
// Discard returns to the saved colors.
await hex("bg").fill("#202040");
await hex("bg").press("Tab");
await main.waitForFunction(() => getComputedStyle(document.documentElement).getPropertyValue("--rs-bg").trim() === "#202040");
await editor.getByRole("button", { name: "Discard", exact: true }).click();
await main.waitForFunction(() => getComputedStyle(document.documentElement).getPropertyValue("--rs-bg").trim() === "#151617");
// Duplicate saves a copy and selects it.
await editor.getByRole("button", { name: "Duplicate", exact: true }).click();
await editor.getByRole("status").filter({ hasText: "Saved RuneStone Dark copy copy." }).waitFor();
await settings.waitForFunction(() => document.documentElement.dataset.palette === "custom:runestone-dark-copy-copy");
assert.equal((await main.evaluate(() => window.__TEST__.settings("customThemes"))).length, 2);
// A narrow pane keeps the editor inside the window, and keyboard focus is visible.
// A 520-pixel window without the outline leaves the editor about 215 pixels, below its 240-pixel breakpoint.
await main.getByRole("button", { name: "Hide outline", exact: true }).click();
await main.setViewportSize({ width: 520, height: 760 });
assert.ok(await editor.evaluate(node => node.clientWidth <= 240));
await hex("accent").focus();
assert.equal(await editor.evaluate(node => node.scrollWidth <= node.clientWidth + 1), true, "The editor does not scroll sideways in a narrow pane");
assert.notEqual(await hex("accent").evaluate(node => getComputedStyle(node).outlineStyle), "none");
await main.screenshot({ path: path.join(output, "editor-narrow.png") });
await main.setViewportSize({ width: 1200, height: 820 });
await settings.setViewportSize({ width: 360, height: 640 });
assert.equal(await settings.evaluate(() => document.documentElement.scrollWidth <= innerWidth), true, "Narrow settings do not overflow");
const box = await cards.boundingBox();
assert(box.x >= 0 && box.x + box.width <= 360, "The cards fit at 360 pixels");
await settings.screenshot({ path: path.join(output, "picker-cards-360.png") });
// Closing the editor with a change keeps the saved theme in the window.
await hex("accent").fill("#00ff00");
await hex("accent").press("Tab");
await main.waitForFunction(() => getComputedStyle(document.documentElement).getPropertyValue("--rs-accent").trim() === "#00ff00");
await main.getByRole("button", { name: /^Close .*Theme editor$/ }).click();
await editor.waitFor({ state: "detached" });
await main.waitForFunction(() => getComputedStyle(document.documentElement).getPropertyValue("--rs-accent").trim() === "#ff7a1a");
assert.deepEqual([...main.errors, ...settings.errors], []);
console.log("PASS: picker cards, the editor tab, live preview, contrast badges, Save, Discard, Duplicate, and narrow windows");
});