diff --git a/packages/loomy/index.mjs b/packages/loomy/index.mjs index 34c03d7..96bf411 100644 --- a/packages/loomy/index.mjs +++ b/packages/loomy/index.mjs @@ -80,8 +80,11 @@ const beijingMidnight = () => Math.floor((Date.now() + 8 * 3600e3) / 86400e3) * // wallet has spent; the permanent balance has nothing spent to tell, so the // total is spent of spent + left, and with nothing left it is all used. It // renews with the daily wallet at Beijing midnight, when the daily credits -// left are lost (runsOutAt, runsOut), so routing spends those first. null -// with neither wallet told. +// left are lost (runsOutAt, runsOut), so routing spends those first. With +// the daily credits spent and permanent ones left, midnight loses nothing, +// so the total names no reset: smart routing puts an account whose reset +// does lose credits first (plugins#1153). Used up, it names midnight, when +// it is back. null with neither wallet told. function totalOf(dailyWallet, dailyLeft, permanent) { if (dailyLeft == null && permanent == null) return null const spent = dailyWallet?.amount ?? 0 @@ -96,8 +99,12 @@ function totalOf(dailyWallet, dailyLeft, permanent) { unit: "credits", } if (dailyWallet) { - w.resetsAt = beijingMidnight() - if (dailyWallet.span) w.span = dailyWallet.span + // no span either: magpie reads a window with a span and no reset as + // renewing a span from now, which would rank it again + if (dailyLeft > 0 || left === 0) { + w.resetsAt = beijingMidnight() + if (dailyWallet.span) w.span = dailyWallet.span + } if (dailyLeft > 0) Object.assign(w, { runsOutAt: w.resetsAt, runsOut: dailyLeft }) } return w diff --git a/packages/loomy/index.test.mjs b/packages/loomy/index.test.mjs index 8c64346..7701edb 100644 --- a/packages/loomy/index.test.mjs +++ b/packages/loomy/index.test.mjs @@ -361,13 +361,19 @@ describe("usage", () => { // the daily wallet at 100% is shown, set aside expect(ws.find((w) => w.name === "每日赠送积分")).toMatchObject({ used: 100, aside: true }) expect(ws.find((w) => w.name === "永久积分")).toMatchObject({ display: "1200 credits", aside: true }) - // nothing of the daily wallet left: nothing lapses at midnight + // nothing of the daily wallet left: nothing lapses at midnight, so the + // total names no reset and smart routing puts accounts whose reset + // loses credits ahead of it (plugins#1153); the daily wallet beside it + // still shows midnight expect(total.runsOutAt).toBeUndefined() - expect(total.resetsAt).toBe(_internal.beijingMidnight()) + expect(total.resetsAt).toBeUndefined() + expect(total.span).toBeUndefined() + expect(ws.find((w) => w.name === "每日赠送积分").resetsAt).toBe(_internal.beijingMidnight()) // both gone: used up, back at Beijing midnight ws = await read({ balance: 0, dailyBalance: 0, dailyLimitPoints: 5000, dailyRemainingPoints: 0 }) expect(counted(ws)[0]).toMatchObject({ used: 100, amount: 5000, limit: 5000 }) + expect(counted(ws)[0].resetsAt).toBe(_internal.beijingMidnight()) // 3,000 daily credits left are lost at midnight: runsOutAt / runsOut ws = await read({ balance: 1200, dailyBalance: 3000, dailyLimitPoints: 5000, dailyRemainingPoints: 3000 }) diff --git a/packages/loomy/package.json b/packages/loomy/package.json index 3c7d442..2271846 100644 --- a/packages/loomy/package.json +++ b/packages/loomy/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-loomy-auth", - "version": "0.1.8", + "version": "0.1.9", "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",