@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 257 lines 8.32 KB
Raw
1 import { StrictMode } from "react";
2 import { describe, expect, it, vi } from "vite-plus/test";
3 import { page, userEvent } from "vite-plus/test/browser";
4
5 import { act, render } from "../../../test/render";
6 import {
7 CUSTOM_THEMES_STORAGE_KEY,
8 getCustomThemes,
9 installCustomTheme,
10 invalidateCustomThemes,
11 parseThemeFile,
12 themeColorToHex,
13 THEME_FILE_VERSION,
14 } from "../../themePalette";
15 import { importOpenVsxThemeExtension, type OpenVsxThemeExtension } from "../../openVsxThemes";
16 import { ThemeImportDialog } from "./ThemeImportDialog";
17
18 const searchOpenVsxThemes = vi.hoisted(() => vi.fn());
19
20 vi.mock("../../openVsxThemes", () => ({
21 searchOpenVsxThemes,
22 importOpenVsxThemeExtension: vi.fn(),
23 }));
24
25 const extension: OpenVsxThemeExtension = {
26 id: "acme.nightfall",
27 collectionId: "acme.nightfall",
28 name: "Nightfall",
29 publisher: "acme",
30 description: "A dark theme",
31 downloadCount: 10,
32 iconUrl: null,
33 sourceUrl: null,
34 manifestUrl: "https://example.test/manifest",
35 sha256Url: "https://example.test/sha256",
36 vsixUrl: "https://example.test/vsix",
37 version: "1.0.0",
38 license: "MIT",
39 };
40
41 describe("ThemeImportDialog", () => {
42 // StrictMode replays ref attach/detach on mount, which must not end the session.
43 it("searches Open VSX under StrictMode", async () => {
44 searchOpenVsxThemes.mockReset();
45 searchOpenVsxThemes.mockResolvedValue([extension]);
46 await using _screen = await render(
47 <StrictMode>
48 <ThemeImportDialog
49 open
50 onOpenChange={() => {}}
51 onImported={() => true}
52 onImportedMany={() => {}}
53 />
54 </StrictMode>,
55 );
56
57 // One input event: keystrokes more than the search debounce apart would search a prefix first.
58 await userEvent.fill(page.getByLabelText("Search Open VSX themes"), "night");
59
60 await expect.element(page.getByRole("heading", { name: "Nightfall" })).toBeVisible();
61 expect(searchOpenVsxThemes).toHaveBeenCalledWith(
62 "night",
63 expect.objectContaining({ sortBy: "downloadCount" }),
64 );
65 });
66 });
67
68 const theme = (name: string, accent = "#3b82f6", css?: string) => ({
69 version: THEME_FILE_VERSION,
70 name,
71 appearance: "dark",
72 colors: { accent },
73 ...(css ? { css } : {}),
74 });
75
76 const themeFile = (name: string, accent?: string, css?: string) =>
77 new File([JSON.stringify(theme(name, accent, css))], `${name}.json`, {
78 type: "application/json",
79 });
80
81 const DUSK_CSS = ".chat-header { height: 3rem; }";
82
83 it.each(["Update existing", "Keep both"])(
84 "keeps batch failures visible after %s",
85 async (choice) => {
86 using _themes = {
87 [Symbol.dispose]() {
88 window.localStorage.removeItem(CUSTOM_THEMES_STORAGE_KEY);
89 invalidateCustomThemes();
90 },
91 };
92 installCustomTheme(parseThemeFile(theme("Dusk")));
93 const onOpenChange = vi.fn();
94 await using _screen = await render(
95 <ThemeImportDialog
96 open
97 onOpenChange={onOpenChange}
98 onImported={() => true}
99 onImportedMany={() => {}}
100 />,
101 );
102 const input = document.querySelector<HTMLInputElement>('input[type="file"]')!;
103 await userEvent.upload(input, [
104 themeFile("Dawn"),
105 themeFile("Dusk", "#f97316", DUSK_CSS),
106 new File(["{"], "broken.json", { type: "application/json" }),
107 ]);
108
109 await expect.element(page.getByText("Already installed")).toBeVisible();
110 await expect.element(page.getByText(/^broken\.json:/)).toBeVisible();
111 expect(getCustomThemes().map((theme) => theme.label)).toContain("Dawn");
112 await page.getByRole("button", { name: choice }).click();
113 invalidateCustomThemes();
114 const label = choice === "Update existing" ? "Dusk" : "Dusk (1)";
115 expect(getCustomThemes().find((theme) => theme.label === label)).toMatchObject({
116 colors: parseThemeFile(theme("Dusk", "#f97316")).colors,
117 css: DUSK_CSS,
118 });
119 await expect.element(page.getByText(/^broken\.json:/)).toBeVisible();
120 expect(onOpenChange).not.toHaveBeenCalledWith(false);
121 },
122 );
123
124 function isolateThemes() {
125 const previous = localStorage.getItem(CUSTOM_THEMES_STORAGE_KEY);
126 localStorage.removeItem(CUSTOM_THEMES_STORAGE_KEY);
127 invalidateCustomThemes();
128 return {
129 [Symbol.dispose]() {
130 if (previous === null) localStorage.removeItem(CUSTOM_THEMES_STORAGE_KEY);
131 else localStorage.setItem(CUSTOM_THEMES_STORAGE_KEY, previous);
132 invalidateCustomThemes();
133 },
134 };
135 }
136
137 it("sizes an oversized theme file just under 1 MB as 1.00 MB", async () => {
138 using _themes = isolateThemes();
139 await using _screen = await render(
140 <ThemeImportDialog
141 open
142 onOpenChange={() => {}}
143 onImported={() => true}
144 onImportedMany={() => {}}
145 />,
146 );
147 const input = document.querySelector<HTMLInputElement>('input[type="file"]')!;
148 await userEvent.upload(
149 input,
150 new File(["x".repeat(1_048_100)], "huge.json", { type: "application/json" }),
151 );
152
153 await expect
154 .element(page.getByText(/^That file is/))
155 .toHaveTextContent(
156 "That file is 1.00 MB. Theme files are only a few KB, so this one was not read (limit 256 KB).",
157 );
158 });
159
160 function themeJsonc(name: string, background: string) {
161 return `{
162 // VS Code themes use JSONC even with a .json extension.
163 "name": "${name}",
164 "colors": {
165 "editor.background": "${background}",
166 },
167 "tokenColors": [],
168 }`;
169 }
170
171 describe("ThemeImportDialog JSONC imports", () => {
172 it.each(["paste", "file"])("imports a VS Code JSONC theme from %s", async (source) => {
173 using _themes = isolateThemes();
174 await using _screen = await render(
175 <ThemeImportDialog
176 open
177 onOpenChange={() => {}}
178 onImported={() => true}
179 onImportedMany={() => {}}
180 />,
181 );
182 const json = themeJsonc("JSONC Night", "#002b36");
183 if (source === "paste") {
184 await page.getByRole("textbox", { name: "Theme JSON" }).fill(json);
185 } else {
186 const input = document.querySelector<HTMLInputElement>('input[type="file"]')!;
187 await userEvent.upload(input, new File([json], "night.json", { type: "application/json" }));
188 await expect.element(page.getByRole("textbox", { name: "Theme JSON" })).toHaveValue(json);
189 }
190 await page.getByRole("button", { name: "Add theme", exact: true }).click();
191
192 expect(getCustomThemes().map((theme) => theme.label)).toEqual(["JSONC Night"]);
193 expect(themeColorToHex(getCustomThemes()[0]!.colors.canvas)).toBe("#002b36");
194 });
195
196 it("imports each VS Code JSONC theme in a file batch", async () => {
197 using _themes = isolateThemes();
198 await using _screen = await render(
199 <ThemeImportDialog
200 open
201 onOpenChange={() => {}}
202 onImported={() => true}
203 onImportedMany={() => {}}
204 />,
205 );
206 const input = document.querySelector<HTMLInputElement>('input[type="file"]')!;
207 await userEvent.upload(input, [
208 new File([themeJsonc("JSONC Night", "#002b36")], "night.json", {
209 type: "application/json",
210 }),
211 new File([themeJsonc("JSONC Day", "#fdf6e3")], "day.json", {
212 type: "application/json",
213 }),
214 ]);
215
216 await vi.waitFor(() => {
217 expect(getCustomThemes().map((theme) => theme.label)).toEqual(["JSONC Night", "JSONC Day"]);
218 });
219 expect(getCustomThemes().map((theme) => themeColorToHex(theme.colors.canvas))).toEqual([
220 "#002b36",
221 "#fdf6e3",
222 ]);
223 });
224 });
225
226 it("does not install a pending community theme after leaving Appearance", async () => {
227 using _themes = isolateThemes();
228 searchOpenVsxThemes.mockReset();
229 searchOpenVsxThemes.mockResolvedValue([extension]);
230 const download = Promise.withResolvers<ReturnType<typeof parseThemeFile>[]>();
231 vi.mocked(importOpenVsxThemeExtension).mockReset();
232 vi.mocked(importOpenVsxThemeExtension).mockReturnValue(download.promise);
233 await using screen = await render(
234 <ThemeImportDialog
235 open
236 onOpenChange={() => {}}
237 onImported={() => true}
238 onImportedMany={() => {}}
239 />,
240 );
241 await userEvent.fill(page.getByLabelText("Search Open VSX themes"), "night");
242 await page.getByRole("button", { name: "Install Nightfall" }).click();
243 await screen.unmount();
244 await act(async () => {
245 download.resolve([
246 {
247 ...parseThemeFile(theme("Nightfall")),
248 collection: { id: extension.collectionId, label: extension.name },
249 },
250 ]);
251 await download.promise;
252 });
253 expect(
254 getCustomThemes(),
255 "expect leaving Appearance to cancel its pending theme installation",
256 ).toEqual([]);
257 });