Files
magpie/internal/usageapi/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

42 lines
1.6 KiB
Go

package usageapi
import (
"net/url"
"path/filepath"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/usage"
)
func TestLedgerFoundGroupNames(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"))
for _, id := range []string{"ds-a", "ds-b"} {
if err := provider.Save(provider.Provider{ID: id, Name: id, Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"deepseek-flash"}}); err != nil {
t.Fatal(err)
}
}
for i, ref := range []string{"auto/deepseek-flash", "group/auto-deepseek-flash"} {
usage.Append(usage.Record{Time: time.Now().Add(-time.Duration(i+1) * time.Minute), Agent: "pi", Provider: "relay", Model: ref, Requested: ref, Input: 10, Status: 200})
}
for _, model := range []string{"auto/deepseek-flash", "group/auto-deepseek-flash"} {
if page := LedgerPage(usage.All, FilterOf(url.Values{"model": {model}}), 0, 20); len(page.Rows) != 2 {
t.Fatalf("model filter %s dropped the other id: %d rows", model, len(page.Rows))
}
}
page := LedgerPage(usage.All, FilterOf(url.Values{"model": {"auto/deepseek-flash"}}), 0, 20)
for _, row := range page.Rows {
if row.Model != "auto/deepseek-flash" || row.Requested != "auto/deepseek-flash" {
t.Fatalf("ledger leaked internal name: model %q, requested %q", row.Model, row.Requested)
}
}
if got := page.By["model"]; len(got) != 1 || got[0].ID != "auto/deepseek-flash" {
t.Fatalf("model menu split: %+v", got)
}
}