Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

openVsxThemes.test.ts

473 lines16.5 KB

1import { sha256 } from "@noble/hashes/sha2";
2import JSZip from "jszip";
3import { afterEach, describe, expect, it, vi } from "vite-plus/test";
4​
5import {
6 importOpenVsxThemeExtension,
7 searchOpenVsxThemes,
8 type OpenVsxThemeExtension,
9} from "./openVsxThemes";
10import { getThemeColorsForMode, themeColorToHex } from "./themePalette";
11​
12const ASSET_ROOT = "https://open-vsx.org/api/demo/theme/1.0.0/file";
13​
14function extensionDetail(overrides: Record<string, unknown> = {}) {
15 return {
16 namespace: "demo",
17 name: "theme",
18 displayName: "Demo Theme",
19 description: "A nice theme",
20 version: "1.0.0",
21 downloadCount: 123456,
22 license: "MIT",
23 repository: "https://github.com/demo/theme",
24 files: {
25 icon: `${ASSET_ROOT}/icon.png`,
26 manifest: `${ASSET_ROOT}/package.json`,
27 sha256: `${ASSET_ROOT}/demo.theme-1.0.0.sha256`,
28 download: `${ASSET_ROOT}/demo.theme-1.0.0.vsix`,
29 },
30 ...overrides,
31 };
33​
34afterEach(() => {
35 vi.useRealTimers();
36 vi.unstubAllGlobals();
37});
38​
39describe("Open VSX themes", () => {
40 it("searches theme extensions and keeps only supported open-source licenses", async () => {
41 const fetchMock = vi.fn(async (input: RequestInfo | URL) => {
42 const url = String(input);
43 if (url.includes("/-/search?")) {
44 return new Response(
45 JSON.stringify({
46 extensions: [
47 { namespace: "demo", name: "theme" },
48 { namespace: "icons", name: "theme" },
49 { namespace: "oversized", name: "theme" },
50 { namespace: "unlicensed", name: "theme" },
51 { namespace: "unavailable", name: "theme" },
52 { namespace: "closed", name: "theme" },
53 { namespace: "huge", name: "theme" },
54 ],
55 }),
56 { status: 200 },
57 );
58 }
59 if (url.endsWith("/icons/theme")) {
60 return new Response(
61 JSON.stringify(
62 extensionDetail({
63 namespace: "icons",
64 files: {
65 ...extensionDetail().files,
66 manifest: `${ASSET_ROOT.replace("demo/theme", "icons/theme")}/package.json`,
67 },
68 }),
69 ),
70 { status: 200 },
71 );
72 }
73 if (url.endsWith("/oversized/theme")) {
74 return new Response(
75 JSON.stringify(
76 extensionDetail({
77 namespace: "oversized",
78 files: {
79 ...extensionDetail().files,
80 download: `${ASSET_ROOT.replace("demo/theme", "oversized/theme")}/oversized.vsix`,
81 },
82 }),
83 ),
84 { status: 200 },
85 );
86 }
87 if (url.endsWith("/unlicensed/theme")) {
88 return new Response(
89 JSON.stringify(
90 extensionDetail({
91 namespace: "unlicensed",
92 files: {
93 ...extensionDetail().files,
94 manifest: `${ASSET_ROOT.replace("demo/theme", "unlicensed/theme")}/package.json`,
95 },
96 }),
97 ),
98 { status: 200 },
99 );
100 }
101 if (url.endsWith("/unavailable/theme")) {
102 return new Response(
103 JSON.stringify(
104 extensionDetail({
105 namespace: "unavailable",
106 files: {
107 ...extensionDetail().files,
108 download: `${ASSET_ROOT.replace("demo/theme", "unavailable/theme")}/unavailable.vsix`,
109 manifest: `${ASSET_ROOT.replace("demo/theme", "unavailable/theme")}/package.json`,
110 },
111 }),
112 ),
113 { status: 200 },
114 );
115 }
116 if (url.endsWith("/closed/theme")) {
117 return new Response(
118 JSON.stringify(extensionDetail({ namespace: "closed", license: "All Rights Reserved" })),
119 { status: 200 },
120 );
121 }
122 if (url.endsWith("/huge/theme")) {
123 return new Response(JSON.stringify({ padding: "x".repeat(256 * 1024) }), { status: 200 });
124 }
125 if (url.endsWith("/oversized/theme/1.0.0/file/oversized.vsix")) {
126 return new Response(null, {
127 headers: { "content-length": String(20 * 1024 * 1024 + 1) },
128 status: 200,
129 });
130 }
131 if (url.endsWith("/unavailable/theme/1.0.0/file/unavailable.vsix")) {
132 return new Response(null, { status: 404 });
133 }
134 if (url.endsWith(".vsix")) {
135 return new Response(null, { headers: { "content-length": "1024" }, status: 200 });
136 }
137 if (url.endsWith("/icons/theme/1.0.0/file/package.json")) {
138 return new Response(JSON.stringify({ contributes: { iconThemes: [{}] } }), { status: 200 });
139 }
140 if (url.endsWith("/unlicensed/theme/1.0.0/file/package.json")) {
141 return new Response(
142 JSON.stringify({ contributes: { themes: [{ path: "./theme.json" }] } }),
143 { status: 200 },
144 );
145 }
146 if (url.endsWith("/unavailable/theme/1.0.0/file/package.json")) {
147 return new Response(
148 JSON.stringify({ license: "MIT", contributes: { themes: [{ path: "./theme.json" }] } }),
149 { status: 200 },
150 );
151 }
152 if (url.endsWith("/demo/theme/1.0.0/file/package.json")) {
153 return new Response(
154 JSON.stringify({ license: "MIT", contributes: { themes: [{ path: "./theme.json" }] } }),
155 { status: 200 },
156 );
157 }
158 return new Response(JSON.stringify(extensionDetail()), { status: 200 });
159 });
160 vi.stubGlobal("fetch", fetchMock);
161​
162 const results = await searchOpenVsxThemes(" dracula ");
163​
164 expect(results).toEqual([
165 expect.objectContaining({
166 id: "demo.theme",
167 name: "Demo Theme",
168 publisher: "demo",
169 downloadCount: 123456,
170 iconUrl: `${ASSET_ROOT}/icon.png`,
171 sourceUrl: "https://github.com/demo/theme",
172 license: "MIT",
173 }),
174 ]);
175 const searchUrl = new URL(String(fetchMock.mock.calls[0]![0]));
176 expect(searchUrl.searchParams.get("query")).toBe("dracula");
177 expect(searchUrl.searchParams.get("category")).toBe("Themes");
178 expect(searchUrl.searchParams.get("sortBy")).toBe("downloadCount");
179 expect(searchUrl.searchParams.get("sortOrder")).toBe("desc");
180 expect(searchUrl.searchParams.get("size")).toBe("16");
181 });
182​
183 it("reports an unavailable search when every detail request fails", async () => {
184 vi.stubGlobal(
185 "fetch",
186 vi.fn(async (input: RequestInfo | URL) => {
187 if (String(input).includes("/-/search?")) {
188 return new Response(
189 JSON.stringify({ extensions: [{ namespace: "demo", name: "theme" }] }),
190 { status: 200 },
191 );
192 }
193 return new Response(null, { status: 429 });
194 }),
195 );
196​
197 await expect(searchOpenVsxThemes("dracula")).rejects.toThrow(
198 "Open VSX theme details are unavailable",
199 );
200 });
201​
202 it("downloads a verified VSIX, reads JSONC includes, and pairs contributed variants", async () => {
203 const zip = new JSZip();
204 zip.file("extension/.gitkeep", "");
205 for (let index = 0; index < 3_000; index += 1) {
206 zip.file(`extension/node_modules/package-${index}.js`, "");
207 }
208 zip.file(
209 "extension/themes/base.jsonc",
210 `{
211 // inherited workbench colors
212 "colors": {
213 "editor.foreground": "#eeeeee",
214 "focusBorder": "#8b5cf6",
215 "editor.findMatchBackground": "#663399",
216 "editor.findMatchForeground": "#ffffff",
217 "editor.findMatchHighlightBackground": "#445566",
218 "editor.findMatchHighlightForeground": "#ffffff",
219 },
220 }`,
221 );
222 zip.file(
223 "extension/themes/demo-dark.json",
224 `{
225 "include": "./base.jsonc",
226 "colors": {
227 "editor.background": "#111111",
228 "editor.findMatchHighlightBackground": "#ffffff20"
229 }
230 }`,
231 );
232 zip.file(
233 "extension/themes/demo-light.json",
234 `{
235 "colors": {
236 "editor.background": "#fafafa",
237 "editor.foreground": "#222222",
238 "focusBorder": "#8b5cf6",
239 "editor.findMatchBackground": "#335577",
240 "editor.findMatchForeground": "#ffffff",
241 "editor.findMatchHighlightBackground": "#bbddff",
242 "editor.findMatchHighlightForeground": "#112233"
243 }
244 }`,
245 );
246 zip.file(
247 "extension/themes/demo.json",
248 `{
249 "colors": {
250 "editor.background": "#181818",
251 "editor.foreground": "#eeeeee",
252 "button.background": "#3b82f6",
253 "input.background": "#334155",
254 "input.border": "#64748b"
255 }
256 }`,
257 );
258 const packagedManifest = {
259 publisher: "demo",
260 name: "theme",
261 version: "1.0.0",
262 license: "MIT",
263 contributes: {
264 themes: [
265 { label: "Demo Dark", uiTheme: "vs-dark", path: "./themes/demo-dark.json" },
266 { label: "Demo Light", uiTheme: "vs", path: "./themes/demo-light.json" },
267 { label: "Demo", uiTheme: "vs-dark", path: "./themes/demo.json" },
268 ],
269 },
270 };
271 let packageBytes = new ArrayBuffer(0);
272 let checksum = "";
273 const rebuildPackage = async () => {
274 zip.file("extension/package.json", JSON.stringify(packagedManifest));
275 packageBytes = await zip.generateAsync({
276 type: "arraybuffer",
277 comment: `PK\u0005\u0006${"x".repeat(26)}`,
278 });
279 checksum = [...sha256(new Uint8Array(packageBytes))]
280 .map((byte) => byte.toString(16).padStart(2, "0"))
281 .join("");
282 };
283 await rebuildPackage();
284 const manifest = {
285 contributes: { themes: [{ label: "Wrong", path: "./themes/missing.json" }] },
286 };
287 const extension: OpenVsxThemeExtension = {
288 collectionId: "open-vsx:demo.theme",
289 id: "demo.theme",
290 name: "Demo Theme",
291 publisher: "demo",
292 description: "",
293 downloadCount: 1,
294 iconUrl: null,
295 sourceUrl: null,
296 license: "MIT",
297 manifestUrl: `${ASSET_ROOT}/package.json`,
298 sha256Url: `${ASSET_ROOT}/demo.theme-1.0.0.sha256`,
299 version: "1.0.0",
300 vsixUrl: `${ASSET_ROOT}/demo.theme-1.0.0.vsix`,
301 };
302 vi.stubGlobal(
303 "fetch",
304 vi.fn(async (input: string | URL | Request) => {
305 const url = String(input);
306 if (url === extension.manifestUrl) {
307 return new Response(JSON.stringify(manifest), { status: 200 });
308 }
309 if (url === extension.sha256Url) return new Response(`${checksum} demo.vsix`);
310 return new Response(packageBytes, {
311 status: 200,
312 headers: { "Content-Length": String(packageBytes.byteLength) },
313 });
314 }),
315 );
316​
317 const themes = await importOpenVsxThemeExtension(extension);
318​
319 expect(themes).toHaveLength(2);
320 expect(new Set(themes.map((theme) => theme.id)).size).toBe(2);
321 expect(themes.every((theme) => /^ovx-[a-z0-9-]+-[0-9a-f]{12}$/.test(theme.id))).toBe(true);
322 expect(themes.every((theme) => theme.collection?.id === "open-vsx:demo.theme")).toBe(true);
323 const paired = themes.find(
324 (theme) =>
325 getThemeColorsForMode(theme, "light") !== null &&
326 getThemeColorsForMode(theme, "dark") !== null,
327 )!;
328 expect(paired.label).toBe("Demo");
329 expect(themeColorToHex(paired.colors.canvas)).toBe("#fafafa");
330 expect(themeColorToHex(getThemeColorsForMode(paired, "dark")!.canvas)).toBe("#111111");
331 expect(themeColorToHex(getThemeColorsForMode(paired, "dark")!.text)).toBe("#eeeeee");
332 const darkColors = getThemeColorsForMode(paired, "dark")!;
333 expect(themeColorToHex(darkColors.searchMatchActiveBackground)).toBe("#663399");
334 expect(themeColorToHex(darkColors.searchMatchActiveForeground)).toBe("#ffffff");
335 expect(themeColorToHex(darkColors.searchMatchBackground)).toBe("#2f2f2f");
336 expect(themeColorToHex(darkColors.searchMatchForeground)).toBe("#ffffff");
337 expect(themeColorToHex(paired.colors.searchMatchActiveBackground)).toBe("#335577");
338 expect(themeColorToHex(paired.colors.searchMatchActiveForeground)).toBe("#ffffff");
339 expect(themeColorToHex(paired.colors.searchMatchBackground)).toBe("#bbddff");
340 expect(themeColorToHex(paired.colors.searchMatchForeground)).toBe("#112233");
341 const solo = themes.find((theme) => theme.id !== paired.id)!;
342 expect(themeColorToHex(solo.colors.input)).toBe("#334155");
343​
344 packagedManifest.contributes.themes[0]!.label = "Renamed Dark";
345 packagedManifest.contributes.themes[1]!.label = "Renamed Light";
346 packagedManifest.contributes.themes[2]!.label = "Renamed Solo";
347 await rebuildPackage();
348 const renamedThemes = await importOpenVsxThemeExtension(extension);
349 expect(renamedThemes.map((theme) => theme.id)).toEqual(themes.map((theme) => theme.id));
350​
351 packagedManifest.contributes.themes.push({
352 label: "Duplicate Demo",
353 uiTheme: "vs-dark",
354 path: "./themes/demo.json",
355 });
356 await rebuildPackage();
357 const duplicatePathThemes = await importOpenVsxThemeExtension(extension);
358 expect(new Set(duplicatePathThemes.map((theme) => theme.id)).size).toBe(
359 duplicatePathThemes.length,
360 );
361 expect(themes.every(({ id }) => duplicatePathThemes.some((theme) => theme.id === id))).toBe(
362 true,
363 );
364 packagedManifest.contributes.themes.pop();
365​
366 const emojiLabelThemes = await importOpenVsxThemeExtension({
367 ...extension,
368 name: `${"A".repeat(47)}🎨`,
369 });
370 expect(emojiLabelThemes[0]?.collection?.label).toBe("A".repeat(47));
371​
372 const oversizedGraphemeThemes = await importOpenVsxThemeExtension({
373 ...extension,
374 name: `e${"\u0301".repeat(50)}`,
375 });
376 expect(oversizedGraphemeThemes[0]?.collection?.label).toBe(extension.collectionId);
377​
378 packagedManifest.contributes.themes[2]!.path = "./themes/missing.json";
379 await rebuildPackage();
380 await expect(importOpenVsxThemeExtension(extension)).rejects.toThrow(
381 "could not be imported safely",
382 );
383 packagedManifest.contributes.themes[2]!.path = "./themes/demo.json";
384​
385 packagedManifest.license = "Proprietary";
386 await rebuildPackage();
387 await expect(importOpenVsxThemeExtension(extension)).rejects.toThrow(
388 "does not match its advertised license",
389 );
390​
391 Reflect.deleteProperty(packagedManifest, "license");
392 await rebuildPackage();
393 await expect(importOpenVsxThemeExtension(extension)).rejects.toThrow(
394 "does not match its advertised license",
395 );
396 });
397​
398 it("stops import work when the request is cancelled", async () => {
399 const packageBytes = new Uint8Array([1, 2, 3]);
400 const checksum = [...sha256(packageBytes)]
401 .map((byte) => byte.toString(16).padStart(2, "0"))
402 .join("");
403 const controller = new AbortController();
404 const extension: OpenVsxThemeExtension = {
405 collectionId: "open-vsx:demo.theme",
406 id: "demo.theme",
407 name: "Demo Theme",
408 publisher: "demo",
409 description: "",
410 downloadCount: 1,
411 iconUrl: null,
412 sourceUrl: null,
413 license: "MIT",
414 manifestUrl: `${ASSET_ROOT}/package.json`,
415 sha256Url: `${ASSET_ROOT}/demo.theme-1.0.0.sha256`,
416 version: "1.0.0",
417 vsixUrl: `${ASSET_ROOT}/demo.theme-1.0.0.vsix`,
418 };
419 vi.stubGlobal(
420 "fetch",
421 vi.fn(async (input: string | URL | Request) => {
422 const url = String(input);
423 if (url === extension.manifestUrl) {
424 return new Response(
425 JSON.stringify({ contributes: { themes: [{ path: "./theme.json" }] } }),
426 );
427 }
428 if (url === extension.sha256Url) {
429 controller.abort();
430 return new Response(checksum);
431 }
432 return new Response(packageBytes);
433 }),
434 );
435​
436 await expect(importOpenVsxThemeExtension(extension, controller.signal)).rejects.toMatchObject({
437 name: "AbortError",
438 });
439 });
440​
441 it("rejects a package whose Open VSX checksum does not match", async () => {
442 const extension: OpenVsxThemeExtension = {
443 collectionId: "open-vsx:demo.theme",
444 id: "demo.theme",
445 name: "Demo Theme",
446 publisher: "demo",
447 description: "",
448 downloadCount: 1,
449 iconUrl: null,
450 sourceUrl: null,
451 license: "MIT",
452 manifestUrl: `${ASSET_ROOT}/package.json`,
453 sha256Url: `${ASSET_ROOT}/demo.theme-1.0.0.sha256`,
454 version: "1.0.0",
455 vsixUrl: `${ASSET_ROOT}/demo.theme-1.0.0.vsix`,
456 };
457 vi.stubGlobal(
458 "fetch",
459 vi.fn(async (input: string | URL | Request) => {
460 const url = String(input);
461 if (url === extension.manifestUrl) {
462 return new Response(
463 JSON.stringify({ contributes: { themes: [{ path: "./theme.json" }] } }),
464 );
465 }
466 if (url === extension.sha256Url) return new Response("0".repeat(64));
467 return new Response(new Uint8Array([1, 2, 3]));
468 }),
469 );
470​
471 await expect(importOpenVsxThemeExtension(extension)).rejects.toThrow("integrity check");
472 });
473});