jiezhengj on Discord asked to set the per-group policies once instead of
in each group. The Routing groups page's Settings card, under the list,
now has three rows: Rate limited (keeps its place / goes to the back),
Slow to start (wait / 30 s ... 4 min) and Every member busy (off / 1 min
... 4 min). They are saved in settings.json as groupSink, groupFirstToken
and groupPatience (settings.SetGroupDefaults, POST /api/groups/defaults).
A group follows the default for each value it leaves unset, and its own
value always wins:
- Sinks() is the group's Sink, or else the default unless NoSink, a new
field for an explicit "Keeps its place".
- FirstTokenOr() is the group's own FirstToken (-1, FirstTokenWait, is an
explicit Wait), or else the default. The gateway reads it where it
reads a group's first-token time.
- Waits() reads the default when Patience is 0. 60 is an explicit 1 min.
Groups saved before have 0 and no NoSink, so they follow the default,
which starts at magpie's own (keep its place, wait, 60s). Nothing a user
set changes. The group editor's three rows gain a Default option, on for
a group that sets none, and its hint names the value it follows. Segments
only, with no native select.
Run on mote, at 06b2daeff rebased on origin/main 2dac9b84e:
- go test on the new tests TestGroupDefaultsUnlessItsOwn,
TestGroupFirstTokenFollowsTheDefault, TestGroupSinkFollowsTheDefault
and TestGroupDefaultsSaved, plus TestKeysAreWhatTheSaveWrites and the
existing first-token and sink tests: ok.
- Broken by hand (Waits, FirstTokenOr and Sinks ignoring settings): the
three provider/gateway tests went red.
- GUI: group-defaults (new), group-patience, group-first-token,
group-found-off and rate-limited-sink, in Chromium and WebKit, in en,
zh, zh-TW, ja and de, at 1100 and 420: 56/56 pass. With the old
client.js and routing.css, group-defaults, group-patience and
group-first-token fail 44 of 44.
- GOOS=linux go vet -tags nogui ./... and GOOS=windows go vet ./...:
clean. gofmt -l: clean.
Still running when this was pushed (owner: never hold a verified commit
behind the suite): go test -timeout 45m on internal/provider,
internal/gateway, internal/gui, internal/settings and
internal/settingsapi, then the whole GUI suite under suite.lock. Any red
they show gets its own issue.
Not tried: the real app with real groups under real rate limits. Only
the gateway tests' fakes exercise the default sink and first-token paths.
Reported-by: jiezhengj on Discord
135 lines
8.6 KiB
JavaScript
135 lines
8.6 KiB
JavaScript
// Run with Node's test runner and Playwright on the module path; see README.md.
|
|
// The groups magpie finds on its own (auto-<model>) can be turned off all
|
|
// at once (蓝猫 on Discord: "路由分组会自动创建,可以关闭掉吗"), not only
|
|
// removed one by one. A switch by the routing groups' list, with what it
|
|
// does in words, says whether they are found; clicked off, it posts
|
|
// groups/found {on:false}, the found groups leave the list and the user's
|
|
// stay, and the page says which agent was moved off a found group to its
|
|
// model from one provider. Clicked on again, they are back. Neither click
|
|
// moves the page, and the switch has no left-border accent. In English and
|
|
// Chinese, 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");
|
|
|
|
const assets = path.resolve(__dirname, "../assets");
|
|
const models = [
|
|
{ id: "a/m", name: "m", providerName: "A", icon: "generic" },
|
|
{ id: "b/m", name: "m", providerName: "B", icon: "generic" },
|
|
{ id: "a/x", name: "x", providerName: "A", icon: "generic" },
|
|
];
|
|
const info = (ids) => ids.map((id) => ({ id, ready: true }));
|
|
const found = { id: "auto-m", name: "Model M", members: ["a/m", "b/m"], auto: true, ready: true, memberInfo: info(["a/m", "b/m"]) };
|
|
const mine = { id: "mine", name: "Mine", members: ["a/x", "b/m"], ready: true, memberInfo: info(["a/x", "b/m"]) };
|
|
const groups = (on, moved) => ({ models, pools: [], deciders: [], found: on, groups: on ? [mine, found] : [mine], ...(moved ? { moved } : {}) });
|
|
|
|
const words = {
|
|
en: { label: "Find groups on their own", onHint: "becomes a group of them", offHint: "only the groups you made or changed",
|
|
moved: "Claude Code moved to a/m", back: "Found groups are on" },
|
|
zh: { label: "自动创建路由组", onHint: "会自动成组", offHint: "只列出和提供你新建或改过的组",
|
|
moved: "Claude Code 已改用 a/m", back: "已开启自动创建路由组" },
|
|
};
|
|
|
|
function serve(lang, posts) {
|
|
let on = true;
|
|
const state = { agents: [{ id: "claude", name: "Claude Code", path: "/test/claude", 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(on));
|
|
if (url.pathname === "/api/groups/found") {
|
|
const body = JSON.parse(r.request().postData() || "{}");
|
|
posts.push(body);
|
|
on = body.on;
|
|
return json(groups(on, on ? null : [{ agent: "Claude Code", field: "model", from: "group/auto-m", to: "a/m" }]));
|
|
}
|
|
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 ["en", "zh"]) {
|
|
const w = words[lang];
|
|
test(`${engine} ${lang}: found groups turned off and on`, async (t) => {
|
|
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
|
const page = await (await browser.newContext({ viewport: { width: 1100, height: 900 }, 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}-group-found-off.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));
|
|
await page.goto("http://magpie.test/?view=groups");
|
|
const card = (name) => page.locator(".rt-group", { hasText: name });
|
|
await card("Model M").waitFor();
|
|
const box = page.locator(".rt-gsec .rt-gfound");
|
|
const sw = box.locator("button[role=switch]");
|
|
assert.equal(await sw.getAttribute("aria-label"), w.label);
|
|
assert.equal(await sw.getAttribute("aria-checked"), "true");
|
|
assert((await box.textContent()).includes(w.label));
|
|
assert((await box.textContent()).includes(w.onHint), "it says what found groups are");
|
|
// under the list, in the page's Settings, the names in agents' lists
|
|
// (for every group) after it
|
|
const order = await page.evaluate(() => {
|
|
const s = document.querySelector(".rt-gsec");
|
|
return [...s.children].map((c) => c.className).filter((c) => c !== "rt-goff");
|
|
});
|
|
assert.deepEqual(order.slice(0, 4), ["row-head", "list rt-groups", "row-head rt-gset-head", "list rt-gset"]);
|
|
assert.deepEqual(await page.evaluate(() => [...document.querySelector(".rt-gset").children].map((c) => c.className)), ["rt-gfound", "rt-gnames", "rt-gdef rt-gdef-sink", "rt-gdef rt-gdef-first", "rt-gdef rt-gdef-patience"]);
|
|
const border = await sw.evaluate((e) => { const c = getComputedStyle(e); return c.borderLeftWidth !== c.borderRightWidth || c.borderLeftColor !== c.borderRightColor; });
|
|
assert.equal(border, false, "no left-border accent");
|
|
|
|
await box.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).map((e) => [e.id || e.className, e.scrollTop]).join(";"));
|
|
const before = await at();
|
|
|
|
await sw.click();
|
|
await page.waitForFunction(() => document.querySelector(".rt-gfound button[role=switch]")?.getAttribute("aria-checked") === "false");
|
|
assert.deepEqual(posts, [{ on: false }]);
|
|
await card("Model M").waitFor({ state: "detached" }); // the found group left the list
|
|
assert.equal(await card("Mine").count(), 1, "the user's group stays");
|
|
assert((await box.textContent()).includes(w.offHint), "it says what is listed now");
|
|
await page.waitForFunction((m) => document.querySelector("#status").textContent.includes(m), w.moved);
|
|
assert.equal(await at(), before, "the click moved nothing");
|
|
|
|
await box.locator("button[role=switch]").click();
|
|
await card("Model M").waitFor();
|
|
assert.deepEqual(posts, [{ on: false }, { on: true }]);
|
|
assert.equal(await box.locator("button[role=switch]").getAttribute("aria-checked"), "true");
|
|
await page.waitForFunction((m) => document.querySelector("#status").textContent.includes(m), w.back);
|
|
assert.equal(await at(), before, "nor did the second");
|
|
|
|
const missing = await page.evaluate(() => [
|
|
"Find groups on their own", "Found groups are on", "Found groups are off: only yours are listed and served",
|
|
"A model two or more of your providers serve becomes a group of them (auto/…). Switch it off to list and serve only the groups you made or changed.",
|
|
"Off: only the groups you made or changed are listed and served. An agent set to a found group is moved to its model from one provider, and a request still naming one goes there too.",
|
|
"No group yet. New group makes one of any models you like.",
|
|
].filter((k) => !I18N.zh[k]));
|
|
assert.deepEqual(missing, [], "every string has its Chinese");
|
|
assert.deepEqual(errors, []);
|
|
});
|
|
}
|
|
}
|