loomy 0.1.5: the daily wallet sends the share spent and the credits spent (#889)

magpie reads a window's `used` as the share spent (0-100) and `amount`
as what was spent of `limit`. Loomy sent the credits spent as `used` and
what was left as `amount`, so 800 spent of 5,000 read as 800%, and an
account window cap of 4 credits compared the 4,200 left with the cap:
it held a fresh account and let a spending one go past it (9won_25273
on Discord, over by more than 1,000). Now 16 and 800. A zero allowance
is 0, never NaN. The permanent wallet comes back as a balance alone, with
no total, so it stays an amount shown aside.

Run on mote at origin/main f54991b9 + this: bun test in packages/loomy
31/31. Broken by hand (the old used/amount put back): 3 usage tests red,
the new fresh-and-empty test among them.
Not tried: a real Loomy account.

Reported-by: 9won_25273 on Discord
This commit is contained in:
yetone
2026-11-10 00:26:38 +08:00
parent f54991b965
commit c61d83d0a1
3 changed files with 42 additions and 5 deletions
+9 -1
View File
@@ -510,14 +510,22 @@ export async function LoomyAuthPlugin({ client } = {}) {
// limit/remaining pair alone is enough (remaining IS its number);
// with only dailyBalance it is an amount without a limit; with
// neither, no daily wallet at all — a missing one is not 0
// In magpie's window, `used` is the share spent (0–100) and
// `amount` the credits spent of `limit` (plugins#889): sent as a
// count and as what was left, a percentage cap read 800 spent as
// 800% and an amount cap compared the credits left with the cap
const dailySpent = dailyLimit != null && dailyRemaining != null ? Math.min(dailyLimit, Math.max(0, dailyLimit - dailyRemaining)) : null
const dailyWallet =
dailyLimit != null && dailyRemaining != null
? { name: "每日赠送积分", used: Math.max(0, dailyLimit - dailyRemaining), amount: dailyRemaining, limit: dailyLimit, unit: "credits", resetsAt: beijingMidnight(), span: 86400 }
? { name: "每日赠送积分", used: dailyLimit > 0 ? (100 * dailySpent) / dailyLimit : 0, amount: dailySpent, limit: dailyLimit, unit: "credits", resetsAt: beijingMidnight(), span: 86400 }
: daily != null
? { name: "每日赠送积分", used: 0, display: `${daily} credits`, aside: true }
: null
const windows = [
...(dailyWallet ? [dailyWallet] : []),
// the permanent wallet comes back as a balance alone, with no
// total and nothing spent, so there is no share or spent amount
// a cap could read: it stays an amount shown aside
...(permanent != null ? [{ name: "永久积分", used: 0, display: `${permanent} credits`, aside: true }] : []),
]
// no `signIn` on a clean read: that absence is how magpie takes an
+32 -3
View File
@@ -304,13 +304,40 @@ describe("usage", () => {
expect(usage.signIn).toBeUndefined()
const daily = usage.windows.find((w) => w.name === "每日赠送积分")
// the vendor's own numbers: spent = dailyLimitPoints - dailyRemainingPoints
// magpie reads `used` as the share spent (0–100) and `amount` as the
// credits spent of `limit` (plugins#889)
expect(daily.limit).toBe(5000)
expect(daily.amount).toBe(fake.state.dailyBalance)
expect(daily.amount).toBe(5000 - fake.state.dailyBalance)
expect(daily.used).toBe((100 * (5000 - fake.state.dailyBalance)) / 5000)
expect(daily.unit).toBe("credits")
expect(daily.resetsAt).toBeGreaterThan(Date.now())
const permanent = usage.windows.find((w) => w.name === "永久积分")
expect(permanent.display).toBe("5000 credits")
})
test("a fresh daily wallet reads as nothing spent, and an empty one as all of it (plugins#889)", async () => {
const realFetch = globalThis.fetch
const reply = (limit, remaining) => (input, init) => {
const url = typeof input === "string" ? input : String(input?.url ?? input)
if (String(url).includes("/api/v2/points/records"))
return new Response(JSON.stringify({ code: "000000", desc: "ok", data: { balance: 5000, dailyBalance: remaining, dailyLimitPoints: limit, dailyRemainingPoints: remaining, list: [], total: 0 } }), { status: 200 })
return realFetch(input, init)
}
const daily = async (limit, remaining) => {
globalThis.fetch = reply(limit, remaining)
return (await plugin.auth.usage(getAuth)).windows.find((w) => w.name === "每日赠送积分")
}
try {
// a cap of 4 credits must not hold an account that has spent none
expect(await daily(5000, 5000)).toMatchObject({ used: 0, amount: 0, limit: 5000 })
expect(await daily(5000, 0)).toMatchObject({ used: 100, amount: 5000, limit: 5000 })
// a zero allowance is no share spent, never NaN
expect(await daily(0, 0)).toMatchObject({ used: 0, amount: 0, limit: 0 })
} finally {
globalThis.fetch = realFetch
}
})
test("a daily wallet without the limit pair is an amount without a limit", async () => {
const realFetch = globalThis.fetch
globalThis.fetch = (input, init) => {
@@ -336,8 +363,10 @@ describe("usage", () => {
const pairOnly = await plugin.auth.usage(getAuth)
const dw = pairOnly.windows.find((w) => w.name === "每日赠送积分")
expect(dw).toBeTruthy()
expect(dw.used).toBe(800)
expect(dw.amount).toBe(4200)
// 800 spent of 5,000: 16% used, 800 credits spent (plugins#889) —
// not 800% and not the 4,200 left
expect(dw.used).toBe(16)
expect(dw.amount).toBe(800)
expect(dw.limit).toBe(5000)
expect(pairOnly.windows.find((w) => w.name === "永久积分")).toBeUndefined()
} finally {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-loomy-auth",
"version": "0.1.4",
"version": "0.1.5",
"description": "OpenCode & Magpie provider plugin for Loomy (讯飞, loomyad.xunfei.cn): SMS sign-in, chat, models, the two point wallets and the daily first-login grant",
"type": "module",
"main": "./opencode.mjs",