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

77 lines
2.2 KiB
Go

package provider
import (
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/catalog"
)
// liveFree writes the list a vendor's /models answered, where
// catalog.Live reads it: what Available goes by, and so FreeModel.
func liveFree(t *testing.T, id, json string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(catalog.LivePath(id)), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(catalog.LivePath(id), []byte(json), 0o644); err != nil {
t.Fatal(err)
}
catalog.Reset()
}
// FreeModel is what Free first goes by: a model its vendor lists at no cost
// is free, one the vendor prices is not, and a model the list doesn't name
// is not either — nothing says it costs nothing, so Free first puts it with
// the paid ones rather than inventing a saving.
func TestFreeModelGoesByTheVendorsOwnList(t *testing.T) {
prefsHome(t)
p := Provider{ID: "fv", Name: "FV", Key: "sk-fv", Chat: "http://127.0.0.1:9/v1"}
if err := Save(p); err != nil {
t.Fatal(err)
}
liveFree(t, "fv", `{"models":[
{"id":"free-one","free":true},
{"id":"paid-one","cost":{"input":3,"output":15}},
{"id":"zero-priced"}
]}`)
got, err := Find("fv")
if err != nil {
t.Fatal(err)
}
if !got.FreeModel("free-one") {
t.Error("a model its vendor lists free isn't free")
}
if got.FreeModel("paid-one") {
t.Error("a model its vendor prices as paid is free")
}
if got.FreeModel("nobody-lists-this") {
t.Error("a model the list doesn't name is free")
}
}
// The vendor's own word is what decides it, however many models it so
// marks: a pool where some are free and some are not puts only the marked
// ones first, and one where none is marked behaves as In order.
func TestFreeModelIsOnlyWhereTheVendorSaysSo(t *testing.T) {
prefsHome(t)
p := Provider{ID: "mv", Name: "MV", Key: "sk-mv", Chat: "http://127.0.0.1:9/v1"}
if err := Save(p); err != nil {
t.Fatal(err)
}
liveFree(t, "mv", `{"models":[{"id":"marked","free":true},{"id":"plain"},{"id":"also-marked","free":true}]}`)
got, err := Find("mv")
if err != nil {
t.Fatal(err)
}
for _, model := range []string{"marked", "also-marked"} {
if !got.FreeModel(model) {
t.Errorf("%s isn't free", model)
}
}
if got.FreeModel("plain") {
t.Error("plain is free")
}
}