Files
magpie/internal/routes/freefirst_test.go
T
yetone 00d8aac596 routing: Free quota by expiry spends the soonest-lapsing free quota first (wowbbs)
wowbbs (and shamen_xiaohong) keep check-in free accounts in one group:
WorkBuddy CN x2, MiniMax Code, TraeCode CN and Qoder CN. They asked that
the group spend the free quota that lapses soonest first, go on to the next
when one is used up or has lapsed, and use a paid account only when no free
quota is left.

This adds `expiring` (provider.FreeExpiring, routes.FreeExpiring). It is a
group's only, picked in the Routing page's group menu, with
`magpie group set <g> routing=expiring` or in the TUI. Nothing changes for a
group or provider that doesn't pick it. A provider keeps refusing it, as it
refuses `cheap`.

- routes: Seat.FreePlan and Strategy.ByPlan. FreeExpiring has three tiers:
  - free seats (Free, a model at no cost, or FreePlan) by Expires, the
    earliest of the window renewals and Lapse, to the hour; an unknown
    expiry goes last and ties keep their listed order;
  - then the paid seats, as Smart orders them;
  - then the spent and resting seats, by when they come back.
- provider.FreePlan reads an account's plan word: free, trial, 免费, 试用,
  体验, 體驗.
- gateway: weighRouted fills FreePlan when the strategy asks. The trace's
  Weighed rows gain `expires`, so the Routing page's story can say "its free
  quota is lost soonest, in 1d 2h". There are also lines for "a free one with
  no expiry told" and "no free quota left, so the paid one".
- GUI: an option in the group menu, the mode line, the story lines, and
  i18n in zh, zh-TW, ja and de. app.js only adds `expiring` to ROTATION,
  paid for by a shorter comment beside it, so the core is 11 bytes smaller
  than at its base.
- docs: gateway-routing.md (order of keys and accounts) and
  plugin-runtime.md (Seat and Strategies).

Plugins whose usage doesn't say when their free quota lapses got issues
rather than edits:
- magpie-community/plugins#718: Trae sums its packs; the soonest end comes
  from any pack, spent or past, and there is no runsOutAt.
- magpie-community/plugins#719: Qoder's plan is the raw userType
  personal_standard, which doesn't read as free, and its add-on, shared and
  dedicated pools have no expiry.
WorkBuddy (Free plan, resetsAt and runsOutAt) and MiniMax (Free, runsOutAt
on credits) already report what this needs.

Run, all on mote via mote-run at this tree (base 0ee424e3e):
- gofmt -l internal plugins: empty. go build ./....
- go vet of routes, gateway, provider, cli and tui; the same with
  GOOS=linux -tags nogui and with GOOS=windows.
- go test ./internal/routes ./plugins/rotation ./internal/tui: ok.
- provider -run TestFreePlan|Group|Routing: ok.
- cli -run Routing|Group: ok.
- gateway -run TestGroupSpendsFreeQuota|Wallet|Free|Cheapest|Routing|Weigh: ok.
- internal/gui -run Budget|Size: TestCoreScriptBudget is red, already red
  at the base (1,534,154 > 1,533,802, #5139). This change takes the core
  down 11 bytes and doesn't raise the budget.
- GUI, node --test in chromium and webkit, en, zh, zh-TW, ja and de, wide
  and 420px:
  - routing-free-expiring (new), routing-group-strategy, routing-cheapest-first,
    i18n-keys, builtins, group-member-free and provider-count-free: 176 of
    176 pass;
  - every routing-*.test.cjs: 2,478 of 2,520 pass. The 42 reds are in ten
    files (routing-rule-remove, rule-drag, rules-line, rule-models,
    row-swap, steady, order-unknown, steps-cap, story-shrinks and
    keys-fold). Run alone at this tree they give 27 reds, and at the base
    0ee424e3e 25, the same tests. They are filed as #5088, #4585, #3096,
    #3789, #3799, #4627, #4628 and #4629.

Again after rebasing on origin/main 0e804be29: gofmt; vet of routes,
gateway and provider; the same Go tests: ok. routing-free-expiring,
routing-group-strategy and i18n-keys: 62 of 62 pass. The core is 1,534,034
here, against main's 1,534,045.

Red on the old code: TestGroupSpendsFreeQuotaSoonestToExpireFirst at
0ee424e3e gives "pro,wb1,wb2,mm,qoder" where it wants
"mm,wb2,qoder,wb1,pro".

Broken by hand: with FreePlan never set, the gateway test gives
"pro,mm,wb2,qoder,wb1" (red). routing-free-expiring.test.cjs reads strings
that exist only with this change. routing-group-strategy's two menu counts
(11) went red with the new option, and they now count 12.

Not tried: no real account of any vendor. WorkBuddy, MiniMax, Trae and
Qoder check-in accounts were not routed live. The order was tested on
fabricated allowances shaped as their plugins report them. The full
`go test ./...` runs after the push.
2026-11-04 17:11:55 +08:00

82 lines
2.4 KiB
Go

package routes
import (
"slices"
"testing"
)
// FreeFirst spends what costs nothing before what spends a subscription's
// allowance: the free seats in their listed order, then the paid ones in
// theirs — nothing free left behind a paid one, and failover inside a tier
// staying in the order the user set.
func TestFreeFirstPutsFreeAheadOfPaid(t *testing.T) {
cases := []struct {
name string
seats []Seat
want []int
}{
{
// b and d cost nothing: they go first, each where it stood
name: "free among paid",
seats: []Seat{{Key: "a"}, {Key: "b", Free: true}, {Key: "c"}, {Key: "d", Free: true}},
want: []int{1, 3, 0, 2},
},
{
// every seat free: their order alone
name: "all free",
seats: []Seat{{Key: "a", Free: true}, {Key: "b", Free: true}},
want: []int{0, 1},
},
{
// none free: In order, so a routing set on a pool without free
// models changes nothing
name: "none free",
seats: []Seat{{Key: "a"}, {Key: "b"}, {Key: "c"}},
want: []int{0, 1, 2},
},
{
name: "the last alone free",
seats: []Seat{{Key: "a"}, {Key: "b"}, {Key: "c", Free: true}},
want: []int{2, 0, 1},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
if got := Apply(FreeFirst, "group/g", c.seats); !slices.Equal(got, c.want) {
t.Fatalf("order = %v, want %v", got, c.want)
}
})
}
}
// FreeFirst asks the gateway for Free (Strategy.ByFree), since nothing else
// fills it; a routing that doesn't is not to pay for reading each model
// list. Free quota by expiry ("expiring") counts a model that costs
// nothing as free quota too, so it is the other that asks.
func TestFreeFirstIsByFree(t *testing.T) {
if !FreeFirst.ByFree || !FreeExpiring.ByFree {
t.Fatal("FreeFirst or FreeExpiring doesn't ask the gateway for Free")
}
for name, s := range Builtin {
if name == "free" || name == "expiring" {
continue
}
if s.ByFree {
t.Fatalf("%s weighs by Free too, and would read every model list", name)
}
}
}
// The routing a provider or group is saved with is the one routes.Find
// knows: "free" is in Builtin, so plugins/rotation serves it and a saved
// one isn't read as an unknown routing (tried in its listed order).
func TestFreeFirstIsABuiltin(t *testing.T) {
s, ok := Builtin["free"]
if !ok {
t.Fatal("\"free\" is not among Builtin")
}
if s.Order == nil {
t.Fatal("\"free\" has no order")
}
}