qoder 0.2.59: usage carries each pool's count and the plan's refresh, as Qoder's own panel shows them (jzing.draw)

jzing.draw on Discord (#feedback, 2026-11-03): Qoder's 我的用量 said
套餐内 Credits 1,817 / 2,000 (剩余 183, refreshing 2026-11-22) and
资源包 2,400 / 3,400 (剩余 1,000). magpie's Routing row said a bare
"剩余 15%", which matched neither pool, so it read as the two not added up.

They were added up already (Total credits since 0.2.44, #454), but every
window sent only a share and a display string. The Routing row shows a
count only for a window with limit > 0, so it had no way to say which
figure it was. Now:

- every pool window and Total credits carry amount, limit and unit, so
  magpie says "1,183 / 5,400 credits · 22% left" on the Routing row and
  the Usage card, which is Qoder's 183 + 1,000.
- the plan's Credits window carries resetsAt = expiresAt, Qoder's
  "将于 … 刷新" date (renewsOn in Qoder 0.4.3's buildUsageItemGroups).

Routing still counts the pools together and sets each pool aside; that
didn't change.

Run (mote, bun 1.4.2): bun test packages/qoder: 382 pass, 0 fail.
Broken by hand: with amount/limit/unit and resetsAt dropped from the
pool windows, the new test ("the plan and the resource pack read as
Qoder's own panel does…") fails.

Not tried: no real Qoder account. The fixture is built from the
reporter's screenshot numbers. The reporter's "15%" (85% used) isn't
reproduced from those numbers: the two pools together leave 22%. It may
be a reading taken later than the screenshot, or a dedicated resource
package Qoder's panel didn't show (the plugin counts those). Without
their reading this can't be told apart.
This commit is contained in:
yetone
2026-11-03 16:36:34 +08:00
parent 4586f8623b
commit 1984cf65dd
5 changed files with 55 additions and 19 deletions
+1 -1
View File
@@ -289,7 +289,7 @@ test("usage is read from openapi.qoder.com.cn with the device token", async () =
}
const s = store(signedIn())
const hooks = await QoderCNAuthPlugin({ client: s.client })
expect(await hooks.auth.usage(s.get)).toEqual({ plan: "pro", windows: [{ name: "Credits", used: 40, display: "40 / 100 credits" }], signIn: "kept" })
expect(await hooks.auth.usage(s.get)).toEqual({ plan: "pro", windows: [{ name: "Credits", used: 40, display: "40 / 100 credits", amount: 40, limit: 100, unit: "credits" }], signIn: "kept" })
})
test("the config declares qoder-cn on its gateway, with the tiers Qoder CN's CLI names", async () => {
+6 -2
View File
@@ -2692,7 +2692,11 @@ function parseUsage(env) {
const used = b.used != null ? b.used : total - b.remaining
if (used < 0) return
const unit = b.unit || "credits"
const w = { name: b.name || name, used: Math.min(100, (100 * used) / total), display: `${gfmt(used)} / ${gfmt(total)} ${unit}` }
// the count too, so magpie says "183 / 2,000 credits left" as Qoder's
// own panel does, not a bare share (jzing.draw on Discord); the plan's
// credits refresh when the plan says, as the panel's "refreshes on" does
const w = { name: b.name || name, used: Math.min(100, (100 * used) / total), display: `${gfmt(used)} / ${gfmt(total)} ${unit}`, amount: used, limit: total, unit }
if (name === "Credits" && until) w.resetsAt = until
out.windows.push(w)
if (SPENT_IN_TURN.has(name)) turn.push({ w, used: Math.min(used, total), total, unit })
}
@@ -2705,7 +2709,7 @@ function parseUsage(env) {
const used = turn.reduce((a, p) => a + p.used, 0)
const total = turn.reduce((a, p) => a + p.total, 0)
for (const p of turn) p.w.aside = true
out.windows.unshift({ name: "Total credits", used: Math.min(100, (100 * used) / total), display: `${gfmt(used)} / ${gfmt(total)} ${turn[0].unit}` })
out.windows.unshift({ name: "Total credits", used: Math.min(100, (100 * used) / total), display: `${gfmt(used)} / ${gfmt(total)} ${turn[0].unit}`, amount: used, limit: total, unit: turn[0].unit })
}
const dedicated = field("dedicatedResourcePackages", "dedicated_resource_packages")
for (const b of Array.isArray(dedicated) ? dedicated : []) add(b, "Dedicated credits")
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-qoder-auth",
"version": "0.2.58",
"version": "0.2.59",
"description": "Qoder and QwenWork (千问办公) subscriptions in OpenCode and magpie: qoder.com, qoder.cn, gateway.qwenwork.cn and the enterprise flagship, VPC and dedicated domains",
"type": "module",
"main": "./opencode.mjs",
+2 -2
View File
@@ -255,7 +255,7 @@ test("qoder: lapsed job refresh re-exchanges the stored PAT on the real path", a
expect(usage).toBe("openapi.qoder.sh/sash/api/v2/me/usage")
expect(out).toEqual({
plan: "pro",
windows: [{ name: "Credits", used: 10, display: "10 / 100 credits" }],
windows: [{ name: "Credits", used: 10, display: "10 / 100 credits", amount: 10, limit: 100, unit: "credits" }],
signIn: "renewed",
})
// the saved credential is the renewed one, still carrying the PAT
@@ -295,7 +295,7 @@ test("qoder: PAT account usage reads the usage endpoint with the job token", asy
expect(auth).toBe("Bearer jt-live")
expect(out).toEqual({
plan: "pro",
windows: [{ name: "Credits", used: 25, display: "50 / 200 credits" }],
windows: [{ name: "Credits", used: 25, display: "50 / 200 credits", amount: 50, limit: 200, unit: "credits" }],
signIn: "kept",
})
})
+45 -13
View File
@@ -60,11 +60,11 @@ test("a refused device token is rotated, saved, and the usage read with it", asy
expect(await p.usage()).toEqual({
plan: "pro",
windows: [
{ name: "Total credits", used: (100 * 45) / 170, display: "45 / 170 credits" },
{ name: "Credits", used: 25, display: "25 / 100 credits", aside: true },
{ name: "Add-on credits", used: 20, display: "10 / 50 credits", aside: true },
{ name: "Shared credits", used: 50, display: "10 / 20 credits", aside: true },
{ name: "Team", used: 20, display: "2 / 10 credits" },
{ name: "Total credits", used: (100 * 45) / 170, display: "45 / 170 credits", amount: 45, limit: 170, unit: "credits" },
{ name: "Credits", used: 25, display: "25 / 100 credits", amount: 25, limit: 100, unit: "credits", aside: true },
{ name: "Add-on credits", used: 20, display: "10 / 50 credits", amount: 10, limit: 50, unit: "credits", aside: true },
{ name: "Shared credits", used: 50, display: "10 / 20 credits", amount: 10, limit: 20, unit: "credits", aside: true },
{ name: "Team", used: 20, display: "2 / 10 credits", amount: 2, limit: 10, unit: "credits" },
],
signIn: "kept",
})
@@ -115,8 +115,8 @@ test("snake_case fields, an expiry, units, and pools too thin to show", () => {
plan: "Pro Trial",
until: "2026-10-01T00:00:00.000Z",
windows: [
{ name: "Credits", used: 75, display: "1.5e+06 / 2e+06 tokens" },
{ name: "Dedicated credits", used: 100, display: "9 / 5 credits" },
{ name: "Credits", used: 75, display: "1.5e+06 / 2e+06 tokens", amount: 1500000, limit: 2000000, unit: "tokens", resetsAt: "2026-10-01T00:00:00.000Z" },
{ name: "Dedicated credits", used: 100, display: "9 / 5 credits", amount: 9, limit: 5, unit: "credits" },
],
})
expect(_internal.when("2026-10-01T08:00:00+08:00")).toBe("2026-10-01T00:00:00.000Z")
@@ -141,15 +141,47 @@ test("a spent plan beside add-on credits left is no account used up (#454)", ()
},
})
expect(u.windows).toEqual([
{ name: "Total credits", used: (100 * 545) / 1400, display: "545 / 1400 credits" },
{ name: "Credits", used: 100, display: "300 / 300 credits", aside: true },
{ name: "Add-on credits", used: (100 * 245) / 1100, display: "245 / 1100 credits", aside: true },
{ name: "Total credits", used: (100 * 545) / 1400, display: "545 / 1400 credits", amount: 545, limit: 1400, unit: "credits" },
{ name: "Credits", used: 100, display: "300 / 300 credits", amount: 300, limit: 300, unit: "credits", aside: true },
{ name: "Add-on credits", used: (100 * 245) / 1100, display: "245 / 1100 credits", amount: 245, limit: 1100, unit: "credits", aside: true },
])
// what routing counts: the windows not set aside
const counted = u.windows.filter((w) => !w.aside)
expect(counted.map((w) => Math.round(100 - w.used))).toEqual([61])
})
// jzing.draw on Discord (2026-11-03): Qoder's own 我的用量 said 套餐内
// Credits 1,817 / 2,000 (剩余 183, refreshing 2026-11-22) and 资源包
// 2,400 / 3,400 (剩余 1,000), while magpie's Routing row said a bare
// "剩余 15%" that matched neither. What the account has left together is
// told with its count, each pool beside it with its own, and the plan's
// with when it refreshes, so every figure Qoder shows is one magpie shows.
test("the plan and the resource pack read as Qoder's own panel does, with counts and the plan's refresh", () => {
const u = _internal.parseUsage({
displayMode: "qoder",
qoderUsage: {
userType: "personal_professional",
expiresAt: 1795276800000, // 2026-11-22 00:00 +08:00
isQuotaExceeded: false,
userQuota: { total: 2000, used: 1817, remaining: 183, percentage: 0.9085, unit: "credits" },
addOnQuota: { total: 3400, used: 2400, remaining: 1000, percentage: 0.7059, unit: "credits" },
},
})
expect(u.plan).toBe("personal_professional")
expect(u.until).toBe("2026-11-21T16:00:00.000Z")
expect(u.windows).toEqual([
{ name: "Total credits", used: (100 * 4217) / 5400, display: "4217 / 5400 credits", amount: 4217, limit: 5400, unit: "credits" },
{ name: "Credits", used: (100 * 1817) / 2000, display: "1817 / 2000 credits", amount: 1817, limit: 2000, unit: "credits", aside: true, resetsAt: "2026-11-21T16:00:00.000Z" },
{ name: "Add-on credits", used: (100 * 2400) / 3400, display: "2400 / 3400 credits", amount: 2400, limit: 3400, unit: "credits", aside: true },
])
// what Qoder's panel says is left, pool by pool, is what magpie's counts leave
const left = (w) => w.limit - w.amount
expect(u.windows.map(left)).toEqual([1183, 183, 1000])
// routing counts the two together: 22% left, not the plan's 9%
const counted = u.windows.filter((w) => !w.aside)
expect(counted.map((w) => Math.round(100 - w.used))).toEqual([22])
})
test("one pool alone is shown as before, with nothing set aside", () => {
// the owner's account (2026-10-28, read-only): a plan of 0 and add-on credits
const u = _internal.parseUsage({
@@ -161,7 +193,7 @@ test("one pool alone is shown as before, with nothing set aside", () => {
addOnQuota: { total: 600, used: 7, remaining: 593, percentage: 0.02, unit: "credits" },
},
})
expect(u.windows).toEqual([{ name: "Add-on credits", used: (100 * 7) / 600, display: "7 / 600 credits" }])
expect(u.windows).toEqual([{ name: "Add-on credits", used: (100 * 7) / 600, display: "7 / 600 credits", amount: 7, limit: 600, unit: "credits" }])
})
test("pools in different units are not added up", () => {
@@ -170,8 +202,8 @@ test("pools in different units are not added up", () => {
qoderUsage: { userQuota: { total: 100, used: 100, unit: "tokens" }, addOnQuota: { total: 50, used: 0, unit: "credits" } },
})
expect(u.windows).toEqual([
{ name: "Credits", used: 100, display: "100 / 100 tokens" },
{ name: "Add-on credits", used: 0, display: "0 / 50 credits" },
{ name: "Credits", used: 100, display: "100 / 100 tokens", amount: 100, limit: 100, unit: "tokens" },
{ name: "Add-on credits", used: 0, display: "0 / 50 credits", amount: 0, limit: 50, unit: "credits" },
])
})