Files
magpie/internal/provider/cursor_price_test.go
T
yetone 7b5e773873 provider: a model at a window (claude-opus-5-5@300k) is priced as its model, so Cursor's sized ids have a cost
RyuJeXu on X (tweet 2117971061103100279), building an iOS board on magpie's
usage data: "cursor的用量好像不计费呀". Cursor calls showed no cost.

Cause: the Cursor plugin lists each model once per context size
(claude-opus-5-5@300k, @1m, gpt-5.5@272k, ...), lists them all at cost 0
(no price, pluginPrice), and its reply echoes the asked id, so usage rows
carry the sized id. pricedNames only took a window off grok- ids, so every
other sized id fell through the maker catalogs with no price. The plain ids
(claude-opus-5-5) were priced at the maker's list price all along.

Fix: pricedNames also tries the id with an "@<digits>k|m" window taken off
(modelWindow), for every provider. The window names the context size, not
a different model; long-context tiers still follow the call's own prompt
tokens through Price.At. Dated ids (claude-opus-4-5@20251101) and ids with
anything else after the @ are left alone. The regexp runs only for an id
with an @, since every listed model is priced on each Providers poll.

Tests:
- internal/provider/cursor_price_test.go: a moved Cursor provider prices
  @300k, @1m, upper case, a maker-prefixed id and gpt-5.5@272k/@1m as the
  plain model, through ListPrice, MakerPrice, PricedName(For), and its
  tier at 300K prompt tokens; a dated id, "@272", "@", "@272kb", an
  unknown model and auto stay as they were.
- internal/usage/cursor_window_cost_test.go: a Cursor row for
  claude-opus-5-5@300k costs what the plain id does ($6 each), auto stays
  unpriced, the page total is $12.
- grok_price_test.go: gpt-6-astra@500k under grok is now priced ($10/$50);
  grok-4.7-build@500k still isn't.

Run (all through mote-run, go1.26.8, base 823e7c250, the committed tree):
- gofmt -l internal/provider internal/usage: clean.
- go test -tags nogui -count=1 -timeout 45m ./internal/provider
  ./internal/usage ./internal/usageapi: ok 239s / 75s / 1s.
- GOOS=windows go vet and GOOS=linux go vet -tags nogui of provider,
  usage, usageapi: ok.
- Real catalog (a copy of the owner's models.json) before the change: every
  non-Grok sized Cursor id was unpriced; plain ids priced.

Broken by hand: with the old models.go put back, the two new tests fail on
every sized id (usage total 6 instead of 12) and TestGrokEffortListPrice
fails on gpt-6-astra@500k.

Not tried / not changed:
- No real Cursor call; the owner's ledger copy has only auto rows.
- Cursor's own auto and composer-2.5, and its own spellings (glm-5p3,
  deepseek-v41-flash-1017) and gemini-3.1-pro have no catalog price and
  stay unpriced; the plugin would have to give them a cost.
- Not ./... and no GUI suite: the change is in provider's price lookup
  only.
2026-11-04 22:45:40 +08:00

100 lines
4.2 KiB
Go

package provider
import (
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/plugin"
)
// windowCatalog is a catalog with a Claude model priced past 200K as
// models.dev writes it, a GPT model, and a model whose id has an @ in it of
// its own.
const windowCatalog = `{
"anthropic":{"id":"anthropic","models":{
"claude-opus-5-5":{"id":"claude-opus-5-5","cost":{"input":4,"output":20,"cache_read":0.2,"cache_write":5,
"tiers":[{"tier":{"type":"context","size":200000},"input":8,"output":30}]}},
"claude-opus-4-5@20251101":{"id":"claude-opus-4-5@20251101","cost":{"input":5,"output":25}}}},
"openai":{"id":"openai","models":{
"gpt-5.5":{"id":"gpt-5.5","cost":{"input":1.25,"output":10,"cache_read":0.125}}}}}`
func windowHome(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755)
os.WriteFile(catalog.CachePath(), []byte(windowCatalog), 0o644)
catalog.Reset()
t.Cleanup(catalog.Reset)
}
// The Cursor plugin lists a model once per context size Cursor's picker
// has it at (claude-opus-5-5@300k, claude-opus-5-5@1m, gpt-5.5@272k), and
// every call on one was counted at no cost: no catalog lists the sized id,
// and only a Grok id was read as its model at a window (RyuJeXu on X:
// "cursor的用量好像不计费"). Each is its model at that window, at the
// model's price, the long-prompt tier with it; the plain id is priced as
// before, whether the plugin serves the account or the provider is gone.
func TestCursorWindowIDsPricedAsTheirModel(t *testing.T) {
windowHome(t)
cursor := Provider{ID: "cursor", Name: "Cursor", Account: &Account{plugin: &plugin.Provider{ID: "cursor"}}}
for _, c := range []struct {
id string
in, out float64
}{
{"claude-opus-5-5", 4, 20},
{"claude-opus-5-5@300k", 4, 20},
{"claude-opus-5-5@1m", 4, 20},
{"Claude-Opus-5-5@1M", 4, 20},
{"anthropic/claude-opus-5-5@300k", 4, 20},
{"gpt-5.5@272k", 1.25, 10},
{"gpt-5.5@1m", 1.25, 10},
} {
for name, price := range map[string]func(string) (catalog.Price, bool){"ListPrice": cursor.ListPrice, "MakerPrice": MakerPrice} {
pr, ok := price(c.id)
if !ok || pr.Input != c.in || pr.Output != c.out {
t.Errorf("%s(%q) = %+v, %v; want $%v / $%v", name, c.id, pr, ok, c.in, c.out)
}
}
}
// the window doesn't set the rate, the prompt does: a 300K prompt at
// either size is at the model's long tier, a short one at its base
pr, _ := cursor.ListPrice("claude-opus-5-5@1m")
if long := pr.At(300_000); long.Input != 8 || long.Output != 30 {
t.Errorf("claude-opus-5-5@1m at 300K = %+v; want the >200K tier $8 / $30", long)
}
if short := pr.At(50_000); short.Input != 4 {
t.Errorf("claude-opus-5-5@1m at 50K = %+v; want $4", short)
}
if got := PricedName("claude-opus-5-5@300k"); got != "claude-opus-5-5" {
t.Errorf("PricedName(claude-opus-5-5@300k) = %q; want claude-opus-5-5", got)
}
if got := PricedNameFor("cursor", "gpt-5.5@272k"); got != "gpt-5.5" {
t.Errorf("PricedNameFor(cursor, gpt-5.5@272k) = %q; want gpt-5.5", got)
}
}
// Only a window is read off an id: an @ with a date after it is part of
// the model's own id (a dated id, claude-opus-4-5@20251101, priced as listed),
// an @ with no k or m is no window outside Grok's ids, and a sized id of a
// model no catalog prices stays unpriced.
func TestWindowOnlyReadOffAnID(t *testing.T) {
windowHome(t)
if pr, ok := MakerPrice("claude-opus-4-5@20251101"); !ok || pr.Input != 5 {
t.Errorf("MakerPrice(claude-opus-4-5@20251101) = %+v, %v; want its own $5", pr, ok)
}
if got := pricedNames("claude-opus-4-5@20251101"); len(got) != 1 {
t.Errorf("pricedNames(claude-opus-4-5@20251101) = %q; want the id alone", got)
}
cursor := Provider{ID: "cursor", Name: "Cursor", Account: &Account{plugin: &plugin.Provider{ID: "cursor"}}}
for _, id := range []string{"gpt-5.5@272", "gpt-5.5@", "gpt-5.5@272kb", "muse-wonder-1.0@300k", "auto", "@300k"} {
if pr, ok := cursor.ListPrice(id); ok {
t.Errorf("ListPrice(%q) = %+v; want unpriced", id, pr)
}
}
}