Files
magpie/internal/gui/tests/claude-statusline.test.cjs
T
yetone bc10a61985 agents: Claude Code's status line, the 5h and 7d use, is a switch on its row (steven_4422)
steven_4422 on Discord asked for the 5h and 7d account use in Claude
Code. magpie has had `magpie statusline` since #1997 (kensenzhao), but
it was found only in docs/reference.md and needed a hand-written
settings.json entry naming a `magpie` the app doesn't put on PATH.

Claude Code's row now has a "status line" square beside ultracode (the
same toggle, now a table, TOGGLES in app.js), and `magpie claude
statusline on|off` in the CLI. On writes settings.json's statusLine as
{type: command, command: "<this magpie's full path> statusline"},
quoted for a POSIX shell (Git Bash on Windows, forward slashes).
The command reads the gateway's kept windows with a 1s timeout and
prints nothing when it can't, as before.

- A status line of the user's own is never written over: the square
  isn't offered while one is there, on is refused, and off and
  Disconnect leave it byte for byte. Ours is a single plain command
  whose program is magpie/magpie-* and first argument statusline, so a
  hand-written one from the reference counts and a compound (&&, |, ;,
  $(...)) is the user's.
- An unreadable settings.json is not written over.
- Disconnect takes magpie's status line out with the rest.
- Quiet: Claude Code reruns a changed statusLine command in its open
  sessions, so no restart notice.
- Not offered for a WSL distro, and a distro's settings.json isn't read
  for the options (a stopped one would start, #3896). A stopped
  distro's fields fall back to magpie's models as options, so without a
  case in wsl.go its square would have listed models
  (TestWSLClaudeNoStatusLine).

Run on mote (go1.26.8, load ~400):
- at f33e6d112 (rebased on 3088872e6): go test -count=1
  ./internal/agent ./internal/cli ./internal/gui. Red:
  TestWSLColdPlaceOpensNothing (mine: Options read a distro's
  settings.json; fixed), TestCoreScriptBudget (5 bytes over after the
  rebase; a comment trimmed, no raise), TestEveryRouteHasACommandOrIsNamed
  (#5026, from 0872bc2a0 on main), TestMainWindowCorners/macOS_26_SDK
  (#3949, macOS 27). go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./...: ok.
- GUI at f33e6d112: every agent-*, claude-*, i18n*, field* and tweak*
  file, both engines: 1294 of 1296. agent-stale-copies (webkit ja 980,
  #1949) and claude-subagent-tag (webkit ja 1100, filed #5040) timed out
  under load; at d85b4e49b both files and claude-statusline.test.cjs
  (en, zh, zh-TW, ja, de at 900 and 400 wide) alone: 130 of 130.
- at d85b4e49b (the fixes above): go test ./internal/agent ok;
  TestCoreScriptBudget ok.
- rebased on f37742e14 (a key's spend cap took the core to 21 bytes
  under its budget; this change's 207 bytes are paid for by foldSame's
  comment cut, no raise), at the pushed head: go test ./internal/agent
  ok (318s); TestCoreScriptBudget ok (1,533,597 of 1,533,802);
  claude-statusline, i18n-keys and claude-effort GUI files, both
  engines: 46 of 46.
- gofmt -l clean.

Broken by hand, each red then restored:
- the user's-own guard off: TestClaudeStatusLineLeavesTheUsers
- the Unwire call taken out: TestClaudeStatusLineGoesOnDisconnect
- the file wiped before the write: TestClaudeStatusLineUnreadable
- the compound check off: TestClaudeStatusCommand
- the WSL asleep case off: TestWSLClaudeNoStatusLine
- the here check after the read: TestWSLColdPlaceOpensNothing
- the statusline entry out of TOGGLES: claude-statusline 15/15 red

Not tried: a real Claude Code session showing it (and its hot
reload); Windows Git Bash and PowerShell with a real path; the Linux
AppImage path; WSL is not offered at all.
2026-11-03 16:03:14 +08:00

109 lines
6.2 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// Claude Code's status line (steven_4422 on Discord: the 5h and 7d use in
// Claude Code): a square on Claude Code's row, beside ultracode's, that a
// click turns on (posting statusline on) and off again, lit while on, its
// title saying what it shows and the change said in the status, in every
// language. A row whose settings.json has a status line of the user's own
// gets no options from magpie and shows no square. Chromium and WebKit,
// the window and the narrow tray panel.
const assert = require("node:assert/strict");
const fs = require("node:fs/promises");
const path = require("node:path");
const { test } = require("node:test");
const { chromium, webkit } = require("playwright");
const assets = path.resolve(__dirname, "../assets");
const NOTE = "the 5h and 7d use of the accounts magpie serves, under Claude Code's prompt";
const claude = (id, value, offered) => ({
id, name: "Claude Code " + id, icon: "claudecode-color", path: "/test/" + id, wired: true,
fields: [
{ key: "model", label: "model", value: "claude-opus-5-5", options: [{ value: "claude-opus-5-5", icon: "claude-color", ref: "claude/claude-opus-5-5" }] },
{ key: "statusline", label: "status line", value, options: offered ? [{ value: "on", note: NOTE }] : [] },
],
});
function serve(lang, sets) {
let on = false;
const st = () => ({ agents: [claude("mine", on ? "on" : "", true), claude("theirs", "", false)], profiles: [], settings: { lang, theme: "light" } });
return async (route) => {
const req = route.request(), url = new URL(req.url());
if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` });
if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/state") return route.fulfill({ json: st() });
if (url.pathname === "/api/set") {
const b = req.postDataJSON();
sets.push(b);
if (b.field === "statusline") on = b.value === "on";
return route.fulfill({ json: st() });
}
if (url.pathname === "/api/plugins") return route.fulfill({ json: { plugins: [] } });
if (url.pathname === "/api/usage/quotas") return route.fulfill({ json: [] });
if (url.pathname === "/api/groups") return route.fulfill({ json: { groups: [] } });
if (url.pathname === "/api/providers") return route.fulfill({ json: { providers: [], gateway: { running: true } } });
if (url.pathname.startsWith("/api/")) return route.fulfill({ json: {} });
const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)];
await route.fulfill({ body: await fs.readFile(file), contentType });
};
}
const words = {
en: { label: "status line", note: "5h and 7d use", said: "status line → on" },
zh: { label: "状态栏", note: "5 小时和 7 天用量", said: "状态栏 → 开启" },
"zh-TW": { label: "狀態列", note: "5 小時和 7 天用量", said: "狀態列 → 開啟" },
ja: { label: "ステータスライン", note: "5 時間と 7 日の使用量", said: "ステータスライン → オン" },
de: { label: "Statuszeile", note: "7-Tage-Nutzung", said: "Statuszeile → an" },
};
const row = (id) => `.row.agent[data-id="${id}"]`;
const square = (id) => `${row(id)} .field[data-key="statusline"]`;
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of Object.keys(words)) {
const w = words[lang];
test(`${engine} ${lang}: Claude Code's status line is a switch`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
const errors = [];
t.after(async () => {
if (errors.length) console.log(errors);
await browser.close();
});
for (const mode of ["", "panel"]) {
await t.test(mode || "window", async () => {
const sets = [];
const page = await (await browser.newContext({ viewport: { width: mode ? 400 : 900, height: 560 }, reducedMotion: "reduce" })).newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang, sets));
await page.goto("http://magpie.test/" + (mode ? "?mode=" + mode : ""));
await page.locator(row("mine")).waitFor();
// one row open at a time: the user's first, then magpie's
for (const id of ["theirs", "mine"]) {
await page.locator(`${row(id)} ${mode ? ".ag-sum" : ".ag-link"}`).click();
await page.waitForTimeout(mode ? 700 : 200);
if (id === "theirs") assert.equal(await page.locator(square("theirs")).count(), 0, "a square over the user's own status line");
}
const sq = page.locator(square("mine")).first();
await sq.waitFor();
assert.equal(await sq.getAttribute("aria-pressed"), "false");
const title = await sq.getAttribute("title");
assert.ok(title.includes(w.label) && title.includes(w.note), `title ${JSON.stringify(title)}`);
const box = await sq.boundingBox();
assert.ok(box && box.x >= 0 && box.x + box.width <= (mode ? 400 : 900), "the square is off the page");
await sq.click();
await page.waitForTimeout(300);
assert.deepEqual(sets, [{ agent: "mine", field: "statusline", value: "on" }]);
assert.ok((await page.locator("#status").textContent()).includes(w.said), await page.locator("#status").textContent());
const lit = page.locator(square("mine")).first();
assert.equal(await lit.getAttribute("aria-pressed"), "true");
assert.match(await lit.getAttribute("class"), /\bset\b/);
await lit.click();
await page.waitForTimeout(300);
assert.deepEqual(sets[1], { agent: "mine", field: "statusline", value: "" });
});
}
assert.deepEqual(errors, []);
});
}
}