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

openVsxThemes.browser.test.ts

159 lines5.9 KB

1import { sha256 } from "@noble/hashes/sha2";
2import JSZip from "jszip";
3import { describe, expect, it, vi } from "vite-plus/test";
4​
5import { importOpenVsxThemeExtension, type OpenVsxThemeExtension } from "./openVsxThemes";
6​
7const ASSET_ROOT = "https://open-vsx.org/api/demo/theme/1.0.0/file";
8const EXTENSION: OpenVsxThemeExtension = {
9 collectionId: "open-vsx:demo.theme",
10 id: "demo.theme",
11 name: "Demo Theme",
12 publisher: "demo",
13 description: "",
14 downloadCount: 1,
15 iconUrl: null,
16 sourceUrl: null,
17 license: "MIT",
18 manifestUrl: `${ASSET_ROOT}/package.json`,
19 sha256Url: `${ASSET_ROOT}/demo.theme-1.0.0.sha256`,
20 version: "1.0.0",
21 vsixUrl: `${ASSET_ROOT}/demo.theme-1.0.0.vsix`,
22};
23​
24async function serveExtension(
25 files: Record<string, string>,
26 themes = [{ label: "Demo", uiTheme: "vs-dark", path: "./themes/demo.json" }],
27) {
28 const manifest = {
29 publisher: "demo",
30 name: "theme",
31 version: "1.0.0",
32 license: "MIT",
33 contributes: { themes },
34 };
35 const zip = new JSZip().file("extension/package.json", JSON.stringify(manifest));
36 for (const [path, contents] of Object.entries(files)) zip.file(`extension/${path}`, contents);
37 const packageBytes = await zip.generateAsync({ type: "arraybuffer" });
38 const checksum = [...sha256(new Uint8Array(packageBytes))]
39 .map((byte) => byte.toString(16).padStart(2, "0"))
40 .join("");
41 vi.stubGlobal(
42 "fetch",
43 vi.fn(async (input: string | URL | Request) => {
44 const url = String(input);
45 if (url === EXTENSION.manifestUrl) return new Response(JSON.stringify(manifest));
46 if (url === EXTENSION.sha256Url) return new Response(checksum);
47 return new Response(packageBytes);
48 }),
49 );
50 return { [Symbol.dispose]: () => vi.unstubAllGlobals() };
52​
53const tmTheme = (rules: string) => `<?xml version="1.0" encoding="UTF-8"?>
54<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
55<plist version="1.0"><dict>
56 <key>name</key><string>Demo</string>
57 <key>settings</key><array>${rules}</array>
58</dict></plist>`;
59​
60const defaults = (foreground: string) =>
61 `<dict><key>settings</key><dict><key>foreground</key><string>${foreground}</string></dict></dict>`;
62​
63const rule = (scope: string, foreground: string) =>
64 `<dict><key>scope</key><string>${scope}</string><key>settings</key><dict><key>foreground</key><string>${foreground}</string></dict></dict>`;
65​
66describe("Open VSX themes", () => {
67 it("reads token colors from a packaged TextMate theme file", async () => {
68 using _openVsx = await serveExtension({
69 "themes/demo.json": JSON.stringify({
70 colors: { "editor.background": "#151521" },
71 tokenColors: "./syntax/demo.tmTheme",
72 }),
73 "themes/syntax/demo.tmTheme": tmTheme(
74 defaults("#ddeeff") + rule("keyword", "#ff22cc") + rule("string", "#22ccff"),
75 ),
76 });
77​
78 const [theme] = await importOpenVsxThemeExtension(EXTENSION);
79​
80 expect(theme?.syntax?.dark).toMatchObject({
81 foreground: { color: "#ddeeff" },
82 keyword: { color: "#ff22cc" },
83 string: { color: "#22ccff" },
84 });
85 });
86​
87 it("reads a packaged TextMate theme whose file name ends in .json", async () => {
88 using _openVsx = await serveExtension({
89 "themes/demo.json": JSON.stringify({
90 colors: { "editor.background": "#151521" },
91 tokenColors: "./tokens.json",
92 }),
93 "themes/tokens.json": tmTheme(rule("keyword", "#ff22cc")),
94 });
95​
96 const [theme] = await importOpenVsxThemeExtension(EXTENSION);
97​
98 expect(theme?.syntax?.dark).toMatchObject({ keyword: { color: "#ff22cc" } });
99 });
100​
101 it("lets an including theme's editor foreground override its base's token file default", async () => {
102 using _openVsx = await serveExtension({
103 "themes/base.json": JSON.stringify({ tokenColors: "./base.tmTheme" }),
104 "themes/base.tmTheme": tmTheme(defaults("#ddeeff") + rule("keyword", "#ff22cc")),
105 "themes/demo.json": JSON.stringify({
106 include: "./base.json",
107 colors: { "editor.background": "#151521", "editor.foreground": "#aabbcc" },
108 }),
109 });
110​
111 const [theme] = await importOpenVsxThemeExtension(EXTENSION);
112​
113 expect(theme?.syntax?.dark).toMatchObject({
114 foreground: { color: "#aabbcc" },
115 keyword: { color: "#ff22cc" },
116 });
117 });
118​
119 it("counts a TextMate theme shared by many variants once", async () => {
120 const files: Record<string, string> = {
121 "themes/shared.tmTheme": tmTheme(rule("keyword", "#ff22cc")),
122 };
123 const themes = Array.from({ length: 36 }, (_, variant) => {
124 for (let depth = 0; depth < 8; depth += 1) {
125 files[`themes/v${variant}-${depth}.json`] = JSON.stringify(
126 depth < 7
127 ? { include: `./v${variant}-${depth + 1}.json` }
128 : { colors: { "editor.background": "#151521" }, tokenColors: "./shared.tmTheme" },
129 );
130 }
131 return { label: `Demo ${variant}`, uiTheme: "vs-dark", path: `./themes/v${variant}-0.json` };
132 });
133 using _openVsx = await serveExtension(files, themes);
134​
135 const imported = await importOpenVsxThemeExtension(EXTENSION);
136​
137 expect(imported).toHaveLength(36);
138 expect(imported[35]?.syntax?.dark).toMatchObject({ keyword: { color: "#ff22cc" } });
139 });
140​
141 it("keeps an included theme's packaged token colors beneath its own", async () => {
142 using _openVsx = await serveExtension({
143 "themes/base.json": JSON.stringify({ tokenColors: "./base.tmTheme" }),
144 "themes/base.tmTheme": tmTheme(rule("keyword", "#ff22cc") + rule("string", "#22ccff")),
145 "themes/demo.json": JSON.stringify({
146 include: "./base.json",
147 colors: { "editor.background": "#151521" },
148 tokenColors: [{ scope: "string", settings: { foreground: "#ccff22" } }],
149 }),
150 });
151​
152 const [theme] = await importOpenVsxThemeExtension(EXTENSION);
153​
154 expect(theme?.syntax?.dark).toMatchObject({
155 keyword: { color: "#ff22cc" },
156 string: { color: "#ccff22" },
157 });
158 });
159});