raccoon 0.1.9: a grant past expire_at the balance still holds keeps the Credits window (#936)
ULTIMATE215's card (0.1.8) showed one account as "9623 credits" with no bar, beside two accounts with Credits windows (47 / 11,600, 0 / 9,300). queryUsage builds the window only when the unexpired grant bills cover the balance, and skips every grant whose expire_at has passed. The vendor takes an expired grant's remainder some time after expire_at, so for a while the balance (9,623) is more than the unexpired grants (9,600 from 9,000 reward + daily 300s) and the card fell back to balance only. Now the expired grants are counted back in, the most recently expired first, only until they cover the balance. runsOutAt/runsOut still come from unexpired grants only. When even the expired grants cannot make up the balance, the card stays balance-only as before. Run on xiaohuanxiong.com-shaped fixtures (bill rows with expire_at, as in the existing tests and #223's live bills), on the Windows box (win-run at this tree, Bun; mote refused SSH and WSL failed to start): bun test packages/raccoon 51 pass, 0 fail; bun scripts/check.mjs raccoon ok. Broken by hand: with the count-back line removed, "a grant past expire_at the balance still holds keeps the Credits window" goes red. Not tried: a real Raccoon account (none here, and the browser is not signed in to xiaohuanxiong.com). That the vendor's balance lags expiry is inferred from the reporter's numbers, not seen in a raw bills reply; the issue asks the reporter to confirm. Reported-by: ULTIMATE215
This commit is contained in:
@@ -678,18 +678,22 @@ async function queryUsage(token) {
|
||||
const plan = !entitlement ? undefined : proEnable ? (planType && planType !== "none" ? planType : "Pro") : "Free"
|
||||
const expireAt = office?.pro_expired_time || office?.active_plan?.subscription_expired_at || null
|
||||
|
||||
// Sum only what the vendor actually reports as granted in unexpired bills.
|
||||
// Sum only what the vendor actually reports as granted in bills.
|
||||
// If bills don't account for the current balance, report the balance with no window.
|
||||
let grantedFromBills = 0
|
||||
let soonestExpMs = 0
|
||||
let expiringPts = 0
|
||||
const expired = []
|
||||
const now = Date.now()
|
||||
for (const item of Array.isArray(bills?.items) ? bills.items : []) {
|
||||
const pts = Math.floor(Number(item?.points) || 0)
|
||||
if (pts <= 0) continue
|
||||
const expStr = typeof item?.expire_at === "string" ? item.expire_at : ""
|
||||
const expMs = expStr ? Date.parse(expStr) : 0
|
||||
if (expMs > 0 && expMs <= now) continue
|
||||
if (expMs > 0 && expMs <= now) {
|
||||
expired.push({ pts, expMs })
|
||||
continue
|
||||
}
|
||||
grantedFromBills += pts
|
||||
if (expMs > now) {
|
||||
if (!soonestExpMs || expMs < soonestExpMs) {
|
||||
@@ -700,6 +704,16 @@ async function queryUsage(token) {
|
||||
}
|
||||
}
|
||||
}
|
||||
// A grant past its expire_at can still be in the balance: the vendor takes
|
||||
// what is left of it some time later, not at expire_at (plugins #936: a
|
||||
// 9,623 balance against 9,600 of unexpired grants). Until then the balance
|
||||
// is made of it too, so the latest expired grants are counted back in, the
|
||||
// newest first, only as far as the balance needs them.
|
||||
expired.sort((a, b) => b.expMs - a.expMs)
|
||||
for (const g of expired) {
|
||||
if (grantedFromBills >= avail) break
|
||||
grantedFromBills += g.pts
|
||||
}
|
||||
const runsOutAt = soonestExpMs ? new Date(soonestExpMs).toISOString() : null
|
||||
const runsOut = soonestExpMs ? Math.min(avail, expiringPts) : 0
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-raccoon-auth",
|
||||
"version": "0.1.8",
|
||||
"version": "0.1.9",
|
||||
"description": "OpenCode & Magpie provider plugin for SenseTime Raccoon (商汤小浣熊, xiaohuanxiong.com)",
|
||||
"type": "module",
|
||||
"main": "./opencode.mjs",
|
||||
|
||||
@@ -449,6 +449,70 @@ test("setting_info's balance fills the Credits window the bills account for (plu
|
||||
expect(u.windows[0]).toMatchObject({ name: "Credits", amount: 81, limit: 9600 })
|
||||
})
|
||||
|
||||
// The balance in plugins #936's screenshot (9,623 credits) is more than the
|
||||
// grants not yet past their expire_at: the vendor takes an expired grant's
|
||||
// remainder some time after expire_at, so the newest expired grant is still in it.
|
||||
const iso = (ms) => new Date(ms).toISOString().replace("Z", "+00:00")
|
||||
const day = 86_400_000
|
||||
|
||||
test("a grant past expire_at the balance still holds keeps the Credits window (plugins #936)", async () => {
|
||||
const validTok = fakeJwt({ iss: "74350b", name: "Mason", exp: Math.floor(Date.now() / 1000) + 3600 })
|
||||
const now = Date.now()
|
||||
startServer({
|
||||
"GET /api/web/points/v1/balance": forbidden,
|
||||
"GET /api/web/auth/v1/entitlement_info": () => Response.json({ code: 0, data: { office: { pro_enable: false } } }),
|
||||
"GET /api/web/office/v3/setting_info": () => Response.json({ code: 0, data: { available_points: 9623 } }),
|
||||
"GET /api/web/points/v1/bills": () =>
|
||||
Response.json({
|
||||
code: 0,
|
||||
data: {
|
||||
items: [
|
||||
{ bill_id: "d4", biz_type: "daily_grant", points: 300, expire_at: iso(now + 5 * day) },
|
||||
{ bill_id: "d3", biz_type: "daily_grant", points: 300, expire_at: iso(now + 4 * day) },
|
||||
{ bill_id: "d2", biz_type: "daily_grant", points: 300, expire_at: iso(now - 3_600_000) },
|
||||
{ bill_id: "d1", biz_type: "daily_grant", points: 300, expire_at: iso(now - 2 * day) },
|
||||
{ bill_id: "r1", biz_type: "reward_grant", points: 9000 },
|
||||
],
|
||||
},
|
||||
}),
|
||||
})
|
||||
const hooks = await RaccoonAuthPlugin({ client: {} })
|
||||
const u = await hooks.auth.usage(async () => ({ type: "oauth", access: validTok, refresh: "rt1", expires: Date.now() + 3600_000 }))
|
||||
expect(u.balance).toBeUndefined()
|
||||
// d2 (expired an hour ago) is counted back; d1 is not needed and stays out
|
||||
expect(u.windows[0]).toMatchObject({
|
||||
name: "Credits",
|
||||
amount: 277,
|
||||
limit: 9900,
|
||||
display: "277 / 9900",
|
||||
runsOutAt: new Date(Date.parse(iso(now + 4 * day))).toISOString(),
|
||||
runsOut: 300,
|
||||
})
|
||||
})
|
||||
|
||||
test("expired grants that cannot make up the balance leave it balance-only (plugins #936)", async () => {
|
||||
const validTok = fakeJwt({ iss: "74350b", name: "Mason", exp: Math.floor(Date.now() / 1000) + 3600 })
|
||||
const now = Date.now()
|
||||
startServer({
|
||||
"GET /api/web/points/v1/balance": () => Response.json({ code: 0, data: { available_points: 5270 } }),
|
||||
"GET /api/web/auth/v1/entitlement_info": () => Response.json({ code: 0, data: {} }),
|
||||
"GET /api/web/points/v1/bills": () =>
|
||||
Response.json({
|
||||
code: 0,
|
||||
data: {
|
||||
items: [
|
||||
{ bill_id: "d2", biz_type: "daily_grant", points: 300, expire_at: iso(now + day) },
|
||||
{ bill_id: "d1", biz_type: "daily_grant", points: 300, expire_at: iso(now - day) },
|
||||
],
|
||||
},
|
||||
}),
|
||||
})
|
||||
const hooks = await RaccoonAuthPlugin({ client: {} })
|
||||
const u = await hooks.auth.usage(async () => ({ type: "oauth", access: validTok, refresh: "rt1", expires: Date.now() + 3600_000 }))
|
||||
expect(u.balance).toBe("5270 credits")
|
||||
expect(u.windows).toEqual([])
|
||||
})
|
||||
|
||||
test("a readable balance asks setting_info nothing (plugins #520)", async () => {
|
||||
const validTok = fakeJwt({ iss: "74350b", name: "Mason", exp: Math.floor(Date.now() / 1000) + 3600 })
|
||||
let asked = 0
|
||||
|
||||
Reference in New Issue
Block a user