Files
plugins/packages/agent-console/test/v1/ac11.test.ts
T
qingnian108 86778e6646 agent-console 1.3.7: auto-detect unlabelled code blocks, html-only sanitize, Windows desktop reconnect through Magpie's Try again (#792)
Code blocks are drawn in the same pass as the Markdown, so a streaming reply no longer flickers. Two fixes from the review on 40eb3027:
- An unlabelled fence is detected by highlight.js again (`highlightAuto`, behind a 128-entry LRU). Only a fence that is still open at the end of a streaming message stays plain "text" (`isClosedFence`, which follows marked's fence rule).
- DOMPurify is back to the html profile only. The code-block icons are per-load random placeholders until after sanitize, and `codeBlockKey` is gone.

The Windows desktop reconnect is also fixed. `safeBackUrl` (gate, wrapper and web) takes loopback Magpie pages only, never `http://wails.localhost`. WebView2 can't route a 127.0.0.1 frame's request there, so it showed its gray error page. In the desktop window the wrapper, or an unwrapped daemon page, posts `{magpie:1, method:"error"}` instead, and Magpie's pluginpages.js answers with its Try again card. A stale stored wails back URL is dropped.

Reviewed at c520fa46 (merge base 1eb9473f = main) on mote (mote-run, HOME=$(mktemp -d), package copied out of the workspace):
- npm ci; `npm run check` clean; `npm run verify:build` OK for all 5 files.
- `npx vitest run` with CHROME_PATH set to Playwright's Chromium 1248: 77 files, 567 passed, 17 skipped, 0 failed. markdown-render-browser ran (it went red under the hand-breaks).
- `bun scripts/check.mjs agent-console` ok. Repo `bun test`: 6449 pass, 0 fail.
- Broken by hand, each red:
  - autoDetect forced on while streaming: markdown-render-browser red.
  - `svg: true` put back in the profile: markdown-render-browser red.
  - Auto-detection removed: codeBlock and markdown-render-browser red.
  - LRU eviction removed: codeBlock red.
  - `isClosedFence` accepting any backtick run: codeBlock red.
  - wails.localhost accepted again in gate.mjs, then in backToMagpie.ts: ac11 red (3 and 2 tests).
  - The same in wrapper.mjs: wrapper red (4 tests).
  - The stale stored back kept: wrapper red.
- Read magpie's pluginpages.js on main: `method:"error"` from the page's own frame calls `broke(key, why)`.

Not tried: a real WebView2 on Windows (the reconnect path), or a real streaming reply in the desktop app.
2026-11-06 22:01:43 +08:00

448 lines
25 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 1.1.0 fixes (QA B1–B21) and the self-heal after a plugin host restart.
import { describe, expect, it, beforeEach, afterEach } from "vitest";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import { pathToFileURL } from "node:url";
import { freePort, request } from "./helpers-net.js";
import { launchArgv, launchFromSettings, launchKey } from "../../daemon/src/providers/launch.js";
import { clampMode } from "../../daemon/src/providers/modes.js";
import { parseSettingsPatch, sanitizeStored, SettingsStore } from "../../daemon/src/store/settings.js";
import { mapSessionUpdate, newMapCtx } from "../../daemon/src/providers/acp/mapUpdate.js";
import { parseCodexConfig } from "../../daemon/src/providers/codex/provider.js";
import { mapNotification, newCodexMapState } from "../../daemon/src/providers/codex/map.js";
import { costIsReal } from "../../daemon/src/providers/claude/map.js";
import { setAppLocale as setLocale, tr } from "../../daemon/src/util/i18n.js";
import { safeBackUrl as webSafeBackUrl, backUrlFromHash, backTarget, watchReconnect, askMagpieRetry } from "../../web/src/api/backToMagpie.js";
import { resolveTheme, normalizeThemeSetting } from "../../web/src/layout/theme.js";
import { parseArgsDraft, parseIdleDraft } from "../../web/src/components/settingsFields.js";
const gateUrl = pathToFileURL(join(resolve(__dirname, "..", ".."), "lib", "gate.mjs")).href;
describe("launch command (B3/B5)", () => {
it("appends the user's args after the built-in ones; prefixArgs go first", () => {
expect(launchArgv(["acp"], { cmd: "opencode", args: ["--foo"] })).toEqual(["acp", "--foo"]);
expect(launchArgv(["app-server"], { cmd: "node", prefixArgs: ["fx.mjs"], args: ["-v"] })).toEqual(["fx.mjs", "app-server", "-v"]);
expect(launchArgv(["x"])).toEqual(["x"]);
});
it("reads the settings and ignores an empty executable", () => {
const s = { providerCommand: { codex: { executable: " /opt/codex ", args: ["a", 3 as unknown as string] }, claude: { executable: " " } } };
expect(launchFromSettings(s as never, "codex")).toEqual({ cmd: "/opt/codex", args: ["a"] });
expect(launchFromSettings(s as never, "claude")).toBeUndefined();
expect(launchFromSettings({} as never, "codex")).toBeUndefined();
});
it("cache key changes with the command", () => {
expect(launchKey()).toBe("");
expect(launchKey({ cmd: "a", args: ["1"] })).not.toBe(launchKey({ cmd: "a", args: ["2"] }));
});
});
describe("clampMode (B8/B9)", () => {
it("keeps a supported mode, else default, else the first", () => {
expect(clampMode(["default", "acceptEdits", "bypassPermissions"], "acceptEdits")).toBe("acceptEdits");
expect(clampMode(["default", "acceptEdits", "bypassPermissions"], "plan")).toBe("default");
expect(clampMode(["plan", "build"] as never, "acceptEdits")).toBe("plan");
expect(clampMode(["default"], undefined)).toBe("default");
});
});
describe("settings schema (B18, B6)", () => {
it("accepts known fields and rejects bad ones", () => {
expect(parseSettingsPatch({ theme: "dark", idleCloseMinutes: 45 }).ok).toBe(true);
expect(parseSettingsPatch({ theme: "purple" }).ok).toBe(false);
expect(parseSettingsPatch({ idleCloseMinutes: 2 }).ok).toBe(false);
expect(parseSettingsPatch({ idleCloseMinutes: 241 }).ok).toBe(false);
expect(parseSettingsPatch({ idleCloseMinutes: 7.5 }).ok).toBe(false);
expect(parseSettingsPatch({ nope: 1 }).ok).toBe(false);
expect(parseSettingsPatch({ providerCommand: { opencode: { executable: "/x", args: ["--a"] } } }).ok).toBe(true);
expect(parseSettingsPatch({ providerCommand: { opencode: { args: "--a" } } }).ok).toBe(false);
expect(parseSettingsPatch({ providerEnabled: { codex: "yes" } }).ok).toBe(false);
});
it("drops wrongly typed stored values and clamps idle minutes", () => {
const s = sanitizeStored({ theme: 3, showThinking: "no", idleCloseMinutes: 1, sendKey: "enter" });
expect(s.theme).toBeUndefined();
expect(s.showThinking).toBeUndefined();
expect(s.sendKey).toBe("enter");
expect(s.idleCloseMinutes).toBe(5);
expect(sanitizeStored({ idleCloseMinutes: 9999 }).idleCloseMinutes).toBe(240);
});
it("store: removed settings are gone from the defaults", async () => {
const dir = mkdtempSync(join(tmpdir(), "ac11-"));
try {
const st = new SettingsStore(dir);
const s = (await st.get()) as Record<string, unknown>;
expect("defaultEffort" in s).toBe(false);
expect("density" in s).toBe(false);
expect("idleExitMinutes" in s).toBe(false);
} finally { rmSync(dir, { recursive: true, force: true }); }
});
});
describe("ACP text around tool calls (B1)", () => {
it("text before and after a tool call are separate items", () => {
const ctx = newMapCtx();
const a = mapSessionUpdate({ sessionUpdate: "agent_message_chunk", content: { type: "text", text: "before" } } as never, ctx);
mapSessionUpdate({ sessionUpdate: "tool_call", toolCallId: "t1", title: "ls", kind: "execute", status: "pending" } as never, ctx);
mapSessionUpdate({ sessionUpdate: "tool_call_update", toolCallId: "t1", status: "completed" } as never, ctx);
const b = mapSessionUpdate({ sessionUpdate: "agent_message_chunk", content: { type: "text", text: "after" } } as never, ctx);
const ida = (a[0] as { itemId: string }).itemId;
const idb = (b[0] as { itemId: string }).itemId;
expect(ida).not.toBe(idb);
// consecutive chunks stay in one item
const c = mapSessionUpdate({ sessionUpdate: "agent_message_chunk", content: { type: "text", text: "!" } } as never, ctx);
expect((c[0] as { itemId: string }).itemId).toBe(idb);
});
});
describe("Codex (B10, B11, B15)", () => {
it("reads model_provider/model and the provider's models, profile first", () => {
const cfg = parseCodexConfig([
'model = "gpt-5"', 'model_provider = "openai"', 'profile = "ds"',
"[profiles.ds]", 'model = "deepseek-chat"', 'model_provider = "magpie"',
"[model_providers.magpie]", 'models = ["deepseek-chat", "deepseek-reasoner"]',
"[model_providers.other]", 'models = ["nope"]',
].join("\n"));
expect(cfg.modelProvider).toBe("magpie");
expect(cfg.model).toBe("deepseek-chat");
expect(cfg.providerModels).toEqual(["deepseek-chat", "deepseek-reasoner"]);
});
it("saves raw reasoning when there is no summary", () => {
const st = newCodexMapState();
mapNotification("item/reasoning/textDelta", { itemId: "r1", delta: "think " }, st);
mapNotification("item/reasoning/textDelta", { itemId: "r1", delta: "hard" }, st);
const evs = mapNotification("item/completed", { item: { type: "reasoning", id: "r1", summary: [], content: [] } }, st);
expect(evs).toContainEqual({ type: "reasoning", itemId: "r1", text: "think hard" });
});
it("remembers a fileChange item's changes for the approval card", () => {
const st = newCodexMapState();
const changes = [{ path: "a.txt", kind: { type: "add" }, diff: "hi" }];
mapNotification("item/started", { item: { type: "fileChange", id: "f1", changes } }, st);
expect(st.fileChangesByItemId.get("f1")).toEqual(changes);
});
});
describe("Claude cost (B14)", () => {
it("keeps Claude Code's cost only for Claude models", () => {
expect(costIsReal({ "claude-sonnet-4-5": {} })).toBe(true);
expect(costIsReal({ "deepseek-chat": {} })).toBe(false);
expect(costIsReal({ "claude-haiku": {}, "deepseek-chat": {} })).toBe(false);
expect(costIsReal(undefined)).toBe(true);
});
});
describe("daemon i18n (B13)", () => {
afterEach(() => setLocale("zh"));
it("permission options follow the UI language", () => {
setLocale("en");
expect(tr("perm.allow")).toBe("Allow");
expect(tr("http.tooLarge")).toContain("50 MB");
setLocale("zh");
expect(tr("perm.allow")).toBe("允许");
});
});
describe("self-heal back URL (host restart)", () => {
const good = "http://127.0.0.1:3430/_magpie/plugin/6434536d5169e4148a4e62c188afe049/index.html";
const cases: [string, string | null][] = [
[good, good],
[good + "#frag", good],
["http://localhost:3425/_magpie/plugin/abc/page/index.html?x=1", "http://localhost:3425/_magpie/plugin/abc/page/index.html?x=1"],
["http://[::1]:3430/_magpie/plugin/abc/index.html", "http://[::1]:3430/_magpie/plugin/abc/index.html"],
// Magpie's desktop window is never a back target (Windows WebView2 can't navigate a 127.0.0.1 frame there: gray error page)
["http://wails.localhost/_magpie/plugin/abc/index.html", null],
["http://wails.localhost/_magpie/plugin/abc/page/index.html", null],
["https://wails.localhost/_magpie/plugin/abc/page/index.html?x=1", null],
["http://wails.localhost:8080/_magpie/plugin/a/index.html", null],
["https://localhost:4430/_magpie/plugin/a/index.html", "https://localhost:4430/_magpie/plugin/a/index.html"],
["http://wails.localhost/_magpie/plugin/abc/evil.html", null],
["http://evil-wails.localhost/_magpie/plugin/abc/index.html", null],
["http://wails.localhost.evil.com/_magpie/plugin/abc/index.html", null],
["http://sub.wails.localhost/_magpie/plugin/abc/index.html", null],
["https://evil.example/_magpie/plugin/abc/index.html", null],
["http://127.0.0.1.evil.example/_magpie/plugin/abc/index.html", null],
["http://user:pw@127.0.0.1:3430/_magpie/plugin/abc/index.html", null],
["http://user:pw@wails.localhost/_magpie/plugin/abc/index.html", null],
["javascript:alert(1)//127.0.0.1/_magpie/plugin/a/index.html", null],
["file:///_magpie/plugin/abc/index.html", null],
["wails://localhost/_magpie/plugin/abc/index.html", null], // macOS/Linux: no http nav from gate; use Magpie error postMessage
["http://127.0.0.1:3430/other/index.html", null],
["http://127.0.0.1:3430/_magpie/plugin/../../x/index.html", null],
["http://127.0.0.1:3430/_magpie/plugin/abc/evil.html", null],
["", null],
];
it("web and gate accept only Magpie plugin pages on loopback", async () => {
const { safeBackUrl } = await import(/* @vite-ignore */ gateUrl);
for (const [input, want] of cases) {
expect(webSafeBackUrl(input), input).toBe(want);
expect(safeBackUrl(input), input).toBe(want);
}
});
it("rejects the Windows desktop host wails.localhost on every platform (1.3.5 / 1.3.6 took it on Windows)", async () => {
const { safeBackUrl } = await import(/* @vite-ignore */ gateUrl);
const win = "http://wails.localhost/_magpie/plugin/a/page/index.html";
// an extra platform / UA argument (the 1.3.5 signature) changes nothing
for (const os of ["win32", "linux", "darwin"]) expect((safeBackUrl as (r: string, p: string) => unknown)(win, os)).toBeNull();
for (const ua of ["Mozilla/5.0 (Windows NT 10.0; Win64; x64)", "Macintosh", "X11; Linux x86_64", ""]) {
expect((webSafeBackUrl as (r: string, u: string) => unknown)(win, ua)).toBeNull();
}
expect(safeBackUrl(win)).toBeNull();
expect(webSafeBackUrl(win)).toBeNull();
expect(backUrlFromHash(`#t=abc&b=${encodeURIComponent(win)}`)).toBeNull();
expect(backTarget(`#t=x&b=${encodeURIComponent(win)}`, 1234)).toBeNull();
});
it("unwrapped daemon page without a back URL asks a framing Magpie for Try again, and does nothing top-level", () => {
const g = globalThis as unknown as { window?: unknown };
const had = "window" in g, prev = g.window;
try {
const posted: unknown[] = [];
const parent = { postMessage: (d: unknown) => posted.push(d) };
g.window = { parent };
expect(askMagpieRetry("本地服务已断开")).toBe(true);
expect(posted).toEqual([{ magpie: 1, method: "error", params: { message: "本地服务已断开" } }]);
const top: Record<string, unknown> = {};
top.parent = top;
g.window = top;
expect(askMagpieRetry("x")).toBe(false);
delete g.window;
expect(askMagpieRetry("x")).toBe(false);
} finally {
if (had) g.window = prev; else delete g.window;
}
});
it("reads b= from the fragment next to the token", () => {
expect(backUrlFromHash(`#t=abc&b=${encodeURIComponent(good)}`)).toBe(good);
expect(backUrlFromHash("#t=abc")).toBeNull();
expect(backUrlFromHash(`#t=abc&b=${encodeURIComponent("https://evil.example/")}`)).toBeNull();
});
it("going back carries the moment the connection dropped", () => {
expect(backTarget(`#t=x&b=${encodeURIComponent(good)}`, 1234)).toBe(good + "?ac_lost=1234");
expect(backTarget(`#t=x&b=${encodeURIComponent(good)}`, 0)).toBe(good);
expect(backTarget("#t=x", 1234)).toBeNull();
});
it("gate forwards a valid back URL only in the fragment", async () => {
const { createGate } = await import(/* @vite-ignore */ gateUrl);
const port = await freePort();
const daemonPort = await freePort();
const key = "c".repeat(64);
const token = "a".repeat(64);
const gate = createGate({ port, key, token, ensureDaemon: async () => ({ port: daemonPort }) });
await gate.listen();
try {
const ok = await request(port, `/open?k=${key}&theme=dark&back=${encodeURIComponent(good)}`);
expect(ok.status).toBe(302);
expect(ok.headers.location).toBe(`http://127.0.0.1:${port}/app?theme=dark#t=${token}&p=${daemonPort}&b=${encodeURIComponent(good)}`);
const bad = await request(port, `/open?k=${key}&back=${encodeURIComponent("https://evil.example/")}`);
expect(bad.headers.location).toBe(`http://127.0.0.1:${port}/app?#t=${token}&p=${daemonPort}`);
// without the key there's still nothing
expect((await request(port, `/open?back=${encodeURIComponent(good)}`)).status).toBe(403);
const wails = "http://wails.localhost/_magpie/plugin/tok123/index.html";
const win = await request(port, `/open?k=${key}&back=${encodeURIComponent(wails)}`);
expect(win.status).toBe(302);
// never forwarded, on any OS: the wrapper asks Magpie for its Try again card instead
expect(win.headers.location).toBe(`http://127.0.0.1:${port}/app?#t=${token}&p=${daemonPort}`);
const lookalike = await request(port, `/open?k=${key}&back=${encodeURIComponent("http://evil-wails.localhost/_magpie/plugin/tok/index.html")}`);
expect(lookalike.headers.location).toBe(`http://127.0.0.1:${port}/app?#t=${token}&p=${daemonPort}`);
} finally {
await gate.close();
}
});
it("goes back after the connection stays down, not on a short blip", async () => {
let listener: (s: string) => void = () => {};
let state = "open";
const fake = { onState: (fn: (s: string) => void) => { listener = fn; return () => {}; }, getState: () => state };
let stuck = 0;
watchReconnect(fake, () => stuck++, 40);
state = "reconnecting"; listener(state);
await new Promise((r) => setTimeout(r, 15));
state = "open"; listener(state);
await new Promise((r) => setTimeout(r, 60));
expect(stuck).toBe(0);
state = "reconnecting"; listener(state);
listener("reconnecting");
await new Promise((r) => setTimeout(r, 70));
expect(stuck).toBe(1);
});
});
describe("theme (B4)", () => {
it("explicit light/dark wins over the host; auto follows the host, then the OS", () => {
expect(resolveTheme("light", "dark", "dark")).toEqual({ theme: "light", hostColors: false });
expect(resolveTheme("dark", "dark", "light")).toEqual({ theme: "dark", hostColors: true });
expect(resolveTheme("auto", "dark", "light")).toEqual({ theme: "dark", hostColors: true });
expect(resolveTheme("auto", null, "dark")).toEqual({ theme: "dark", hostColors: false });
expect(normalizeThemeSetting("bogus")).toBe("auto");
});
});
describe("settings fields (B5, B6)", () => {
it("args: JSON string array or empty", () => {
expect(parseArgsDraft("")).toBeUndefined();
expect(parseArgsDraft(' ["--foo", "bar"] ')).toEqual(["--foo", "bar"]);
expect(parseArgsDraft("[")).toBeNull();
expect(parseArgsDraft("[1]")).toBeNull();
expect(parseArgsDraft('"--foo"')).toBeNull();
});
it("idle minutes: whole number 5–240", () => {
expect(parseIdleDraft("1")).toBeNull();
expect(parseIdleDraft("5")).toBe(5);
expect(parseIdleDraft("240")).toBe(240);
expect(parseIdleDraft("241")).toBeNull();
expect(parseIdleDraft("12.5")).toBeNull();
expect(parseIdleDraft("")).toBeNull();
});
});
describe("boot.js (self-heal)", () => {
async function runBoot(sessions: unknown[], href = "http://127.0.0.1:3430/_magpie/plugin/abc/index.html"): Promise<string | null> {
const { readFileSync } = await import("node:fs");
const vm = await import("node:vm");
const code = readFileSync(join(resolve(__dirname, "..", ".."), "page", "boot.js"), "utf8");
let replaced: string | null = null;
let n = 0;
const ctx: Record<string, unknown> = {
setTimeout: (fn: () => void) => setImmediate(fn),
location: { href, replace: (u: string) => { replaced = u; } },
document: {
getElementById: () => null, querySelector: () => null,
createElement: () => ({ remove() {} }),
head: { appendChild(s: { onload?: () => void }) { ctx.AgentConsoleSession = sessions[Math.min(n++, sessions.length - 1)]; setImmediate(() => s.onload?.()); } },
},
URLSearchParams, URL, Date, Object, String, JSON, Math, Number, encodeURIComponent, unescape, btoa,
};
ctx.globalThis = ctx;
ctx.window = ctx;
vm.runInNewContext(code, ctx);
for (let i = 0; i < 200 && !replaced; i++) await new Promise((r) => setImmediate(r));
return replaced;
}
const key1 = "1".repeat(64), key2 = "2".repeat(64);
it("skips a killed host's stale session.js and opens the new gate, passing its own page as back", async () => {
const url = await runBoot([
{ v: 2, port: 3488, key: key1, ts: Date.now() - 60_000, beat: true },
{ v: 2, port: 3488, key: key1, ts: Date.now() - 60_000, beat: true },
{ v: 2, port: 3488, key: key2, ts: Date.now(), beat: false },
]);
expect(url).toBeTruthy();
const u = new URL(url!);
expect(u.searchParams.get("k")).toBe(key2);
expect(u.searchParams.get("back")).toBe("http://127.0.0.1:3430/_magpie/plugin/abc/index.html");
});
it("after a lost connection, ignores a session.js refreshed before it (the killed host's)", async () => {
const lost = Date.now() - 3000;
const url = await runBoot([
{ v: 2, port: 3488, key: key1, ts: lost - 1000 },
{ v: 2, port: 3488, key: key2, ts: Date.now() },
], `http://127.0.0.1:3430/_magpie/plugin/abc/index.html?ac_lost=${lost}`);
const u = new URL(url!);
expect(u.searchParams.get("k")).toBe(key2);
// ac_lost isn't passed on, so it can't pile up
expect(u.searchParams.get("back")).toBe("http://127.0.0.1:3430/_magpie/plugin/abc/index.html");
});
it("accepts an old session.js that never beat (page never opened) on a normal load", async () => {
const url = await runBoot([{ v: 2, port: 3488, key: key1, ts: Date.now() - 3_600_000, beat: false }]);
expect(new URL(url!).searchParams.get("k")).toBe(key1);
});
it("after a lost connection, waits for a beat newer than the loss (gate still up, daemon crashed)", async () => {
const lost = Date.now() - 2000;
const url = await runBoot([
{ v: 2, port: 3488, key: key1, ts: lost - 3000, beat: true },
{ v: 2, port: 3488, key: key1, ts: lost - 3000, beat: true },
{ v: 2, port: 3488, key: key1, ts: Date.now(), beat: true },
], `http://127.0.0.1:3430/_magpie/plugin/abc/index.html?ac_lost=${lost}`);
expect(new URL(url!).searchParams.get("k")).toBe(key1);
});
it("after a lost connection, a new host's first (non-beating) file written after the loss is used", async () => {
const lost = Date.now() - 4000;
const url = await runBoot([
{ v: 2, port: 3488, key: key1, ts: lost - 1000, beat: true },
{ v: 2, port: 3488, key: key2, ts: lost + 2500, beat: false },
], `http://127.0.0.1:3430/_magpie/plugin/abc/index.html?ac_lost=${lost}`);
expect(new URL(url!).searchParams.get("k")).toBe(key2);
});
it("still accepts a session.js without ts (older host)", async () => {
const url = await runBoot([{ v: 2, port: 3488, key: key1 }]);
expect(new URL(url!).searchParams.get("k")).toBe(key1);
});
});
describe("turn end duration fallback (ACP agents report none)", () => {
it("uses turn_started → turn_ended timestamps when durationMs is missing", async () => {
const { newTimelineState, applyEvent } = await import("../../web/src/timeline/reducer.js");
const st = newTimelineState();
applyEvent(st, { seq: 1, ts: 1000, threadId: "t", turnId: "u", type: "turn_started", model: null, mode: "default", effort: null } as never);
applyEvent(st, { seq: 2, ts: 5200, threadId: "t", turnId: "u", type: "turn_ended", status: "completed" } as never);
const te = st.items.find((i) => i.kind === "turnend") as { durationMs?: number };
expect(te.durationMs).toBe(4200);
});
});
describe("daemon log: a closed console pipe (host killed hard) is not a crash", () => {
it("swallows EPIPE on stdout and stops writing to the console", async () => {
const { log, consoleClosed } = await import("../../daemon/src/log.js");
log("info", "before");
expect(consoleClosed()).toBe(false);
const err = Object.assign(new Error("write EPIPE"), { code: "EPIPE" });
// Without a listener this emit would throw (and in the daemon become an uncaughtException)
expect(() => process.stdout.emit("error", err)).not.toThrow();
expect(consoleClosed()).toBe(true);
expect(() => log("info", "after")).not.toThrow();
});
});
describe("monochrome accent: Magpie's accent is ignored, neutrals still follow Magpie", () => {
it("drops accent tokens (named or bare) but passes every other token through", async () => {
const { applyMagpieTokensFromSearch, isAccentToken } = await import("../../web/src/layout/applyMagpieTokens.js");
const g = globalThis as Record<string, unknown>;
const saved = g.document;
const set = new Map<string, string>();
g.document = { documentElement: { style: { setProperty: (k: string, v: string) => set.set(k, v) } } };
try {
const enc = (o: object) => "?tokens=" + Buffer.from(JSON.stringify(o)).toString("base64");
applyMagpieTokensFromSearch(enc({ accent: "#4f46e5", "accent-soft": "#eeedfd", "accent-fg": "#ffffff", bg: "#f4f4f6", fg: "#1c1c21", line: "#e3e3e8", red: "#d64545", font: "system-ui" }));
expect(set.has("--magpie-accent")).toBe(false);
expect(set.has("--magpie-accent-soft")).toBe(false);
expect(set.has("--magpie-accent-fg")).toBe(false);
expect(set.get("--magpie-bg")).toBe("#f4f4f6");
expect(set.get("--magpie-fg")).toBe("#1c1c21");
expect(set.get("--magpie-line")).toBe("#e3e3e8");
expect(set.get("--magpie-red")).toBe("#d64545");
expect(set.get("--magpie-font")).toBe("system-ui");
applyMagpieTokensFromSearch(enc({ "--accent": "#ff0000", "--accent-2": "#ff0000", "magpie-accent": "#ff0000", "--magpie-accent-fg": "#000" }));
expect([...set.keys()].some((k) => /accent/i.test(k))).toBe(false);
expect(isAccentToken("accent")).toBe(true);
expect(isAccentToken("--accent-soft")).toBe(true);
expect(isAccentToken("accented")).toBe(false);
expect(isAccentToken("bg")).toBe(false);
} finally {
g.document = saved;
}
});
it("tokens.css: neutral accent, no teal, no --magpie-accent anywhere in web/src styles", async () => {
const { readFileSync, readdirSync } = await import("node:fs");
const stylesDir = join(resolve(__dirname, "..", ".."), "web", "src", "styles");
const css = readFileSync(join(stylesDir, "tokens.css"), "utf8");
const decl = (block: string, v: string) => new RegExp(`${v}:\\s*([^;]+);`).exec(block)?.[1]?.trim() ?? "";
const light = css.slice(css.indexOf(":root {"), css.indexOf(":root[data-theme=\"dark\"]"));
const dark = css.slice(css.indexOf(":root[data-theme=\"dark\"]"), css.indexOf(":root[data-host-colors]"));
const isGrey = (c: string) => {
const m = /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i.exec(c);
return !!m && m[1] === m[2] && m[2] === m[3];
};
for (const block of [light, dark]) {
for (const v of ["--accent", "--accent-2", "--accent-fg", "--accent-soft", "--run-bg", "--run-fg", "--cp-focus"]) {
expect(isGrey(decl(block, v)), `${v} = ${decl(block, v)}`).toBe(true);
}
}
// light: charcoal accent with white text; dark: light accent with dark text
expect(parseInt(decl(light, "--accent").slice(1, 3), 16)).toBeLessThan(0x50);
expect(decl(light, "--accent-fg").toLowerCase()).toBe("#ffffff");
expect(parseInt(decl(dark, "--accent").slice(1, 3), 16)).toBeGreaterThan(0xc0);
expect(parseInt(decl(dark, "--accent-fg").slice(1, 3), 16)).toBeLessThan(0x40);
for (const f of readdirSync(stylesDir)) {
const src = readFileSync(join(stylesDir, f), "utf8");
expect(src, f).not.toMatch(/#(0f766e|14b8a6|2dd4bf|0d9488|115e59|5eead4|99f6e4|ccfbf1|04201c)\b/i);
expect(src, f).not.toMatch(/--magpie-accent/);
}
});
});