diff --git a/internal/provider/credits_daily.go b/internal/provider/credits_daily.go index 74b07fd8e..cdac4fef9 100644 --- a/internal/provider/credits_daily.go +++ b/internal/provider/credits_daily.go @@ -16,8 +16,12 @@ package provider // window is counted on its own, by provider, account and window name, so // a card's several pools (Qoder's Add-on credits, Kiro's Free trial) never // mix. A pool's breakdown set aside (CodeArts' buckets under its total) -// counts only when the card has nothing else, and of one allowance seen -// over several spans (Open Design's 5 hours and week) only the longest. +// is told by what the readings used: one set aside counts unless the +// 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 +// 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 // again or a credit pack gone: what was used between the two can't be @@ -84,6 +88,11 @@ type creditsAccount struct { Since string `json:"since"` Last creditsReading `json:"last"` Days map[string]float64 `json:"days,omitempty"` + // Part is set on a window set aside that the last reading it used + // anything in took for another window's credits broken down (see + // noteDailyCredits); before it used any, on one set aside beside a + // window in its unit that isn't. + Part bool `json:"part,omitempty"` } // creditsMu keeps two readings in one magpie from writing the file at once; @@ -232,6 +241,10 @@ func creditOf(w QuotaWindow) (creditWindow, bool) { // creditWindows are q's windows counted by the day, from a reading just // made: none of a card that failed or one kept from before. +// +// Those set aside are among them: whether one is a pool of its own or the +// breakdown of another is told by what each reading used (see +// noteDailyCredits), not by its being set aside. func creditWindows(q SubscriptionQuota) []creditWindow { if q.Error != "" || q.AsOf != nil { return nil // a reading kept from before isn't a new one @@ -242,11 +255,6 @@ func creditWindows(q SubscriptionQuota) []creditWindow { all = append(all, c) } } - // a pool's breakdown set aside counts only when nothing else does: - // CodeArts' buckets are its total's credits again - if slices.ContainsFunc(all, func(c creditWindow) bool { return !c.aside }) { - all = slices.DeleteFunc(all, func(c creditWindow) bool { return c.aside }) - } // one allowance over several spans (5 hours, a week): the longest's // count alone, which a rolling short one's drops don't cut into for i := 0; i < len(all); i++ { @@ -284,7 +292,17 @@ func noteDailyCredits(qs []SubscriptionQuota, now time.Time) { day := now.Local().Format("2006-01-02") changed := false for _, q := range qs { - for _, c := range creditWindows(q) { + ws := creditWindows(q) + type step struct { + c creditWindow + a *creditsAccount + d float64 + } + var steps []step + // what the card's windows used since their last readings, by unit: + // those counted (n) and those set aside (aside) + n, aside := map[string]float64{}, map[string]float64{} + for _, c := range ws { key := creditsKey(q, c.name) a := accts[key] if old := accts[creditsOldKey(q)]; a == nil && old != nil && c.name == "Credits" { @@ -294,7 +312,11 @@ func noteDailyCredits(qs []SubscriptionQuota, now time.Time) { changed = true } if a == nil { - a = &creditsAccount{Since: day} + // 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 }) + a = &creditsAccount{Since: day, Part: part} accts[key] = a a.Last = creditsReading{Used: c.used, Total: c.total, At: now} changed = true @@ -303,7 +325,28 @@ func noteDailyCredits(qs []SubscriptionQuota, now time.Time) { if c.used == a.Last.Used && c.total == a.Last.Total { continue } - if d := c.used - a.Last.Used; d > 0 { + d := max(c.used-a.Last.Used, 0) + if c.aside { + aside[c.unit] += d + } else { + n[c.unit] += d + } + steps = append(steps, step{c, a, d}) + } + for _, s := range steps { + c, a, d := s.c, s.a, s.d + if c.aside && d > 0 { + // A window set aside is a pool of its own (WorkBuddy's + // Extra credits, Kiro's bonuses, jiabubu) unless the + // 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 + if a.Part { + d = 0 + } + } + if d > 0 { if a.Days == nil { a.Days = map[string]float64{} } @@ -373,6 +416,9 @@ 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 { + continue // another window's breakdown, counted there + } slices.SortFunc(d.Days, func(x, y DayCredits) int { return strings.Compare(x.Day, y.Day) }) all = append(all, d) } diff --git a/internal/provider/credits_daily_pools_test.go b/internal/provider/credits_daily_pools_test.go new file mode 100644 index 000000000..220d0207d --- /dev/null +++ b/internal/provider/credits_daily_pools_test.go @@ -0,0 +1,67 @@ +package provider + +import ( + "encoding/json" + "reflect" + "testing" + "time" + + "github.com/yetone/magpie/internal/testenv" +) + +// TestDailyCreditsPoolSetAside: a pool set aside is counted by the day as +// any other (jiabubu on Discord: WorkBuddy's credits a day said 0 while +// only its Extra credits were spent). WorkBuddy's card is the plugin's +// own for a China-site free account (CN_FREE_SUMMARY in the plugins repo's +// workbuddy/usage.test.mjs): the plan's Credits, and Extra credits set +// aside until the plan's are spent. A breakdown set aside (CodeArts' +// buckets, TestDailyCreditsEveryVendor) still counts nothing of its own. +func TestDailyCreditsPoolSetAside(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) } + wb := func(plan, extra float64, spent bool) SubscriptionQuota { + pa, ea := "", `,"aside":true` + if spent { + pa, ea = `,"aside":true`, "" + } + return pluginWindowsCard(t, "workbuddy", "jiabubu", `[{"name":"Credits","used":`+jsonNum(100*plan/500)+`,"display":"`+jsonNum(plan)+` / 500","amount":`+jsonNum(plan)+`,"limit":500,"unit":"credits"`+pa+`},`+ + `{"name":"Extra credits","used":`+jsonNum(100*extra/2900)+`,"display":"`+jsonNum(extra)+` / 2900","amount":`+jsonNum(extra)+`,"limit":2900,"unit":"credits"`+ea+`}]`) + } + kiro := func(month, bonus float64) SubscriptionQuota { + return pluginWindowsCard(t, "kiro", "k", `[{"name":"Credits","used":1,"display":"x","amount":`+jsonNum(month)+`,"limit":50,"unit":"credits","span":2592000},`+ + `{"name":"Bonus credits","used":1,"display":"x","amount":`+jsonNum(bonus)+`,"limit":500,"unit":"credits","aside":true}]`) + } + read := func(at time.Time, qs ...SubscriptionQuota) { noteDailyCredits(qs, at) } + read(day(1, 9), wb(0, 281.9999965, false), kiro(50, 0)) + // only the extra credits move: WorkBuddy spends them first + read(day(1, 12), wb(0, 290.5, false), kiro(50, 12)) + // the first reading of the shown pool alone: nothing yet + if d := withDailyCredits([]SubscriptionQuota{wb(0, 290.5, false)}, day(1, 13))[0]; d.Daily == nil || len(d.MoreDaily) != 1 { + t.Fatalf("after the extra credits moved: %+v %+v", d.Daily, d.MoreDaily) + } + read(day(2, 9), wb(20, 290.5, false), kiro(50, 30)) + // the plan's spent: the plugin sets its window aside instead + read(day(2, 12), wb(500, 290.5, true)) + read(day(2, 15), wb(500, 300.5, true)) + + got := withDailyCredits([]SubscriptionQuota{wb(500, 300.5, true), kiro(50, 30)}, day(2, 16)) + want := map[string][]*DailyCredits{ + "jiabubu": { + {Since: "2026-11-01", Days: []DayCredits{{"2026-11-02", 500}}, Window: "Credits"}, + {Since: "2026-11-01", Days: []DayCredits{{"2026-11-01", 8.5}, {"2026-11-02", 10}}, Window: "Extra credits"}, + }, + "k": { + {Since: "2026-11-01", Days: []DayCredits{}, Window: "Credits"}, + {Since: "2026-11-01", Days: []DayCredits{{"2026-11-01", 12}, {"2026-11-02", 18}}, Window: "Bonus credits"}, + }, + } + for _, q := range got { + all := append([]*DailyCredits{q.Daily}, q.MoreDaily...) + if !reflect.DeepEqual(all, want[q.User]) { + b, _ := json.Marshal(all) + w, _ := json.Marshal(want[q.User]) + t.Errorf("%s's days: %s, want %s", q.Provider, b, w) + } + } +}