raccoon 0.1.2: usage reads the whole bill history, not the newest 200 rows (plugins #223)

The bills endpoint ignores paging.offset and always reports total 0, so
it can only be asked for the newest N rows. 200 rows covered under a day
of an active account and left out the large grants a balance is made
of, so the usage card's limit was far too small. queryUsage now asks
for 1000 rows, then 5000 if the first page came back full.

Run: bun test packages/raccoon (30 pass); the new test fails on the old
index.mjs (1 fail); bun scripts/check.mjs raccoon ok; full bun test
1833 pass, 0 fail. Not tried against a real Raccoon account.
This commit is contained in:
yetone
2026-10-19 20:46:11 +08:00
parent 3edbb18510
commit d34f78b69a
3 changed files with 41 additions and 2 deletions
+14 -1
View File
@@ -494,11 +494,24 @@ async function claimDesktopOnboardingGrant(token) {
return { points: 0, settled: false }
}
// allBills is the account's whole bill history, newest first. The bills
// endpoint ignores paging.offset and always says total 0, so it can only be
// asked for the newest N rows; 200 covered under a day of an active account
// and left out the large grants a balance is made of (plugins #223).
async function allBills(token) {
let bills = null
for (const limit of [1000, 5000]) {
bills = await apiGetJson(`/api/web/points/v1/bills?paging.limit=${limit}&paging.offset=0`, token)
if (!Array.isArray(bills?.items) || bills.items.length < limit) break
}
return bills
}
async function queryUsage(token) {
const [points, entitlement, bills] = await Promise.all([
apiGetJson("/api/web/points/v1/balance", token),
apiGetJson("/api/web/auth/v1/entitlement_info", token).catch(() => null),
apiGetJson("/api/web/points/v1/bills?paging.limit=200&paging.offset=0", token).catch(() => null),
allBills(token).catch(() => null),
])
const avail = Math.floor(Number(points?.available_points) || 0)
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-raccoon-auth",
"version": "0.1.1",
"version": "0.1.2",
"description": "OpenCode & Magpie provider plugin for SenseTime Raccoon (商汤小浣熊, xiaohuanxiong.com)",
"type": "module",
"main": "./index.mjs",
+26
View File
@@ -233,6 +233,32 @@ test("usage reports Credits window when unexpired bills account for total grante
})
})
test("usage reads bills past the newest 200 rows, where the large grants are (plugins #223)", async () => {
const validTok = fakeJwt({ iss: "74350b", name: "Mason", exp: Math.floor(Date.now() / 1000) + 3600 })
// newest first, as the endpoint answers: 1200 spends, then the grants
const items = []
for (let i = 0; i < 1200; i++) items.push({ bill_id: `s${i}`, biz_type: "consume", points: -1 })
items.push({ bill_id: "g1", biz_type: "daily_grant", points: 300, expire_at: "2029-10-24T23:59:59+08:00" })
items.push({ bill_id: "g2", biz_type: "reward_grant", points: 3000 })
items.push({ bill_id: "g3", biz_type: "reward_grant", points: 3000 })
const asked = []
startServer({
"GET /api/web/points/v1/balance": () => Response.json({ code: 0, data: { available_points: 5100 } }),
"GET /api/web/auth/v1/entitlement_info": () => Response.json({ code: 0, data: {} }),
"GET /api/web/points/v1/bills": (req, u) => {
// offset is ignored and total is always 0, as plugins #223 found
const limit = Number(u.searchParams.get("paging.limit"))
asked.push(limit)
return Response.json({ code: 0, data: { items: items.slice(0, limit), paging: { offset: 0, limit, total: 0 } } })
},
})
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()
expect(u.windows[0]).toMatchObject({ name: "Credits", amount: 1200, limit: 6300, display: "1200 / 6300" })
expect(asked).toEqual([1000, 5000])
})
test("checkin preserves renewed tokens, claims once, and returns done on second call", async () => {
const staleTok = fakeJwt({ iss: "74350b", name: "Mason", exp: Math.floor(Date.now() / 1000) - 5 })
const freshTok = fakeJwt({ iss: "74350b", name: "Mason", exp: Math.floor(Date.now() / 1000) + 3600 })