Files
magpie/internal/routes/freefirst_test.go
T
keeuu 75e769a9f9 routing: Free first spends the models that cost nothing before the ones that spend (#3054)
A provider or group can be routed `free`. Seats whose model the vendor
serves at no cost to the allowance or credits go first, in listed order.
The rest follow, also in listed order. Free means the vendor's own
catalog.Model.Free word, or else a $0 list price on every count
(provider.FreeModel). A pool with no free model behaves as In order.

routes.Seat gains Free and Strategy gains ByFree. weighRouted works Free
out only for a strategy that weighs by it, so other routings never read
a model list for it.

It is wired through every list a routing appears in:
- both save whitelists;
- plugins/rotation's Builtin;
- app.js's ROUTINGS (appScriptBudget 1,269,000 → 1,271,000 for the
  description) and the Routing page's options, hints and trace text;
- zh-TW, ja and de strings;
- the TUI's routings;
- the CLI's aliases (free, free-first).

Written by keeuu (issue #3054, three commits), squashed onto dee5c9d75
and reviewed:
- the harm checks were clean;
- FreeModel runs only for ByFree, so its Available() walk costs nothing
  for other routings;
- accounts can list different models, so Free first means something for
  a provider's own accounts too, not only for groups.

Run at this head:
- gofmt -l: clean.
- go vet: darwin (internal/routes, provider, gateway, cli, tui, gui);
  GOOS=windows ./internal/...; GOOS=linux -tags nogui ./... — all clean.
- go test ./internal/routes ./internal/provider -run
  'Free|Routing|Strateg|Builtin' ok; ./internal/gateway -run
  'FreeFirst|Weigh|Routing' ok.
- Broken by hand: with FreeFirst returning paid before free,
  internal/routes' Free tests and
  TestFreeFirstGroupSpendsTheFreeMemberFirst go red. Restored, green.
- go test ./... -count=1 -timeout 45m, at load average ~460 from other
  sessions: 62 packages ok, 3 red:
  - internal/gui TestMainWindowCorners (#1936/#2645, AppKit window
    killed at its deadline under load);
  - internal/gateway TestClaudeAccountsSpentFallBackToCodex ("no
    answer"): ok alone in 46s;
  - internal/agent timed out at 45m inside TestSnowSmallModel (1s in):
    the package is ok alone in 1519s.
- GUI, both engines (node --test, playwright 1.62.0), 23 files:
  account-arrange, builtins, context-text, context-scroll-end,
  context-window, group-member-weight, key-weight, rate-limited-sink,
  routing-reset-deadline, routing-compaction, routing-key-windows,
  routing-save, routing-sealed-task, routing-weight-chip, search-keys,
  i18n-keys, panel-routing-pick, panel-routing-providers, panel-routing,
  routing-columns, gui-de, gui-ja, gui-zh-tw. 230 of 236 pass.
  routing-save (the 6/7 counts), routing-columns, i18n-keys and the three
  language files are green.
  - account-arrange "every mode" (#2200): red in both engines, alone,
    and on origin/main dee5c9d75 too.
  - builtins chromium "Plugins › Built-in" (#2960): a 5s wait for the
    Plugins page. Red alone and on dee5c9d75 too; the webkit run checks
    the new Routing strategies subtitle and passes.
  - context-window and context-text, single cases in different
    language/engine pairs on each run: 5s waits under load (#1544,
    #2900). Untouched by this change.

Co-authored-by: keeuu <159448501+keeuu@users.noreply.github.com>
2026-10-20 20:08:25 +08:00

81 lines
2.2 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.
func TestFreeFirstIsByFree(t *testing.T) {
if !FreeFirst.ByFree {
t.Fatal("FreeFirst doesn't ask the gateway for Free")
}
for name, s := range Builtin {
if name == "free" {
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")
}
}