Files
magpie/internal/provider/workbuddy_runs_out_test.go
T
yetoneandskygunner 9733ed1b4a usage: a plugin window that doesn't say used is no reading (#3650)
A plugin usage window that leaves out `used` was read as 0 in host.js
usageOf. The card then showed "100% left" (or NaN% once the GUI saw a
missing value), and allowanceOf ranked the account as the one with all
of its allowance left, so smart routing put spent accounts first.

Now a left-out used is null from the host, plugin.UsageWindow.Used is a
*float64, and quotaOfPlugin makes such a window Text and Aside: the card
shows its display (else "No allowance reported"), with no percentage and
no meter, and routing leaves it out. An account whose windows all tell no
share has no allowance and is not known to routing (routedAllowance), as
one never read: not preferred and not rested. A window that says used:0
is a reading at 0, as before.

Ported from skygunner's fix/usage-absent-used (6791aa99). Changed from
the branch:
- lastAllowances (the reading kept on disk) built an empty allowance for
  such an account, which routing read as known with all of it left. Both
  it and Allowances now go through routedAllowance.
- quotaText shows "No allowance reported" for a no-share window with no
  display, rather than a blank.
- A host-level test (the bun host with the fake plugin) and the GUI test
  widened to both engines, every language, 900 and 390px, used and left.
- The branch's check-in wording change is its own commit.
Note: an account whose windows are all aside or text was known with 0%
used before; it is now not known, so loginRoom won't pick it as the
next account with room.

Run at this tree (base 9298d2a7c):
- go test ./internal/provider -run 'Absent|NoShare|PluginUsage|PluginText|
  AllowancePooled|PluginPool|GLMPlan|WorkBuddyCredits|QuotasDedupe'  ok
- go test ./internal/plugin ok; gofmt -l empty; go vet ./...,
  GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./... ok
- GUI: usage-no-reading.test.cjs chromium+webkit ok; i18n-keys ok;
  TestCoreScriptBudget ok (budget not raised)
Broken by hand, each red, then restored:
- quotaOfPlugin without the Text/Aside branch: 3 absent tests red
- routedAllowance always ok and allowanceOf not skipping Text:
  TestRoutingKnowsNoAccountThatSaidNoShare red
- host.js's old used line: TestPluginHostPassesAnAbsentUsedOn red
- usage-no-reading on origin/main's assets: red in both engines
  ("12 / 300 · NaN% used")
Not tried: a real Loomy account, a live routed request.

Co-authored-by: skygunner <skygunner@users.noreply.github.com>
2026-10-26 22:42:59 +08:00

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: fptr(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)
}
}