/** * 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 { 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; /** 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 }) => { 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 { 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); }); });