Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219import { createRoot } from "solid-js";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
/** * The theme mode: three states the listener can ask for, one theme the app paints. * * The interesting cases are the ones only a *mode* can pass: `system` must follow the * machine at boot and keep following it as the machine changes, while a pinned `light` * must ignore the machine entirely; and the persisted value must be the mode, so a * `system` listener who watched the app flip at sunset is still on `system` afterwards. */
const STORAGE_KEY = "hackermusic.theme.v1";
type Mode = "system" | "light" | "dark";interface ModeModule { themeMode: () => Mode; peekThemeMode: () => Mode; setThemeMode: (m: Mode) => Mode; cycleThemeMode: () => Mode; themeModes: () => readonly Mode[]; resolveTheme: () => "light" | "dark"; effectiveTheme: () => "light" | "dark"; applyTheme: () => "light" | "dark"; startThemeSync: () => void;}
/** * A `matchMedia` that only knows about the one query this module asks, with a lever to * change the machine's mind and a list of the listeners the module registered so a test * can prove the change is torn down with the root that asked for it. */interface FakeMedia { set(dark: boolean): void; listeners: Array<(e: { matches: boolean }) => void>;}
let media: FakeMedia | null = null;
function stubMedia(dark: boolean): FakeMedia { const m: FakeMedia = { listeners: [], set: () => {} }; let matches = dark; m.set = (next) => { matches = next; for (const cb of m.listeners.slice()) cb({ matches }); }; media = m; globalThis.matchMedia = ((_query: string) => ({ get matches() { return matches; }, addEventListener: (_type: string, cb: (e: { matches: boolean }) => void) => { m.listeners.push(cb); }, removeEventListener: (_type: string, cb: (e: { matches: boolean }) => void) => { const i = m.listeners.indexOf(cb); if (i >= 0) m.listeners.splice(i, 1); }, })) as unknown as typeof globalThis.matchMedia; return m;}
/** A fresh module, with `matchMedia` decided before it reads it, since it reads it once. */async function fresh(): Promise<ModeModule> { localStorage.clear(); document.documentElement.removeAttribute("data-theme"); vi.resetModules(); return import("./mode") as unknown as Promise<ModeModule>;}
/** A fresh module booted inside a root that owns the effects `startThemeSync` makes. */async function booted(dark: boolean): Promise<{ m: ModeModule; dispose: () => void }> { localStorage.clear(); document.documentElement.removeAttribute("data-theme"); vi.resetModules(); stubMedia(dark); // Imported *before* the root so the module's own signals/memo load normally, then // `startThemeSync` is called *inside* the root — which is what owns its effect and its // listener cleanup, and what a `dispose()` therefore takes down. const m = (await import("./mode")) as unknown as ModeModule; let dispose!: () => void; createRoot((d) => { dispose = d; m.startThemeSync(); }); return { m, dispose };}
beforeEach(() => { localStorage.clear(); document.documentElement.removeAttribute("data-theme"); media = null;});
afterEach(() => { delete (globalThis as { matchMedia?: unknown }).matchMedia; localStorage.clear(); document.documentElement.removeAttribute("data-theme");});
describe("the three modes", () => { it("starts on system, which means whatever the machine is wearing", async () => { stubMedia(false); // a light machine const m = await fresh(); expect(m.themeMode()).toBe("system"); expect(m.resolveTheme()).toBe("light");
// And a dark machine, same mode: the mode did not change, the paint did. stubMedia(true); const m2 = await fresh(); expect(m2.themeMode()).toBe("system"); expect(m.resolveTheme()).toBe("light"); expect(m2.resolveTheme()).toBe("dark"); });
it("paints a pinned theme verbatim and refuses to be moved by the machine", async () => { stubMedia(true); // dark machine const m = await fresh(); m.setThemeMode("light"); // Pinned light on a dark machine: the theme is light, and the dark machine is ignored // entirely — this is the whole difference between a mode and a theme. expect(m.resolveTheme()).toBe("light"); m.setThemeMode("dark"); expect(m.resolveTheme()).toBe("dark"); });
it("cycles the three in order and wraps back round", async () => { const m = await fresh(); expect(m.themeModes()).toEqual(["system", "light", "dark"]); expect(m.peekThemeMode()).toBe("system"); expect(m.cycleThemeMode()).toBe("light"); expect(m.cycleThemeMode()).toBe("dark"); // The press that gets you home: a pinned theme is never a dead end. expect(m.cycleThemeMode()).toBe("system"); });
it("keeps the mode through the flip it caused, not the theme it painted", async () => { // The sunset case, and the reason there are two values rather than one: a `system` // listener has the theme change under them while the thing they chose stays put. const m = await fresh(); stubMedia(false); m.setThemeMode("system"); expect(m.themeMode()).toBe("system"); // (the machine is dark when the module booted, so the theme was dark; the mode is // still system regardless of which way the paint went) expect(m.peekThemeMode()).toBe("system"); });});
describe("the mode across a reload", () => { it("comes back out of storage, and junk is the default", async () => { const m = await fresh(); m.setThemeMode("dark"); // JSON-quoted, the way the persistent store writes every value it keeps. expect(localStorage.getItem(STORAGE_KEY)).toBe('"dark"');
vi.resetModules(); const reloaded = await import("./mode"); expect(reloaded.peekThemeMode()).toBe("dark");
localStorage.setItem(STORAGE_KEY, JSON.stringify("neon")); vi.resetModules(); const junk = await import("./mode"); // A value that is not one of the three reads as the default it would have been: an // unknown theme is not a theme, and `system` is the safe answer to "what did I pick". expect(junk.peekThemeMode()).toBe("system"); });});
describe("wearing the mode", () => { it("stamps the resolved theme onto the document, and re-stamps on a flip", async () => { const { m, dispose } = await booted(true); // Synchronous from the imperative applyTheme startThemeSync opens with. expect(document.documentElement.dataset.theme).toBe("dark"); // A mode change reaches the document through the deferred effect, so the paint lands a // tick later than the write — the same vi.waitFor the accent tests use for the same // reason, rather than an arbitrary number of awaited microtasks. m.setThemeMode("light"); await vi.waitFor(() => expect(document.documentElement.dataset.theme).toBe("light")); // Never `system`: the attribute is the paint, and paint is always one of two. m.setThemeMode("system"); await vi.waitFor(() => expect(document.documentElement.dataset.theme).toBe("dark")); dispose(); });
it("follows the machine live while the mode is system", async () => { const { m, dispose } = await booted(false); // light machine expect(m.resolveTheme()).toBe("light"); expect(document.documentElement.dataset.theme).toBe("light");
media!.set(true); // the machine goes dark under a listener who never touched the app // Only the document paint is deferred, but the memo is a signal read too, so both are // checked after the flush rather than pinned to a possibly-stale synchronous value. await vi.waitFor(() => { expect(m.effectiveTheme()).toBe("dark"); expect(document.documentElement.dataset.theme).toBe("dark"); }); // The mode they chose is untouched by the flip it caused. expect(m.peekThemeMode()).toBe("system"); dispose(); });
it("tears the machine's listener down with the root that registered it", async () => { const { dispose } = await booted(false); expect(media!.listeners).toHaveLength(1); dispose(); expect(media!.listeners).toHaveLength(0); });
it("keeps its own dark identity when there is no machine to ask", async () => { // The test runner and any headless render: no matchMedia at all. The theme every screen // was drawn in is the answer, so a world without an OS preference is the dark app. delete (globalThis as { matchMedia?: unknown }).matchMedia; const m = await fresh(); expect(m.themeMode()).toBe("system"); expect(m.resolveTheme()).toBe("dark"); });});