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.
66 lines
2.4 KiB
Go
66 lines
2.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// cdredfox on X: in a Smart group of WorkBuddy accounts, the one whose
|
|
// credits left run out first goes first, since what it has left is lost
|
|
// then; the Credits window has no reset, so before they all ranked alike
|
|
// and kept their order. Its share used still decides first: one all but
|
|
// spent waits behind, whatever its credits' end.
|
|
func TestSmartSpendsWorkBuddyCreditsRunningOutFirst(t *testing.T) {
|
|
old := allowances
|
|
defer func() { allowances = old }()
|
|
now := time.Now()
|
|
share := map[string]provider.Allowance{}
|
|
allowances = func(string) map[string]provider.Allowance { return share }
|
|
p := provider.Provider{ID: "workbuddy-ai", Account: &provider.Account{Agent: "workbuddy-ai", User: "a@x.com"}}
|
|
acct := func(user string) candidate {
|
|
return candidate{p: provider.Provider{ID: "workbuddy-ai", Account: &provider.Account{Agent: "workbuddy-ai", User: user}}, model: "hy3", rest: "workbuddy-ai#" + user}
|
|
}
|
|
credits := func(used float64, in time.Duration) provider.Allowance {
|
|
l := provider.Limit{Used: used, Amount: used * 22.5, Of: 2250, Unit: "credits"}
|
|
if in > 0 {
|
|
l.RunsOut = now.Add(in)
|
|
}
|
|
return provider.Allowance{l}
|
|
}
|
|
order := func(cs ...candidate) string {
|
|
got, _ := weigh(p, cs, "hy3", provider.Chat)
|
|
var s []string
|
|
for _, c := range got {
|
|
s = append(s, c.p.Account.User[:1])
|
|
}
|
|
return strings.Join(s, "")
|
|
}
|
|
|
|
share["a@x.com"] = credits(10, 18*24*time.Hour)
|
|
share["b@x.com"] = credits(30, 30*time.Hour)
|
|
if got := order(acct("a@x.com"), acct("b@x.com")); got != "ba" {
|
|
t.Fatalf("credits ending sooner first: %s", got)
|
|
}
|
|
// one not known (an older plugin) after those known
|
|
share["a@x.com"] = credits(10, 0)
|
|
if got := order(acct("a@x.com"), acct("b@x.com")); got != "ba" {
|
|
t.Fatalf("not known last: %s", got)
|
|
}
|
|
// ending the same hour: their order, for the cache
|
|
hour := now.Truncate(time.Hour).Add(31 * time.Hour).Sub(now)
|
|
share["a@x.com"] = credits(10, hour+40*time.Minute)
|
|
share["b@x.com"] = credits(30, hour+10*time.Minute)
|
|
if got := order(acct("a@x.com"), acct("b@x.com")); got != "ab" {
|
|
t.Fatalf("same hour keeps order: %s", got)
|
|
}
|
|
// all but spent: behind, though its credits end first
|
|
share["a@x.com"] = credits(10, 18*24*time.Hour)
|
|
share["b@x.com"] = credits(99, 30*time.Hour)
|
|
if got := order(acct("b@x.com"), acct("a@x.com")); got != "ab" {
|
|
t.Fatalf("spent last: %s", got)
|
|
}
|
|
}
|