WorkBuddy's Credits window had no reset, so in a Smart group every WorkBuddy account ranked alike. workbuddy plugin 0.1.14 (plugins f0274d6) reads WorkBuddy's own package lists (billing/meter/get-user-resource-paid- packages and -free-packages, the calls WorkBuddy's client makes) and reports on the Credits window when the soonest-ending package that still has credits ends (runsOutAt) and how many credits it holds (runsOut). The daily Free Plan slice (CapacityType 4) and code 001 aren't counted: they renew daily. magpie carries runsOutAt from the plugin's usage window into QuotaWindow and the routing Limit. Allowance.Renewal, which only Smart ranks on, treats it as the window's reset when it is sooner than the real one. Pace, the tiers, For, Full and resting are unchanged. The mover's min is raised to 0.1.14 so moved users get the plugin. The usage card and account meter say "232.4 expire in 5h" in every language. Run: - plugin: bun test, 63 pass; 2 of the 3 new tests fail on 0.1.13's index.mjs. Verified live on a workbuddy-ai account in a sandbox HOME: Bonus Pack 232.4 left ending 10-12T16:39Z ranked before Pro (10-28). - TestWorkBuddyCreditsRunOutForRouting and TestSmartSpendsWorkBuddyCreditsRunningOutFirst: both fail with the Renewal change removed by hand. - GUI: credits-run-out.test.cjs in webkit and chromium, 5 languages, at 360/440/1000px, red on the old app.js; with i18n-keys, quota-*, panel-usage, reset-each-title, usage-fold: 117 pass. - go test ./... green (agent and gateway rerun with -timeout 30m after the default 10m ran out under load from other sessions; 378s and 492s). - gofmt -l clean; GOOS=linux and windows go vet of provider, plugin, gateway clean. Not tried: the CN workbuddy site (its token was stale, 401), so the CN package list is assumed to have the same shape. The built-in WorkBuddy (not moved users) doesn't get this. The account-line tooltip has no GUI test of its own.
62 lines
2.6 KiB
Go
62 lines
2.6 KiB
Go
package provider
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/plugin"
|
|
)
|
|
|
|
// cdredfox on X: WorkBuddy's credits come in packages that each end on
|
|
// their own day, and the Credits window has no reset. The plugin (0.1.14)
|
|
// says when the soonest credits left run out (runsOutAt), and Smart
|
|
// routing takes it for the window's renewal, so the account whose credits
|
|
// end first is spent first. The window is the one workbuddy 0.1.14 sends
|
|
// for the WorkBuddy AI account it was tried on: the Bonus Pack's 232.4
|
|
// credits end 10-13 00:39:27 +08.
|
|
func TestWorkBuddyCreditsRunOutForRouting(t *testing.T) {
|
|
now := time.Date(2026, 10, 11, 12, 0, 0, 0, time.UTC)
|
|
credits := func(runsOutAt string) plugin.Usage {
|
|
return plugin.Usage{Plan: "Pro", Windows: []plugin.UsageWindow{{Name: "Credits", Used: 100 * 17.6 / 2250,
|
|
Display: "17.6 / 2250", Amount: 17.6, Limit: 2250, Unit: "credits", RunsOutAt: runsOutAt, RunsOut: 232.4}}}
|
|
}
|
|
q := quotaOfPlugin(SubscriptionQuota{}, credits("2026-10-12T16:39:27.000Z"))
|
|
w := q.Windows[0]
|
|
at := time.Date(2026, 10, 12, 16, 39, 27, 0, time.UTC)
|
|
if w.RunsOutAt == nil || !w.RunsOutAt.Equal(at) || w.RunsOut != 232.4 || w.ResetsAt != nil {
|
|
t.Fatalf("window: %+v", w)
|
|
}
|
|
if r := allowanceOf(q.Windows, now).Renewal("hy3", now); len(r) != 1 || !r[0].Equal(at) {
|
|
t.Fatalf("renewal: %v, want %v", r, at)
|
|
}
|
|
// an older plugin, or a list it couldn't read: not known, as before
|
|
q = quotaOfPlugin(SubscriptionQuota{}, credits(""))
|
|
if q.Windows[0].RunsOutAt != nil {
|
|
t.Fatalf("no runsOutAt: %+v", q.Windows[0])
|
|
}
|
|
if r := allowanceOf(q.Windows, now).Renewal("hy3", now); len(r) != 1 || !r[0].IsZero() {
|
|
t.Fatalf("renewal not known: %v", r)
|
|
}
|
|
// already past: not known, the window isn't taken as renewing then
|
|
q = quotaOfPlugin(SubscriptionQuota{}, credits("2026-10-11T11:00:00Z"))
|
|
if r := allowanceOf(q.Windows, now).Renewal("hy3", now); len(r) != 1 || !r[0].IsZero() {
|
|
t.Fatalf("passed: %v", r)
|
|
}
|
|
// a window that resets says so, and runs out later: its reset stands;
|
|
// sooner, what is left is lost then, and that is its renewal
|
|
reset := now.Add(48 * time.Hour)
|
|
a := Allowance{{Used: 10, Resets: reset, RunsOut: now.Add(72 * time.Hour)}}
|
|
if r := a.Renewal("m", now); !r[0].Equal(reset) {
|
|
t.Fatalf("later run-out moved the reset: %v", r)
|
|
}
|
|
a[0].RunsOut = now.Add(24 * time.Hour)
|
|
if r := a.Renewal("m", now); !r[0].Equal(now.Add(24 * time.Hour)) {
|
|
t.Fatalf("sooner run-out: %v", r)
|
|
}
|
|
// the share used and the rest are the window's, as before: For,
|
|
// Full and the rest don't read RunsOut
|
|
if used, _ := a.For("m", now); used != 10 {
|
|
t.Fatalf("used: %v", used)
|
|
}
|
|
}
|