Files
magpie/internal/gui/tests/claude-mode.test.cjs
T
yetone 546cdbde6b gui tests: claude-mode moves the pointer off the page once the editor is opened (#4638)
The "both modes on hover" tests found the hover card up before their
hover in WebKit. The click that opens the editor leaves the pointer on
the provider row, at (450,110) at 900px and (210,110) at 420px, and the
editor draws its TUI / claude -p switch there: a probe printed the
switch at [306,96,515,121] (en 900) and [171,78,378,126] (en 420), and
the card up in those runs. WebKit sends mouseenter for a pointer that
never moved once something is drawn under it. openEditor now moves the
pointer to (0,0) right after the click, before the editor is read.

Run in WSL (magpie-desktop, Playwright 1.62.0) at origin/main 276c806db:
- before (main): the webkit hover tests red in each run, 1 to 3 of 10
  (ja, de, en, at 900 and 420; which ones changes from run to run);
- after: BROWSER=webkit, the hover tests 3 times, 10/10 each; the whole
  file in chromium and webkit 44/44.

Not tried: mote (its disk is full); the rest of the GUI suite (no other
file uses this openEditor).

Fixes #4638
2026-11-01 22:27:23 +08:00

216 lines
14 KiB
JavaScript
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.
// Run with Node's test runner and Playwright on the module path; see README.md.
// A Claude subscription's editor says whether its requests run Claude
// Code's interactive mode (TUI) or claude -p, switches between them right
// there (the owner, 11-01), and hovering it shows what each is and what
// each is good for (the owner, 10-31). Here:
// - on, its last request interactive: TUI pressed, the last request said;
// - hovering or focusing the switch shows both modes, the one chosen
// marked, each with its advantage, inside the window, and says nothing
// ever runs as -p with the interactive mode on; moving away hides it;
// - turned off: claude -p pressed, every request said to run as it;
// pressing TUI saves claudePrintMode false with the rest kept, reads the
// mode again and draws TUI pressed; pressing claude -p turns it back;
// - on where it can't run (Claude Code in WSL whose home wasn't found):
// TUI pressed, and Claude requests are said to fail, and why;
// - another subscription (Codex), and Only switch, which serves nothing,
// have no such row, and /api/claude/mode isn't read for them.
// The mode is read from /api/claude/mode as the editor opens, and again
// after a switch.
// Chromium and WebKit, 900 and 420 wide, in every language, the API faked.
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 ago = (min) => new Date(Date.now() - min * 60e3).toISOString();
const USER = "me@example.invalid";
const claude = (switchOnly) => ({ id: "claude", name: "Claude Code", icon: "claude", models: [{ id: "claude-opus-5-5", on: true }], agents: [], key: {}, switchOnly,
account: { agent: "claude", agentName: "Claude Code", agentIcon: "claude", user: USER, logins: [{ user: USER, plan: "max", active: true, on: true }] } });
const codex = { id: "codex", name: "Codex", icon: "codex", models: [{ id: "gpt-6", on: true }], agents: [], key: {},
account: { agent: "codex", agentName: "Codex", agentIcon: "codex", user: USER, logins: [{ user: USER, plan: "plus", active: true, on: true }] } };
const RUNS = {
tui: { tui: true, last: { tui: true, at: ago(3) } },
off: { tui: false, why: "off", last: { tui: false, why: "off", at: ago(3) } },
wsl: { tui: false, why: "wsl-home", wsl: true },
};
// each language's words, as the i18n table has them
const W = {
en: { row: "Claude Code runs as", tui: "Interactive mode (TUI)", inUse: "In use", lastTui: "interactive mode", allP: "Every request runs as claude -p", wsl: "WSL" },
zh: { row: "Claude Code 运行方式", tui: "交互模式(TUI)", inUse: "使用中", lastTui: "交互模式", allP: "每个请求都以 claude -p 运行", wsl: "WSL" },
"zh-TW": { row: "Claude Code 執行方式", tui: "互動模式(TUI)", inUse: "使用中", lastTui: "互動模式", allP: "每個請求都以 claude -p 執行", wsl: "WSL" },
ja: { row: "Claude Code の実行方式", tui: "対話モード(TUI)", inUse: "使用中", lastTui: "対話モード", allP: "すべてのリクエストを claude -p で実行します", wsl: "WSL" },
de: { row: "Claude Code läuft als", tui: "Interaktiver Modus (TUI)", inUse: "In Verwendung", lastTui: "interaktiver Modus", allP: "Jede Anfrage läuft als claude -p", wsl: "WSL" },
};
function serve(lang, run, switchOnly, reads) {
const settings = { lang, theme: "light", redact: true, claudePrintMode: run.why === "off" };
return async (route) => {
const url = new URL(route.request().url());
const json = (data) => route.fulfill({ json: data });
if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:false};` });
if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/state") return json({ agents: [], profiles: [], settings });
if (url.pathname === "/api/settings") {
if (route.request().method() === "POST") {
reads.saved.push(JSON.parse(route.request().postData()));
Object.assign(settings, reads.saved.at(-1));
}
return json(settings);
}
if (url.pathname === "/api/providers") return json({ providers: [claude(switchOnly), codex], presets: [], excluded: [], gateway: { running: true, window: true }, plugins: [] });
// the mode as the setting has it now: turned on, the machine's own
if (url.pathname === "/api/claude/mode") {
reads.n++;
if (settings.claudePrintMode) return json(RUNS.off);
return json(run.why === "off" ? { tui: true } : run);
}
if (url.pathname === "/api/groups") return json({ groups: [], pools: [] });
if (url.pathname.startsWith("/api/")) return json({});
if (!/^\/[\w./-]*$/.test(url.pathname) || url.host !== "magpie.test") return route.fulfill({ status: 404, body: "" });
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)];
try { await route.fulfill({ body: await fs.readFile(file), contentType }); } catch { await route.fulfill({ status: 404, body: "" }); }
};
}
async function openEditor(browser, lang, run, { id = "claude", width = 900, switchOnly = false } = {}) {
const page = await (await browser.newContext({ viewport: { width, height: 760 } })).newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [], reads = { n: 0, saved: [] };
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang, run, switchOnly, reads));
await page.goto("http://magpie.test/?view=providers");
// the first draw waits on the page's first reads: a load's room, not a click's (#4470)
const row = page.locator(`.row.provider[data-id="${id}"]`);
await row.waitFor({ timeout: 60000 });
await row.click();
// the click's pointer stays where the editor draws its switch, and
// WebKit opens the hover card for a pointer that never moved (#4638):
// the pointer leaves the page before the editor is read
await page.mouse.move(0, 0);
await page.locator("#modal .ebody").waitFor();
return { page, errors, reads };
}
// the row's label and value, in a slot that lays out as the editor's rows
const runRow = (page) => page.locator("#modal .claude-mode", { has: page.locator(".cc-run") });
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const width of [900, 420]) for (const lang of Object.keys(W)) {
const w = W[lang];
test(`${engine} ${width} ${lang}: the interactive mode in use, its last request said, both modes on hover`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const { page, errors, reads } = await openEditor(browser, lang, RUNS.tui, { width });
const row = runRow(page);
await row.waitFor();
const tr = (k, v) => page.evaluate(([k, v]) => t(k, v), [k, v]);
assert.equal((await row.locator(":scope > label").textContent()).trim(), w.row);
assert(await row.locator(":scope > label").evaluate((l) => l.compareDocumentPosition(document.querySelector("#modal .accts")) & Node.DOCUMENT_POSITION_FOLLOWING), "the row is above the accounts");
const pick = row.locator(".cc-pick");
assert.deepEqual(await pick.locator(".opt").evaluateAll((bs) => bs.map((b) => [b.textContent.trim(), b.getAttribute("aria-pressed"), b.disabled])),
[[w.tui, "true", false], ["claude -p", "false", false]], "TUI pressed, both can be pressed");
assert(await pick.evaluate((e) => { const r = e.getBoundingClientRect(); return r.width > 0 && r.right <= innerWidth + 1; }), `the switch fits at ${width}`);
assert.match(await row.locator(".cc-said").textContent(), new RegExp(w.lastTui.replace(/[()]/g, "\\$&") + "$"));
assert.equal(await page.locator(".cc-tip").count(), 0);
await pick.hover();
const tip = page.locator(".cc-tip");
await tip.waitFor();
const modes = await tip.locator(".cc-m").evaluateAll((ms) => ms.map((m) => ({ on: m.classList.contains("on"), name: m.querySelector(".h b").textContent, now: m.querySelector(".now")?.textContent || "", good: m.querySelectorAll("p.good").length, bad: m.querySelectorAll("p.bad").length })));
assert.deepEqual(modes, [
{ on: true, name: w.tui, now: w.inUse, good: 1, bad: 0 },
{ on: false, name: "claude -p", now: "", good: 1, bad: 1 },
]);
// nothing in the card says a request falls back to -p (the owner, 11-01)
assert.equal((await tip.locator(".foot").textContent()).trim(), await tr("Switched here or in Settings › Models › Replies. With the interactive mode on, nothing ever runs as -p: a request it can't take, or one it fails to start for, fails saying why."));
assert.equal((await tip.locator(".cc-m").nth(1).locator("p.good").textContent()).trim(), await tr("It starts at once, and takes what the interactive mode can't yet: a JSON schema."));
const box = await tip.boundingBox();
assert(box.x >= 0 && box.y >= 0 && box.x + box.width <= width && box.y + box.height <= 760, `the card is inside the window: ${JSON.stringify(box)}`);
if (lang !== "en") {
// every line of the card is in the reader's language
const text = await tip.textContent();
assert(!/magpie opens Claude Code as a person does|Your plan counts|It starts at once|Switched here/.test(text), `English left in the card: ${text}`);
}
await page.mouse.move(5, 5);
await tip.waitFor({ state: "detached" });
// the keyboard reaches it too
await pick.locator(".opt").first().focus();
await tip.waitFor();
assert.equal(reads.n, 1, "the mode is read once, not on each draw");
assert.deepEqual(errors, []);
});
if (width !== 900) continue;
test(`${engine} ${lang}: turned off, it says every request is -p, and switches to TUI and back right there`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const { page, errors, reads } = await openEditor(browser, lang, RUNS.off);
const row = runRow(page);
await row.waitFor();
const opt = (i) => row.locator(".cc-pick .opt").nth(i);
const pressed = () => row.locator(".cc-pick .opt").evaluateAll((bs) => bs.map((b) => b.getAttribute("aria-pressed")));
assert.deepEqual(await pressed(), ["false", "true"], "claude -p pressed");
assert.equal((await row.locator(".cc-said").textContent()).trim(), w.allP);
await opt(1).hover();
await page.locator(".cc-tip").waitFor();
assert.deepEqual(await page.locator(".cc-tip .cc-m").evaluateAll((ms) => ms.map((m) => m.classList.contains("on"))), [false, true], "-p is the one in use");
await opt(0).click();
for (let i = 0; i < 100 && reads.n < 2; i++) await page.waitForTimeout(50);
await page.waitForFunction(() => document.querySelector("#modal .cc-pick .opt")?.getAttribute("aria-pressed") === "true");
assert.equal(reads.saved.length, 1, "saved once");
assert.equal(reads.saved[0].claudePrintMode, false);
assert.equal(reads.saved[0].redact, true, "the rest of the settings kept");
assert(!("claudeTUIOnly" in reads.saved[0]), "the retired setting isn't sent");
assert.equal(reads.n, 2, "the mode is read again after the switch");
assert.equal(await row.locator(".cc-said").count(), 0, "nothing to say before a request has run");
assert.equal(await page.locator("#modal").count(), 1, "the editor stays open");
await opt(1).click();
for (let i = 0; i < 100 && reads.n < 3; i++) await page.waitForTimeout(50);
assert.equal(reads.saved[1]?.claudePrintMode, true, "and back to -p");
await page.waitForFunction(() => document.querySelectorAll("#modal .cc-pick .opt")[1]?.getAttribute("aria-pressed") === "true");
assert.equal((await row.locator(".cc-said").textContent()).trim(), w.allP);
assert.deepEqual(errors, []);
});
test(`${engine} ${lang}: on where it can't run, Claude requests are said to fail, and why`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const { page, errors } = await openEditor(browser, lang, RUNS.wsl);
const row = runRow(page);
await row.waitFor();
const tr = (k, v) => page.evaluate(([k, v]) => t(k, v), [k, v]);
assert.equal(await row.locator(".cc-pick .opt").first().getAttribute("aria-pressed"), "true", "TUI is still the one chosen: never -p");
const said = (await row.locator(".cc-said.bad").textContent()).trim();
assert.equal(said, await tr("Claude requests fail: {why}", { why: await tr("magpie couldn't find the home folder of Claude Code in WSL") }));
assert.match(said, new RegExp(w.wsl));
if (lang !== "en") assert(!/Claude requests fail|couldn't find/.test(said), said);
assert.deepEqual(errors, []);
});
}
test(`${engine}: another subscription has no such row`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const { page, errors, reads } = await openEditor(browser, "en", RUNS.tui, { id: "codex" });
await page.locator("#modal .acc").first().waitFor();
assert.equal(await page.locator("#modal .cc-run, #modal .claude-mode").count(), 0);
assert.equal(reads.n, 0, "nothing is read for a row not shown");
assert.deepEqual(errors, []);
});
test(`${engine}: Only switch, which serves nothing, has no such row`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const { page, errors, reads } = await openEditor(browser, "en", RUNS.tui, { switchOnly: true });
await page.locator("#modal .acc").first().waitFor();
assert.equal(await page.locator("#modal .cc-run, #modal .claude-mode").count(), 0);
assert.equal(reads.n, 0, "nothing is read for a row not shown");
assert.deepEqual(errors, []);
});
}