| 1 | import assert from "node:assert/strict"; |
| 2 | import fs from "node:fs/promises"; |
| 3 | import { fileURLToPath } from "node:url"; |
| 4 | import { chooseInline, createWorkbench } from "./workbench-fixture.mjs"; |
| 5 | import { |
| 6 | presets, |
| 7 | themeCatalog, |
| 8 | parseThemeDocument, |
| 9 | sameTheme, |
| 10 | } from "../src/appearance-state.mjs"; |
| 11 | const output = fileURLToPath( |
| 12 | new URL( |
| 13 | "../../docs/test-reports/2026-11-02-builtin-themes/", |
| 14 | import.meta.url, |
| 15 | ), |
| 16 | ); |
| 17 | await fs.mkdir(output, { recursive: true }); |
| 18 | let exported; |
| 19 | const { browser, page, state, errors } = await createWorkbench({ |
| 20 | nativeRequest: (body) => { |
| 21 | if (body.action === "exportTheme") exported = body; |
| 22 | return true; |
| 23 | }, |
| 24 | }); |
| 25 | const checks = []; |
| 26 | const open = async () => { |
| 27 | await page.getByRole("button", { name: /^设置/ }).click(); |
| 28 | await page.getByRole("button", { name: "外观", exact: true }).click(); |
| 29 | await page.getByRole("heading", { name: "外观", exact: true }).waitFor(); |
| 30 | }; |
| 31 | const saved = () => |
| 32 | page |
| 33 | .locator(".appearance-save-state") |
| 34 | .filter({ hasText: "更改自动保存,即时生效" }) |
| 35 | .waitFor(); |
| 36 | const select = async (variant, id) => { |
| 37 | await chooseInline(page |
| 38 | .getByLabel((variant === "light" ? "浅色" : "深色") + "主题预设", { |
| 39 | exact: true, |
| 40 | }), id); |
| 41 | await saved(); |
| 42 | }; |
| 43 | const shot = async (name) => { |
| 44 | await page.locator(".settings-content").evaluate((el) => el.scrollTo(0, 0)); |
| 45 | await page.screenshot({ |
| 46 | path: output + name + ".png", |
| 47 | animations: "disabled", |
| 48 | }); |
| 49 | }; |
| 50 | try { |
| 51 | await page.emulateMedia({ colorScheme: "light" }); |
| 52 | await open(); |
| 53 | for (const variant of ["light", "dark"]) { |
| 54 | const picker = page.getByLabel( |
| 55 | (variant === "light" ? "浅色" : "深色") + "主题预设", |
| 56 | { exact: true }, |
| 57 | ); |
| 58 | await picker.click(); |
| 59 | const list = page.getByRole("listbox"); |
| 60 | assert.equal(await list.locator(".inline-select-group").count(), 4); |
| 61 | assert.equal( |
| 62 | await list.getByRole("option").count(), |
| 63 | Object.keys(presets[variant]).length, |
| 64 | ); |
| 65 | await picker.press("Escape"); |
| 66 | |
| 67 | for (const id of Object.keys(presets[variant])) { |
| 68 | await select(variant, id); |
| 69 | const theme = state.appearance[variant + "Theme"]; |
| 70 | assert.equal(theme.preset, id); |
| 71 | assert.deepEqual(theme.colors, presets[variant][id].colors); |
| 72 | assert.deepEqual(theme.terminal, presets[variant][id].terminal); |
| 73 | } |
| 74 | } |
| 75 | await select("light", "vscode-modern"); |
| 76 | await select("dark", "vscode-modern"); |
| 77 | assert.equal( |
| 78 | await page.evaluate( |
| 79 | () => |
| 80 | getComputedStyle(document.querySelector(".settings-sidebar")) |
| 81 | .backgroundColor, |
| 82 | ), |
| 83 | "rgb(248, 248, 248)", |
| 84 | ); |
| 85 | await shot("vscode-modern-light"); |
| 86 | await page.getByRole("button", { name: "深色", exact: true }).click(); |
| 87 | await saved(); |
| 88 | await shot("vscode-modern-dark"); |
| 89 | await select("light", "sepia"); |
| 90 | await select("dark", "night"); |
| 91 | await page.getByRole("button", { name: "浅色", exact: true }).click(); |
| 92 | await saved(); |
| 93 | await shot("reading-sepia"); |
| 94 | checks.push( |
| 95 | `真实下拉选取全部 ${Object.keys(presets.light).length + Object.keys(presets.dark).length} 套配色,四组分类、界面及终端色板完整应用`, |
| 96 | ); |
| 97 | await select("light", "color-vision"); |
| 98 | await select("dark", "color-vision"); |
| 99 | assert.equal( |
| 100 | await page.evaluate(() => document.documentElement.dataset.translucent), |
| 101 | "false", |
| 102 | ); |
| 103 | for (const label of ["浅色主题状态配色预览", "深色主题状态配色预览"]) { |
| 104 | const preview = page.getByLabel(label, { exact: true }); |
| 105 | for (const text of ["成功", "警告", "错误"]) |
| 106 | await preview.getByText(text, { exact: true }).waitFor(); |
| 107 | assert.equal(await preview.locator("svg").count(), 3); |
| 108 | } |
| 109 | assert.equal( |
| 110 | await page.evaluate(() => |
| 111 | getComputedStyle(document.documentElement).getPropertyValue("--success"), |
| 112 | ), |
| 113 | "#005a9c", |
| 114 | ); |
| 115 | assert.equal( |
| 116 | await page.evaluate(() => |
| 117 | getComputedStyle(document.documentElement).getPropertyValue("--danger"), |
| 118 | ), |
| 119 | "#a63b00", |
| 120 | ); |
| 121 | await shot("color-vision-light"); |
| 122 | await page.getByRole("button", { name: "深色", exact: true }).click(); |
| 123 | await saved(); |
| 124 | await shot("color-vision-dark"); |
| 125 | await select("light", "high-contrast"); |
| 126 | await select("dark", "high-contrast"); |
| 127 | await shot("high-contrast-dark"); |
| 128 | await page.getByRole("button", { name: "浅色", exact: true }).click(); |
| 129 | await saved(); |
| 130 | await shot("high-contrast-light"); |
| 131 | |
| 132 | await select("light", "paper"); |
| 133 | assert.equal(state.appearance.lightTheme.colors.success, undefined); |
| 134 | assert.equal(state.appearance.lightTheme.terminal.green, "#426e37"); |
| 135 | await select("light", "color-vision"); |
| 136 | await select("dark", "color-vision"); |
| 137 | await page.getByLabel("主题名称", { exact: true }).fill("我的蓝橙搭配"); |
| 138 | await page.getByRole("button", { name: "另存为主题", exact: true }).click(); |
| 139 | await page.getByRole("status").filter({ hasText: "主题已保存" }).waitFor(); |
| 140 | const snapshot = structuredClone(state.appearance); |
| 141 | await page.getByRole("button", { name: "导出主题", exact: true }).click(); |
| 142 | await page.getByRole("status").filter({ hasText: "主题已导出" }).waitFor(); |
| 143 | assert.ok(sameTheme(parseThemeDocument(exported.text), snapshot)); |
| 144 | await fs.writeFile( |
| 145 | output + "accessible-export.cage-theme.json", |
| 146 | exported.text, |
| 147 | ); |
| 148 | await page.reload(); |
| 149 | await open(); |
| 150 | assert.equal( |
| 151 | await page.getByLabel("浅色主题预设", { exact: true }).getAttribute("value"), |
| 152 | "color-vision", |
| 153 | ); |
| 154 | assert.equal( |
| 155 | await page.getByLabel("已保存的主题", { exact: true }).getAttribute("value"), |
| 156 | state.appearance.customThemes[0].id, |
| 157 | ); |
| 158 | assert.ok(sameTheme(state.appearance, snapshot)); |
| 159 | checks.push( |
| 160 | "蓝橙状态图标与文字、高对比不透明窗口、跨预设清除旧覆盖、命名保存/导出/刷新读回", |
| 161 | ); |
| 162 | await page.setViewportSize({ width: 820, height: 558 }); |
| 163 | await shot("builtin-minimum"); |
| 164 | assert.equal( |
| 165 | await page.evaluate( |
| 166 | () => document.documentElement.scrollWidth > innerWidth, |
| 167 | ), |
| 168 | false, |
| 169 | ); |
| 170 | await page.getByRole("button", { name: "返回工作台", exact: true }).click(); |
| 171 | await page |
| 172 | .getByRole("button", { name: "启动 Cage 环境", exact: true }) |
| 173 | .click(); |
| 174 | await page |
| 175 | .getByRole("button", { name: "新建终端", exact: true }) |
| 176 | .first() |
| 177 | .click(); |
| 178 | await page.getByRole("menuitem", { name: "普通终端", exact: true }).click(); |
| 179 | await page.getByRole("tab", { name: "终端 1", exact: true }).waitFor(); |
| 180 | await page.waitForFunction( |
| 181 | () => |
| 182 | getComputedStyle(document.querySelector(".xterm-scrollable-element")) |
| 183 | .backgroundColor === "rgb(255, 255, 255)", |
| 184 | ); |
| 185 | const connections = state.terminalConnections; |
| 186 | await open(); |
| 187 | await select("light", "high-contrast"); |
| 188 | await page.getByRole("button", { name: "深色", exact: true }).click(); |
| 189 | await saved(); |
| 190 | await page.getByRole("button", { name: "返回工作台", exact: true }).click(); |
| 191 | await page.waitForFunction( |
| 192 | () => |
| 193 | getComputedStyle(document.querySelector(".xterm-scrollable-element")) |
| 194 | .backgroundColor === "rgb(18, 27, 36)", |
| 195 | ); |
| 196 | assert.equal(state.terminalConnections, connections); |
| 197 | await page.setViewportSize({ width: 1280, height: 850 }); |
| 198 | await page.screenshot({ |
| 199 | path: output + "terminal-color-vision.png", |
| 200 | animations: "disabled", |
| 201 | }); |
| 202 | checks.push("820px 无横向溢出;明暗切换实时更新已有终端,连接保持"); |
| 203 | assert.deepEqual(errors, []); |
| 204 | assert.ok( |
| 205 | !state.requests.some( |
| 206 | (request) => request.path === "/settings" && request.method === "POST", |
| 207 | ), |
| 208 | ); |
| 209 | const result = { |
| 210 | passed: true, |
| 211 | families: themeCatalog.length, |
| 212 | light: Object.keys(presets.light).length, |
| 213 | dark: Object.keys(presets.dark).length, |
| 214 | checks, |
| 215 | scope: "真实前端、xterm 和隔离 API;不连接日常 VM", |
| 216 | }; |
| 217 | await fs.writeFile(output + "result.json", JSON.stringify(result, null, 2)); |
| 218 | console.log(JSON.stringify(result)); |
| 219 | } finally { |
| 220 | await browser.close(); |
| 221 | } |