Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/trueberryless/ai-disclosure. A practical guide for transparent AI usage disclosure across software ai-disclosure.trueberryless.org
Something went wrong. Try again.
2.8 kB · 107 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108import { describe, expect, test } from "vitest";
import { THEME_STORAGE_KEY, applyThemePreference, getThemeAttributes, parseThemePreference, readThemePreference, storeThemePreference,} from "../../src/libs/theme";
describe("parseThemePreference", () => { test.each(["system", "light", "dark"])("accepts %s", (value) => { expect(parseThemePreference(value)).toBe(value); });
test.each([null, undefined, "", "Dark", "blue", 1])( "falls back to system for %s", (value) => { expect(parseThemePreference(value)).toBe("system"); } );});
describe("getThemeAttributes", () => { test("lets the system decide by default", () => { expect(getThemeAttributes("system")).toEqual({ codeTheme: undefined, colorScheme: "light dark", }); });
test("pins the code theme for explicit preferences", () => { expect(getThemeAttributes("light")).toEqual({ codeTheme: "catppuccin-latte", colorScheme: "light", }); expect(getThemeAttributes("dark")).toEqual({ codeTheme: "catppuccin-mocha", colorScheme: "dark", }); });});
describe("applyThemePreference", () => { function createRoot() { return { dataset: {} as Record<string, string | undefined>, style: { colorScheme: "" }, } as unknown as HTMLElement; }
test("sets the code theme and color scheme", () => { const root = createRoot();
applyThemePreference(root, "dark");
expect(root.dataset["theme"]).toBe("catppuccin-mocha"); expect(root.style.colorScheme).toBe("dark"); });
test("removes the code theme for the system preference", () => { const root = createRoot();
applyThemePreference(root, "light"); applyThemePreference(root, "system");
expect(root.dataset["theme"]).toBeUndefined(); expect(root.style.colorScheme).toBe("light dark"); });});
describe("theme storage", () => { const throwingStorage = { getItem() { throw new Error("blocked"); }, setItem() { throw new Error("blocked"); }, };
test("reads a stored preference", () => { expect( readThemePreference({ getItem: (key) => (key === THEME_STORAGE_KEY ? "dark" : null), }) ).toBe("dark"); });
test("falls back to system when storage is empty or blocked", () => { expect(readThemePreference({ getItem: () => null })).toBe("system"); expect(readThemePreference(throwingStorage)).toBe("system"); });
test("stores a preference and ignores blocked storage", () => { const values = new Map<string, string>();
storeThemePreference( { setItem: (key, value) => values.set(key, value) }, "light" );
expect(values.get(THEME_STORAGE_KEY)).toBe("light"); expect(() => storeThemePreference(throwingStorage, "light")).not.toThrow(); });});