Files
magpie/internal/gui/tests/agent-here.test.cjs
T
yetone 2ab9235711 agents: an agent's picks can be kept to this computer, as an MCP server can
chenxiang0010 on Discord: 「agent当前选择的模型可不可以像mcp那样 只对当前设备生效」.
Sync carries every agent's picks (model, effort...) between computers;
an MCP server has an opt-in "This computer only" that sync and backup
files never carry. Agents now have the same: the row's menu (right-click
or ...) has This computer only, off by default, so an upgrade changes no
pick on any computer.

- settings: AgentsHere, this computer's own (KeepOwn), and kept by a save
  of the Settings page (settingsapi.Apply), which doesn't send it.
- davsync: each kept agent adds "<id>.*" to the own set (withHere, isOwn),
  so a sync neither pushes its fields nor brings the server's; a Restore
  from the server leaves them. sync-state.json records the list as of the
  last sync (Here): hashes before a sync use it and after use the current
  list, so turning it on or off is no change on either side, and a changed
  list forces the sync past its early returns. Turned off, the next pick
  goes up again. Other agents still sync both ways; the implicit own
  fields (hayden4608) are unchanged.
- backup.Restore skips a kept agent in a backup file.
- gui: POST /api/agents/here; the row says "This computer only" beside
  the version (hidden in the tray panel and in a narrow row), menu ticks.
  Core words moved from the library plugin's i18n into core i18n
  (zh/zh-TW/ja/de). Comments in renderAgents cut to pay for it under the
  core budget (no raise).
- cli: the route is named under stage 3 (no command yet).
- docs/subsystems/sync-backup.md describes it.

Run on mote, at the base eb8b840dd (Go and the GUI files below) and again
at this head's base for internal/gui (core budget) and agent-here,
i18n-keys, agent-menu-label:
- go test -tags nogui ./internal/davsync ./internal/settings
  ./internal/backup ./internal/gui ./internal/settingsapi: ok
- go test ./internal/cli: ok but TestEveryRouteHasACommandOrIsNamed,
  red on main for POST /api/usage/checkin-account alone (#4826)
- GOOS=linux and GOOS=windows go vet ./...: ok; gofmt -l: clean
- GUI, chromium + webkit: agent-here (en/zh/zh-TW/ja/de x 1100/640/360),
  i18n-keys, sync-own-fields, mcp-local-server, agent-menu-label:
  90/90 ok
- the whole GUI suite: cut off at test 3783 by a session restart; the
  rest (key-* on) is still running at push. Reds seen so far, under
  three GUI suites at once on mote, none in an agent file: account-eye,
  balance-fix (Sub2API, /api/user/self), codex-auto-reset, context-map,
  editor-advanced-align; being checked at main and filed there.

Broken by hand, each red then restored:
- hereOf returns nil: TestAgentHereThroughSync "B's kept picks went up"
- backup.Restore's kept skip off: "B after a backup file"
- KeepOwn line dropped: TestAgentsHereIsThisComputers "another
  computer's kept agents came in"
- TestSettingsSaveKeepsWhatItDoesNotSend went red on this change before
  settingsapi kept AgentsHere (a Settings save would have dropped it).

Not tried: a real two-computer sync with real accounts; no CLI/TUI
command for the flag; Settings -> Sync doesn't list kept agents; WSL
agent ids (codex@wsl:...) only through the key format.
2026-11-03 02:27:20 +08:00

112 lines
5.9 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// An agent's picks can be kept to this computer, as an MCP server can
// (chenxiang0010 on Discord: 「agent当前选择的模型可不可以像mcp那样 只对当前设备生效」).
// The row's menu has This computer only: it posts agents/here, is ticked
// once on, and the window's row says it beside the version, in every
// language, in Chromium and WebKit, in the window and the tray panel.
// Off again, the tick and the words go. No backend: faked here.
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 CHECK = "m3.5 8.5 3 3 6-7";
function fixture(lang, posts) {
let here = [];
const settings = () => ({ lang, theme: "light", agentsHere: here });
const state = () => ({
agents: [{
id: "codex", name: "Codex", path: "~/.codex/config.toml", icon: "codex", wired: true,
fields: [{ key: "model", label: "model", value: "glm-5.3-flash", options: [{ value: "glm-5.3-flash", label: "GLM-5.3-Flash", icon: "generic" }] }],
}],
profiles: [], settings: settings(),
});
async function serve(route) {
const req = route.request(), url = new URL(req.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:true};` });
if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/state") return json(state());
if (url.pathname === "/api/agents/here") {
const b = req.postDataJSON();
posts.push(b);
here = here.filter((x) => x !== b.agent);
if (b.on) here.push(b.agent);
return json(settings());
}
if (url.pathname === "/api/usage/quotas") return json([]);
if (url.pathname === "/api/groups") return json({ groups: [] });
if (url.pathname === "/api/agents/cli") return json({ agents: { codex: { version: "0.166.0" } }, pending: false });
if (url.pathname === "/api/providers") return json({ providers: [], gateway: { running: true } });
if (url.pathname === "/api/plugins") return json({ plugins: [] });
if (url.pathname.startsWith("/api/")) return 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)];
const body = await fs.readFile(file).catch(() => null);
await route.fulfill(body ? { body, contentType } : { status: 404, body: "" });
}
return { serve };
}
const row = '.row.agent[data-id="codex"]';
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) {
for (const where of ["window", "narrow", "panel"]) {
test(`${engine} ${lang} ${where}: an agent's picks kept to this computer`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const size = { window: { width: 1100, height: 700 }, narrow: { width: 640, height: 700 }, panel: { width: 360, height: 640 } }[where];
const page = await browser.newPage({ viewport: size });
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [], posts = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", fixture(lang, posts).serve);
await page.goto("http://magpie.test/" + (where === "panel" ? "?mode=panel" : ""));
await page.locator(row).waitFor();
const name = await page.evaluate(() => t("This computer only"));
if (lang !== "en") assert.notEqual(name, "This computer only", "not translated");
const item = () => page.locator(".row-menu .rm-item", { hasText: name });
const open = async () => {
await page.locator(row).click({ button: "right" });
await item().waitFor();
};
const ticked = () => item().locator("path").getAttribute("d").then((d) => d === CHECK);
await open();
assert.equal(await ticked(), false, "ticked before it was turned on");
await item().click();
await page.waitForFunction(() => !document.querySelector(".row-menu"));
assert.deepEqual(posts, [{ agent: "codex", on: true }]);
const tag = page.locator(`${row} .ag-here`);
if (where === "panel") assert.equal(await tag.count(), 0, "the panel's row has no room for it");
else {
await tag.waitFor();
assert.equal(await tag.textContent(), name);
// the row keeps its width and the name stays whole
const fit = await page.locator(row).evaluate((r) => {
const n = r.querySelector(".who .name");
return { over: r.scrollWidth - r.clientWidth, cut: n.scrollWidth - n.clientWidth };
});
assert.ok(fit.over <= 0 && fit.cut <= 0, `the row overflows: ${JSON.stringify(fit)}`);
}
await open();
assert.equal(await ticked(), true, "not ticked once on");
// off again: the tick and the words go
await item().click();
await page.waitForFunction(() => !document.querySelector(".row-menu"));
assert.deepEqual(posts.at(-1), { agent: "codex", on: false });
await page.waitForFunction((sel) => !document.querySelector(sel), `${row} .ag-here`);
await open();
assert.equal(await ticked(), false, "still ticked once off");
assert.deepEqual(errors, []);
});
}
}
}