Files
magpie/internal/cli/model_alias_cli_test.go
T
yetone ea9454d2d8 usage: count two ids of one model as one, magpie model alias (SlouchyQuill507 on Discord)
A vendor names one model two ways (deepseek-v4.1-flash and deepseek-flash),
and Usage, Usage > Sessions and `magpie usage` showed two rows. Now:

  magpie model alias deepseek-v4.1-flash deepseek-flash
  magpie model alias deepseek-v4.1-flash            (what it is counted as)
  magpie model alias deepseek-v4.1-flash --reset
  magpie model aliases

kept as settings.json "usageAliases" {from: to}. Off until one is written.
Ids match as routing groups match them (provider.sameModel: vendor/ prefix,
case, "_", snapshot date), at every provider; a chain is followed to its
end; an alias to itself, a loop, or a flag as an id is refused. A loop
written into the file by hand counts each model under its own id.

Display only: requests, agents' model lists, /v1/models, routing groups,
Same as and prices never read it. Each call is priced by the id it was
made under and only then counted under the alias; the alias's own price
is never put on the other id's calls, and a merged row is priced only
when every id in it is. Applied in usage summarize/direct (Models
groups), the gateway sessions projection (alias read before
gatewayCache's lock; the cache key hashes usageAliases), and
sessions.List/Get/Find/Stats (read before storeMu/mu, merged into new
slices so the memo is untouched; ModelEffort rows merged too). A failed
settings.json read keeps the last aliases read, and SetUsageAlias refuses
to write over an unreadable file (EditUsageAliases under fileMu). The GUI
settings save carries usageAliases. No GUI control.

Not aliased: the Requests ledger's per-request model and its model
filter, which show each call's own id.

Run (worktree at f4640c0ae, rebased onto 2b4c0664e with no overlap):
- gofmt -l: clean. go vet ./... darwin, GOOS=linux -tags nogui, GOOS=windows: ok.
- go test -count=1 -timeout 45m (sandbox HOME) internal/usage,
  sessions, provider, cli, settings, gui, tui: all ok.
- Real binary in a sandbox HOME: two usage.jsonl calls priced $2 and
  $100 per M: `magpie usage` showed two model rows ($2.00, $100), and
  after `magpie model alias` one row, 2.0M, 2 calls, $102; the session
  row too. alias/aliases/--reset/loop/self output checked by hand.

Broken by hand, each red then restored:
- no alias in summarizeFrom: TestUsageAlias* red
- nil alias into the gateway projection: TestUsageAlias* red
- statsIn without aliasStats / List without aliasSession:
  TestSessionsCountAliasedModelsAsOne red
- merged usage row keeps Priced true: TestAliasStatsLeavesItsInputAlone red
- loop check off / self check by == / last-good read off / flag check off:
  TestUsageAliasRefusesSelfAndLoops, TestUsageAliasUnreadSettings,
  TestModelAliasCmd red
- GUI save drops UsageAliases: TestSettingsSaveKeepsWhatItDoesNotSend red

Not tried: the GUI pages in a browser (no app.js change; the Usage and
Sessions pages read the same rows), the full go test ./... before push,
Windows box.
2026-10-24 02:48:50 +08:00

56 lines
2.2 KiB
Go

package cli
import (
"strings"
"testing"
"github.com/yetone/magpie/internal/settings"
)
// magpie model alias counts a model's usage under another id, shows it,
// lists it under aliases and takes it off with --reset; it refuses a
// model aliased to itself, a loop, and a flag taken as a model id.
func TestModelAliasCmd(t *testing.T) {
groupsHome(t)
out, err := runOut(t, "model", "aliases")
if err != nil || !strings.Contains(out, "no model is counted under another id yet") {
t.Fatalf("empty list: %q %v", out, err)
}
if out, err = runOut(t, "model", "alias", "deepseek-v4.1-flash", "deepseek-flash"); err != nil || !strings.Contains(out, "counted as deepseek-flash") {
t.Fatalf("set: %q %v", out, err)
}
if got := settings.Load().UsageAliases; got["deepseek-v4.1-flash"] != "deepseek-flash" {
t.Fatalf("settings: %v", got)
}
if out, err = runOut(t, "model", "alias", "deepseek/deepseek-v4.1-flash"); err != nil || !strings.Contains(out, "deepseek-v4.1-flash") || !strings.Contains(out, "counted as deepseek-flash") {
t.Fatalf("show: %q %v", out, err)
}
if out, err = runOut(t, "model", "aliases"); err != nil || !strings.Contains(out, "deepseek-v4.1-flash → deepseek-flash") {
t.Fatalf("list: %q %v", out, err)
}
for _, bad := range [][]string{
{"model", "alias", "deepseek-flash", "DeepSeek_Flash"},
{"model", "alias", "deepseek-flash", "deepseek-v4.1-flash"},
{"model", "alias", "deepseek-flash", "--force"},
} {
if _, err := runOut(t, bad...); err == nil {
t.Fatalf("took %v", bad)
}
}
if out, err = runOut(t, "model", "alias", "--help"); err != nil || !strings.Contains(out, "magpie model alias <model> <name>") {
t.Fatalf("--help: %q %v", out, err)
}
if got := settings.Load().UsageAliases; len(got) != 1 {
t.Fatalf("a refused alias was written: %v", got)
}
if out, err = runOut(t, "model", "alias", "deepseek-v4.1-flash", "--reset"); err != nil || !strings.Contains(out, "its own id again") {
t.Fatalf("reset: %q %v", out, err)
}
if got := settings.Load().UsageAliases; got != nil {
t.Fatalf("after reset: %v", got)
}
if out, err = runOut(t, "model", "alias", "deepseek-v4.1-flash"); err != nil || !strings.Contains(out, "counted under its own id") {
t.Fatalf("show after reset: %q %v", out, err)
}
}