| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { |
| 4 | defaultAppearance, |
| 5 | normalizeAppearance, |
| 6 | themeVariables, |
| 7 | terminalTheme, |
| 8 | themeDocument, |
| 9 | parseThemeDocument, |
| 10 | sameTheme, |
| 11 | colorGroups, |
| 12 | terminalColorGroups, |
| 13 | } from "./appearance-state.mjs"; |
| 14 | |
| 15 | test("element overrides stay independent, reset returns to derived colors, and terminal follows live values", () => { |
| 16 | const appearance = normalizeAppearance(structuredClone(defaultAppearance)); |
| 17 | const theme = appearance.lightTheme; |
| 18 | const before = themeVariables(theme, "light"); |
| 19 | theme.colors = { |
| 20 | chrome: "#123456", |
| 21 | button: "#654321", |
| 22 | buttonInk: "#abcdef", |
| 23 | selection: "#aabbcc", |
| 24 | }; |
| 25 | theme.terminal = { background: "#112233", red: "#fedcba", cursor: "#445566" }; |
| 26 | theme.surface = "#fafafa"; |
| 27 | const vars = themeVariables(theme, "light"); |
| 28 | assert.equal(vars["--chrome"], "#123456"); |
| 29 | assert.equal(vars["--button-ink"], "#abcdef"); |
| 30 | assert.equal(vars["--surface"], "#fafafa"); |
| 31 | assert.equal(vars["--border"], vars["--line"]); |
| 32 | assert.notEqual(vars["--line"], before["--line"]); |
| 33 | const terminal = terminalTheme(theme, "light"); |
| 34 | assert.equal(terminal.background, "#112233"); |
| 35 | assert.equal(terminal.red, "#fedcba"); |
| 36 | assert.equal(terminal.selectionBackground, "#aabbcc"); |
| 37 | delete theme.colors.chrome; |
| 38 | assert.notEqual(themeVariables(theme, "light")["--chrome"], "#123456"); |
| 39 | }); |
| 40 | |
| 41 | test("theme document round trips both variants and all editable colors, without device preferences or library", () => { |
| 42 | const appearance = normalizeAppearance(structuredClone(defaultAppearance)); |
| 43 | appearance.lightTheme.colors = Object.fromEntries( |
| 44 | colorGroups.flatMap((group) => |
| 45 | group.entries.map(([key]) => [key, "#123456"]), |
| 46 | ), |
| 47 | ); |
| 48 | appearance.darkTheme.terminal = Object.fromEntries( |
| 49 | terminalColorGroups.flatMap((group) => |
| 50 | group.entries.map(([key]) => [key, "#abcdef"]), |
| 51 | ), |
| 52 | ); |
| 53 | appearance.darkTheme.fonts.code = "Menlo"; |
| 54 | const document = themeDocument(" 自定义主题 ", appearance); |
| 55 | const imported = parseThemeDocument(JSON.stringify(document)); |
| 56 | assert.equal(imported.name, "自定义主题"); |
| 57 | assert.ok(sameTheme(appearance, imported)); |
| 58 | assert.deepEqual(Object.keys(imported).sort(), [ |
| 59 | "darkTheme", |
| 60 | "format", |
| 61 | "lightTheme", |
| 62 | "name", |
| 63 | "version", |
| 64 | ]); |
| 65 | const copy = structuredClone(imported); |
| 66 | copy.lightTheme.colors = Object.fromEntries( |
| 67 | Object.entries(copy.lightTheme.colors).reverse(), |
| 68 | ); |
| 69 | assert.ok(sameTheme(copy, imported)); |
| 70 | copy.darkTheme.terminal.red = "#000000"; |
| 71 | assert.equal(sameTheme(copy, imported), false); |
| 72 | }); |
| 73 | |
| 74 | test("invalid theme files are rejected before any mutation", () => { |
| 75 | const valid = themeDocument("主题", defaultAppearance); |
| 76 | const reject = (mutate) => { |
| 77 | const data = structuredClone(valid); |
| 78 | mutate(data); |
| 79 | assert.throws(() => parseThemeDocument(JSON.stringify(data))); |
| 80 | }; |
| 81 | for (const input of [ |
| 82 | "not json", |
| 83 | "null", |
| 84 | "[]", |
| 85 | "{}", |
| 86 | " ".repeat(256 * 1024 + 1), |
| 87 | ]) |
| 88 | assert.throws(() => parseThemeDocument(input)); |
| 89 | reject((data) => { |
| 90 | data.version = 2; |
| 91 | }); |
| 92 | reject((data) => { |
| 93 | data.name = " "; |
| 94 | }); |
| 95 | reject((data) => { |
| 96 | delete data.darkTheme; |
| 97 | }); |
| 98 | reject((data) => { |
| 99 | data.lightTheme.accent = "#abc"; |
| 100 | }); |
| 101 | reject((data) => { |
| 102 | data.lightTheme.colors = { chrome: "red" }; |
| 103 | }); |
| 104 | reject((data) => { |
| 105 | data.lightTheme.colors = { unknown: "#abcdef" }; |
| 106 | }); |
| 107 | reject((data) => { |
| 108 | data.lightTheme.terminal = { foreground: "url(https://example.com)" }; |
| 109 | }); |
| 110 | reject((data) => { |
| 111 | data.lightTheme.fonts.code = "Menlo; color:red"; |
| 112 | }); |
| 113 | reject((data) => { |
| 114 | data.lightTheme.contrast = 10.5; |
| 115 | }); |
| 116 | reject((data) => { |
| 117 | data.mode = "dark"; |
| 118 | }); |
| 119 | assert.equal(defaultAppearance.lightTheme.accent, "#339cff"); |
| 120 | }); |
| 121 | |
| 122 | test("all built-in palettes produce valid, independently editable theme documents", async () => { |
| 123 | const { themeCatalog, presets, presetNames, presetGroups, applyThemePreset } = |
| 124 | await import("./appearance-presets.mjs"); |
| 125 | assert.equal( |
| 126 | new Set(themeCatalog.map((item) => item.id)).size, |
| 127 | themeCatalog.length, |
| 128 | ); |
| 129 | assert.ok(Object.keys(presets.light).length >= 10); |
| 130 | assert.ok(Object.keys(presets.dark).length >= 12); |
| 131 | for (const item of themeCatalog) { |
| 132 | assert.ok(presetGroups.includes(item.group)); |
| 133 | assert.equal(presetNames[item.id], item.name); |
| 134 | for (const variant of ["light", "dark"]) { |
| 135 | if (!item[variant]) continue; |
| 136 | const current = structuredClone(defaultAppearance[variant + "Theme"]); |
| 137 | current.colors = { danger: "#123456", toolbar: "#123456" }; |
| 138 | current.terminal = { foreground: "#123456", brightRed: "#123456" }; |
| 139 | current.fonts.code = "Menlo"; |
| 140 | const applied = applyThemePreset(current, variant, item.id); |
| 141 | const appearance = normalizeAppearance({ [variant + "Theme"]: applied }); |
| 142 | const imported = parseThemeDocument( |
| 143 | JSON.stringify(themeDocument(item.name, appearance)), |
| 144 | ); |
| 145 | assert.ok(sameTheme(appearance, imported), item.id); |
| 146 | assert.equal(applied.fonts.code, "Menlo"); |
| 147 | assert.notEqual(applied.colors.danger, "#123456"); |
| 148 | assert.notEqual(applied.terminal.foreground, "#123456"); |
| 149 | assert.ok( |
| 150 | Object.values(themeVariables(applied, variant)).every( |
| 151 | (value) => !String(value).includes("NaN"), |
| 152 | ), |
| 153 | ); |
| 154 | applied.colors.line = "#123456"; |
| 155 | applied.terminal.red = "#123456"; |
| 156 | assert.notEqual(presets[variant][item.id].colors.line, "#123456"); |
| 157 | assert.notEqual(presets[variant][item.id].terminal.red, "#123456"); |
| 158 | } |
| 159 | } |
| 160 | }); |
| 161 | |
| 162 | function contrastRatio(left, right) { |
| 163 | const luminance = (color) => { |
| 164 | const [r, g, b] = [1, 3, 5] |
| 165 | .map((index) => parseInt(color.slice(index, index + 2), 16) / 255) |
| 166 | .map((value) => |
| 167 | value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4, |
| 168 | ); |
| 169 | return 0.2126 * r + 0.7152 * g + 0.0722 * b; |
| 170 | }; |
| 171 | const a = luminance(left), |
| 172 | b = luminance(right); |
| 173 | return (Math.max(a, b) + 0.05) / (Math.min(a, b) + 0.05); |
| 174 | } |
| 175 | |
| 176 | test("accessible presets keep text, state colors, focus, borders and all ANSI text legible", async () => { |
| 177 | const { applyThemePreset } = await import("./appearance-presets.mjs"); |
| 178 | for (const id of ["color-vision", "high-contrast"]) |
| 179 | for (const variant of ["light", "dark"]) { |
| 180 | const theme = applyThemePreset( |
| 181 | defaultAppearance[variant + "Theme"], |
| 182 | variant, |
| 183 | id, |
| 184 | ); |
| 185 | const vars = themeVariables(theme, variant), |
| 186 | terminal = terminalTheme(theme, variant); |
| 187 | const threshold = id === "high-contrast" ? 7 : 4.5; |
| 188 | const check = (foreground, background, minimum, name) => |
| 189 | assert.ok( |
| 190 | contrastRatio(foreground, background) >= minimum, |
| 191 | `${id}/${variant}/${name}: ${contrastRatio(foreground, background).toFixed(3)} < ${minimum}`, |
| 192 | ); |
| 193 | for (const key of [ |
| 194 | "--ink", |
| 195 | "--strong", |
| 196 | "--muted", |
| 197 | "--link", |
| 198 | "--success", |
| 199 | "--danger", |
| 200 | "--warning", |
| 201 | "--inactive", |
| 202 | ]) { |
| 203 | for (const background of [ |
| 204 | "--paper", |
| 205 | "--chrome", |
| 206 | "--chrome-dark", |
| 207 | "--hover", |
| 208 | "--selected", |
| 209 | "--input", |
| 210 | ]) { |
| 211 | |
| 212 | if ( |
| 213 | ["--hover", "--selected"].includes(background) && |
| 214 | !["--ink", "--strong"].includes(key) |
| 215 | ) |
| 216 | continue; |
| 217 | check( |
| 218 | vars[key], |
| 219 | vars[background], |
| 220 | threshold, |
| 221 | `${key} on ${background}`, |
| 222 | ); |
| 223 | } |
| 224 | } |
| 225 | check(vars["--sidebar-ink"], vars["--sidebar"], threshold, "sidebar"); |
| 226 | check(vars["--toolbar-ink"], vars["--toolbar"], threshold, "toolbar"); |
| 227 | check(vars["--button-ink"], vars["--button"], threshold, "button"); |
| 228 | for (const background of ["--paper", "--chrome", "--input"]) { |
| 229 | check(vars["--line"], vars[background], 3, "border"); |
| 230 | check(vars["--focus"], vars[background], 3, "focus"); |
| 231 | } |
| 232 | for (const group of terminalColorGroups) |
| 233 | for (const [key] of group.entries) { |
| 234 | if ( |
| 235 | ["background", "selectionBackground", "cursorAccent"].includes(key) |
| 236 | ) |
| 237 | continue; |
| 238 | check( |
| 239 | terminal[key], |
| 240 | terminal.background, |
| 241 | threshold, |
| 242 | `terminal ${key}`, |
| 243 | ); |
| 244 | } |
| 245 | assert.equal(theme.opaqueWindows, true); |
| 246 | assert.notEqual(vars["--success"], vars["--danger"]); |
| 247 | check( |
| 248 | terminal.foreground, |
| 249 | terminal.selectionBackground, |
| 250 | threshold, |
| 251 | "terminal selection", |
| 252 | ); |
| 253 | check(terminal.cursorAccent, terminal.cursor, threshold, "cursor text"); |
| 254 | } |
| 255 | }); |