Lutra/ui/tests/settings/fontSettings.test.ts
danny 086bc8a925 T1: four more themes, theme picker, theme file import, editor font settings
Plum Night, River, Paper and Reed (designed by Amy, approved by Daniel)
join the brand pair. theme.list/current/select/import, a picker with
live preview (view.selectTheme) and "Import Theme…" (view.importTheme);
imported themes are validated and stored in <data_dir>/themes. Editor
font family and line height settings apply live. Docs: docs/themes.md.

Gate: fmt, clippy and the rest pass up to cargo test, where only the
unrelated lutra-terminal PTY test a_full_buffer_stops_the_shell timed
out under load; to be re-run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 19:31:34 +01:00

178 lines
6.7 KiB
TypeScript

/**
* Tests of the editor font settings and imported theme names in the
* settings store (C9 T1): `editor.fontFamily`, `editor.fontSize` and
* `editor.lineHeight` drive their tokens live and survive a theme switch;
* imported themes show by name; a change of `appearance.theme` reloads the
* list so a newly imported theme appears. The engine is a fake behind the
* mocked Tauri `invoke` and `listen`.
*/
import { flushPromises } from "@vue/test-utils";
import { createPinia, setActivePinia } from "pinia";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const invoke = vi.hoisted(() => vi.fn());
const listen = vi.hoisted(() => vi.fn());
vi.mock("@tauri-apps/api/core", () => ({ invoke }));
vi.mock("@tauri-apps/api/event", () => ({ listen }));
import {
FONT_FAMILY_TOKEN,
FONT_SIZE_TOKEN,
LINE_HEIGHT_TOKEN,
MONO_FALLBACK,
fontTokenValue,
toSettingDefs,
useSettingsStore,
valueLabel,
type SettingDef,
} from "../../src/stores/settings";
import { applyTheme, setTokenOverride } from "../../src/theme";
/** The theme setting as `settings.list` describes it, with or without an import. */
function themeDef(imported: boolean): Record<string, unknown> {
return {
key: "appearance.theme",
type: "enum",
default: "system",
enumValues: ["lutra-dark", "lutra-river", ...(imported ? ["dusk"] : []), "system"],
...(imported ? { valueNames: { dusk: "My Dusk", ghost: "Not listed", "lutra-river": 5 } } : {}),
labelKey: "settings.appearance.theme.label",
descriptionKey: "settings.appearance.theme.description",
section: "appearance",
};
}
/** Whether the fake engine already imported `dusk`. */
let imported: boolean;
/** The fake engine's values. */
let values: Record<string, unknown>;
/** Every `engine_execute` command, in order. */
let commands: string[];
/** The engine event callbacks. */
let listeners: ((payload: unknown) => void)[];
const root = document.documentElement;
beforeEach(() => {
setActivePinia(createPinia());
imported = false;
values = {
"appearance.theme": "lutra-dark",
"editor.fontSize": 14,
"editor.fontFamily": "",
"editor.lineHeight": 150,
};
commands = [];
listeners = [];
root.removeAttribute("style");
listen.mockReset();
listen.mockImplementation((_name: string, callback: (event: { payload: unknown }) => void) => {
listeners.push((payload) => {
callback({ payload });
});
return Promise.resolve(() => undefined);
});
invoke.mockReset();
invoke.mockImplementation((cmd: string, args?: { command: string; params: Record<string, unknown> }) => {
if (cmd !== "engine_execute" || !args) {
return Promise.resolve(null);
}
commands.push(args.command);
switch (args.command) {
case "settings.list":
return Promise.resolve([themeDef(imported)]);
case "settings.get": {
const keys = Array.isArray(args.params.keys) ? (args.params.keys as string[]) : Object.keys(values);
return Promise.resolve({ values: Object.fromEntries(keys.map((k) => [k, values[k]])) });
}
default:
return Promise.resolve(null);
}
});
});
afterEach(() => {
useSettingsStore().stop();
for (const token of [FONT_SIZE_TOKEN, FONT_FAMILY_TOKEN, LINE_HEIGHT_TOKEN]) {
setTokenOverride(token, null);
}
});
/**
* Sends an engine event to every subscriber.
*
* @param payload - the event.
*/
async function emit(payload: unknown): Promise<void> {
for (const listener of listeners) {
listener(payload);
}
await flushPromises();
}
describe("fontTokenValue", () => {
it("turns each font setting into its token value", () => {
expect(fontTokenValue("editor.fontSize", 16)).toBe("16px");
expect(fontTokenValue("editor.fontSize", 0)).toBeNull();
expect(fontTokenValue("editor.fontSize", "16")).toBeNull();
expect(fontTokenValue("editor.lineHeight", 150)).toBe("1.5");
expect(fontTokenValue("editor.lineHeight", 120)).toBe("1.2");
expect(fontTokenValue("editor.lineHeight", 99)).toBeNull();
expect(fontTokenValue("editor.lineHeight", 1.5)).toBeNull();
expect(fontTokenValue("editor.fontFamily", "")).toBeNull();
expect(fontTokenValue("editor.fontFamily", " ")).toBeNull();
expect(fontTokenValue("editor.fontFamily", ' "Fira Code" ')).toBe(`"Fira Code", ${MONO_FALLBACK}`);
expect(fontTokenValue("editor.fontFamily", "x; color: red")).toBeNull();
expect(fontTokenValue("editor.fontFamily", "a}b")).toBeNull();
expect(fontTokenValue("editor.other", 3)).toBeNull();
});
});
describe("editor font settings", () => {
it("apply on load and live on settingsChanged, above any theme", async () => {
await useSettingsStore().start();
expect(root.style.getPropertyValue(FONT_SIZE_TOKEN)).toBe("14px");
expect(root.style.getPropertyValue(LINE_HEIGHT_TOKEN)).toBe("1.5");
expect(root.style.getPropertyValue(FONT_FAMILY_TOKEN)).toBe("");
values["editor.fontFamily"] = "Iosevka";
values["editor.lineHeight"] = 180;
await emit({ kind: "settingsChanged", keys: ["editor.fontFamily", "editor.lineHeight"] });
expect(root.style.getPropertyValue(FONT_FAMILY_TOKEN)).toBe(`Iosevka, ${MONO_FALLBACK}`);
expect(root.style.getPropertyValue(LINE_HEIGHT_TOKEN)).toBe("1.8");
applyTheme({
id: "lutra-river",
dark: true,
tokens: { [FONT_FAMILY_TOKEN]: "monospace", [LINE_HEIGHT_TOKEN]: "1.5", "--lt-bg": "#1a2125" },
});
expect(root.style.getPropertyValue(FONT_FAMILY_TOKEN)).toBe(`Iosevka, ${MONO_FALLBACK}`);
expect(root.style.getPropertyValue(LINE_HEIGHT_TOKEN)).toBe("1.8");
values["editor.fontFamily"] = "";
await emit({ kind: "settingsChanged", keys: ["editor.fontFamily"] });
expect(root.style.getPropertyValue(FONT_FAMILY_TOKEN)).toBe("monospace");
});
});
describe("imported themes in the settings", () => {
it("show by the name the engine sends", () => {
const [def] = toSettingDefs([themeDef(true)]) as SettingDef[];
expect(def?.valueNames).toEqual({ dusk: "My Dusk" });
expect(valueLabel(def as SettingDef, "dusk")).toBe("My Dusk");
expect(valueLabel(def as SettingDef, "lutra-river")).toBe("River");
expect(valueLabel(def as SettingDef, "unknown-id")).toBe("unknown-id");
});
it("appear after an import, since a theme change reloads the list", async () => {
const store = useSettingsStore();
await store.start();
expect(store.defs[0]?.enumValues).not.toContain("dusk");
imported = true;
values["appearance.theme"] = "dusk";
await emit({ kind: "settingsChanged", keys: ["appearance.theme"] });
expect(store.defs[0]?.enumValues).toContain("dusk");
expect(store.values["appearance.theme"]).toBe("dusk");
expect(commands.filter((c) => c === "settings.list")).toHaveLength(2);
});
});