Files
magpie/internal/gateway/countfree_test.go
T
yetone 45de2ddb96 provider: a provider the user counts free goes first under Free first (jiabubu on Discord)
jiabubu asked on Discord #help for a way to mark a provider, model or key
as free, so that Free first routing spends it first. Their example is
ModelScope, which has a free daily quota. Until now routes.Seat.Free
came only from the vendor catalog's Free flag, and there was no way for
the user to set it.

Provider.CountFree ("Free for me" in the provider editor's Advanced fold,
`magpie provider set <id> free=yes|no` in the CLI) makes FreeModel true
for every model of that provider. Routing therefore treats it like a
vendor's free model: Free first uses it before paid ones, and Cheapest
first weighs it at nothing, because PriceAt, which only routing reads,
returns 0 for a free model. Prices shown (ListPrice and the model list)
and the costs Usage counts (EffectivePrice) are unchanged. The mark says
what to spend first, not what a call cost, and a quota that runs out
still bills at the listed price. Key and account (subscription)
providers both carry it, through withPicks, the account literal and the
copy merge.

Tests:
- internal/gateway TestFreeFirstSpendsTheProviderTheUserCountsFree:
  - a Free first group [paidv/m, mine/m] sends its first call to mine
    once mine is marked, and the trace row says Free;
  - EffectivePrice is still the listed 3/15;
  - Cheapest picks mine first;
  - unmarked, paidv goes first again.
- internal/gui TestProviderSaveKeepsCountFree: the preset and typed-in
  providers save the mark and clear it, and /api/providers reports it.
- internal/cli TestProviderSetFree: free=yes and free=no work; the value
  shows in `provider <id>`; free=maybe, an empty value and --help are
  refused and change nothing.
- GUI internal/gui/tests/provider-count-free.test.cjs covers the
  ModelScope preset, a custom provider saved with the mark, and a Codex
  account. It runs chromium and webkit, in en, zh, zh-TW, ja and de, at
  widths 900 and 440. It checks the Advanced fold, the check and save
  POST bodies, that the row fits with no sideways scroll, and that each
  string has a translation in I18N.

Core script budget: rebased on fa5e86cf2, the core's scripts came to
1,538,236 bytes, 880 over coreScriptBudget, which TestCoreScriptBudget
caught. The budget is not raised. countFreeField's comment and five
long comments on the editor's helpers (proxyPicker, concurrencyField,
priceRateField, pluginOptionsField) are cut down, so app.js ends 212
bytes smaller than before this change.

Run (mote via mote-run, at this change's tree on base 8ee75adbf):
- go test of internal/gateway (-run FreeFirst|InOrder), internal/gui,
  internal/cli and internal/provider: ok.
- GUI test: chromium 30/30 on mote. WebKit could not launch on mote at
  all at the time (a bare webkit.launch probe timed out at 60s, and
  other sessions' WebKit launches were stuck there too). WebKit was run
  on Linux instead (wsl-run, magpie-desktop): 30/30.
- gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./... and
  GOOS=windows go vet ./... ok; go build for darwin, linux (nogui) and
  windows ok.
- At the pushed head (rebased on fa5e86cf2, comments cut): go test of
  internal/gui, internal/cli, internal/provider and internal/gateway
  -run FreeFirst|CountFree ok (internal/gui was red only on
  TestCoreScriptBudget before the cut); node --check app.js ok; the GUI
  test in chromium 30/30.
- The full Go suite (go test -tags nogui -timeout 45m ./...) is still
  running on mote at ship time, per the owner's ship-at-once rule. Any
  red it finds gets a follow-up commit. The whole GUI suite was not run:
  mote's WebKit cannot launch and the Linux box was running other
  sessions' GUI suites.

Broken by hand:
- With the CountFree line taken out of FreeModel, all three Go tests
  went red, e.g. 'Free first went to "paidv" (free false) first'.
- With `pr.CountFree = in.CountFree` taken out of the preset save
  branch, TestProviderSaveKeepsCountFree went red ("the preset's mark
  wasn't kept").

Not tried:
- A real ModelScope account; the tests use fake upstreams.
- A per-model mark (optional in the request); the mark is
  provider-wide.
- The TUI editor (the CLI setter covers the terminal).
2026-10-31 04:21:21 +08:00

93 lines
3.2 KiB
Go

package gateway
import (
"net/http"
"sync/atomic"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// A provider the user counts free (provider.CountFree; jiabubu on Discord:
// ModelScope's free daily quota) is spent first by Free first, though its
// vendor's list says nothing of it being free and it is listed second. Its
// price stays as set: the mark orders the routing, it doesn't
// reprice what Usage counts. Cheapest first weighs it at
// nothing too. Turned off, the listed order is back.
func TestFreeFirstSpendsTheProviderTheUserCountsFree(t *testing.T) {
fresh(t)
var paid, mine atomic.Int32
answer := func(n *atomic.Int32) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
n.Add(1)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"id":"ok","choices":[{"message":{"role":"assistant","content":"hi"}}]}`))
}
}
serveOn(t, "paidv", "sk-paid", []string{"m"}, answer(&paid))
serveOn(t, "mine", "sk-mine", []string{"m"}, answer(&mine))
// both priced, the marked one dearer: nothing but the user's word says
// it costs them nothing
for id, pr := range map[string]catalog.Price{"paidv/m": {Input: 1, Output: 2}, "mine/m": {Input: 3, Output: 15}} {
if err := provider.SetModelPrice(id, &pr); err != nil {
t.Fatal(err)
}
}
if err := provider.SaveGroup(provider.Group{
Name: "G", Members: []string{"paidv/m", "mine/m"}, Routing: provider.FreeFirst,
}); err != nil {
t.Fatal(err)
}
if err := provider.SetCountFree("mine", true); err != nil {
t.Fatal(err)
}
s := New()
first := func() Weighed {
t.Helper()
code, body := postAs(t, s, "", `{"model":"group/g","messages":[{"role":"user","content":"x"}]}`)
if code != 200 {
t.Fatalf("code %d: %s", code, body)
}
last := s.trace.routes[len(s.trace.routes)-1]
if len(last.Order) != 2 {
t.Fatalf("the trace should weigh both members: %+v", last.Order)
}
return last.Order[0]
}
if w := first(); w.Provider != "mine" || !w.Free {
t.Fatalf("Free first went to %q (free %v) first, want the one counted free", w.Provider, w.Free)
}
if mine.Load() != 1 || paid.Load() != 0 {
t.Fatalf("answered by mine %d, paid %d", mine.Load(), paid.Load())
}
// what Usage counts a call at
if pr, ok := provider.EffectivePrice("mine", "m"); !ok || pr.Input != 3 || pr.Output != 15 {
t.Fatalf("counting it free repriced it: %+v %v", pr, ok)
}
// Cheapest first weighs it at nothing too, though its set price is
// the dearer
if err := provider.SaveGroup(provider.Group{
Name: "G", Members: []string{"paidv/m", "mine/m"}, Routing: provider.Cheapest,
}); err != nil {
t.Fatal(err)
}
if w := first(); w.Provider != "mine" {
t.Fatalf("Cheapest first went to %q first, want the one counted free", w.Provider)
}
if err := provider.SaveGroup(provider.Group{
Name: "G", Members: []string{"paidv/m", "mine/m"}, Routing: provider.FreeFirst,
}); err != nil {
t.Fatal(err)
}
if err := provider.SetCountFree("mine", false); err != nil {
t.Fatal(err)
}
if w := first(); w.Provider != "paidv" || w.Free {
t.Fatalf("turned off, Free first went to %q (free %v) first, want paidv as listed", w.Provider, w.Free)
}
}