Files
magpie/internal/gui/tests/group-member-effort-map.test.cjs
T
yetone 3441035a2e routing: a group's member maps the reasoning level asked to the one sent
A Routing group that relays one long task across vendors sent every
member the same effort word, but one vendor's high is another's xhigh
(LuckymanJC on Discord). A member that follows the group may now carry
a map from the level asked to the level sent: Group.EffortMaps, json
effortMaps, keyed by the member's id as Members spells it.

- Default: no map, or a level the map doesn't name, goes as asked, so
  every group saved before is unchanged and nothing of the user's is
  overridden. A member fixed at an effort (or with several levels) and
  a rule's UseEffort still win over the map.
- SaveGroup keeps a map only for a plain member of the group (not
  :effort, not :a+b, not group/), drops pairs that send what was
  asked, lowercases, and refuses a word that isn't a reasoning level.
- Carried by member rename (groupfast RenameMember and the all-groups
  rename), provider rename, fold (and its fingerprint), share/import
  and CanonicalRef; on a collision the map already spelled so wins.
- Gateway: the level asked is the suffix's/agent pick, then the turn's
  pick, then the request's own (askedLevel: agentEffort, plus none for
  an Anthropic request with thinking disabled); the target is sent with
  withFixedEffort, fitted to the model's catalog levels unless none, and
  the trace's try records mappedFrom. Per candidate, so fallback and
  retries send each member its own; count_tokens sends no member effort.
- The group lists a mapped member's own levels plus the map's keys to
  agents (mappedLevels in buildGroupEntries).
- GUI: the member's reasoning menu has "Map levels…" (app menus only):
  a level asked, then As asked or one of the model's levels. The chip
  reads "Mapped reasoning", its title the pairs. Follow the group or a
  fixed level drops the map. zh, zh-TW, ja and de.
- docs/subsystems/gateway-routing.md says all of it.

Run (mote, at this tree):
- gofmt -l: clean. go vet (provider, gateway, gui), GOOS=linux go vet
  -tags nogui ./..., GOOS=windows go vet ./...: clean.
- go test ./internal/provider/: ok. go test -run
  'Effort|Level|Group|Fallback|Rule|Auto|Fixed' ./internal/gateway/: ok.
- node --test group-member-effort-map (30: chromium + webkit, en zh
  zh-TW ja de, 1100 and 420px) and group-member-levels: pass;
  i18n-en-no-cjk: pass. i18n-keys' "a plugin's words are its own" is
  red only for the three words it is red for on origin/main 922cc0ccf
  (#5332, #5426, #5482); this change's words are its own (two that Go
  source said too were reworded).
- go test -run 'Group|Budget|I18n' ./internal/gui/: ok.

Broken by hand, each red, then restored:
- gateway mappedLevel(nil, in): every TestMemberEffortMap* red.
- mappedLevels left out of buildGroupEntries: both levels tests red.
- group_share's rename left out: the import case red.
- RenameMember's rename left out: its case red.
- SaveGroup's cleanEffortMaps call left out: TestSaveGroupEffortMaps red.
- the editor save without effortMaps: 30 of 30 GUI red; moveTo's map
  cleanup left out: 10 of 30 red.

Not tried: a real cross-vendor relay with real accounts (the mapping is
checked on the request bodies the gateway sends to fake upstreams). The
CLI and TUI keep the map but have no editor for it. The full Go suite
and whole GUI suite run after the push, per the loop rules.

Reported-by: LuckymanJC (Discord)
2026-11-07 20:19:50 +08:00

205 lines
12 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// A group that relays one task across vendors sends each member the level
// the vendor means (LuckymanJC on Discord: one vendor's high is another's
// xhigh). In the group editor a member's reasoning menu has "Map levels…":
// a level asked, then the one sent for it, from the model's own levels or
// "As asked". The chip says the member is mapped and its title the pairs;
// the save carries effortMaps for that member only; a fixed level picked
// later drops the map, and so does "Follow the group". A member fixed at a
// level that gets a map follows the group again. Nothing on the page
// moves. In every language, Chromium and WebKit, at a wide and a narrow
// window.
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: "oa/gpt-6.1-sol", name: "gpt-6.1-sol", providerName: "OpenAI", icon: "generic", efforts: ["low", "medium", "high", "xhigh"], canFast: true },
{ id: "rl/glm-5", name: "glm-5", providerName: "Relay", icon: "generic", efforts: ["low", "high"] },
];
const groups = (members, effortMaps) => ({
models, pools: [],
groups: [{ id: "sol", name: "Sol", members, effortMaps, routing: "order", ready: true,
memberInfo: members.map((id) => ({ id, ready: true, canFast: id.startsWith("oa/") })) }],
});
const words = {
en: { edit: "Edit", save: "Save", map: "Map levels…", follow: "Follow the group", mapped: "Mapped reasoning", asked: "As asked",
title: "Sent low → medium, high → xhigh; other levels as asked" },
zh: { edit: "编辑", save: "保存", map: "映射强度…", follow: "跟随分组", mapped: "映射推理强度", asked: "按要求" },
"zh-TW": { edit: "編輯", save: "儲存", map: "映射強度…", follow: "跟隨分組", mapped: "映射推理強度", asked: "按要求" },
ja: { edit: "編集", save: "保存", map: "レベルを対応付け…", follow: "グループに従う", mapped: "推論レベルを対応付け", asked: "要求どおり" },
de: { edit: "Bearbeiten", save: "Speichern", map: "Stufen zuordnen…", follow: "Der Gruppe folgen", mapped: "Zugeordnetes Reasoning", asked: "Wie angefragt" },
};
function serve(lang, posts, members, maps) {
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(() => {}); // nothing more comes
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(members, maps));
if (url.pathname.startsWith("/api/groups/")) {
posts.push({ path: url.pathname, body: JSON.parse(r.request().postData() || "{}") });
return json(groups(members, maps));
}
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 });
};
}
async function open(engine, lang, width, members, t, maps) {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
const page = await (await browser.newContext({ viewport: { width, height: 1600 }, reducedMotion: "reduce" })).newPage();
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}-group-member-effort-map.png`) });
}
await 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, members, maps));
await page.goto("http://magpie.test/?view=groups");
const card = page.locator(".rt-group", { hasText: "Sol" });
await card.waitFor();
return { page, card, errors, posts };
}
const keys = ["Map levels…", "a level in place of the one asked", "Mapped reasoning", "Sent {pairs}; other levels as asked", "As asked"];
const option = (page, text) => page.locator("#pop:not([hidden]) #list li:not(.group)", { has: page.locator(".v", { hasText: new RegExp("^" + text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + "$") }) });
const shut = (page) => page.waitForFunction(() => document.querySelector("#pop").hidden);
async function save(page, w) {
const b = page.locator(".rt-gedit button.primary", { hasText: w.save });
await b.scrollIntoViewIfNeeded();
await b.click();
await page.waitForFunction(() => !document.querySelector(".rt-gedit"));
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of Object.keys(words)) {
for (const width of [1100, 420]) {
const w = words[lang];
test(`${engine} ${lang} ${width}px: a group's member maps the levels asked`, async (t) => {
const { page, card, errors, posts } = await open(engine, lang, width, ["oa/gpt-6.1-sol", "rl/glm-5"], t);
await card.locator("button", { hasText: w.edit }).click();
const ed = page.locator(".rt-gedit");
const row = (name) => ed.locator(".fbrow", { has: page.locator(".n", { hasText: name }) });
const chip = () => row("gpt-6.1-sol").locator("button.rt-fixed:not(.rt-fast)");
await chip().waitFor();
await chip().evaluate((x) => x.scrollIntoView({ block: "center" })); // by the test, as the reader would
await page.waitForTimeout(200);
const at = () => page.evaluate(() => [...document.querySelectorAll("*")].filter((e) => e.scrollTop && !e.closest("#pop")).map((e) => [e.id || e.className, e.scrollTop]).join(";") + "|" + Math.round(document.querySelector(".rt-gedit").getBoundingClientRect().top));
const before = await at();
const pair = async (from, to) => {
await chip().click();
await option(page, w.map).click();
// every level that can be asked
await option(page, from).waitFor();
assert.deepEqual(await page.locator("#pop #list li:not(.group) .v").allTextContents(), ["none", "minimal", "low", "medium", "high", "xhigh", "max"]);
await option(page, from).click();
// then as asked, or the model's own others
await option(page, w.asked).waitFor();
assert.deepEqual(await page.locator("#pop #list li:not(.group) .v").allTextContents(), [w.asked, ...["low", "medium", "high", "xhigh"].filter((v) => v !== from)]);
await option(page, to).click();
await shut(page);
};
await pair("high", "xhigh");
await pair("low", "medium");
await pair("max", "xhigh");
await pair("max", w.asked); // back to as asked
assert.equal((await chip().textContent()).trim(), w.mapped);
assert.match(await chip().getAttribute("class"), /\bon\b/);
if (lang === "en") assert.equal(await chip().getAttribute("title"), w.title);
assert((await chip().getAttribute("title")).includes("high → xhigh"));
assert.equal(await at(), before, "the picks moved nothing");
// the first menu has "map" as what it is now; the next menu says each pair
await chip().click();
await option(page, w.map).waitFor();
assert.match(await option(page, w.map).getAttribute("class"), /\bcur\b/);
await option(page, w.map).click();
await option(page, "high").waitFor();
assert.equal((await option(page, "high").locator(".n, .note").first().textContent()).trim(), "→ xhigh");
await page.keyboard.press("Escape");
await shut(page);
const fits = await page.evaluate(() => document.documentElement.scrollWidth <= document.documentElement.clientWidth);
assert(fits, "no sideways scroll");
const missing = await page.evaluate((keys) => keys.filter((k) => !I18N.zh[k] || !I18N["zh-TW"][k] || !I18N.ja[k] || !I18N.de[k]), keys);
assert.deepEqual(missing, [], "every string has each language");
await save(page, w);
const sent = posts.find((p) => p.path === "/api/groups/save");
assert.deepEqual(sent.body.members, ["oa/gpt-6.1-sol", "rl/glm-5"]);
assert.deepEqual(sent.body.effortMaps, { "oa/gpt-6.1-sol": { high: "xhigh", low: "medium" } });
assert.deepEqual(errors, []);
});
}
test(`${engine} ${lang}: a fixed level or the group drops a map, and a map frees a fixed one`, async (t) => {
const w = words[lang];
const { page, card, errors, posts } = await open(engine, lang, 1100, ["oa/gpt-6.1-sol", "rl/glm-5:high", "oa/gpt-6.1-sol:low"], t,
{ "oa/gpt-6.1-sol": { high: "xhigh" } });
await card.locator("button", { hasText: w.edit }).click();
const ed = page.locator(".rt-gedit");
const chips = ed.locator("button.rt-fixed:not(.rt-fast)");
await chips.first().waitFor();
assert.equal((await chips.nth(0).textContent()).trim(), w.mapped, "the saved map is read");
// a fixed level drops the map: back on the group, it isn't there
await chips.nth(0).click();
await option(page, w.follow).waitFor();
await page.locator("#pop:not([hidden]) #list li:not(.group)").nth(3).click(); // high
await shut(page);
assert.notEqual((await chips.nth(0).textContent()).trim(), w.mapped);
await chips.nth(0).click();
await option(page, w.follow).click();
await shut(page);
assert.notEqual((await chips.nth(0).textContent()).trim(), w.mapped);
// a mapped one back on the group's own
await chips.nth(0).click();
await option(page, w.map).click();
await option(page, "high").click();
await option(page, "xhigh").click();
await shut(page);
assert.equal((await chips.nth(0).textContent()).trim(), w.mapped);
await chips.nth(0).click();
await option(page, w.follow).click();
await shut(page);
assert.notEqual((await chips.nth(0).textContent()).trim(), w.mapped);
// a member fixed at high, mapped: low to high, and it follows the group
await chips.nth(1).click();
await option(page, w.map).click();
await option(page, "low").click();
await option(page, w.asked).waitFor();
await page.locator("#pop:not([hidden]) #list li:not(.group)").nth(1).click(); // the model's other level, high
await shut(page);
assert.equal((await chips.nth(1).textContent()).trim(), w.mapped);
await save(page, w);
const sent = posts.find((p) => p.path === "/api/groups/save");
assert.deepEqual(sent.body.members, ["oa/gpt-6.1-sol", "rl/glm-5", "oa/gpt-6.1-sol:low"]);
assert.deepEqual(sent.body.effortMaps, { "rl/glm-5": { low: "high" } });
assert.deepEqual(errors, []);
});
}
}