diff --git a/packages/raccoon/index.mjs b/packages/raccoon/index.mjs index 8300703..79f3f35 100644 --- a/packages/raccoon/index.mjs +++ b/packages/raccoon/index.mjs @@ -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) diff --git a/packages/raccoon/package.json b/packages/raccoon/package.json index 4489ff1..b1d1ca3 100644 --- a/packages/raccoon/package.json +++ b/packages/raccoon/package.json @@ -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", diff --git a/packages/raccoon/raccoon.test.mjs b/packages/raccoon/raccoon.test.mjs index 5ba94bb..5a9c984 100644 --- a/packages/raccoon/raccoon.test.mjs +++ b/packages/raccoon/raccoon.test.mjs @@ -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 })