Files
magpie/internal/provider/usage_autoref_test.go
T
yetoneandNorethSea 4921bf5a97 usage, sessions: one model option for a found routing group's two ids (#5892, Norethcy on X)
A found routing group is called by its public id (auto/<model>) and is
kept as group/auto-<slug>; calls recorded under either were counted as two
models, so Usage › Sessions' model filter listed the same group twice and
the ledger, summaries and session lists split its calls.

Ported from NorethSea's 30d6261cd on their fork: UsageAlias names a found
group by its public id before the user's alias (both spellings, old
session files too), sessions' lists, the ledger keyer, the summary cache's
key and Codex's model/effort read through it, and a cache keyed on the
group names (UsageNamesKey) is read again when a group is renamed. A user's
own provider named "auto" keeps auto/<model> (autoRefsOn).

Changed in review: the patch's ledger filter compared every record with
provider.SameRef, which reads the providers (customIDs, a stat of
providers.json) for each record a filter passes over. FilterOf, the one
place a Filter is made from a query, now canonicalizes the model once, and
keepsRecord asks the providers only for an auto/ record under a group/
filter (sameGroupModel).

Run (mote's sshd reset every connection all afternoon, so on the Linux box
through wsl-run; the package run at base 720200092, and the -run tests,
quota-holds and i18n-keys again at this head on 208b89422; rebased since onto 44732325e,
a gateway test only):
- go test -count=1 -tags nogui ./internal/gui ./internal/provider
  ./internal/sessions ./internal/usage ./internal/usageapi: ok
- go vet -tags nogui (linux), GOOS=darwin CGO_ENABLED=0 go vet (provider,
  usage, usageapi, sessions), and go vet on Windows (win-run, those and
  internal/gui): clean. gofmt -l: clean.
- GUI: usage-ledger-model-pick, usage-ledger, quota-fit-batched,
  i18n-keys in chromium and webkit. usage-ledger's "newest live route
  needs no return-to-live button" (and zh "request routing links") is red
  the same on origin/main 987eac0aa on that box: #3104, #4078.

Broken by hand:
- keepsRecord back to r.Model != f.Model: TestUsageFoundGroupNames and
  TestLedgerFoundGroupNames red.
- usageGroupNames returning nothing: TestUsageFoundGroupNames (provider,
  usage), TestSessionsFoundGroupNames, TestCodexEffortFoundGroupNames,
  TestLedgerFoundGroupNames red.

Not tried: a macOS run (mote unreachable), the darwin gui package's vet,
and the reporter's own usage log.

Co-authored-by: NorethSea <963979204@qq.com>
2026-11-10 18:01:13 +08:00

62 lines
1.9 KiB
Go

package provider
import (
"encoding/json"
"os"
"testing"
)
// Found groups keep one usage name, even when an agent wrote the old id or
// another vendor spelling into a session. Ordinary models remain distinct.
func TestUsageFoundGroupNames(t *testing.T) {
autoRefHome(t)
alias := UsageAlias()
if alias == nil {
t.Fatal("found group has no usage name mapper")
}
for _, ref := range []string{"group/auto-claude-opus-5-5", "auto/claude-opus-5-5", "auto/claude-opus-5.5"} {
if got := alias(ref); got != "auto/claude-opus-5.5" {
t.Errorf("%s counted as %q", ref, got)
}
if got := alias(ref + ":high[1m]"); got != "auto/claude-opus-5.5:high[1m]" {
t.Errorf("effort/window lost: %q", got)
}
}
for _, ref := range []string{"claude-opus-5-5", "a/claude-opus-5-5", "group/mine", "group/auto-gone", "auto/gone"} {
if got := alias(ref); got != ref {
t.Errorf("unrelated model %q counted as %q", ref, got)
}
}
key := UsageNamesKey()
if _, err := SetUsageAlias("claude-opus-5.5", "opus"); err != nil {
t.Fatal(err)
}
alias = UsageAlias()
if alias("group/auto-claude-opus-5-5") != "opus" || alias("auto/claude-opus-5.5") != "opus" {
t.Fatal("the two ids did not follow the same user alias")
}
if UsageNamesKey() == key {
t.Fatal("a user's alias did not invalidate usage names")
}
}
func TestUsageFoundGroupNamesKeepAutoProvider(t *testing.T) {
autoRefHome(t)
// A provider saved before auto was reserved still owns this namespace.
f := load()
f.Providers = append(f.Providers, Provider{ID: "auto", Name: "Legacy auto", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"claude-opus-5.5"}})
b, err := json.Marshal(f)
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(Path(), b, 0o600); err != nil {
t.Fatal(err)
}
alias := UsageAlias()
for _, ref := range []string{"auto/claude-opus-5.5", "group/auto-claude-opus-5-5"} {
if alias != nil && alias(ref) != ref {
t.Fatalf("legacy provider's %s was renamed", ref)
}
}
}