Files
magpie/internal/gui/tests/codex-client.test.cjs
T
yetone 926b1e7dfb provider, gui: put back what 8c7328f3a took out of main by mistake (35aaaa4b9, 2fa79aef3, 797a23cad)
8c7328f3a (remote quotas: a Remote set to Select none still tells its
cards) was committed from a stale tree. Beside its own three files
(docs/subsystems/providers-accounts.md, remote_quotas_scope.go,
remote_quotas_test.go) it wrote 21 files back to their state before
35aaaa4b9, which took out:

- 35aaaa4b9, the Codex client setting (codex_request.go, codex_tier.go,
  settings.go, settingsapi.go, api.go, app.js, i18n.js, index.html,
  codex-client.test.cjs and the rest);
- 2fa79aef3, a save of the bytes a file already holds changes nothing
  (#6289: logins.go, logins_unchanged_test.go);
- 797a23cad, the tray's words (traywords.go, traytip_words_test.go).

The 21 files are checked out from 8c7328f3a^. After this commit
`git diff 8c7328f3a^ --stat` names only the three remote-quota files,
so main is the three lost commits plus 8c7328f3a's own change.

Run (mica, at this tree): gofmt -l clean; go test -count=1 -tags nogui
./internal/provider ./internal/settings ./internal/settingsapi ok.
./internal/cli and . are red on main already, not from this: the
codex-client and codex-open routes have no notYet entry, and
catalog.go's mmddSnapshot is compiled at launch (1ca2c62c4). Both are
fixed in the next two commits.
2026-11-13 02:00:40 +08:00

129 lines
7.6 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// The Codex magpie presents to ChatGPT (ouhthuo on X): a row in Codex's
// settings on the Agents page. Its button says the choice, "Codex CLI" by
// default; the app's menu (no native select) offers Codex CLI and Codex
// Desktop, and a pick posts settings/codex-client on its own: "" or
// "desktop". Every language, Chromium and WebKit, a wide and a narrow
// window; no backend, the API is 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 { watchSettingsReads, agentRow, openAgentSettings } = require("./reader.cjs");
const assets = path.resolve(__dirname, "../assets");
function server(lang, posted) {
let cur = { theme: "light", lang, version: "0.1.400", titleModels: [], codexClient: "" };
return async (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:false};` });
if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
// agents below Codex, so its opened row is not the list's end: a choice
// that rewraps its row at the very bottom must move the list
if (url.pathname === "/api/state") return json({ agents: [agentRow("codex", "Codex"), agentRow("claude", "Claude Code"), agentRow("pi", "Pi"), agentRow("opencode", "OpenCode"), agentRow("gemini", "Gemini CLI"), agentRow("kimi", "Kimi CLI"), agentRow("droid", "Droid"), agentRow("cline", "Cline"), agentRow("qwen", "Qwen Code"), agentRow("crush", "Crush")], profiles: [], settings: { lang, theme: "light" } });
if (url.pathname === "/api/settings/codex-client") {
const body = req.postDataJSON();
posted.push(["codex-client", body]);
cur = { ...cur, codexClient: body.client };
return json(cur);
}
if (url.pathname === "/api/settings") {
if (req.method() === "POST") posted.push(["settings", req.postDataJSON()]);
return json(cur);
}
if (url.pathname === "/api/plugins") return json({ plugins: [] });
if (url.pathname === "/api/usage/quotas") return json([]);
if (url.pathname === "/api/groups") return json({ groups: [], models: [] });
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: "" });
}
};
}
// the words as i18n.js has them
const W = {
en: { name: "Codex client", cli: "Codex CLI", desktop: "Codex Desktop" },
zh: { name: "Codex 客户端", cli: "Codex 命令行", desktop: "Codex 桌面版" },
"zh-TW": { name: "Codex 用戶端", cli: "Codex 命令列", desktop: "Codex 桌面版" },
ja: { name: "Codex クライアント", cli: "Codex CLI", desktop: "Codex Desktop" },
de: { name: "Codex-Client", cli: "Codex CLI", desktop: "Codex Desktop" },
};
const engines = process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"];
const langs = process.env.LANGS ? process.env.LANGS.split(",") : Object.keys(W);
for (const engine of engines) {
for (const lang of langs) {
for (const width of [900, 400]) {
test(`${engine} ${lang} ${width}px: the Codex client is picked from the app menu in its settings`, async (t) => {
const w = W[lang];
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
const page = await (await browser.newContext({ viewport: { width, height: 720 }, reducedMotion: "reduce" })).newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [], posted = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", server(lang, posted));
t.after(async () => {
if (process.env.ARTIFACT_DIR) {
await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true });
await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-${lang}-${width}-codex-client.png`) });
}
await browser.close();
});
await watchSettingsReads(page);
await page.goto("http://magpie.test/?view=agents");
await openAgentSettings(page);
const row = page.locator('.row.agent[data-id="codex"] #codexClientRow');
const pick = page.locator("#codexClientPick button");
await pick.waitFor();
assert.equal((await row.locator(".name").textContent()).trim(), w.name);
assert.equal((await pick.textContent()).trim(), w.cli, "Codex CLI by default");
assert.equal(await page.locator("select").count(), 0, "a native select");
// only Codex's settings have it
await page.locator('.row.agent[data-id="claude"] .ag-link').click();
await page.locator('.row.agent[data-id="claude"] .ag-prefs').waitFor();
assert.equal(await page.locator('.row.agent[data-id="claude"] #codexClientRow').count(), 0, "the row in Claude Code's settings");
await openAgentSettings(page);
await pick.waitFor();
await pick.scrollIntoViewIfNeeded();
const box = await pick.boundingBox();
assert(box && box.x >= 0 && box.x + box.width <= width, `the button runs off the window: ${JSON.stringify(box)}`);
assert(await page.evaluate(() => document.scrollingElement.scrollWidth <= innerWidth), "the page scrolls sideways");
// where the button sits on screen: a pick that moved the page would
// move it. At a narrow width the row rewraps as the choice's words
// change (the button under the name or beside it), which moves the
// button with no scroll at all, so it is held to its place only
// while the row keeps its height.
const top = () => pick.evaluate((b) => Math.round(b.getBoundingClientRect().top));
const tall = () => row.evaluate((r) => Math.round(r.getBoundingClientRect().height));
for (const [label, client] of [[w.desktop, "desktop"], [w.cli, ""]]) {
const y = await top(), h = await tall();
await pick.click();
const menu = page.locator(".proto-menu");
await menu.waitFor();
const items = (await menu.locator(".pm-item .pm-name").allTextContents()).map((s) => s.trim());
assert.deepEqual(items, [w.cli, w.desktop]);
const mb = await menu.boundingBox();
assert(mb.x >= 0 && mb.x + mb.width <= width, `the menu runs off the window: ${JSON.stringify(mb)}`);
await menu.locator(".pm-item").filter({ has: page.locator(".pm-name", { hasText: new RegExp("^" + label + "$") }) }).click();
await page.locator("#codexClientPick button", { hasText: new RegExp("^\\s*" + label + "\\s*$") }).waitFor();
assert.deepEqual(posted.at(-1), ["codex-client", { client }]);
await page.waitForTimeout(200);
if (await tall() === h) assert(Math.abs((await top()) - y) <= 1, `picking ${label} moved the button from ${y} to ${await top()}`);
}
assert.equal(posted.length, 2, JSON.stringify(posted));
assert.deepEqual(errors, []);
});
}
}
}