diff --git a/packages/workbuddy/index.mjs b/packages/workbuddy/index.mjs index 926c438..c91bfaa 100644 --- a/packages/workbuddy/index.mjs +++ b/packages/workbuddy/index.mjs @@ -3141,8 +3141,15 @@ function endOf(p) { // the account whose credits expire first. Only packages the summary // counts, with credits left, and not the daily slice; null when none. // only narrows it to the packages of one window (the split's, usageOf). -function runsOutOf(sum, packages, now = Date.now(), only = () => true) { +// +// A reward the summary doesn't list but the detail lists do (a daily +// check-in's credits, each with its own end) is counted too when site is +// given, as WorkBuddy's own panel counts them (see listedExtra): the +// account serves them like any others, so an account whose check-in +// credits lapse first is spent first (cdredfox on X). +function runsOutOf(sum, packages, now = Date.now(), only = () => true, site = null) { const counted = new Set((sum?.Packages ?? []).map((p) => p?.PackageCode)) + if (site) for (const c of site.ai ? [...EXTRA_CODES, ...EXTRA_AI] : EXTRA_CODES) counted.add(c) let at = 0, amount = 0 for (const p of packages ?? []) { if (!counted.has(p?.PackageCode) || !only(p?.PackageCode) || p?.PackageCode === DAILY_CODE || String(p?.CapacityType) === SLICE) continue @@ -4035,10 +4042,9 @@ function makePlugin(site) { // split by the summary (usageOf): each says its own // packages' soonest lapse, the whole the soonest of both Object.assign(credits, runsOutOf(sum, packages, Date.now(), ofPlan) ?? {}) - Object.assign(extra, runsOutOf(sum, packages, Date.now(), (code) => !ofPlan(code)) ?? {}) + Object.assign(extra, runsOutOf(sum, packages, Date.now(), (code) => !ofPlan(code), site) ?? {}) out.windows.splice(out.windows.findIndex((w) => w.name === "Total credits"), 1, totalOf(credits, extra)) } else { - Object.assign(credits, runsOutOf(sum, packages) ?? {}) // the summary of the plan's credits alone: the extra ones // only the detail lists have (listedExtra) are their window, // and the whole stops the account, as with the summary's @@ -4046,6 +4052,8 @@ function makePlugin(site) { // account at "500 / 500" on its extra credits) const kinds = kindsOf(sum, site) const more = kinds.plan.total > 0 && kinds.extra.total === 0 ? listedExtra(sum, packages, site) : null + // with no window of their own, their lapse is the one window's + Object.assign(credits, runsOutOf(sum, packages, Date.now(), () => true, more ? null : site) ?? {}) if (more) { const { runsOutAt, runsOut, ...counts } = more const w = { ...creditWindow("Extra credits", counts), ...(runsOutAt ? { runsOutAt, runsOut } : {}) } diff --git a/packages/workbuddy/usage.test.mjs b/packages/workbuddy/usage.test.mjs index 7226364..bcf8e69 100644 --- a/packages/workbuddy/usage.test.mjs +++ b/packages/workbuddy/usage.test.mjs @@ -809,3 +809,43 @@ test("a WorkBuddy AI card carries the free model's window from the ledger", asyn rmSync(ledger, { force: true }) } }) + +// cdredfox on X (2026-11-11): the Credits card said only the plan's end, +// while each daily check-in reward ends on its own day. A reward the +// summary doesn't list, beside extra credits it does, was in no window +// and in no expiry. Shapes as the 2026-10-26 China-site free account's +// (CN_FREE: 赠送积分, code 007, one month after the day it was given). +test("check-in rewards the summary doesn't list still say when they lapse (cdredfox on X)", async () => { + const { SITES } = _internal + 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 grant = (day, left) => ({ PackageCode: "TCACA_code_007_nzdH5h4Nl0", PackageName: "赠送积分", CapacityType: 1, CycleCapacitySizePrecise: "100", CycleCapacityRemainPrecise: left, + CycleStartTime: `2036-09-${day} 01:34:43`, CycleEndTime: `2036-10-${day} 01:34:42`, DeductionEndTime: Date.parse(`2036-10-${day}T01:34:42+08:00`), ExpiredTime: "", Status: 0 }) + // the summary lists the plan and a bonus; the detail's rewards are in neither + const sum = LIVE_SUMMARY + const paid = on(LIVE_PAID.Accounts[0]), bonus = on(LIVE_FREE.Accounts[0]) + const early = grant("05", "40"), later_ = grant("20", "100") + const now = Date.parse("2036-09-25T00:00:00Z") + // one soonest, whatever order the lists come in + expect(_internal.runsOutOf(sum, [paid, bonus, later_, early], now, (c) => !_internal.planCodes(sum, SITES.workbuddy)(c), SITES.workbuddy)) + .toEqual({ runsOutAt: "2036-10-04T17:34:42.000Z", runsOut: 40 }) + // without the site, as before: only what the summary lists + expect(_internal.runsOutOf(sum, [paid, bonus, later_, early], now)).toEqual({ runsOutAt: new Date(bonus.DeductionEndTime).toISOString(), runsOut: 1910.8000000299999 }) + // a spent reward, or one past its end, is not what lapses + expect(_internal.runsOutOf(sum, [paid, bonus, grant("05", "0")], now, () => true, SITES.workbuddy)?.runsOut).toBe(1910.8000000299999) + + serve({ + "/billing/meter/get-user-resource-summary": () => ok(sum), + "/billing/meter/get-user-resource-paid-packages": () => ok({ TotalCount: 1, Accounts: [paid] }), + "/billing/meter/get-user-resource-free-packages": () => ok({ TotalCount: 3, Accounts: [bonus, later_, early] }), + }) + const u = await usageOf(WorkBuddyAuthPlugin, { type: "oauth", access: "a", refresh: "r", expires: later(), uid: "u" }) + const total = u.windows.find((w) => w.name === "Total credits") + const extra = u.windows.find((w) => w.name === "Extra credits") + const lapse = new Date(early.DeductionEndTime).toISOString() + expect(Date.parse(lapse)).toBeLessThan(bonus.DeductionEndTime) + expect(extra.runsOut).toBe(40) + expect(extra.runsOutAt).toBe(lapse) + expect(total.runsOut).toBe(40) + expect(total.runsOutAt).toBe(lapse) +})