Files
magpie/internal/provider/plugin_usage_text_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

41 lines
1.4 KiB
Go

package provider
import (
"encoding/json"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/plugin"
)
// A plugin's row set aside with text and nothing counted (AutoClaw's
// 积分余额, Cline's balance, Cursor's Auto + Composer note) is that text:
// QuotaWindow.Text, carried to /api/quotas' span, so the GUI, TUI and CLI
// don't read its used 0 as "100% left". A window that counts, a 0 with a
// limit, and one not set aside (Cursor's On-demand "$0 / $20", a real 0%)
// are readings and stay so.
func TestPluginTextRowIsText(t *testing.T) {
q := quotaOfPlugin(SubscriptionQuota{Provider: "autoclaw-plugin", User: "u"}, plugin.Usage{Windows: []plugin.UsageWindow{
{Name: "积分余额", Display: "1,234 积分", Aside: true},
{Name: "5 hours", Used: fptr(40), Display: "40 / 100"},
{Name: "Credits", Display: "0 / 50", Limit: 50, Aside: true},
{Name: "On-demand", Used: fptr(0), Display: "$0 / $20"},
{Name: "Bare", Aside: true},
}})
want := []bool{true, false, false, false, false}
for i, w := range q.Windows {
if w.Text != want[i] {
t.Errorf("%s: Text %v", w.Name, w.Text)
}
}
r := quotaReport([]SubscriptionQuota{q}, nil, nil, time.Now())
if len(r) != 1 || len(r[0].Windows) != 5 || !r[0].Windows[0].Text || r[0].Windows[1].Text {
t.Fatalf("report %+v", r)
}
b, _ := json.Marshal(r[0].Windows[:2])
if s := string(b); strings.Count(s, `"text":true`) != 1 {
t.Fatalf("json %s", s)
}
}