diff --git a/internal/provider/credits_daily.go b/internal/provider/credits_daily.go index cdac4fef9..9a48da25d 100644 --- a/internal/provider/credits_daily.go +++ b/internal/provider/credits_daily.go @@ -20,7 +20,8 @@ package provider // card's counted windows in its unit used at least as much over the same // span, which makes it their credits again; a pool set aside on its own // (WorkBuddy's Extra credits, which it spends while the plan's stand -// still: jiabubu) counts as any other. Of one allowance seen over several +// still: jiabubu) counts as any other, and so do the pools set aside that +// together grant what a counted window does, its whole (poolsOfWhole). Of one allowance seen over several // spans (Open Design's 5 hours and week) only the longest counts. // // A reading that has used less than the one before is a cycle started @@ -293,6 +294,7 @@ func noteDailyCredits(qs []SubscriptionQuota, now time.Time) { changed := false for _, q := range qs { ws := creditWindows(q) + pools := poolsOfWhole(ws) type step struct { c creditWindow a *creditsAccount @@ -314,8 +316,9 @@ func noteDailyCredits(qs []SubscriptionQuota, now time.Time) { if a == nil { // one set aside beside a counted window in its unit is // taken for that window's breakdown until it uses - // credits that window doesn't - part := c.aside && slices.ContainsFunc(ws, func(x creditWindow) bool { return !x.aside && x.unit == c.unit }) + // credits that window doesn't; one of a whole's pools + // never is + part := c.aside && !pools[c.name] && slices.ContainsFunc(ws, func(x creditWindow) bool { return !x.aside && x.unit == c.unit }) a = &creditsAccount{Since: day, Part: part} accts[key] = a a.Last = creditsReading{Used: c.used, Total: c.total, At: now} @@ -341,7 +344,7 @@ func noteDailyCredits(qs []SubscriptionQuota, now time.Time) { // counted windows in its unit used at least as much in // the same span: then it is their credits broken down // (CodeArts' buckets under its 总积分), counted there. - a.Part = n[c.unit] > 0 && aside[c.unit] <= n[c.unit]+0.005 + a.Part = !pools[c.name] && n[c.unit] > 0 && aside[c.unit] <= n[c.unit]+0.005 if a.Part { d = 0 } @@ -380,6 +383,38 @@ func noteDailyCredits(qs []SubscriptionQuota, now time.Time) { _ = writeFileAtomic(path, b) } +// poolsOfWhole are the windows set aside that a counted window is the +// whole of: together, in its unit, they grant what it grants (WorkBuddy's +// Credits and Extra credits under its Total credits, plugin 0.2.24 on). +// Each is a pool of its own and counts its own days beside the whole's, +// so the pools' days add up to the whole's (yapechan on Discord: 今天 0 +// for 积分 and 额外积分 beside 34.06 for 积分合计, the pools taken for the +// whole's breakdown and counted there). A bucket that is only part of +// what the whole grants (CodeArts' 基础积分 under its 总积分) stays its +// breakdown. +func poolsOfWhole(ws []creditWindow) map[string]bool { + out := map[string]bool{} + for _, whole := range ws { + if whole.aside { + continue + } + var of []string + sum := 0.0 + for _, c := range ws { + if c.aside && c.unit == whole.unit { + of = append(of, c.name) + sum += c.total + } + } + if len(of) > 1 && math.Abs(sum-whole.total) <= 0.005 { + for _, name := range of { + out[name] = true + } + } + } + return out +} + // withDailyCredits gives each card in qs the days of its credits windows, // as kept: Daily the first's, MoreDaily the rest's. func withDailyCredits(qs []SubscriptionQuota, now time.Time) []SubscriptionQuota { @@ -395,6 +430,13 @@ func withDailyCredits(qs []SubscriptionQuota, now time.Time) []SubscriptionQuota out := slices.Clone(qs) for i, q := range out { var all []*DailyCredits + var ws []creditWindow + for _, w := range q.Windows { + if c, ok := creditOf(w); ok { + ws = append(ws, c) + } + } + pools := poolsOfWhole(ws) for _, w := range q.Windows { c, ok := creditOf(w) if !ok || slices.ContainsFunc(all, func(d *DailyCredits) bool { return d.Window == c.name }) { @@ -416,7 +458,7 @@ func withDailyCredits(qs []SubscriptionQuota, now time.Time) []SubscriptionQuota d.Days = append(d.Days, DayCredits{Day: day, Used: used}) } } - if a.Part && len(d.Days) == 0 { + if a.Part && !pools[c.name] && len(d.Days) == 0 { continue // another window's breakdown, counted there } slices.SortFunc(d.Days, func(x, y DayCredits) int { return strings.Compare(x.Day, y.Day) }) diff --git a/internal/provider/credits_daily_pools_test.go b/internal/provider/credits_daily_pools_test.go index 220d0207d..7795dc6e1 100644 --- a/internal/provider/credits_daily_pools_test.go +++ b/internal/provider/credits_daily_pools_test.go @@ -65,3 +65,59 @@ func TestDailyCreditsPoolSetAside(t *testing.T) { } } } + +// TestDailyCreditsPoolsOfWhole: WorkBuddy's card since its plugin's +// 0.2.24 sets both pools aside, Credits and Extra credits, and Total +// credits, the two together, is what stops the account +// (packages/workbuddy usage.test.mjs, CN_FREE_SUMMARY: 0 / 500, 282 / +// 2900, 282 / 3400). yapechan on Discord: credits a day read 今天 0 for +// 积分 and 额外积分 and 34.06 for 积分合计, the pools taken for the total's +// breakdown. Each pool counts its own days, and they add up to the +// whole's. +func TestDailyCreditsPoolsOfWhole(t *testing.T) { + testenv.SetHome(t, t.TempDir()) + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + day := func(d, h int) time.Time { return time.Date(2026, 11, d, h, 0, 0, 0, time.Local) } + win := func(name string, used, limit float64, aside bool) string { + a := "" + if aside { + a = `,"aside":true` + } + return `{"name":"` + name + `","used":` + jsonNum(100*used/limit) + `,"display":"` + jsonNum(used) + ` / ` + jsonNum(limit) + `","amount":` + jsonNum(used) + `,"limit":` + jsonNum(limit) + `,"unit":"credits"` + a + `}` + } + wb := func(plan, extra float64) SubscriptionQuota { + return pluginWindowsCard(t, "workbuddy", "yapechan", `[`+win("Credits", plan, 500, true)+`,`+win("Extra credits", extra, 2900, true)+`,`+win("Total credits", plan+extra, 3400, false)+`]`) + } + read := func(at time.Time, qs ...SubscriptionQuota) { noteDailyCredits(qs, at) } + read(day(7, 8), wb(0, 281.9999965)) + // the extra credits move alone, then the plan's alone, then both + read(day(7, 9), wb(0, 300)) + read(day(7, 10), wb(16.06, 300)) + read(day(7, 11), wb(20, 310)) + + got := withDailyCredits([]SubscriptionQuota{wb(20, 310)}, day(7, 12))[0] + all := append([]*DailyCredits{got.Daily}, got.MoreDaily...) + sums := map[string]float64{} + for _, d := range all { + for _, x := range d.Days { + sums[d.Window] += x.Used + } + } + if len(all) != 3 || sums["Credits"] != 20 || sums["Extra credits"] != 28 || sums["Total credits"] != 48 { + b, _ := json.Marshal(all) + t.Fatalf("days: %s, want Credits 20 + Extra credits 28 = Total credits 48", b) + } + + // a CodeArts-like bucket that is only part of its whole stays its + // breakdown (TestDailyCreditsEveryVendor), even beside a second one + part := func(total, a, b float64) SubscriptionQuota { + return pluginWindowsCard(t, "codearts", "hw", `[`+win("总积分", total, 30000, false)+`,`+win("基础积分", a, 10000, true)+`,`+win("赠送积分", b, 5000, true)+`]`) + } + read(day(7, 8), part(100, 100, 0)) + read(day(7, 9), part(150, 150, 0)) + got = withDailyCredits([]SubscriptionQuota{part(150, 150, 0)}, day(7, 12))[0] + if got.Daily == nil || got.Daily.Window != "总积分" || len(got.MoreDaily) != 0 { + b, _ := json.Marshal(append([]*DailyCredits{got.Daily}, got.MoreDaily...)) + t.Fatalf("a partial breakdown's days: %s, want 总积分 alone", b) + } +}