diff --git a/packages/workbuddy/README.md b/packages/workbuddy/README.md index 19c2636..cabab25 100644 --- a/packages/workbuddy/README.md +++ b/packages/workbuddy/README.md @@ -359,7 +359,10 @@ credit packs) and platform rewards (gifted and activity credits, against plan_bonus, addon and benefit); a summary without them is split by package code as WorkBuddy's client does. Only one of the two stops the account, since WorkBuddy serves it while any credits are left: the extra -window is set aside until the plan's credits are spent. A summary with +window is set aside until the plan's credits are spent, then the plan's +is. Extra credits only the detail lists carry (a China-site free +account's summary can list the plan's base alone) are their own "Extra +credits" window by the same rule (ferock on Discord). A summary with only one kind keeps the one "Credits" window (akic404 on Discord; since 0.2.16 the plan's bonus is extra too, Jiabubu on Discord). diff --git a/packages/workbuddy/index.mjs b/packages/workbuddy/index.mjs index c916595..c30d499 100644 --- a/packages/workbuddy/index.mjs +++ b/packages/workbuddy/index.mjs @@ -3903,24 +3903,30 @@ function makePlugin(site) { const aside = extra ? (stops === credits ? extra : credits) : null if (aside) Object.assign(aside, runsOutOf(sum, packages, Date.now(), (code) => ofPlan(code) === (aside === credits)) ?? {}) // the summary of the plan's credits alone: the extra ones - // only the detail lists have (listedExtra) are their window, - // always set aside. Whether WorkBuddy spends them once the - // plan's are gone isn't seen, so the plan's window stops the - // account as it did before. + // only the detail lists have (listedExtra) are their window. + // As with the summary's split (usageOf), one of the two stops + // the account: the plan's, until its credits are spent with + // extra ones left. WorkBuddy goes on serving such an account on + // its extra credits (ferock on Discord, a China-site account + // at "500 / 500" moved off while its extra ones were left). const kinds = !extra && kindsOf(sum, site) const more = kinds && kinds.plan.total > 0 && kinds.extra.total === 0 ? listedExtra(sum, packages, site) : null if (more) { const { runsOutAt, runsOut, ...counts } = more - const w = { ...creditWindow("Extra credits", counts), aside: true, ...(runsOutAt ? { runsOutAt, runsOut } : {}) } + const w = { ...creditWindow("Extra credits", counts), ...(runsOutAt ? { runsOutAt, runsOut } : {}) } + const planSpent = credits.amount >= credits.limit && counts.used < counts.total + if (planSpent) credits.aside = true + else w.aside = true out.windows.splice(out.windows.indexOf(credits) + 1, 0, w) - // and, as with the split's, the soonest of them to lapse is + // and, as with the split's, the soonest credits to lapse are // the account's too, on the window that stops it: routing // reads none set aside, so an account whose extra credits // expire first went unknown to it (Kayphoon on Discord) - const at = runsOutAt ? Date.parse(runsOutAt) : 0 - const was = credits.runsOutAt ? Date.parse(credits.runsOutAt) : 0 - if (at > 0 && (!was || at < was)) Object.assign(credits, { runsOutAt, runsOut }) - else if (at > 0 && at === was) credits.runsOut = (credits.runsOut ?? 0) + runsOut + const [stops, other] = planSpent ? [w, credits] : [credits, w] + const at = other.runsOutAt ? Date.parse(other.runsOutAt) : 0 + const was = stops.runsOutAt ? Date.parse(stops.runsOutAt) : 0 + if (at > 0 && (!was || at < was)) Object.assign(stops, { runsOutAt: other.runsOutAt, runsOut: other.runsOut }) + else if (at > 0 && at === was) stops.runsOut = (stops.runsOut ?? 0) + other.runsOut } } catch {} } diff --git a/packages/workbuddy/package.json b/packages/workbuddy/package.json index 64f3e0b..531d467 100644 --- a/packages/workbuddy/package.json +++ b/packages/workbuddy/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-workbuddy-auth", - "version": "0.2.21", + "version": "0.2.22", "description": "OpenCode provider plugins for WorkBuddy's plans: WorkBuddy (CodeBuddy, copilot.tencent.com) and WorkBuddy AI (www.workbuddy.ai)", "type": "module", "main": "./opencode.mjs", diff --git a/packages/workbuddy/usage.test.mjs b/packages/workbuddy/usage.test.mjs index 10fd241..ca1f7a9 100644 --- a/packages/workbuddy/usage.test.mjs +++ b/packages/workbuddy/usage.test.mjs @@ -588,6 +588,43 @@ test("extra credits only the detail lists have are a window of their own, set as expect(_internal.listedExtra(summary, [{ ...topup, CycleCapacitySizePrecise: "" }], SITES.workbuddy, now)).toBeNull() }) +// ferock on Discord (2026-11-05, a China-site account): "额外积分不计算在内, +// 基础积分用完 magpie 就切换账号了,其实账号还能用额外积分". The card read +// "500 / 500" on the plan's window alone, and that window stopped the +// account while extra credits only the detail lists carry were left. As +// with the summary's split, the plan's window is set aside once it is spent +// with extra credits left, and the extra window stops the account. +test("plan credits spent with extra ones left from the detail lists: the extra window stops the account (ferock on Discord)", async () => { + const year = Date.UTC(2036, 9, 1) - Date.UTC(2026, 9, 1) + const on = (p) => ({ ...p, DeductionEndTime: p.DeductionEndTime + year, CycleStartTime: "2036" + p.CycleStartTime.slice(4), CycleEndTime: "2036" + p.CycleEndTime.slice(4) }) + const base = { ...CN_FREE.Accounts[1], CycleCapacitySizePrecise: "500", CycleCapacityRemainPrecise: "0", SlicePeriodUsageDetails: [{ SlicePeriodCapacitySizePrecise: "500", SlicePeriodCapacityRemainPrecise: "0" }] } + const gift = { ...CN_FREE.Accounts[0], CycleCapacitySizePrecise: "300", CycleCapacityRemainPrecise: "280" } + const topup = { PackageCode: "TCACA_code_009_0XmEQc2xOf", PackageName: "加量包", CapacityType: 1, CycleCapacitySizePrecise: "1000", CycleCapacityRemainPrecise: "1000", + CycleStartTime: "2026-10-02 10:00:00", CycleEndTime: "2027-10-02 09:59:59", DeductionEndTime: Date.parse("2027-10-02T01:59:59Z"), ExpiredTime: "", Status: 0 } + const plan = { ...CN_FREE_SUMMARY.Packages[1], CycleRemainCapacity: "0", CycleUsedCapacity: "500" } + const lists = (accounts) => serve({ + "/billing/meter/get-user-resource-summary": () => ok({ ...CN_FREE_SUMMARY, Packages: [plan] }), + "/billing/meter/get-user-resource-paid-packages": () => ok({ TotalCount: 1, Accounts: [on(topup)] }), + "/billing/meter/get-user-resource-free-packages": () => ok({ TotalCount: accounts.length, Accounts: accounts.map(on) }), + }) + const auth = { type: "oauth", access: "a", refresh: "r", expires: later(), uid: "u" } + lists([gift, base]) + let u = await usageOf(WorkBuddyAuthPlugin, auth) + expect(u.windows.slice(0, 2).map((w) => [w.name, w.display, w.used, !!w.aside])).toEqual([["Credits", "500 / 500", 100, true], ["Extra credits", "20 / 1300", (100 * 20) / 1300, false]]) + // the window that stops the account says the soonest lapse, as routing + // reads none set aside; the plan's keeps its cycle + expect(u.windows[1]).toMatchObject({ runsOut: 280, runsOutAt: new Date(on(gift).DeductionEndTime).toISOString() }) + expect(u.windows[0].resetsAt).toBe("2036-10-31T15:59:59.000Z") + // both spent: the plan's stops it again, at 100% + serve({ + "/billing/meter/get-user-resource-summary": () => ok({ ...CN_FREE_SUMMARY, Packages: [plan] }), + "/billing/meter/get-user-resource-paid-packages": () => ok({ TotalCount: 1, Accounts: [on({ ...topup, CycleCapacityRemainPrecise: "0" })] }), + "/billing/meter/get-user-resource-free-packages": () => ok({ TotalCount: 2, Accounts: [on({ ...gift, CycleCapacityRemainPrecise: "0" }), on(base)] }), + }) + u = await usageOf(WorkBuddyAuthPlugin, auth) + expect(u.windows.slice(0, 2).map((w) => [w.name, w.used, !!w.aside])).toEqual([["Credits", 100, false], ["Extra credits", 100, true]]) +}) + test("a summary that already splits the credits, or lists with no extra ones, keep the card as it was", async () => { const empty = () => ok({ TotalCount: 0, Accounts: [] }) serve({