Files
magpie/internal/provider/price_same_as_test.go
T
yetone 357ffac7be usage: a model's Same as prices its calls when no list does (#2344, liuweifeng)
A model a provider serves under its own id (a WorkBuddy kimi-k3-1, a
relay's renamed gpt) had no price unless the user typed one, even when the
user had already told magpie in the editor's "Same as" box which model it
is. Its calls showed no API-price cost in Usage, window holds had no price
to weigh, and `magpie model price` said unpriced.

Now, when neither the user's prices, the provider's list nor models.dev's
maker price covers a model, provider.SamePriceIn takes the maker's list
price of the model it is the same as (settings.ModelSameAs), and the
provider's price rate still applies. A price the user set, or a list that
prices the model, always wins; nothing is inferred without a Same as.
The same fallback is used by priceOf (Usage costs, re-priced when the
ledger is read, so past calls pick it up), usage's apiPricer (window
holds), the model editor's greyed list price, and `magpie model price`,
which names where the price came from. The "Same as" tooltip says so in
en, zh, zh-TW, ja and de.

Run:
- go test -p 1 -tags nogui ./internal/provider ./internal/usage
  ./internal/cli ./internal/gui (focused: TestSameAsPricesAnUnlistedModel,
  TestHoldsPriceTheModelItIsTheSameAs, TestModelEditorShowsTheSameAsPrice,
  TestModelPriceCmdSaysWhereThePriceCameFrom, plus the packages): ok
- GUI (chromium + webkit): provider-same-as, i18n-keys, model-price-edit,
  11/11 pass
- gofmt -l clean; go vet -tags nogui for darwin, linux, windows: clean

Broken by hand, each went red:
- SamePriceIn dropped from priceOf: "kimi-k3-1 ... false; want $0.3"
- usage's apiPricer fallback removed: "wasn't its API price: <nil>"
- the GUI editor branch removed: "list <nil>"
- the CLI source line disabled: TestModelPriceCmd... FAIL
- the zh tooltip left untranslated: provider-same-as.test.cjs 0/2
A prefix-stripping branch passed when removed (MakerPrice already resolves
vendor-prefixed ids like moonshotai/kimi-k3), so it was dropped.

Not tried: a real WorkBuddy account's usage after the change.

Full suite: SUITE_LINE
2026-10-15 23:08:03 +08:00

73 lines
2.5 KiB
Go

package provider
import (
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/settings"
)
// #2344 (liuweifeng): a provider's own id that no list prices (WorkBuddy's
// kimi-k3-1, a plugin's qfmodel) was unpriced even when the user had said
// which model it is (Same as, ModelSameAs). Its calls are costed at that
// model's maker's list price, at the provider's price rate; the user's own
// price and the provider's list still come first, and a model with no Same
// as is never matched by its name.
func TestSameAsPricesAnUnlistedModel(t *testing.T) {
priceHome(t)
if err := os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(catalog.CachePath(), []byte(`{
"moonshotai":{"id":"moonshotai","models":{"kimi-k3":{"id":"kimi-k3","cost":{"input":0.6,"output":2.5,"cache_read":0.15}}}},
"relaycat":{"id":"relaycat","models":{"listed":{"id":"listed","cost":{"input":9,"output":9}}}}
}`), 0o644); err != nil {
t.Fatal(err)
}
catalog.Reset()
if err := Save(Provider{ID: "wb", Name: "WB", Key: "k", Chat: "https://wb.example/v1",
Models: []string{"kimi-k3-1", "qfmodel", "listed", "other"}, Catalog: "relaycat", PriceRate: 0.5}); err != nil {
t.Fatal(err)
}
if pr, ok := EffectivePrice("wb", "kimi-k3-1"); ok {
t.Fatalf("priced before any Same as: %+v", pr)
}
s := settings.Load()
s.ModelSameAs = map[string]string{
"wb/kimi-k3-1": "kimi-k3",
"wb/qfmodel": "moonshotai/kimi-k3", // a vendor's prefix
"wb/listed": "kimi-k3",
"gone/kimi-k3-1": "kimi-k3",
}
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
for _, c := range []struct {
pid, model string
in float64
}{
{"wb", "kimi-k3-1", 0.3}, // Kimi K3's $0.6, at the rate
{"wb", "qfmodel", 0.3},
{"wb", "listed", 4.5}, // its provider's own list comes first
{"gone", "kimi-k3-1", 0.6},
} {
if pr, ok := EffectivePrice(c.pid, c.model); !ok || pr.Input != c.in {
t.Errorf("EffectivePrice(%q, %q) = %+v, %v; want $%v in", c.pid, c.model, pr, ok, c.in)
}
}
if pr, ok := EffectivePrice("wb", "other"); ok {
t.Errorf("a model with no Same as was priced: %+v", pr)
}
// the user's own price for it still wins
s = settings.Load()
s.ModelPrices = map[string]settings.ModelPrice{"wb/kimi-k3-1": statePrice(1)}
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
if pr, _ := EffectivePrice("wb", "kimi-k3-1"); pr.Input != 1 {
t.Errorf("the Same as price beat the user's own: %+v", pr)
}
}