Files
magpie/internal/provider/peak_test.go
T
yetone 8921ef8056 provider: the Command Code plan prices DeepSeek V4.1 Flash, with its peak hours (treeman on Discord)
treeman_26897 (#help): in the price editor the Command Code plan's
deepseek/deepseek-v4.1-flash showed empty boxes, and typing a price lost
the peak/off-peak split.

What was wrong, on the plan (served by its plugin since the retire):
- No price at all. models.dev's DeepSeek now lists V4.1 Flash only as
  deepseek-flash, by the dated canonical deepseek/deepseek-v4.1-flash-1017,
  so no maker price matches the undated id, and the plugin's own model list
  gives cost 0 (none). Its calls were counted unpriced, at nothing, and the
  editor had no list price to show. models.dev's own "commandcode" catalog
  does list it ($0.15/$0.60, cache read $0.003); the Command Code API-key
  preset already read it through its endpoint, the plan didn't.
  ListPrice now falls back to that catalog for the plan (vendorListPrice),
  after the maker's price, so a model DeepSeek prices keeps DeepSeek's.
- No peak hours. PeakOf judged the vendor by the provider's URLs, and a
  plugin provider's are plugin://, which matched nothing. plugin:// URLs
  are now left out, so the plan is told by its id as before.
- A user's own price was flat at every hour. It is now the off-peak base,
  doubled in the peak hours of a vendor that bills by the hour (DeepSeek,
  Command Code's DeepSeek V4, OpenCode Go's two), as the vendor does to
  its own price. Chosen over a second "peak price" field: smaller, no new
  UI. The editor's hint says it ("Off-peak price, the list's or one set
  here: ... doubles it", en/zh/zh-TW/ja/de).
  Behaviour change for existing users: a price already set on such a model
  now counts double in those hours on the Usage page. It was not left
  alone because the vendor itself doubles whatever the off-peak price is;
  a price set for every other model is unchanged (tested: OpenCode Zen and
  an OpenAI model hold at every hour).

Tests:
- TestCommandCodePlanPrice (new, models.dev's bytes of 2026-10-30): the
  plan's plugin provider lists 0.15/0.6/0.003, 0.30 at peak; DeepSeek V4
  Pro keeps DeepSeek's 0.66; a set price is 0.1 off-peak, 0.2 at peak; a
  relay doesn't take Command Code's catalog.
- TestPeakOf: the plan with plugin:// URLs, its flash-fast, another plugin.
- TestPriceOfPeak: a set price doubles at peak; Zen and OpenAI set prices
  don't.
- model-price-edit.test.cjs: the hint names a price set here.

Broken by hand (each red, then put back):
- peak.go at origin/main: TestPeakOf plugin case and four
  TestCommandCodePlanPrice checks red.
- vendorListPrice off: "list price ... false", "unpriced".
- the user price's Peak line removed: TestPriceOfPeak (3 keys) and
  TestCommandCodePlanPrice (2 keys) red.
- origin/main's app.js with the new GUI test: model-price-edit red.

Run:
- mote-run go test ./... -timeout 45m -count=1 at acdc85ac8 + this change:
  every package ok but internal/gui, whose 10 reds are its real-window
  AppKit tests (TestDockJoinLeadsCommandTab, TestPanelCloseHidesIt,
  TestMainWindowCorners, ...) killed at their deadlines while pboard was
  down on mote (pgrep pboard empty, crash reports 10-29 22:45): #4125.
  The size budgets in it passed.
- mote-run GOOS=linux go vet ./internal/provider, GOOS=windows go vet
  ./internal/provider ./internal/gui: ok.
- wsl-run model-price-edit.test.cjs: Chromium 2/2, WebKit 2/2.
- wsl-run, Chromium and WebKit: i18n-keys, gui-ja, gui-zh-tw,
  model-price-edit, model-price-tiers, provider-price-rate,
  usage-unpriced-names, usage-unpriced-sum, commandcode-risk.
  Each engine 27 pass, 1 fail: gui-ja's "ja has every zh string", on
  the Routing plugin's connection-check string from da3c5e0c0, red on
  origin/main a0cb8b646 too; fixed in the next commit (gui-ja and
  i18n-keys 7/7 after it).
- After rebasing onto origin/main 2df46e3c8 (which added i18n.js lines):
  mote-run go test ./internal/provider: ok; internal/gui -run
  'Budget|Size': ok; wsl-run i18n-keys, gui-ja, gui-zh-tw,
  model-price-edit with the next commit: 14/14.

Not tried:
- No real Command Code bill compared with magpie's count, and no real
  plan account used: the prices are models.dev's, the hours are the
  ones 9e73f69af already uses for Command Code.
- The whole GUI suite: the WSL box had ten other suites running (some for
  4h+); it runs after this push and is reported separately.
- Not checked against the real catalog: whether another relay with no
  catalog of its own that serves the undated deepseek-v4.1-flash is
  unpriced the same way since models.dev's dated canonical.
2026-10-30 23:00:01 +08:00

231 lines
11 KiB
Go

package provider
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/settings"
)
// ccPlugin is the Command Code plan as its plugin serves it: its
// requests go to plugin://, which names no vendor (treeman on Discord).
var ccPlugin = Provider{ID: CommandCodePlanID, Chat: pluginBase + CommandCodePlanID + "/v1", Responses: pluginBase + CommandCodePlanID + "/v1"}
// Only the vendors whose own pricing bills DeepSeek's models by the hour
// are: DeepSeek's API, Command Code's DeepSeek V4 models (not its flat
// deepseek-v4-flash-fast) and OpenCode Go's two. OpenCode Zen, a relay, a
// lookalike host and every other model keep one price.
func TestPeakOf(t *testing.T) {
preset := func(id string) Provider {
p, err := FromPreset(id)
if err != nil {
t.Fatal(err)
}
return p
}
for _, c := range []struct {
name string
p Provider
model string
want catalog.Peak
}{
{"deepseek", preset("deepseek"), "deepseek-flash", catalog.PeakDeepSeek},
{"deepseek pro", preset("deepseek"), "deepseek-v4-pro", catalog.PeakDeepSeek},
{"commandcode", preset("commandcode"), "deepseek/deepseek-v4.1-flash", catalog.PeakWeekdays},
{"commandcode pro", preset("commandcode"), "deepseek/deepseek-v4-pro", catalog.PeakWeekdays},
{"commandcode flash-fast", preset("commandcode"), "deepseek/deepseek-v4-flash-fast", ""},
{"commandcode other", preset("commandcode"), "moonshotai/kimi-k3", ""},
{"commandcode plan", Provider{ID: CommandCodePlanID}, "deepseek/deepseek-v4.1-flash", catalog.PeakWeekdays},
{"commandcode plan's plugin", ccPlugin, "deepseek/deepseek-v4.1-flash", catalog.PeakWeekdays},
{"commandcode plan's plugin flash-fast", ccPlugin, "deepseek/deepseek-v4-flash-fast", ""},
{"another plugin", Provider{ID: "zen-free", Chat: pluginBase + "zen-free/v1"}, "deepseek-v4.1-flash", ""},
{"opencode go", preset("opencode-go"), "deepseek-v4.1-flash", catalog.PeakWeekdays},
{"opencode go other", preset("opencode-go"), "kimi-k3", ""},
{"opencode zen", preset("opencode-zen"), "deepseek-v4.1-flash", ""},
{"relay", Provider{ID: "relay", Chat: "https://relay.example/v1"}, "deepseek-v4.1-flash", ""},
{"relay named deepseek", Provider{ID: "deepseek", Preset: "deepseek", Chat: "https://relay.example/v1"}, "deepseek-flash", ""},
{"lookalike host", Provider{ID: "x", Chat: "https://api.deepseek.com.evil.example/v1"}, "deepseek-flash", ""},
{"removed deepseek", Provider{ID: "deepseek"}, "deepseek-flash", catalog.PeakDeepSeek},
{"openai", preset("openai"), "gpt-6-astra", ""},
} {
if got := c.p.PeakOf(c.model); got != c.want {
t.Errorf("%s %s: %q, want %q", c.name, c.model, got, c.want)
}
}
}
// useDeepSeekCatalog puts a catalog in the cache that lists DeepSeek's
// deepseek-flash at its off-peak price, as models.dev's api.json does
// (its peak multiplier left out), OpenCode Zen's deepseek-v4.1-flash at
// its flat price and an OpenAI model: no network.
func useDeepSeekCatalog(t *testing.T) {
t.Helper()
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"))
model := func(id string, in, out float64) map[string]any {
return map[string]any{"id": id, "cost": map[string]any{"input": in, "output": out}}
}
b, _ := json.Marshal(map[string]any{
"deepseek": map[string]any{"id": "deepseek", "models": map[string]any{
"deepseek-flash": model("deepseek-flash", 0.15, 0.6), "deepseek-v4.1-flash": model("deepseek-v4.1-flash", 0.15, 0.6)}},
"opencode": map[string]any{"id": "opencode", "models": map[string]any{
"deepseek-v4.1-flash": model("deepseek-v4.1-flash", 0.3, 1.2)}},
"openai": map[string]any{"id": "openai", "models": map[string]any{
"gpt-6-astra": model("gpt-6-astra", 2, 8)}},
})
if err := os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(catalog.CachePath(), b, 0o600); err != nil {
t.Fatal(err)
}
catalog.Reset()
t.Cleanup(catalog.Reset)
}
// A DeepSeek model's list price doubles at DeepSeek's peak hours, at the
// provider's price rate still, and so does a price the user set for it:
// the vendor doubles whatever its off-peak price is. OpenCode Zen's flat
// price and another vendor's model hold at every hour, set or not.
func TestPriceOfPeak(t *testing.T) {
useDeepSeekCatalog(t)
peak := time.Date(2026, 10, 29, 2, 0, 0, 0, time.UTC) // a Thursday
off := time.Date(2026, 10, 29, 11, 0, 0, 0, time.UTC)
preset := func(id string) Provider {
p, err := FromPreset(id)
if err != nil {
t.Fatal(err)
}
return p
}
at := func(s settings.Settings, p Provider, model string, tm time.Time) float64 {
t.Helper()
pr, ok := priceOf(s, p, true, model, time.Time{})
if !ok {
t.Fatalf("%s %s: unpriced", p.ID, model)
}
return pr.In(tm).Input
}
none := settings.Settings{}
ds := preset("deepseek")
if a, b := at(none, ds, "deepseek-flash", peak), at(none, ds, "deepseek-flash", off); a != 0.3 || b != 0.15 {
t.Errorf("deepseek-flash: %v at peak, %v off-peak; want 0.3, 0.15", a, b)
}
half := ds
half.PriceRate = 0.5
if a := at(none, half, "deepseek-flash", peak); a != 0.15 {
t.Errorf("deepseek-flash at a 0.5 rate: %v at peak, want 0.15", a)
}
cc := preset("commandcode") // no catalog of its own: DeepSeek's price
if a, b := at(none, cc, "deepseek/deepseek-v4.1-flash", peak), at(none, cc, "deepseek/deepseek-v4.1-flash", off); a != 0.3 || b != 0.15 {
t.Errorf("commandcode deepseek-v4.1-flash: %v at peak, %v off-peak", a, b)
}
zen := preset("opencode-zen")
if a, b := at(none, zen, "deepseek-v4.1-flash", peak), at(none, zen, "deepseek-v4.1-flash", off); a != 0.3 || b != 0.3 {
t.Errorf("opencode zen deepseek-v4.1-flash: %v at peak, %v off-peak; want 0.3 both", a, b)
}
oa := preset("openai")
if a, b := at(none, oa, "gpt-6-astra", peak), at(none, oa, "gpt-6-astra", off); a != 2 || b != 2 {
t.Errorf("gpt-6-astra: %v at peak, %v off-peak; want 2 both", a, b)
}
// the user's own price for the model, or for every model of DeepSeek
in, out, cache := 0.2, 0.8, 0.02
for _, key := range []string{"deepseek/deepseek-flash", "deepseek/*", AnyPriceKey("deepseek-flash")} {
s := settings.Settings{ModelPrices: map[string]settings.ModelPrice{key: {Input: &in, Output: &out, CacheRead: &cache, CacheWrite: &cache}}}
if a, b := at(s, ds, "deepseek-flash", peak), at(s, ds, "deepseek-flash", off); a != 0.4 || b != 0.2 {
t.Errorf("set at %s: %v at peak, %v off-peak; want 0.4, 0.2", key, a, b)
}
}
for _, c := range []struct {
p Provider
model string
}{{zen, "deepseek-v4.1-flash"}, {oa, "gpt-6-astra"}} {
for _, key := range []string{c.p.ID + "/" + c.model, AnyPriceKey(c.model)} {
s := settings.Settings{ModelPrices: map[string]settings.ModelPrice{key: {Input: &in, Output: &out, CacheRead: &cache, CacheWrite: &cache}}}
if a, b := at(s, c.p, c.model, peak), at(s, c.p, c.model, off); a != 0.2 || b != 0.2 {
t.Errorf("%s set at %s: %v at peak, %v off-peak; want 0.2 both", c.p.ID, key, a, b)
}
}
}
// a provider gone since, priced at its maker's
if pr, ok := priceOf(none, Provider{ID: "deepseek"}, false, "deepseek-flash", time.Time{}); !ok || pr.In(peak).Input != 0.3 {
t.Errorf("removed deepseek: %+v %v", pr, ok)
}
}
// commandCodeCatalog is models.dev's api.json as magpie had it on
// 2026-10-30, these entries byte for byte (descriptions and the like left
// out). DeepSeek lists V4.1 Flash only as deepseek-flash, by a dated
// canonical id, so no maker price is found for Command Code's undated
// deepseek/deepseek-v4.1-flash; Command Code's own catalog lists it.
const commandCodeCatalog = `{
"deepseek":{"id":"deepseek","api":"https://api.deepseek.com","name":"DeepSeek","models":{
"deepseek-flash":{"id":"deepseek-flash","name":"DeepSeek Flash","cost":{"input":0.15,"output":0.6,"cache_read":0.003},"canonical_model_id":"deepseek/deepseek-v4.1-flash-1017"},
"deepseek-v4-pro":{"id":"deepseek-v4-pro","name":"DeepSeek V4 Pro","cost":{"input":0.66,"output":1.98,"cache_read":0.013},"canonical_model_id":"deepseek/deepseek-v4-pro"}}},
"commandcode":{"id":"commandcode","env":["CMD_API_KEY"],"npm":"@ai-sdk/openai-compatible","api":"https://api.commandcode.ai/provider/v1","name":"CommandCode","doc":"https://commandcode.ai/docs/provider","models":{
"deepseek/deepseek-v4.1-flash":{"id":"deepseek/deepseek-v4.1-flash","name":"DeepSeek V4.1 Flash","family":"deepseek-flash","cost":{"input":0.15,"output":0.6,"cache_read":0.003},"canonical_model_id":"deepseek/deepseek-v4.1-flash"},
"deepseek/deepseek-v4-pro":{"id":"deepseek/deepseek-v4-pro","name":"DeepSeek V4 Pro","cost":{"input":0.9,"output":2.7,"cache_read":0.02},"canonical_model_id":"deepseek/deepseek-v4-pro"}}}
}`
// The Command Code plan, served by its plugin, prices DeepSeek V4.1 Flash
// at Command Code's list price, doubled in its peak hours, where it showed
// no price at all and its calls were counted at nothing (treeman on
// Discord). A model DeepSeek prices keeps DeepSeek's price, and a price
// the user set is doubled at peak too.
func TestCommandCodePlanPrice(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"))
if err := os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(catalog.CachePath(), []byte(commandCodeCatalog), 0o600); err != nil {
t.Fatal(err)
}
catalog.Reset()
t.Cleanup(catalog.Reset)
peak := time.Date(2026, 10, 29, 2, 0, 0, 0, time.UTC) // a Thursday
off := time.Date(2026, 10, 29, 11, 0, 0, 0, time.UTC)
price := func(s settings.Settings, model string) catalog.Price {
t.Helper()
pr, ok := priceOf(s, ccPlugin, true, model, time.Time{})
if !ok {
t.Fatalf("%s: unpriced", model)
}
return pr
}
none := settings.Settings{}
if pr, ok := ccPlugin.ListPrice("deepseek/deepseek-v4.1-flash"); !ok || pr.Input != 0.15 || pr.Output != 0.6 || pr.CacheRead != 0.003 {
t.Errorf("list price: %+v %v; want 0.15/0.6/0.003", pr, ok)
}
pr := price(none, "deepseek/deepseek-v4.1-flash")
if a, b := pr.In(peak), pr.In(off); a.Input != 0.3 || a.Output != 1.2 || b.Input != 0.15 || b.Output != 0.6 {
t.Errorf("deepseek-v4.1-flash: %+v at peak, %+v off-peak", a, b)
}
// DeepSeek's own price comes first, as before
if pr := price(none, "deepseek/deepseek-v4-pro"); pr.In(off).Input != 0.66 || pr.In(peak).Input != 1.32 {
t.Errorf("deepseek-v4-pro: %+v", pr)
}
in, out, cache := 0.1, 0.4, 0.0
for _, key := range []string{CommandCodePlanID + "/deepseek/deepseek-v4.1-flash", AnyPriceKey("deepseek/deepseek-v4.1-flash")} {
s := settings.Settings{ModelPrices: map[string]settings.ModelPrice{key: {Input: &in, Output: &out, CacheRead: &cache, CacheWrite: &cache}}}
pr := price(s, "deepseek/deepseek-v4.1-flash")
if a, b := pr.In(peak), pr.In(off); a.Input != 0.2 || a.Output != 0.8 || b.Input != 0.1 || b.Output != 0.4 {
t.Errorf("set at %s: %+v at peak, %+v off-peak", key, a, b)
}
}
// another provider's id says nothing of Command Code's catalog
if _, ok := (Provider{ID: "relay", Chat: "https://relay.example/v1"}).ListPrice("deepseek/deepseek-v4.1-flash"); ok {
t.Error("a relay took Command Code's list price")
}
}