Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

test(theme): qualify Browser appearance with native OS changes

Linuxdazhao8251b4a752b1
diff --cc docs/spec/06-delivery/04-e2e-test-plan.md
index f7347948c,0a60b0c46..7b7ab21a3
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@@ -3868,6 -3848,6 +3868,13 @@@ identify the platform validation still
#### E2E-091: Appearance card selects searchable theme and language pickers
++macOS OS-event acceptance: `PI_E2E_SYSTEM_APPEARANCE=1 node scripts/e2e-browser-system-appearance.mjs`.
++The opt-in probe temporarily changes OS appearance and restores it in a finalizer.
++It mounts the bundled Browser with production lifecycle functions in sandboxed
++Electron, checks light/dark/light paint, fixed app/plugin palettes, view recreation,
++process restart and listener removal. Host/plugin-registry edges are synthetic;
++this does not claim an installed full-app settings journey.
++
- **Preconditions**: App running on macOS; the harness can exercise English,
Simplified Chinese, Traditional Chinese, Turkish, German, Spanish, French,
Korean, and Brazilian Portuguese system locales.
diff --cc docs/zh-CN/spec/04-ux/06-settings-ia.md
index 7e9da03db,7e9da03db..d2ec61cc4
--- a/docs/zh-CN/spec/04-ux/06-settings-ia.md
+++ b/docs/zh-CN/spec/04-ux/06-settings-ia.md
@@@ -80,6 -80,6 +80,9 @@@
- **界面**卡:
- **主题**:可搜索的选择行(与语言相同的锚定菜单)。关闭时的触发器按当前文案收缩,不超过设置控件列,并显示当前名称。菜单把系统、浅色、深色钉在顶部,插件主题在分隔线之后列出,并带「由…提供」提示。搜索匹配名称、描述、id 和插件 id。选择更新 `settings.theme`。
++ With System selected, OS appearance changes refresh the resolved plugin
++ palette and open Browser chrome without another settings write or restart.
++ Explicit light/dark and registered plugin palettes remain fixed.
- **语言**:可搜索的选择行(不是卡片网格)。关闭时的触发器按当前文案收缩,不超过设置控件列,显示当前语言的本地名称,或「跟随系统」。菜单把「跟随系统」钉在顶部并内嵌显示检测到的语言(例如「当前:简体中文」),然后按本地名称(永不翻译的 endonym)和英文名称列出每个已发布语言,供搜索和排序。选择更新 `settings.language`。新增语言只需加一份目录和一行注册表;选择器不硬编码选项列表。
- **字体**:可搜索的选择行(触发器以该字体的字样显示当前字体)
提供「跟随系统」以及由 Electron 主进程枚举的系统已安装字体;
diff --cc docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
index 4f7f956b8,a1e9b5c5d..a7f2fdaa4
--- a/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
@@@ -1484,6 -1467,6 +1484,16 @@@ task-candidate E2E 从请求工作树�
#### E2E-091:外观卡通过可搜索主题和语言选择器选择
++macOS OS-event acceptance: `PI_E2E_SYSTEM_APPEARANCE=1 node scripts/e2e-browser-system-appearance.mjs`.
++The opt-in probe temporarily changes OS appearance and restores it in a finalizer.
++It mounts the bundled Browser with production lifecycle functions in sandboxed
++Electron, checks light/dark/light paint, fixed app/plugin palettes, view recreation,
++process restart and listener removal. Host/plugin-registry edges are synthetic;
++this does not claim an installed full-app settings journey.
++With Browser open, select System and switch OS light → dark → light. The shell
++and Browser toolbar update together. Explicit Light, Dark and registered plugin
++palettes remain fixed across OS changes; repeat after shutdown/restart.
++
- **先决条件**:应用程序在 macOS 上运行;测试工具可以切换英语、简体中文、繁体中文、土耳其语、德语、西班牙语、法语、韩语和巴西葡萄牙语系统区域设置。
- **步骤**:
1) 打开设置 → 外观。
diff --cc scripts/e2e-browser-system-appearance.mjs
index 000000000,000000000..1fbcd7efa
new file mode 100644
--- /dev/null
+++ b/scripts/e2e-browser-system-appearance.mjs
@@@ -1,0 -1,0 +1,114 @@@
++#!/usr/bin/env node
++/** Opt-in macOS appearance acceptance; restore the original OS setting. */
++import assert from "node:assert/strict";
++import { execFileSync, spawn } from "node:child_process";
++import { mkdtemp, rm, writeFile } from "node:fs/promises";
++import { tmpdir } from "node:os";
++import { join, resolve } from "node:path";
++import { build } from "esbuild";
++import { resolveElectronBinary } from "./e2e/boot.mjs";
++
++assert.equal(process.platform, "darwin", "This acceptance uses macOS System Events");
++assert.equal(process.env.PI_E2E_SYSTEM_APPEARANCE, "1", "Set PI_E2E_SYSTEM_APPEARANCE=1 to allow temporary OS appearance changes");
++const root = resolve(import.meta.dirname, "..");
++const osScript = source => execFileSync("osascript", ["-e", 'tell application "System Events" to tell appearance preferences to ' + source], { encoding: "utf8" }).trim();
++const original = osScript("get dark mode");
++assert.ok(original === "true" || original === "false");
++const temp = await mkdtemp(join(tmpdir(), "pi-browser-appearance-"));
++try {
++ await writeFile(join(temp, "preload.cjs"), `
++const { contextBridge, ipcRenderer } = require("electron");
++contextBridge.exposeInMainWorld("pluginBridge", {
++ on: (name, callback) => ipcRenderer.on(name, (_event, value) => callback(value)),
++ invoke: name => ipcRenderer.invoke("appearance-probe", name),
++});
++`);
++ await build({
++ stdin: { sourcefile: "appearance-main.ts", loader: "ts", resolveDir: root, contents: `
++import assert from "node:assert/strict";
++import { execFileSync } from "node:child_process";
++import { app, BrowserWindow, ipcMain, nativeTheme } from "electron";
++import { createApplicationLifecycle } from "./apps/desktop/electron/main/bootstrap/app-lifecycle";
++app.setPath("userData", ${JSON.stringify(join(temp, "profile"))});
++app.on("window-all-closed", () => {});
++app.whenReady().then(async () => {
++ const initialListeners = nativeTheme.listenerCount("updated");
++ let window;
++ const appearanceState = { appThemePreference: "system", pluginPanelTheme: "light", updaterLocale: "en", broadcastAppearanceSignature: "" };
++ const theme = { id: "plugin:fixture/midnight", base: "dark", css: "" };
++ const lifecycle = createApplicationLifecycle({
++ state: { quitting: false }, appState: {}, appearanceState,
++ plugins: { getThemes: () => [theme], broadcastEvent() {} }, logger: { app() {} },
++ getHost: () => null, getRunningSessionIds: () => [],
++ broadcastPluginPanelEvent: (name, payload) => window?.webContents.send(name, payload),
++ } as any);
++ ipcMain.handle("appearance-probe", (_event, name) => name === "app.getAppearance" ? lifecycle.resolveAppearance() : null);
++ const open = async () => {
++ window = new BrowserWindow({ show: false, width: 800, height: 500, webPreferences: {
++ sandbox: true, contextIsolation: true, nodeIntegration: false, backgroundThrottling: false,
++ preload: ${JSON.stringify(join(temp, "preload.cjs"))},
++ } });
++ await window.loadFile(${JSON.stringify(join(root, "apps/desktop/resources/plugins/pi.browser/views/browser.html"))});
++ };
++ const waitFor = async check => {
++ const deadline = Date.now() + 10000;
++ while (!(await check())) {
++ if (Date.now() > deadline) throw new Error("appearance did not propagate within ten seconds");
++ await new Promise(resolve => setTimeout(resolve, 25));
++ }
++ };
++ const setOS = dark => execFileSync("osascript", ["-e", 'tell application "System Events" to tell appearance preferences to set dark mode to ' + dark]);
++ const sample = () => window.webContents.executeJavaScript('({base:document.documentElement.dataset.base,background:getComputedStyle(document.querySelector(".chrome")).backgroundColor})');
++ try {
++ await open();
++ lifecycle.applyAppThemePreference("system");
++ const samples = [];
++ for (const dark of [false, true, false]) {
++ setOS(dark);
++ await waitFor(async () => nativeTheme.shouldUseDarkColors === dark && (await sample()).base === (dark ? "dark" : "light"));
++ assert.equal(appearanceState.pluginPanelTheme, dark ? "dark" : "light");
++ samples.push(await sample());
++ }
++ assert.notEqual(samples[0].background, samples[1].background);
++ assert.equal(samples[0].background, samples[2].background);
++ for (const preference of ["light", "dark", theme.id]) {
++ lifecycle.applyAppThemePreference(preference);
++ const expected = preference === "light" ? "light" : "dark";
++ await waitFor(async () => (await sample()).base === expected);
++ for (const dark of [true, false]) {
++ setOS(dark);
++ assert.equal((await sample()).base, expected);
++ }
++ }
++ lifecycle.applyAppThemePreference("system");
++ window.destroy();
++ await open();
++ await waitFor(async () => (await sample()).base === (nativeTheme.shouldUseDarkColors ? "dark" : "light"));
++ app.on("will-quit", () => {
++ assert.equal(nativeTheme.listenerCount("updated"), initialListeners);
++ console.log("BROWSER_SYSTEM_APPEARANCE " + JSON.stringify({ ok: true, samples, fixedPalettes: 3, reopened: true, listenerRemoved: true }));
++ });
++ app.quit();
++ } catch (error) { console.error(error.stack); app.exit(1); }
++});
++` },
++ outfile: join(temp, "main.cjs"), bundle: true, platform: "node", format: "cjs", external: ["electron"],
++ });
++ const env = { ...process.env };
++ delete env.ELECTRON_RUN_AS_NODE;
++ for (let restart = 0; restart < 2; restart += 1) {
++ const child = spawn(resolveElectronBinary(root).electronBinary, [join(temp, "main.cjs")], { env, stdio: ["ignore", "pipe", "pipe"] });
++ let output = "";
++ for (const stream of [child.stdout, child.stderr]) stream.on("data", chunk => { output += chunk; });
++ const timer = setTimeout(() => child.kill("SIGKILL"), 60000);
++ let code;
++ try { code = await new Promise((resolve, reject) => { child.once("error", reject); child.once("close", resolve); }); }
++ finally { clearTimeout(timer); }
++ assert.equal(code, 0, output);
++ assert.match(output, /BROWSER_SYSTEM_APPEARANCE .*"ok":true/);
++ console.log(output.trim());
++ }
++} finally {
++ osScript("set dark mode to " + original);
++ await rm(temp, { recursive: true, force: true });
++}