Open History to see the last commit
1import { afterEach, describe, expect, it, vi } from "vite-plus/test";
2
3import { environmentThemeDefinition, publishedThemeDefinitions } from "./useEnvironmentTheme";
4import {
5 getThemeDefinition,
6 installCustomTheme,
7 invalidateCustomThemes,
8 setEnvironmentThemes,
9} from "../themePalette";
10
11const NIGHTFALL_THEME = {
12 id: "nightfall",
13 name: "Nightfall",
14 appearance: "dark",
15 canvas: "#1a1b26",
16 accent: "#7aa2f7",
17} as const;
18
19afterEach(() => {
20 setEnvironmentThemes([]);
21});
22
23describe("environment themes", () => {
24 it("drops published themes with reserved ids", () => {
25 const definitions = publishedThemeDefinitions([
26 NIGHTFALL_THEME,
27 { ...NIGHTFALL_THEME, id: "t3-iris", name: "Impostor Iris" },
28 { ...NIGHTFALL_THEME, id: "ocean", name: "Impostor Ocean" },
29 { ...NIGHTFALL_THEME, id: "dark", name: "Impostor Dark" },
30 ]);
31
32 expect(definitions.map((definition) => definition.id)).toEqual(["nightfall"]);
33 });
34
35 it("drops published palettes with no usable colors", () => {
36 const theme = { id: "invalid", name: "Invalid", appearance: "dark" } as const;
37 const definitions = publishedThemeDefinitions([
38 { ...theme, colors: { canvas: "not-a-color" } },
39 { ...theme, id: "unknown-roles", colors: { futureRole: "#ffffff" } },
40 {
41 ...theme,
42 id: "ignored-variant",
43 colors: { canvas: "not-a-color" },
44 variants: { dark: { canvas: "#112233" } },
45 },
46 ]);
47
48 expect(definitions).toEqual([]);
49 });
50
51 it("keeps seeds, partial palettes, and usable alternate appearances", () => {
52 const theme = { name: "Shared", appearance: "dark" } as const;
53 const definitions = publishedThemeDefinitions([
54 NIGHTFALL_THEME,
55 {
56 ...theme,
57 id: "partial",
58 colors: { canvas: "not-a-color", text: "#ffffff", futureRole: "#ffffff" },
59 },
60 {
61 ...theme,
62 id: "light-variant",
63 colors: { canvas: "not-a-color" },
64 variants: { light: { canvas: "#eff1f5" } },
65 },
66 ]);
67
68 expect(definitions.map((definition) => definition.id)).toEqual([
69 "nightfall",
70 "partial",
71 "light-variant",
72 ]);
73 });
74
75 it("lets a theme the user saved win an id collision", () => {
76 const store = new Map<string, string>();
77 vi.stubGlobal("window", {
78 addEventListener: vi.fn(),
79 removeEventListener: vi.fn(),
80 localStorage: {
81 getItem: (key: string) => store.get(key) ?? null,
82 setItem: (key: string, value: string) => store.set(key, value),
83 removeItem: (key: string) => store.delete(key),
84 },
85 });
86 invalidateCustomThemes();
87 try {
88 const environment = environmentThemeDefinition(NIGHTFALL_THEME);
89 setEnvironmentThemes([environment]);
90 installCustomTheme({ ...environment, label: "My Nightfall" });
91
92 expect(getThemeDefinition("nightfall")?.label).toBe("My Nightfall");
93 } finally {
94 vi.unstubAllGlobals();
95 invalidateCustomThemes();
96 }
97 expect(getThemeDefinition("nightfall")?.label).toBe("Nightfall");
98 });
99});