SuperMagic's accounts (mycf_711's magicrew plugin) are prepaid wallets of
points with no total. A plugin can now send a usage window as
{name, balance: 6291, unit: "积分"} with no used or limit.
- host.js passes a finite balance on (at least 0), anything else as null.
plugin.UsageWindow.Balance and provider.QuotaWindow.Balance are *float64.
- quotaOfPlugin keeps the figure and does not make the window Text or
Aside, so routing knows the account with no Used invented. allowanceOf
copies it into Limit.Balance; an empty wallet counts as used up.
Allowance.Left(model) is the least balance of the windows that count
the model.
- weighRouted fills routes.Seat.Left and Seat.LeftIn (the account's
provider and unit). Smart, pace and usage (least used) put the seat
with more left first where they would otherwise tie (byLeft), only
when every seat with a balance shares one LeftIn: balances in two
units or two providers are never compared. Tiers still come first.
- The Usage card, the Routing page (Weighed.Balance: the small row's
text, no bar, a why sentence, the account list), magpie accounts and
the status line show the figure, "6,291 积分 left", never a share.
- Docs: provider-plugins (new section), gateway-routing,
providers-accounts, plugin-runtime, host.js typings and the site's
plugins page (en, zh, ja).
Run, at base f13aafa87, on mote:
- go test -tags nogui -timeout 45m ./...: 72 packages ok; red were
TestQuotaSpanCarriesEveryWindowField (this change's: its fixture now
sets Balance, green after), and on the unchanged base too
TestWSLColdPlaceOpensNothing (#3924), TestFetchAsksASharedBaseOnce and
two siblings (#3854), TestTUIFetchesSubscriptionModels (#3919), all
fixed on main since and run again after the rebase; and
TestPluginStreamsPastTheBudgetWait (#3946), green alone on both.
- GOOS=linux and GOOS=windows go vet -tags nogui ./internal/..., go
build ./..., gofmt -l: clean.
- New tests: TestPluginWalletBalanceIsAReading, TestAllowanceLeftIsTheLeastBalance,
TestPluginHostPassesABalanceOn (through real Bun and host.js),
TestWalletsGoByWhatTheyHaveLeft, TestWalletLeftWithinItsTier,
TestWalletWithMoreLeftGoesFirst, TestQuotaCell's balance cases;
usage-wallet-balance.test.cjs and routing-wallet-balance.test.cjs,
34 of 34 in Chromium and WebKit, en zh zh-TW ja de, 1100/900/420/390px.
- GUI: every quota*, usage*, panel*, routing*, i18n* and tray* file (220
files, 2928 tests), concurrency 4: 130 red, in 21 files. Those 21 files
run again on an unchanged base copy and on this change, concurrency 2:
the same 19 files red on both (126 and 131), the differing subtests
being routing-rule-remove / rules-line / intent-entry flaps that go
both ways. Each is filed (#3679, #2115, #3410, #3404, #3411, #3897,
#1905, #3096, #3593, #3408, #3409, #3799, #3789, #3412, #3947, #3415,
#3414, #1345); routing-story-shrinks had none and is #3954.
After rebasing on origin/main 833f51adc, on mote: Linux and Windows vet,
go build, gofmt clean; go test -tags nogui of internal/routes, provider,
gateway, cli, plugin, gui (the script budgets), agent and tui: all ok;
the two wallet GUI files and i18n-keys: 37 of 38, the red being
i18n-keys' "Runs as" (#3897, not this change).
Broken by hand, each red then restored: quotaReport's Balance copy
removed (TestQuotaSpanCarriesEveryWindowField red); byLeft always nil (routes and
gateway tests red); host.js's balance line removed (host test red);
quotaOfPlugin's balance copy removed (provider tests red); app.js and
client.js at base (both GUI files 34 of 34 red, "0% used" / "已用 0%").
Not tried: a real SuperMagic account or the magicrew plugin (it isn't
in the community repo; it needs to send balance), Windows and Linux
GUI apps, and the WebKitGTK app.
130 lines
4.4 KiB
Go
130 lines
4.4 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 prepaid wallet (SuperMagic's points, #3950) tells how much is left in
|
|
// its unit and nothing it is a share of. Its window keeps that figure,
|
|
// is no text row and no share, and routing knows the account by it with
|
|
// nothing used invented; an empty wallet is used up. Through the decode
|
|
// a plugin's reply takes, as host.js hands it on.
|
|
func TestPluginWalletBalanceIsAReading(t *testing.T) {
|
|
var u plugin.Usage
|
|
if err := json.Unmarshal([]byte(`{"windows":[{"name":"积分","balance":6291,"unit":"积分"}]}`), &u); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
q := quotaOfPlugin(SubscriptionQuota{Provider: "magicrew-plugin", User: "u"}, u)
|
|
w := q.Windows[0]
|
|
if w.Balance == nil || *w.Balance != 6291 || w.Unit != "积分" {
|
|
t.Fatalf("the balance: %+v", w)
|
|
}
|
|
if w.Text || w.Aside || w.Used != 0 {
|
|
t.Errorf("a wallet is a reading with no share invented: Text %v, Aside %v, Used %v", w.Text, w.Aside, w.Used)
|
|
}
|
|
now := time.Now()
|
|
a, ok := routedAllowance("plugin:magicrew", "u", q, now)
|
|
if !ok {
|
|
t.Fatal("a wallet's account is not known to routing")
|
|
}
|
|
if used, _ := a.For("", now); used != 0 {
|
|
t.Errorf("used invented: %v", used)
|
|
}
|
|
if left, unit, ok := a.Left(""); !ok || left != 6291 || unit != "积分" {
|
|
t.Errorf("Left: %v %q %v", left, unit, ok)
|
|
}
|
|
|
|
var empty plugin.Usage
|
|
if err := json.Unmarshal([]byte(`{"windows":[{"name":"积分","balance":0,"unit":"积分"}]}`), &empty); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
e, ok := routedAllowance("plugin:magicrew", "v", quotaOfPlugin(SubscriptionQuota{Provider: "magicrew-plugin", User: "v"}, empty), now)
|
|
if !ok {
|
|
t.Fatal("an empty wallet is not known")
|
|
}
|
|
if used, _ := e.For("", now); used < SpentShare {
|
|
t.Errorf("an empty wallet is not used up: %v", used)
|
|
}
|
|
if left, _, ok := e.Left(""); !ok || left != 0 {
|
|
t.Errorf("an empty wallet's Left: %v %v", left, ok)
|
|
}
|
|
}
|
|
|
|
// Left is the least balance of the windows that count the model, and
|
|
// none when no window told one.
|
|
func TestAllowanceLeftIsTheLeastBalance(t *testing.T) {
|
|
b := func(f float64) *float64 { return &f }
|
|
now := time.Now()
|
|
a := allowanceOf([]QuotaWindow{
|
|
{Name: "Wallet", Balance: b(800), Unit: "pts"},
|
|
{Name: "Opus wallet", Balance: b(30), Unit: "pts", Model: "opus"},
|
|
{Name: "Daily", Used: 20, ResetSecs: 3600},
|
|
}, now)
|
|
if left, _, _ := a.Left("sonnet"); left != 800 {
|
|
t.Errorf("sonnet's: %v", left)
|
|
}
|
|
if left, _, _ := a.Left("opus"); left != 30 {
|
|
t.Errorf("opus's: %v", left)
|
|
}
|
|
if _, _, ok := allowanceOf([]QuotaWindow{{Name: "Daily", Used: 20}}, now).Left(""); ok {
|
|
t.Error("no balance told, yet one is left")
|
|
}
|
|
}
|
|
|
|
// Through the plugin host: a balance the plugin sends reaches magpie as
|
|
// that figure; one that isn't a number is not said.
|
|
func TestPluginHostPassesABalanceOn(t *testing.T) {
|
|
windows := filepath.Join(t.TempDir(), "windows.json")
|
|
if err := os.WriteFile(windows, []byte(`{
|
|
"a@fake": [{"name": "积分", "balance": 6291, "unit": "积分"}],
|
|
"full@fake": [{"name": "积分", "balance": "lots", "unit": "积分", "display": "lots"}]
|
|
}`), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
u := fakePluginUsage(t, windows)
|
|
if w := u["a@fake"].Windows; len(w) != 1 || w[0].Balance == nil || *w[0].Balance != 6291 || w[0].Text {
|
|
t.Errorf("a balance: %+v", w)
|
|
}
|
|
if w := u["full@fake"].Windows; len(w) != 1 || w[0].Balance != nil || !w[0].Text {
|
|
t.Errorf("not a number: %+v", w)
|
|
}
|
|
}
|
|
|
|
// fakePluginUsage signs in the fake plugin's a@fake and full@fake with
|
|
// the windows file it reads (FAKE_WINDOWS) and reads their usage.
|
|
func fakePluginUsage(t *testing.T, windows string) map[string]SubscriptionQuota {
|
|
t.Helper()
|
|
bun := testenv.Bun(t)
|
|
claudeHome(t)
|
|
t.Setenv("MAGPIE_BUN", bun)
|
|
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)
|
|
}
|
|
}
|
|
return LoginUsage(ctx, "plugin:fakeco")
|
|
}
|