Files
magpie/internal/gui/tests/credits-run-out.test.cjs
T
yetone 6f1b432759 provider, gui: smart routing spends WorkBuddy credits that expire soonest first (cdredfox on X)
WorkBuddy's Credits window had no reset, so in a Smart group every
WorkBuddy account ranked alike. workbuddy plugin 0.1.14 (plugins f0274d6)
reads WorkBuddy's own package lists (billing/meter/get-user-resource-paid-
packages and -free-packages, the calls WorkBuddy's client makes) and reports
on the Credits window when the soonest-ending package that still has credits
ends (runsOutAt) and how many credits it holds (runsOut). The daily Free
Plan slice (CapacityType 4) and code 001 aren't counted: they renew daily.

magpie carries runsOutAt from the plugin's usage window into QuotaWindow and
the routing Limit. Allowance.Renewal, which only Smart ranks on, treats it
as the window's reset when it is sooner than the real one. Pace, the tiers,
For, Full and resting are unchanged. The mover's min is raised to 0.1.14 so
moved users get the plugin. The usage card and account meter say "232.4
expire in 5h" in every language.

Run:
- plugin: bun test, 63 pass; 2 of the 3 new tests fail on 0.1.13's
  index.mjs. Verified live on a workbuddy-ai account in a sandbox HOME:
  Bonus Pack 232.4 left ending 10-12T16:39Z ranked before Pro (10-28).
- TestWorkBuddyCreditsRunOutForRouting and
  TestSmartSpendsWorkBuddyCreditsRunningOutFirst: both fail with the
  Renewal change removed by hand.
- GUI: credits-run-out.test.cjs in webkit and chromium, 5 languages, at
  360/440/1000px, red on the old app.js; with i18n-keys, quota-*,
  panel-usage, reset-each-title, usage-fold: 117 pass.
- go test ./... green (agent and gateway rerun with -timeout 30m after the
  default 10m ran out under load from other sessions; 378s and 492s).
- gofmt -l clean; GOOS=linux and windows go vet of provider, plugin,
  gateway clean.

Not tried: the CN workbuddy site (its token was stale, 401), so the CN
package list is assumed to have the same shape. The built-in WorkBuddy
(not moved users) doesn't get this. The account-line tooltip has no GUI
test of its own.
2026-10-11 20:07:19 +08:00

89 lines
5.7 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// cdredfox on X: WorkBuddy's credits come in packages that each end on
// their own day, and the Credits window has no reset. The plugin says when
// the soonest credits left run out (runsOutAt, runsOut), and the Usage
// card says so under the window, "232.4 credits expire tomorrow 00:39 ·
// in 21h", in every language, at a phone's width too, and in its tooltip.
// A window without it, or whose credits ran out already, says nothing of
// it.
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 soon = (h) => new Date(Date.now() + h * 3600e3).toISOString();
const credits = (runsOutAt) => ({ name: "Credits", used: 100 * 17.6 / 2250, display: "17.6 / 2250", amount: 17.6, limit: 2250, unit: "credits", ...(runsOutAt ? { runsOutAt, runsOut: 232.4 } : {}) });
const quotas = [
{ provider: "workbuddy-ai", name: "WorkBuddy AI", icon: "workbuddy-color", plan: "Pro", user: "a@example.com", windows: [credits(soon(30))] },
{ provider: "workbuddy-ai", name: "WorkBuddy AI", icon: "workbuddy-color", plan: "Pro", user: "b@example.com", windows: [credits("")] },
{ provider: "workbuddy-ai", name: "WorkBuddy AI", icon: "workbuddy-color", plan: "Pro", user: "c@example.com", windows: [credits(soon(-1))] },
];
function serve(lang) {
const settings = { lang, theme: "light", currency: "usd", chineseUnits: lang.startsWith("zh") };
const state = { agents: [], profiles: [], settings };
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/settings") return json(settings);
if (url.pathname === "/api/gateway/trace") return url.searchParams.get("wait") ? new Promise(() => {}) : json({ mine: true, now: new Date().toISOString(), seq: 0, totals: {}, routes: [] });
if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] });
if (url.pathname === "/api/groups") return json({ models: [], groups: [], pools: [] });
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 === "/api/usage/quotas") return json(quotas);
if (url.pathname === "/api/plugins") return json({ plugins: [] });
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)];
try { await r.fulfill({ body: await fs.readFile(file), contentType }); } catch { await r.fulfill({ status: 404, body: "" }); }
};
}
const want = {
en: /^232\.4 credits expire .+ · in (29|30)h$/,
zh: /^232\.4 积分 将于 .+ 过期 · (29|30) 小时后$/,
"zh-TW": /^232\.4 積分 將於 .+ 過期 · (29|30) 小時後$/,
ja: /^232\.4 クレジットは .+ に失効 · (29|30) ?時間後$/,
de: /^232\.4 Credits verfallen .+ · in (29|30) Std\.?$/,
};
for (const [name, engine] of [["webkit", webkit], ["chromium", chromium]]) {
test(`${name}: credits that run out say when, under their window, at 360 and 440px in every language`, async () => {
const browser = await engine.launch();
try {
for (const lang of Object.keys(want)) {
for (const width of [360, 440, 1000]) {
const page = await (await browser.newContext({ viewport: { width, height: 900 }, reducedMotion: "reduce" })).newPage();
page.setDefaultTimeout(5000);
await page.route("**/*", serve(lang));
await page.goto("http://magpie.test/?view=usage");
await page.locator(".subscription-card .quota").nth(2).waitFor();
const got = await page.evaluate(() => [...document.querySelectorAll(".subscription-card .quota")].map((q) => {
const card = q.closest(".subscription-card").getBoundingClientRect();
const r = q.querySelector(".quota-reset");
const b = r?.getBoundingClientRect();
return { text: r?.textContent.replace(/\s+/g, " ").trim() ?? "", title: q.title,
inCard: !b || (b.left >= card.left - 1 && b.right <= card.right + 1),
cut: !!r && [...r.children].some((s) => s.scrollWidth > s.clientWidth + 1) };
}).concat([{ pageScroll: document.documentElement.scrollWidth > document.documentElement.clientWidth + 1 }]));
const at = `${lang} ${width}px`;
const { pageScroll } = got.pop();
assert.equal(got.length, 3, at);
const [a, b, c] = got;
assert.match(a.text, want[lang], at);
assert.ok(a.title.includes("232.4"), `${at}: tooltip ${a.title}`);
assert.ok(a.inCard && !a.cut, `${at}: "${a.text}" is cut or past its card`);
assert.equal(b.text, "", `${at}: not known says nothing`);
assert.equal(c.text, "", `${at}: run out already says nothing`);
assert.equal(pageScroll, false, `${at}: the page scrolls sideways`);
await page.close();
}
}
} finally { await browser.close(); }
});
}