Files
magpie/internal/gui/tests/group-compact.test.cjs
T
yetone e18aca1038 agents: an agent's own context window, and a routing group's own Compact at (ul3140773077392, NovaStudiosss on X)
ul3140773077392 on X: dsh compacts earlier than other harnesses at the same
window, so one global size per model fits none. An agent's sheet (Agents,
the agent's Settings line) now holds "Context window": a size for every
model, or model=size pairs (provider/model, group/<id>, provider/*, a bare
model id, *). Empty follows each model's own window. It is applied in
provider.CatalogFor, the choke point every agent's model list goes through,
so dsh's contextWindow, Codex's catalog and Claude Code's window all read
it, and a window set this way makes Codex/Claude compact at that window
(Entry.WindowSet). Stored in settings.AgentContexts, kept by a general
save (settingsapi); POST /api/settings/agent-contexts and
`magpie model window <agent> [model=size...|off]`.

NovaStudiosss on X: Routing, a group's Edit, Advanced already had "Context"
(the window agents are told). It now has "Compact at" beside it
(Group.Compact; compactSet reads it before the members' smallest), also
`magpie group ... compact=`.

GUI: agentwindow.js, loaded on demand through agentcompact.js so app.js
stays inside its budget (TestCoreScriptBudget passes; the "Context window
in {agent}" label reuses the existing "Context window" key). Every agent
with fields now has a sheet; agent-prefs' Pi expectation says so.

Run at the pushed head, on mote: go vet (darwin; linux -tags nogui;
windows) on provider, agent, cli, gui, settings, settingsapi; gofmt clean;
go test provider, cli, settings, settingsapi, gui (TestCoreScriptBudget
and cli's route table fixed here), agent -run Dsh; node --test agent-window
and group-compact (chromium and webkit, en zh zh-TW ja de, 900 and 420px),
agent-compact, agent-prefs, compact-at. Reds seen identically on a
detached origin/main 4e0026661: agent-prefs webkit "in the panel a
Settings line..." (#6139) and old-webkit "compat.js is the first" / "styles
hold up in Safari 15.0" (#5919, #3403).

Broken by hand, each red then restored: withAgentWindows out of CatalogFor
(TestAgentOwnContextWindow, TestDshOwnContextWindow); the group branch in
compactSet (TestGroupOwnCompactAt); the settingsapi keep line
(TestSettingsSaveKeepsWhatItDoesNotSend); the window row in the sheet
(agent-window tests); compact dropped from the group save
(group-compact tests).

Not tried: a real dsh with a real account (its compaction is dsh's own;
the test reads the window magpie hands it), the full GUI suite (queued
behind another run on mote), Windows and Linux runs of the new tests.
2026-11-12 23:01:29 +08:00

123 lines
6.5 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// A routing group carries its own auto-compaction size (NovaStudiosss on X:
// no need to set a context size and a compaction size on each provider
// model). The group editor's Advanced "Compact at" box takes 160k or 1m;
// empty is none (0, the members' own). It is saved with the group and a
// bad length is marked as it is typed. English, Chinese, Traditional
// Chinese, Japanese and German, Chromium and WebKit.
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");
// the rows a group's editor folds under Advanced, opened
const openAdvanced = async (page) => {
const d = page.locator(".rt-gedit .rt-gadv");
await d.waitFor({ state: "attached" });
if (!(await d.evaluate((e) => e.open))) await d.locator(":scope > summary").click();
};
const assets = path.resolve(__dirname, "../assets");
const models = [
{ id: "oa/gpt-6.1-sol", name: "gpt-6.1-sol", providerName: "OpenAI", icon: "generic", context: 1000000 },
{ id: "an/claude-opus-5-5", name: "claude-opus-5-5", providerName: "Anthropic", icon: "generic", context: 200000 },
{ id: "ds/deepseek-flash", name: "deepseek-flash", providerName: "DeepSeek", icon: "generic", context: 128000 },
];
const members = models.map((m) => m.id);
const saved = { context: 0, compact: 0 };
const groups = () => ({
models, pools: [],
groups: [{ id: "sol", name: "Sol", members, routing: "order", ready: true, context: saved.context, compact: saved.compact,
memberInfo: members.map((id) => ({ id, ready: true })) }],
});
const words = {
en: { edit: "Edit", save: "Save", label: "Compact at" },
zh: { edit: "编辑", save: "保存", label: "压缩阈值" },
"zh-TW": { edit: "編輯", save: "儲存", label: "壓縮閾值" },
ja: { edit: "編集", save: "保存", label: "圧縮しきい値" },
de: { edit: "Bearbeiten", save: "Speichern", label: "Komprimieren ab" },
};
function serve(lang, posts) {
const state = { agents: [{ id: "codex", name: "Codex", path: "/test/codex", fields: [] }], profiles: [], settings: { lang, theme: "light" } };
return async (r) => {
const url = new URL(r.request().url());
const json = (data) => r.fulfill({ json: data });
if (url.pathname === "/boot.js") return r.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` });
if (url.pathname === "/wails/runtime.js") return r.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/state") return json(state);
if (url.pathname === "/api/plugins") return json({ plugins: [] });
if (url.pathname === "/api/gateway/trace") {
if (url.searchParams.get("wait")) return new Promise(() => {});
return json({ mine: true, now: new Date().toISOString(), seq: 1, totals: { requests: 0, rerouted: 0, errors: 0 }, routes: [] });
}
if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] });
if (url.pathname === "/api/groups") return json(groups());
if (url.pathname.startsWith("/api/groups/")) {
const body = JSON.parse(r.request().postData() || "{}");
posts.push({ path: url.pathname, body });
saved.context = body.context || 0;
saved.compact = body.compact || 0;
return json(groups());
}
if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true, url: "http://127.0.0.1:3999" } });
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 r.fulfill({ body: await fs.readFile(file), contentType });
};
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of Object.keys(words)) {
const w = words[lang];
test(`${engine} ${lang}: a group carries its own Compact at`, async (t) => {
saved.context = 0;
saved.compact = 0;
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
const page = await (await browser.newContext({ viewport: { width: 1100, height: 2000 }, reducedMotion: "reduce" })).newPage();
t.after(() => browser.close());
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [], posts = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang, posts));
await page.goto("http://magpie.test/?view=groups");
const card = page.locator(".rt-group", { hasText: "Sol" });
await card.waitFor();
const ed = page.locator(".rt-gedit");
const box = ed.locator("input.cmp-in");
const save = async () => {
await ed.locator("button.primary", { hasText: w.save }).click();
await page.waitForFunction(() => !document.querySelector(".rt-gedit"));
return posts.filter((p) => p.path === "/api/groups/save").at(-1).body;
};
const open = async () => { await card.locator("button", { hasText: w.edit }).click(); await openAdvanced(page); await box.waitFor(); };
await open();
assert.equal(await box.getAttribute("aria-label"), w.label);
assert.equal(await box.inputValue(), "", "none until set");
assert.equal((await save()).compact, 0);
await open();
await box.fill("abc");
assert.equal(await box.getAttribute("aria-invalid"), "true");
await box.fill("160k");
assert.equal(await box.getAttribute("aria-invalid"), "false");
assert.equal((await save()).compact, 160000);
await card.locator("button", { hasText: w.edit }).click();
await box.waitFor();
assert.equal(await box.inputValue(), "160k", "the editor opens at what was saved");
assert(await ed.locator(".rt-gadv").evaluate((e) => e.open), "a set size opens Advanced");
await box.fill("");
assert.equal((await save()).compact, 0, "emptied, the group has none");
const missing = await page.evaluate(() => ["Compact at", "Default"].filter((k) => !I18N.zh[k]));
assert.deepEqual(missing, [], "every string has its Chinese");
assert.deepEqual(errors, []);
});
}
}