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

135 lines
5.3 KiB
Go

package provider
import (
"context"
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
"github.com/yetone/magpie/internal/plugin"
"github.com/yetone/magpie/internal/testenv"
)
// A plugin that leaves out used says nothing of how much of a window is
// spent: a 0 is a reading, absent is not. The window goes as its text,
// never "100% left" (QuotaWindow.Text), and has no share in the allowance
// routing ranks on; an account whose windows all say nothing is unknown to
// routing, not the account with the most left (#3650).
func TestPluginWindowWithoutUsedIsNoReading(t *testing.T) {
now := time.Now()
q := quotaOfPlugin(SubscriptionQuota{Provider: "loomy-plugin", User: "u"}, plugin.Usage{Windows: []plugin.UsageWindow{
{Name: "Credits", Display: "0 / 5000"},
{Name: "Daily", Used: fptr(0), Display: "0 / 100", Limit: 100},
}})
if !q.Windows[0].Text || !q.Windows[0].Aside {
t.Errorf("not said: Text %v, Aside %v", q.Windows[0].Text, q.Windows[0].Aside)
}
if q.Windows[1].Text || q.Windows[1].Aside {
t.Errorf("a 0 that was said: Text %v, Aside %v", q.Windows[1].Text, q.Windows[1].Aside)
}
if a := allowanceOf(q.Windows, now); len(a) != 1 || a[0].Name != "Daily" || a[0].Used != 0 {
t.Errorf("the allowance routing ranks on: %+v", a)
}
only := quotaOfPlugin(SubscriptionQuota{Provider: "loomy-plugin", User: "v"}, plugin.Usage{Windows: []plugin.UsageWindow{
{Name: "Credits", Display: "0 / 5000"},
}})
if a := allowanceOf(only.Windows, now); len(a) != 0 {
t.Errorf("a window that said nothing is an allowance: %+v", a)
}
}
// A window that said nothing has no share in the allowance routing ranks
// on, through the decode a plugin's reply takes: the window whose used was
// never sent is absent from it, while one that said 0 is there (#3650).
// This one compiles on the base too, so it is the regression that fails
// before the fix.
func TestAWindowWithoutUsedIsNoAllowance(t *testing.T) {
var u plugin.Usage
if err := json.Unmarshal([]byte(`{"windows":[{"name":"Credits","display":"0 / 5000"},{"name":"Daily","used":0,"limit":100,"display":"0 / 100"}]}`), &u); err != nil {
t.Fatal(err)
}
q := quotaOfPlugin(SubscriptionQuota{Provider: "loomy-plugin", User: "u"}, u)
if a := allowanceOf(q.Windows, time.Now()); len(a) != 1 || a[0].Name != "Daily" {
t.Fatalf("the allowance: %+v", a)
}
}
// Routing, from a fresh reading and from the one kept on disk alike
// (Allowances, lastAllowances: routedAllowance), leaves an account whose
// windows tell no share out, as not known, rather than ranking it as the
// one with all of it left; one whose window said 0 is known (#3650).
func TestRoutingKnowsNoAccountThatSaidNoShare(t *testing.T) {
var said, unsaid plugin.Usage
if err := json.Unmarshal([]byte(`{"windows":[{"name":"Credits","display":"0 / 5000","resetsAt":"2099-01-01T00:00:00Z"}]}`), &unsaid); err != nil {
t.Fatal(err)
}
if err := json.Unmarshal([]byte(`{"windows":[{"name":"Credits","used":0,"display":"0 / 5000","resetsAt":"2099-01-01T00:00:00Z"}]}`), &said); err != nil {
t.Fatal(err)
}
now := time.Now()
if a, ok := routedAllowance("plugin:loomy", "u", quotaOfPlugin(SubscriptionQuota{Provider: "loomy-plugin", User: "u"}, unsaid), now); ok {
t.Errorf("no share said, yet known: %+v", a)
}
a, ok := routedAllowance("plugin:loomy", "v", quotaOfPlugin(SubscriptionQuota{Provider: "loomy-plugin", User: "v"}, said), now)
if used, _ := a.For("", now); !ok || used != 0 {
t.Errorf("a used of 0 that was said: known %v, used %v", ok, used)
}
}
// Through the plugin host: a window the plugin sent without used reaches
// magpie as no share (host.js's usageOf passes it on as null, not 0), so
// the card tells its text and routing doesn't know the account, while a
// window that said used 0 is a reading at 0 (#3650).
func TestPluginHostPassesAnAbsentUsedOn(t *testing.T) {
bun := testenv.Bun(t)
claudeHome(t)
t.Setenv("MAGPIE_BUN", bun)
windows := filepath.Join(t.TempDir(), "windows.json")
if err := os.WriteFile(windows, []byte(`{
"a@fake": [{"name": "Credits", "display": "0 / 5000"}],
"full@fake": [{"name": "Credits", "used": 0, "display": "0 / 5000"}]
}`), 0o600); err != nil {
t.Fatal(err)
}
t.Setenv("FAKE_WINDOWS", windows)
t.Cleanup(plugin.Settle)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
abs, _ := filepath.Abs("../plugin/testdata/fake/index.js")
if _, err := plugin.Add(ctx, abs); err != nil {
t.Fatal(err)
}
if _, err := plugin.Providers(ctx); err != nil {
t.Fatal(err)
}
for _, team := range []string{"a", "full"} {
st, err := StartPluginSignIn("fakeco", 1, map[string]string{"where": "work", "team": team})
if err != nil {
t.Fatal(err)
}
if err := SubmitSignInCallback(st.ID, "good"); err != nil {
t.Fatal(err)
}
}
u := LoginUsage(ctx, "plugin:fakeco")
unsaid, said := u["a@fake"], u["full@fake"]
if len(unsaid.Windows) != 1 || len(said.Windows) != 1 {
t.Fatalf("readings: %+v", u)
}
if w := unsaid.Windows[0]; !w.Text || !w.Aside || w.Display != "0 / 5000" {
t.Errorf("not said: %+v", w)
}
if w := said.Windows[0]; w.Text || w.Aside {
t.Errorf("a 0 that was said: %+v", w)
}
now := time.Now()
if a, ok := routedAllowance("plugin:fakeco", "a@fake", unsaid, now); ok {
t.Errorf("no share said, yet known: %+v", a)
}
if _, ok := routedAllowance("plugin:fakeco", "full@fake", said, now); !ok {
t.Error("a used of 0 that was said is not known")
}
}