Files
magpie/internal/gui/tests/agent-models.test.cjs
T
yetone df27f0882a agent: the 接入 switch and a model picked in the row, both (owner: 对于那种想在 magpie 中一键设置 Agent 模型的用户能否也兼容一下,鱼和熊掌都要兼得); a row opens in place and slides as on iOS (owner: 展开的时候,上/下 padding 变了,导致抖动;展开和折叠都没有 iOS 那种丝滑优雅精致的动画)
Every agent row keeps its 接入 switch and now has its model picker beside it. Picking a magpie model on an agent that isn't connected connects it first (agent.Pick, used by /api/set), so one pick sets the agent up; disconnecting still puts back what the agent had. The intro line says both ways.

An opened row shook: the connected row centred its lines in its 56px min-height, and the 3px row-gap couldn't be folded away, so opening moved the name and the switch and changed the space under them. The first line now has its own height and isn't centred, the row-gap is 0 with the spacing in margins, and the opened part starts where the shut row ended (56/56 at 980px, 90/90 at 560px; the name at 8px and the switch at 19px open and shut). Opening and closing animate the part's height, padding, margin and border on iOS's sheet curve cubic-bezier(.32,.72,0,1), 460 ms open and 340 ms shut, and what's in it fades in from 6px above; a row already open slides shut before another opens. With reduced motion it opens at once.

Gemini CLI's .env made by magpie for its key was left holding a newline after a disconnect (main too): DelEnvFile now removes a dotenv file once the deleted keys were all it held.

TestPickConnectsFirst, the only.env case in TestEnvFile, agent-start-pick and agent-expand-steady (name and switch unmoved, the opened part's top at the shut row's end, at least 3 heights between on the way open and shut, growing/shrinking only, chromium and webkit at 980 and 560px) fail on the old code and pass. Tests that open rows wait for the slide. In a sandbox HOME with real accounts: Claude Code and Gemini CLI picked while not connected answer PONG and disconnect back to what they had; Goose's pick → disconnect round trip is byte-identical (goose itself stops at a Linear MCP OAuth, so no request). go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui for edit, agent and gui under a temp HOME pass; GUI tests that fail here fail the same on 82340f41 (account-arrange, cli-update, mobile-web, provider-levels-scroll, provider-proxy, redact-rule-row, routing-compact, provider-detect-models, plugin-move).
2026-10-04 02:05:10 +08:00

195 lines
10 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// An agent's model list (@ChongkaiX on X: pick which models show in Codex's
// /model): its connected row, opened, counts them by provider and those
// hidden, and Pick opens the list, where a click on a row takes a model out
// or puts it back, at once.
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 models = () => [
{ id: "group:fast", name: "fast", group: "Routing groups", icons: ["openai", "anthropic"] },
...["gpt-5.5", "gpt-5.5-mini", "gpt-5.4", "gpt-5.3-codex", "o4", "o4-mini"].map((m, i) =>
({ id: "openai/" + m, name: m.toUpperCase(), group: "OpenAI", icon: "openai", context: i ? 400e3 : 1e6, inUse: i === 0 })),
...Array.from({ length: 24 }, (_, i) => ({ id: "openrouter/m" + i, name: "Router Model " + i, group: "OpenRouter", icon: "openrouter", context: 128e3, hidden: i > 0 })),
];
function fixture(lang) {
const list = models();
const posts = [];
const count = () => {
const by = [];
for (const m of list.filter((m) => !m.hidden)) {
let g = by.find((x) => x.name === m.group);
if (!g) by.push(g = { name: m.group, icon: m.icon, n: 0 });
g.n++;
}
return { shown: list.filter((m) => !m.hidden).length, listed: list.length, by };
};
const state = () => ({
agents: [{
id: "codex", name: "Codex", path: "/test/config.toml", icon: "codex-color", wired: true,
fields: [{ key: "model", label: "model", value: "magpie/openai/gpt-5.5", options: [{ value: "magpie/openai/gpt-5.5", label: "GPT-5.5", ref: "openai/gpt-5.5" }] }],
models: count(),
}],
profiles: [], settings: { lang, theme: "light" },
});
async function serve(route) {
const req = route.request();
const 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/agent-models/codex") {
if (req.method() === "POST") {
const { hidden } = req.postDataJSON();
posts.push(hidden);
for (const m of list) m.hidden = hidden.includes(m.id) && !m.inUse;
return json({ models: list, count: count() });
}
return json({ models: list });
}
if (url.pathname === "/api/groups") return json({ groups: [] });
if (url.pathname === "/api/providers") return json({ providers: [], gateway: { running: true, window: true } });
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)];
await route.fulfill({ body: await fs.readFile(file), contentType });
}
return { serve, posts };
}
const W = {
en: { entry: "23 hidden", after: "24 hidden", all: "", title: "Codex's model list", current: "Current", shown: "Shown", hideAll: "Hide all", one: "30 hidden", showAll: "Show all" },
zh: { entry: "已隐藏 23 个", after: "已隐藏 24 个", all: "", title: "Codex 的模型列表", current: "在用", shown: "已显示", hideAll: "全部隐藏", one: "已隐藏 30 个", showAll: "全部显示" },
};
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh"]) {
test(`${engine} ${lang}: an agent's model list`, async (t) => {
const w = W[lang];
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
const page = await browser.newPage({ viewport: { width: 1000, height: 700 } });
page.setDefaultTimeout(5000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
const fx = fixture(lang);
await page.route("**/*", fx.serve);
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}-models.png`) });
}
await browser.close();
});
await page.goto("http://magpie.test/");
const row = page.locator('.row.agent[data-id="codex"]');
await row.locator(".ag-link").click();
const entry = row.locator(".ag-exp .ag-chips .ag-quiet");
const hidden = async () => (await row.locator(".ag-exp .ag-chips .ag-hint").allInnerTexts()).join("").trim();
const chips = async () => (await row.locator(".ag-chip").allInnerTexts()).map((s) => s.replace(/\s+/g, " "));
await entry.waitFor();
// the row has slid open (its slide is agent-expand-steady's)
await page.waitForFunction(() => document.getAnimations().length === 0);
assert.equal(await hidden(), w.entry);
assert.deepEqual(await chips(), ["Routing groups 1", "OpenAI 6", "OpenRouter 1"].map((c) => lang === "zh" ? c.replace("Routing groups", "路由组") : c));
// the line under the name counts them too
assert.match(await row.locator(".who .ag-st-t").innerText(), /^(Connected|已接入) · (8 models|.* 里有 8 个模型)/);
const scroll = () => page.evaluate(() => [scrollY, document.scrollingElement.scrollTop, $("#view-agents").scrollTop]);
const was = await scroll();
await entry.click();
const pop = page.locator(".am-pop:not(.leaving)");
await pop.waitFor();
assert.deepEqual(await scroll(), was, "opening it moves nothing");
assert.equal(await pop.locator(".am-t").innerText(), w.title);
// on the screen whole
const pb = await pop.boundingBox();
assert(pb.x >= 0 && pb.y >= 0 && pb.x + pb.width <= 1000 && pb.y + pb.height <= 700, JSON.stringify(pb));
assert(await page.evaluate(() => document.activeElement?.closest(".am-search") !== null), "the search has the keys");
// routing groups first; the long OpenRouter folded, OpenAI open
const groups = await pop.locator(".am-fold .gn").allInnerTexts();
assert.equal(groups.length, 3);
assert.equal(groups[1], "OpenAI");
assert.equal(groups[2], "OpenRouter");
assert.equal(await pop.locator(".am-g").nth(2).locator(".am-mr").count(), 0, "OpenRouter's 24 start folded");
assert.equal(await pop.locator(".am-g").nth(1).locator(".am-mr").count(), 6);
assert.equal(await pop.locator(".am-g").nth(2).locator(".c").innerText(), "1 / 24");
// the one Codex is set to stays
const cur = pop.locator(".am-mr", { hasText: "GPT-5.5" }).first();
assert.equal(await cur.locator(".am-tag").innerText(), w.current);
await cur.click({ force: true }); // aria-disabled: Playwright waits for it to be enabled
assert.equal(fx.posts.length, 0, "the model in use can't be taken out");
assert.equal(await cur.locator(".ck svg").count(), 1);
// a click takes one out at once, the count under the name with it
const mini = pop.locator(".am-mr", { hasText: "O4-MINI" });
await mini.click();
await page.waitForFunction(() => document.querySelector(".ag-chips .ag-hint")?.innerText.includes("24"));
assert.equal(await hidden(), w.after);
assert.equal((await chips())[1], "OpenAI 5");
await page.waitForTimeout(150);
assert.equal(fx.posts.length, 1);
assert(fx.posts[0].includes("openai/o4-mini") && fx.posts[0].includes("openrouter/m5"), "the whole list goes");
assert.equal(await mini.locator(".ck svg").count(), 0);
assert(await mini.evaluate((e) => e.classList.contains("off")));
assert.equal(await pop.locator(".am-g").nth(1).locator(".c").innerText(), "5 / 6");
// a search opens a folded group to what it finds
const q = pop.locator(".am-search input");
await q.fill("router model 7");
assert.equal(await pop.locator(".am-mr").count(), 1);
await q.fill("");
// "Shown": only those shown, one turned off there stays till it changes
await pop.locator(".am-seg button", { hasText: w.shown }).click();
assert.equal(await pop.locator(".am-mr").count(), 1 + 5 + 0, "routing group, OpenAI's 5; OpenRouter folded");
await pop.locator(".am-mr", { hasText: "O4" }).first().click();
assert.equal(await pop.locator(".am-mr").count(), 6);
// the group's "show all" comes on hover
const gh = pop.locator(".am-gh").nth(2);
assert.equal(await gh.locator(".am-all").evaluate((e) => getComputedStyle(e).opacity), "0");
await gh.hover();
await page.waitForTimeout(250);
assert.equal(await gh.locator(".am-all").evaluate((e) => getComputedStyle(e).opacity), "1");
assert.equal(await gh.locator(".am-all").innerText(), w.showAll);
// every one hidden at once, but the one in use
const hideAll = pop.locator(".am-hide"), showAll = pop.locator(".am-reset:not(.am-hide)");
assert.equal(await hideAll.innerText(), w.hideAll);
assert.equal(await showAll.innerText(), w.showAll);
await hideAll.click();
await page.waitForTimeout(150);
assert.equal(fx.posts.at(-1).length, 30);
assert(!fx.posts.at(-1).includes("openai/gpt-5.5"));
assert.equal(await hidden(), w.one);
assert(await hideAll.isDisabled());
assert.equal(await pop.locator(".am-g").nth(1).locator(".c").innerText(), "1 / 6");
// back to all shown
await showAll.click();
await page.waitForTimeout(150);
assert.deepEqual(fx.posts.at(-1), []);
assert.equal(await hidden(), w.all);
assert(await showAll.isDisabled());
assert(!(await hideAll.isDisabled()));
// Esc closes it; so does a click elsewhere
await page.keyboard.press("Escape");
await pop.waitFor({ state: "detached" });
await entry.click();
await pop.waitFor();
await page.mouse.click(900, 650);
await pop.waitFor({ state: "detached" });
assert.deepEqual(errors, []);
});
}
}