workbuddy 0.1.19: an enterprise account's card shows its credits, from the enterprise meter WorkBuddy's usage page reads (magpie #1904, CnsMaple)

An enterprise (team) account's /billing/meter/get-user-resource-summary
answers with an empty Packages list, so the card had no window and no
error ("暂无额度数据"). WorkBuddy's own usage page reads
/billing/meter/get-enterprise-user-usage for such an account, which
answers credit / limitNum and the cycle's start and reset (UTC+8).

auth.usage now asks the enterprise meter when the summary gives no
credits, and shows a Credits window of credit used of limitNum, resetting
at cycleResetTime, with span from cycleStartTime; plan "Enterprise" when
the sign-in has none. A personal account's refusal of that meter (400
10001 "uid or enterpriseID is empty", seen on a real WorkBuddy AI account)
leaves the card as the summary said. An account with packages never asks
it.

Run: HOME=$(mktemp -d) bun test (1172 pass), bun scripts/check.mjs.
The new enterprise test uses the reporter's two answers byte for byte;
with the fallback turned off it fails (no window, plan Free). A real
personal WorkBuddy AI account's usage read through this index.mjs is
unchanged (same window, no enterprise request). Not tried with a real
enterprise account: none here.
This commit is contained in:
yetone
2026-10-13 11:39:45 +08:00
parent 7b09c423c8
commit 169e4e607c
4 changed files with 107 additions and 2 deletions
+69
View File
@@ -309,3 +309,72 @@ test("a detail list that can't be read leaves the card as it was", async () => {
expect(u.error).toBeUndefined()
expect(u.windows[0]).toEqual({ name: "Credits", used: 100 * 17.6 / 2250, display: "17.6 / 2250", amount: 17.6, limit: 2250, unit: "credits" })
})
// magpie #1904 (CnsMaple): an enterprise account's resource summary has no
// packages, and its credits are the enterprise meter's, which WorkBuddy's
// own usage page reads. The two answers below are the reporter's, byte for
// byte, from one token at one moment.
const ENT_SUMMARY = `{
"code": 0,
"msg": "OK",
"data": {
"Packages": [],
"SubscriptionPackageCode": "",
"SubscriptionPackageName": "",
"IsPaidUser": false,
"IsProtectedPriceUser": false
}
}`
const ENT_USAGE = `{
"code": 0,
"msg": "OK",
"data": {
"credit": 196.13,
"cycleStartTime": "2026-10-08 00:00:00",
"cycleEndTime": "2026-11-08 23:59:59",
"limitNum": 2000,
"cycleResetTime": "2026-11-09 00:00:00"
}
}`
// and a personal account's answer to the enterprise meter, from a real
// WorkBuddy AI Pro account on 2026-10-13
const PERSONAL_ENT = `{"code":10001,"msg":"uid or enterpriseID is empty","requestId":"36dd0f64-cb93-41e1-a964-9e3c87fea235"}`
test("an enterprise account's credits are the enterprise meter's, as WorkBuddy's usage page reads them (magpie #1904)", async () => {
const calls = serve({
"/billing/meter/get-user-resource-summary": () => new Response(ENT_SUMMARY),
"/billing/meter/get-enterprise-user-usage": () => new Response(ENT_USAGE),
})
const auth = { type: "oauth", access: "ent-access", refresh: "r", expires: later(), uid: "u9", domain: "www.workbuddy.cn", enterpriseId: "ent-1" }
expect(await usageOf(WorkBuddyAuthPlugin, auth)).toEqual({ signIn: "kept", plan: "Enterprise", windows: [{
name: "Credits", used: (100 * 196.13) / 2000, display: "196.13 / 2000", amount: 196.13, limit: 2000, unit: "credits",
resetsAt: "2026-11-08T16:00:00.000Z", span: 32 * 86400 }] })
const ent = calls.find((c) => c.url.pathname === "/billing/meter/get-enterprise-user-usage")
expect(ent.init.method).toBe("POST")
expect(ent.init.body).toBe("{}")
expect(Object.fromEntries(["authorization", "x-user-id", "x-domain", "x-enterprise-id"].map((h) => [h, ent.headers.get(h)])))
.toEqual({ authorization: "Bearer ent-access", "x-user-id": "u9", "x-domain": "www.workbuddy.cn", "x-enterprise-id": "ent-1" })
})
test("a personal account with no credits isn't given the enterprise's, and its refusal isn't the card's error", async () => {
serve({
"/billing/meter/get-user-resource-summary": () => new Response(ENT_SUMMARY),
"/billing/meter/get-enterprise-user-usage": () => new Response(PERSONAL_ENT, { status: 400 }),
})
const auth = { type: "oauth", access: "a", refresh: "r", expires: later(), uid: "u" }
expect(await usageOf(WorkBuddyAuthPlugin, auth)).toEqual({ signIn: "kept", plan: "Free", windows: [] })
})
test("a personal account with credits never asks the enterprise meter", async () => {
const calls = serve({ "/billing/meter/get-user-resource-summary": () => ok(PAID) })
const auth = { type: "oauth", access: "a", refresh: "r", expires: later(), uid: "u" }
expect((await usageOf(WorkBuddyAuthPlugin, auth)).windows[0].display).toBe("2500 / 10000")
expect(calls.some((c) => c.url.pathname === "/billing/meter/get-enterprise-user-usage")).toBe(false)
})
test("enterpriseUsageOf: no limit, no window; times it can't read are left out", () => {
expect(_internal.enterpriseUsageOf({ credit: 3, limitNum: 0 })).toBe(null)
expect(_internal.enterpriseUsageOf(null)).toBe(null)
expect(_internal.enterpriseUsageOf({ credit: "12.5", limitNum: "100", cycleResetTime: "soon" }))
.toEqual({ name: "Credits", used: 12.5, display: "12.5 / 100", amount: 12.5, limit: 100, unit: "credits" })
})