Files
magpie/internal/gui/tests/agent-telemetry.test.cjs
T
yetone 720200092e agent: turn off an agent's own telemetry from its settings (OipsAnthony on X)
An agent with a documented telemetry switch has a Telemetry row in its
Settings on the Agents page (opened row and settings sheet). Off writes
that switch into the agent's own config the way magpie writes it already:

- Codex: [analytics] enabled = false in config.toml (feedback left alone)
- Claude Code: env DISABLE_TELEMETRY and DISABLE_ERROR_REPORTING = "1"
- Gemini CLI, Qwen Code: privacy.usageStatisticsEnabled = false

Nothing is written until the user turns it off. Off writes only keys the
file lacks and records them in the stash; a key the user set on refuses
Off and names it; one they set off stays, and the row says so. On takes
out only the keys magpie wrote that still hold magpie's value. An
unreadable config is unknown: the row says so and can't be pressed.
Agent.Telemetry is not a Field, so profiles never carry it and Unwire
leaves it. Only agents on this computer (no WSL distro, #3896).
POST /api/agents/telemetry/{id}; strings in zh, zh-TW, ja, de.

The row is paid for in the core budget with sixteen Agents-page comments
cut in app.js (core 1,532,840 of 1,533,802 after the rebase), no raise.

Run (mote, at this head on origin/main 1ad9d157f; vet, the three packages and agent-telemetry, i18n-keys, agent-details-ready, click-scroll again there, 98 of 98):
- go test -run Telemetry ./internal/agent, then go test -count=1
  ./internal/agent ./internal/gui ./internal/cli: ok
- go vet ./..., GOOS=linux -tags nogui, GOOS=windows: ok; gofmt clean
- GUI, chromium and webkit: agent-telemetry (34 pass: every language at
  1100 and 640px, no scroll on click, no select, the user's own off,
  a refusal, the sheet), and 104 agent-*, i18n-*, click-scroll,
  settings-search, panel-agent and agent-settings files: reds only in
  page-width-steady and mobile-web (red the same at origin/main
  5a347ca8c, #5013, #4541) and agent-compact once under concurrency 6
  (passes alone, 8 of 8; #5607; its fixture has no telemetry)
- internal/cli failed once in the first run with its name lost to a
  tail of mine; it passed on a rerun at this head and at origin/main

Broken by hand: skipping the user-on check, dropping the "still ours"
check before a delete, and removing the WSL gate each turned
TestCodexTelemetryLeavesTheUsers, TestClaudeTelemetryChangedSince,
TestUsageStatsTelemetrySwitch and TestTelemetryOnlyHere red. The tests
don't compile on the old code (no Agent.Telemetry).

Not tried: real Codex, Claude Code, Gemini CLI or Qwen Code reading the
written keys (the keys come from their source); Crush (left for later),
OpenCode (no switch); WSL agents; the whole GUI suite (the agent-*,
i18n and agent-settings files only); the full go suite runs after the tag.
2026-11-10 17:28:15 +08:00

161 lines
9.5 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// An agent's own telemetry off from its page (OipsAnthony on X): an agent
// whose state carries telemetry has a Telemetry row in its opened row's
// Settings and in its settings sheet, on until switched. Off posts
// {off: true} and the row follows the state the reply carries; a refusal
// (a value of the user's own) is said and the row goes back. A value of
// the user's own that keeps it off is said under the name. An agent with
// no switch has no row. Every language, wide and narrow, no native select,
// no scroll on click. 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");
function fixture(lang, { own = [], refuse = "" } = {}) {
const settings = { lang, theme: "light" };
const posts = [];
const tel = { off: own.length > 0, own, keys: ["privacy.usageStatisticsEnabled"], file: "/test/.gemini/settings.json" };
const state = () => ({
agents: [
{
id: "gemini", name: "Gemini CLI", path: "/test/.gemini/settings.json", icon: "geminicli-color", wired: true,
fields: [{ key: "model", label: "model", value: "magpie/glm-5", options: [{ value: "magpie/glm-5", label: "GLM-5", ref: "magpie/glm-5" }] }],
telemetry: { ...tel },
},
{
id: "opencode", name: "OpenCode", path: "/test/.config/opencode/opencode.json", icon: "opencode", wired: true,
fields: [{ key: "model", label: "model", value: "magpie/glm-5", options: [{ value: "magpie/glm-5", label: "GLM-5", ref: "magpie/glm-5" }] }],
},
],
profiles: [], settings,
});
async function serve(route) {
const req = route.request(), url = new URL(req.url());
const json = (data, status = 200) => route.fulfill({ json: data, status });
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(state());
if (url.pathname === "/api/agents/telemetry/gemini") {
const body = req.postDataJSON();
posts.push(body);
if (refuse) return json({ error: refuse }, 400);
tel.off = body.off;
return json(state());
}
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: {}, pending: false });
if (url.pathname === "/api/providers") return json({ providers: [], gateway: { running: true } });
if (url.pathname === "/api/plugins") return json({ plugins: [] });
if (url.pathname === "/api/settings") return json(settings);
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)];
try { await route.fulfill({ body: await fs.readFile(file), contentType }); }
catch { await route.fulfill({ status: 404, body: "" }); }
}
return { serve, posts };
}
const W = {
en: { name: "Telemetry", off: "Off", on: "On", ok: "Takes effect when Gemini CLI next starts", own: "Turned off in your own config too: privacy.usageStatisticsEnabled", details: "Details", settings: "Settings" },
zh: { name: "遥测", off: "关闭", on: "开启", ok: "Gemini CLI 下次启动时生效", own: "你自己的配置里也关了:privacy.usageStatisticsEnabled", details: "详情", settings: "设置" },
"zh-TW": { name: "遙測", off: "關閉", on: "開啟", ok: "Gemini CLI 下次啟動時生效", own: "你自己的設定裡也關了:privacy.usageStatisticsEnabled", details: "詳情", settings: "設定" },
ja: { name: "テレメトリ", off: "オフ", on: "オン", ok: "Gemini CLI の次回起動時に反映されます", own: "ご自身の設定でもオフです:privacy.usageStatisticsEnabled", details: "詳細", settings: "設定" },
de: { name: "Telemetrie", off: "Aus", on: "An", ok: "Wirkt beim nächsten Start von Gemini CLI", own: "Auch in deiner eigenen Konfiguration aus: privacy.usageStatisticsEnabled", details: "Details", settings: "Einstellungen" },
};
const gm = '.row.agent[data-id="gemini"]';
const oc = '.row.agent[data-id="opencode"]';
async function open(engine, lang, width, opts, t) {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
const page = await browser.newPage({ viewport: { width, height: 700 } });
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
const fx = fixture(lang, opts);
await page.route("**/*", fx.serve);
t.after(() => browser.close());
await page.goto("http://magpie.test/?view=agents");
await page.locator(gm).waitFor();
await page.locator(`${gm} .ag-link`, { hasText: W[lang].details }).click();
await page.locator(`${gm} .ag-exp .ag-telemetry`).waitFor();
return { page, fx, errors };
}
// until the faked backend has had n posts
async function posted(fx, n) {
for (let i = 0; i < 500 && fx.posts.length < n; i++) await new Promise((r) => setTimeout(r, 10));
}
const pressed = (page, sel) => page.evaluate((s) => document.querySelector(s)?.querySelector(".opt.on")?.textContent, sel);
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of Object.keys(W)) {
for (const width of [1100, 640]) {
test(`${engine} ${lang} ${width}px: Gemini CLI's telemetry off and back`, async (t) => {
const w = W[lang];
const { page, fx, errors } = await open(engine, lang, width, {}, t);
const row = `${gm} .ag-exp .ag-telemetry`;
assert.equal(await page.locator(`${row} .name`).textContent(), w.name);
assert.equal(await pressed(page, row), w.on, "off before it was switched");
assert.equal(await page.locator(`${row} .sub`).count(), 0);
const box = await page.locator(row).boundingBox();
assert.ok(box.x + box.width <= width && box.height > 0, JSON.stringify(box));
const y = await page.evaluate(() => scrollY);
await page.locator(`${row} .opt`, { hasText: w.off }).click();
await page.waitForFunction(([s, off]) => document.querySelector(s)?.querySelector(".opt.on")?.textContent === off, [row, w.off]);
// the reply's state drawn: the status says it
await page.waitForFunction((ok) => document.querySelector("#status")?.textContent === ok, w.ok);
assert.deepEqual(fx.posts, [{ off: true }]);
assert.equal(await pressed(page, row), w.off);
await page.locator(`${row} .opt`, { hasText: w.on }).click();
await posted(fx, 2);
await page.waitForFunction(([s, on]) => document.querySelector(s)?.querySelector(".opt.on")?.textContent === on, [row, w.on]);
assert.deepEqual(fx.posts, [{ off: true }, { off: false }]);
assert.equal(await page.evaluate(() => scrollY), y, "a click scrolled the page");
// the agent with no switch has none
assert.equal(await page.locator(`${oc} .ag-telemetry`).count(), 0);
assert.equal(await page.locator("select").count(), 0, "a native select");
assert.deepEqual(errors, []);
});
}
test(`${engine} ${lang}: the user's own off is said`, async (t) => {
const w = W[lang];
const { page, errors } = await open(engine, lang, 1100, { own: ["privacy.usageStatisticsEnabled"] }, t);
const row = `${gm} .ag-exp .ag-telemetry`;
assert.equal(await pressed(page, row), w.off);
assert.equal(await page.locator(`${row} .sub`).textContent(), w.own);
assert.deepEqual(errors, []);
});
}
test(`${engine}: a refusal is said and the row goes back`, async (t) => {
const refuse = "/test/.gemini/settings.json sets privacy.usageStatisticsEnabled to true; magpie leaves a value of yours as it is (change it there)";
const { page, fx, errors } = await open(engine, "en", 1100, { refuse }, t);
const row = `${gm} .ag-exp .ag-telemetry`;
await page.locator(`${row} .opt`, { hasText: "Off" }).click();
await page.waitForFunction((s) => document.querySelector("#status")?.textContent === s, refuse);
await page.waitForFunction((s) => document.querySelector(s)?.querySelector(".opt.on")?.textContent === "On", row);
assert.deepEqual(fx.posts, [{ off: true }]);
assert.deepEqual(errors, []);
});
test(`${engine}: the agent's menu opens a sheet with the row`, async (t) => {
const { page, fx, errors } = await open(engine, "en", 1100, {}, t);
await page.evaluate(() => openAgentPrefs(state.agents.find((a) => a.id === "gemini")));
const row = "#modal .agent-prefs .ag-telemetry";
await page.locator(row).waitFor();
assert.equal(await page.locator(`#modal .agent-prefs .ehead b`).textContent(), "Gemini CLI settings");
await page.locator(`${row} .opt`, { hasText: "Off" }).click();
await posted(fx, 1);
await page.waitForFunction((s) => document.querySelector(s)?.querySelector(".opt.on")?.textContent === "Off", row);
assert.deepEqual(fx.posts, [{ off: true }]);
assert.deepEqual(errors, []);
});
}