Files
magpie/internal/gui/tests/routing-credits.test.cjs
T
yetone 9ce59b8340 routing: a request's row says the credits its reply said it cost (jiabubu)
jiabubu on Discord (#feedback): the Routing page's request list showed no
credits for a request that spent them. The gateway already read a reply's
usage.credit (#2512) into the usage ledger, which the Usage page shows,
but the route trace's RouteUsage had no field for it.

RouteUsage now carries Credits (the reply's, nil when it said none). The
Routing page's request row shows "N credits" after its cost, or alone
when the request has no known price (it read "—" before), with the Usage
page's help text under the pointer; By session sums them in the session's
cost, shown even when no request in it was priced. The help text "What it
cost in its plan's credits, as the reply said" moves from the Usage
plugin's i18n.js to the core's, since two plugins say it now.

Tests: TestPluginPlanCreditsReachTheLedger also checks the route's usage
carries 0.01 on every endpoint (red without the field: Credits <nil>).
routing-credits.test.cjs: credits alone, priced plus credits, none, and
a session's sum, in chromium and webkit, en zh zh-TW ja de, at 1100 and
420, with no overflow.

Broken by hand: the old client.js makes routing-credits 10 of 10 red.

Run at the pushed head on mote: gofmt -l clean; go test -run 'TestDailyCredits|TestPluginPlanCredits|Credits' ./internal/provider ./internal/gateway ok; GOOS=linux go vet -tags nogui and GOOS=windows go vet of provider, gateway, gui ok (rebased on origin/main b92b3bc5b). The full GUI suite (WSL, both engines, every language) ran at the earlier base 50265256b; its 58 files red on head but not in main's run were run again, and the 11 files still in question were run on head and on origin/main b92b3bc5b side by side: routing-credits and i18n-keys pass, and every other red is a test main fails too, with an open issue (#4489 usage-ledger-ttft, #4719 usage-speed, #3593/#4560/#4585 routing-rules-line, #4713 routing-time-went, #2559/#4966 routing-bodies, #4400 sessions-bar-names, #1745 group-member-info, #5052 account-eye); head shows a few more languages of those same tests.

Not tried: a real WorkBuddy request through the GUI; history written
before this release has no credits in its routes and reads as before.
2026-11-04 01:27:06 +08:00

115 lines
7.7 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// A request whose reply said what it cost in its plan's credits (WorkBuddy's
// usage.credit) says them on its Routing row, as the Usage page's ledger
// does, and its session sums them (jiabubu on Discord: "路由页面的请求列表,
// 消耗积分的请求没有显示积分消耗量"). A request with no known price shows
// its credits alone; one priced shows both; one that said none reads as
// before. In every language, at 1100 and 420. No backend: 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");
function fixture() {
const now = Date.now(), at = (s) => new Date(now - s * 1000).toISOString();
const seat = { id: "workbuddy", provider: "workbuddy", name: "WorkBuddy", who: "jiabubu", kind: "account", model: "glm-5.1" };
const tier = (credits) => ({ provider: "workbuddy", model: "glm-5.1", in: 9, out: 12, ...(credits != null ? { credits } : {}) });
const req = (id, session, ago, usage, extra = {}) => ({ id, seq: id, time: at(ago), agent: "claude", session,
model: "workbuddy/glm-5.1", provider: "workbuddy", order: [seat], tries: [{ id: seat.id, model: "glm-5.1", start: at(ago), done: true, status: 200, ms: 500 }],
done: true, status: 200, ms: 500, tokens: 21, usage, priced: false, cost: 0, ...extra });
return [
req(1, "s-credits", 4, [tier(1.5)]),
req(2, "s-credits", 3, [tier(0.25)]),
req(3, "s-priced", 2, [tier(2)], { priced: true, cost: 0.01 }),
req(4, "s-none", 1, [tier()]),
];
}
function serve(lang, routes) {
return async (route) => {
const url = new URL(route.request().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({ agents: [{ id: "claude", name: "Claude Code", fields: [] }], profiles: [], settings: { lang, theme: "light" } });
if (url.pathname === "/api/gateway/trace") {
if (url.searchParams.has("wait")) return new Promise(() => {});
return json({ mine: true, now: new Date().toISOString(), seq: 4, totals: { requests: 4, rerouted: 0, errors: 0 }, routes });
}
if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] });
if (url.pathname === "/api/gateway/session-titles") return json({ titles: {} });
if (url.pathname === "/api/groups") return json({ groups: [] });
if (url.pathname === "/api/providers") return json({ providers: [], gateway: { running: true, window: true } });
if (url.pathname === "/api/usage/quotas") return json([]);
if (url.pathname === "/api/window/fit") return route.fulfill({ status: 204 });
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)];
return route.fulfill({ body: await fs.readFile(file), contentType });
};
}
// "{n} credits" and the help under the pointer, as i18n.js says them
const words = {
en: { n: (n) => `${n} credits`, one: /^What it cost in its plan's credits, as the reply said$/, some: /What 2 of these requests cost in their plan's credits/ },
zh: { n: (n) => `${n} 积分`, one: /^按回复所说,这次请求消耗的套餐积分$/, some: /其中 2 次请求/ },
"zh-TW": { n: (n) => `${n} 積分`, one: /^依回覆所述,這次請求消耗的方案積分$/, some: /其中 2 次請求/ },
ja: { n: (n) => `${n} クレジット`, one: /^このリクエストが消費したプランのクレジット/, some: /うち 2 件/ },
de: { n: (n) => `${n} Credits`, one: /^Was die Anfrage laut Antwort an Credits des Tarifs gekostet hat$/, some: /Was 2 dieser Anfragen/ },
};
const num = (lang, v) => v.toLocaleString(lang);
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]) {
test(`${engine} ${lang} ${width}: routing says the credits a reply said it cost (jiabubu)`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const w = words[lang], errors = [];
const page = await browser.newPage({ viewport: { width, height: 900 }, reducedMotion: "reduce" });
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang, fixture()));
await page.goto("http://magpie.test/?view=routing");
const rows = page.locator(".rt-req");
await rows.nth(3).waitFor();
// newest first: none said, priced and said, then the two credited alone
const cost = (i) => rows.nth(i).locator(".cost-metric");
assert.equal(await cost(0).count(), 0, "a reply that said no credits and has no price shows no cost, as before");
assert.equal(await cost(1).locator(".v").textContent(), `≈$0.010 · ${w.n(num(lang, 2))}`);
assert.equal(await cost(2).locator(".v").textContent(), w.n(num(lang, 0.25)), "no known price: the credits alone, never a dash");
assert.equal(await cost(3).locator(".v").textContent(), w.n(num(lang, 1.5)));
assert.match(await cost(3).getAttribute("title"), w.one);
assert.equal((await cost(1).getAttribute("title")).split("\n").length, 2, "a priced row says both what the price is and what the credits are");
// By session: a session sums its credits, with or without a price
await page.evaluate(() => localStorage.setItem("magpie.routingBySession", "1"));
await page.reload();
const group = (s) => page.locator("button.rt-session").filter({ has: page.locator(`.nm[title$="${s}"]`) });
await group("s-credits").waitFor();
const sum = group("s-credits").locator(".cost");
assert.equal(await sum.isVisible(), true, "a session of credits alone shows them");
assert.equal(await sum.textContent(), w.n(num(lang, 1.75)));
assert.match(await sum.getAttribute("title"), w.some);
assert.equal(await group("s-priced").locator(".cost").textContent(), `≈$0.010 · ${w.n(num(lang, 2))}`);
assert.equal(await group("s-none").locator(".cost").isVisible(), false, "a session that said none reads as before");
const fit = await page.evaluate(() => ({ client: document.documentElement.clientWidth, scroll: document.documentElement.scrollWidth }));
assert(fit.scroll <= fit.client + 1, `the page overflows at ${width}: ${JSON.stringify(fit)}`);
const reqs = await page.locator(".rt-reqs").evaluate((e) => ({ client: e.clientWidth, scroll: e.scrollWidth }));
assert(reqs.scroll <= reqs.client + 1, `request list overflows at ${width}: ${JSON.stringify(reqs)}`);
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}-routing-credits.png`), fullPage: true });
}
assert.deepEqual(errors, []);
});
}
}
}