Files
magpie/internal/gui/tests/routing-key-provider.test.cjs
T
yetone efcf8d9c0c routing: a key's row is named by its provider, the key after it (iamleehom on X)
iamleehom on X: "为什么路由中不显示供应商名称了?" Two OpenAI-compatible
providers, each with unnamed keys, showed only "sk-4…ab10" and
"sk-a…eb0c" on the Routing stage, with nothing saying which provider
each belongs to.

Cause: 83545a5d8's twin rule added the provider's name only when two
providers' keys had the same name. A masked key always differs from
every other one, so the rule never fired for unnamed keys, and a named
key unique across providers lost its provider too.

Change (plugins/routing/client.js, routing.css):
- A key's stage row is headed by a new span.prov with its provider's
  name. The key's name follows in span.who, muted, and gives up room
  first (flex-shrink stays on .who). .who keeps the key's name, so every
  test and lookup that finds a row by it is unchanged.
- The twin rule's sub-line is no longer needed for key rows; the plan
  name stays there.
- The answered card says "Provider · key" (where()).
- Rows inside a provider's fold are unchanged: the fold heading already
  names the provider once.

Not changed, filed: the request list rows and the decision lines still
name an unnamed key by its masked key alone (#6982).

Tests:
- routing-key-provider.test.cjs (new): the reporter's case, two
  providers with unnamed keys. Rows read [Relay East, sk-4…ab10] and
  [Relay West, sk-a…eb0c], the provider's name is not cut at 1100px,
  the card reads "Relay East · sk-4…ab10" from a live long-poll, and a
  fold's rows keep only the key under a heading naming the provider.
  Every language, both engines, 1100px and 440px.
- routing-twin-keys.test.cjs: re-derived to [prov, who, sub].

Run:
- Broken by hand: routing-key-provider at base 3f4372cc9's client.js on
  Windows, chromium: every non-fold subtest red (null .prov; the card
  read "sk-4…ab10"). On mica at the first design: 42 red, 20 (folds)
  green.
- Screenshots (zh 1100px chromium, en 440px webkit, de 440px chromium)
  checked by eye: "Relay East sk-4…ab10" on each row, nothing overlaps.
- Windows (xiaomi-gpu, win-run, Node 22, Playwright 1.62.0, Chromium +
  WebKit), at 90aa5bc23 (this change on 3f4372cc9): the 324 GUI files
  the change can reach (every routing-*, i18n-keys, and every file that
  opens view=routing or uses .rt- or plugins/routing): 5911 tests, 5737
  pass, 173 fail, 1 cancelled, ~59 min, beside another session's Go
  builds. The 39 red files run again side by side at base 3f4372cc9 and
  at the change (concurrency 4): 155 vs 161 fail of 774. The ones red
  on only one side are on both sides (WebKit timing, Usage,
  rest-try-now, Codex subagent); none reads a key row except
  routing-many-keys, which alone 3 times is red 1 of 3 at base (#5543)
  and green 3 of 3 with the change.
- After rebasing on origin/main 8e1534f5f, on Windows: routing-key-
  provider, routing-twin-keys, group-member-line, group-member-free,
  i18n-keys, routing-answered-still, routing-agents-apart,
  routing-keys-fold, routing-many-keys, routing-model-chip-whole: 296
  tests, 292 pass, 4 fail. Each red is red the same at origin/main
  3126b0bac on the same box: i18n-keys "a plugin's words are its own"
  (#6877), routing-many-keys chromium en (#5543), and
  routing-model-chip-whole en 1600px live in both engines (#6981, filed
  now).
- mica: go test ./internal/gui/ -run 'Budget|Compress|Revalidate|Plugin'
  ok.

Not run: the whole GUI suite at the pushed head (mote and mica were
busy, and the earlier whole-suite run on Windows was stopped because
its tree was stale); the real app; the reporter's own providers.

Reported-by: iamleehom (X)
2026-11-17 21:15:47 +08:00

129 lines
8.1 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// iamleehom on X ("为什么路由中不显示供应商名称了?"): two OpenAI-compatible
// providers' keys, neither named, served gpt-6-astra under Smart, and the
// stage's rows read "sk-4…ab10" and "sk-a…eb0c": the masked keys where the
// provider's name was looked for, the same OpenAI logo on both. A key's row
// is named by its provider, the key after it, muted; the provider's name
// keeps its room at a narrow width, the key's gives it up; a fold of a
// provider's keys names the provider once, and the keys under it by their
// own names; the answered try's card names the provider too. Every
// language, both engines, a wide and a narrow width; the API is faked.
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 now = new Date();
const ago = (s) => new Date(now.getTime() - s * 1e3).toISOString();
const M = "gpt-6-astra";
const key = (provider, name, k) => ({ id: `${provider}#${k}`, provider, name, icon: "openai", who: k, kind: "key", model: M });
const order = [key("relay-east", "Relay East", "sk-4…ab10"), key("relay-west", "Relay West", "sk-a…eb0c")];
const group = { id: "astra", name: "Astra", routing: "", members: ["relay-east/" + M, "relay-west/" + M] };
const first = { id: 1, seq: 1, time: ago(30), agent: "codex", model: "astra", provider: "relay-east", done: true, status: 200, ms: 900, tokens: 1200, group, order,
tries: [{ id: order[0].id, model: M, start: ago(30), done: true, status: 200, ms: 900 }] };
// one provider's six keys, folded (FOLD_AT) and opened
const many = Array.from({ length: 6 }, (_, i) => key("pool", "Pool", `sk-${i}…000${i}`));
const pooled = { id: 2, seq: 2, time: ago(10), agent: "codex", model: "pool/" + M, provider: "pool", done: true, status: 200, ms: 700, tokens: 900, order: many,
tries: [{ id: many[0].id, model: M, start: ago(10), done: true, status: 200, ms: 700 }] };
// live: routes go out on the long poll as the test sends them
function serve(lang, routes, live) {
return async (request) => {
const url = new URL(request.request().url());
const json = (data) => request.fulfill({ json: data });
if (url.pathname === "/boot.js") return request.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` });
if (url.pathname === "/wails/runtime.js") return request.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/state") return json({ agents: [{ id: "codex", name: "Codex", path: "/test/config.toml", fields: [] }], profiles: [], settings: { lang, theme: "light" } });
if (url.pathname === "/api/gateway/trace") {
if (url.searchParams.get("wait")) {
if (!live) await new Promise((r) => setTimeout(r, 20e3));
else if (live.queued.length) return json(live.queued.shift());
else { live.pending.push(request); return; }
}
return json({ mine: true, now: now.toISOString(), seq: routes.length, totals: { requests: routes.length, 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: [], pools: [] });
if (url.pathname === "/api/providers") return json({ providers: [], excluded: [], gateway: { running: true, window: true, url: "http://127.0.0.1:3425" } });
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 request.fulfill({ body: await fs.readFile(file).catch(() => ""), contentType });
};
}
async function open(browser, lang, width, routes, live) {
const context = await browser.newContext({ viewport: { width, height: 900 }, reducedMotion: "reduce" });
const page = await context.newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang, routes, live));
await page.goto("http://magpie.test/?view=routing");
return { context, page, errors };
}
const rowsOf = (page) => page.locator(".rt-accts > li:not(.rt-fold):not(.rt-sub)").filter({ has: page.locator("b span.who") });
const read = (rows) => rows.evaluateAll((ls) => ls.map((li) => {
const b = li.querySelector("b"), prov = b.querySelector("span.prov"), who = b.querySelector("span.who");
return { prov: prov.textContent, name: who.textContent, provCut: prov.scrollWidth > prov.clientWidth + 1 };
}));
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(`${engine}: a key's row is named by its provider`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) {
for (const width of [1100, 440]) {
await t.test(`${lang} ${width}px: two providers' unnamed keys`, async () => {
const { context, page, errors } = await open(browser, lang, width, [first]);
t.after(() => context.close());
const rows = rowsOf(page);
await rows.nth(order.length - 1).waitFor();
const seen = await read(rows);
assert.deepEqual(seen.map((r) => [r.prov, r.name]), [["Relay East", "sk-4…ab10"], ["Relay West", "sk-a…eb0c"]]);
if (width === 1100) for (const r of seen) assert.equal(r.provCut, false, `${r.prov} is whole`);
assert.equal(await page.evaluate(() => document.documentElement.scrollWidth > innerWidth), false, "nothing runs off the page");
assert.deepEqual(errors, []);
});
await t.test(`${lang} ${width}px: the answer's card names the provider`, async () => {
const live = { pending: [], queued: [], seq: 0 };
const { context, page, errors } = await open(browser, lang, width, [], live);
t.after(() => context.close());
const send = (route) => {
const d = { mine: true, now: new Date().toISOString(), seq: ++live.seq, totals: { requests: live.seq, rerouted: 0, errors: 0 }, routes: [route] };
const r = live.pending.shift();
if (r) r.fulfill({ json: d }); else live.queued.push(d);
};
await page.locator(".rt-cap").waitFor({ state: "attached" });
const going = { ...first, time: new Date().toISOString(), done: false, status: 0, ms: 0, tokens: 0,
tries: [{ ...first.tries[0], start: new Date().toISOString(), done: false, status: 0, ms: 0 }] };
send(going);
await rowsOf(page).nth(order.length - 1).waitFor();
send({ ...going, done: true, status: 200, ms: 900, tokens: 1200, tries: [{ ...going.tries[0], done: true, status: 200, ms: 900 }] });
const who = page.locator(".rt-cap .rt-done > .who");
await who.waitFor();
assert.equal(await who.textContent(), "Relay East · sk-4…ab10");
assert.deepEqual(errors, []);
});
await t.test(`${lang} ${width}px: a fold names the provider once`, async () => {
const { context, page, errors } = await open(browser, lang, width, [pooled]);
t.after(() => context.close());
const fold = page.locator(".rt-accts > li.rt-fold");
await fold.waitFor();
assert.equal(await fold.locator("b span.who").textContent(), "Pool");
if ((await fold.getAttribute("aria-expanded")) !== "true") await fold.click();
const rows = rowsOf(page);
await rows.nth(many.length - 1).waitFor();
const seen = await read(rows);
assert.deepEqual(seen.map((r) => [r.prov, r.name]), many.map((k) => ["", k.who]));
assert.deepEqual(errors, []);
});
}
}
});
}