From 3d7b67aeb314b826a5a160dee404dc27b2aefc66 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 11 Nov 2026 08:15:30 +0800 Subject: [PATCH] workbuddy 0.2.27: check-in rewards the summary doesn't list say when they lapse (cdredfox on X) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Credits and Total windows said when the soonest credits left run out (runsOutOf), but only over the packages the resource summary lists. A daily check-in reward (code 007, each with its own end) that only the detail lists have, beside extra credits the summary does list, was in no window and in no expiry, so Smart routing never saw those credits lapsing. runsOutOf now also counts the extra codes WorkBuddy's panel counts (EXTRA_CODES, plus the free month and gift on WorkBuddy AI) when given the site, on the extra window, and on the single window when no window of its own holds them. Run: bun test packages/workbuddy (336 pass, HOME=$(mktemp -d)); the new test fails on the old code (broken by hand: stashed index.mjs, red). Not tried: a real account (the desktop sign-in on this machine was stale, so the real list shape of a check-in reward is the 2026-10-26 fixture's, 007 "赠送积分", not seen live today). --- packages/workbuddy/index.mjs | 14 ++++++++--- packages/workbuddy/usage.test.mjs | 40 +++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) 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) +})