Files
magpie/internal/usage/gateway_alias_test.go
T
yetone f53c4a1a14 usage: three tests hold the clock, so midnight never falls between a call and Today (#6486)
TestAliasCallsAreTheGatewaysNotDirect stamped its direct call 6 minutes
before time.Now() and asked Direct(Today), so in the six minutes after
local midnight the call was yesterday's: red on mote at 00:00 PST and on
macOS CI at 00:07 UTC (run 43523008440). TestLoopsCountedPerModel and
TestRequestsCountedOnceWithTheirRetries do the same with stamps up to 9 s
back (same class). All three now take their time from holdClock at a
fixed noon, as TestPeriodsReadTheClock does.

Run (mica, at 783b69b8c + this):
- the three tests -count=3 in TZ=UTC and TZ=Asia/Shanghai: ok
  (and in UTC, Asia/Shanghai, America/Los_Angeles on mote: ok)
- broken by hand: held at 00:00:03 instead of noon, all three red
  (gateway_alias_test.go:44 "0 calls", loop_count_test.go:55,
  retried_requests_test.go:55), so they read the held clock
- go test -count=1 ./internal/usage: ok 53s; go vet ./internal/usage: ok
Not run: the full suite (test-only change in one package).
Not changed: #6474 (TestWalletCapHolds, the 5 minutes before midnight)
is another package's sibling and stays open.
2026-11-14 08:31:52 +08:00

69 lines
3.3 KiB
Go

package usage
import (
"testing"
"time"
"github.com/yetone/magpie/internal/sessions"
)
// CrazyJal on X: Claude Desktop's Code tab writes the id the gateway listed
// the model by (magpie-<number>.anthropic.<Claude model>) as the model of
// each turn, and the gateway's own log counts the turn under the model that
// served it. The file's call is the same call: it showed again under the
// alias, a row for a model that is none, its cost counted twice. A call under
// a model of the agent's own stays (it never went through the gateway).
func TestAliasCallsAreTheGatewaysNotDirect(t *testing.T) {
now := holdClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local)) // midnight never falls between a call and Today (#6486)
tk := sessions.Tokens{Input: 120, Output: 40}
logs := []sessions.Call{
{Time: now.Add(-1 * time.Minute), Agent: "claude-desktop", Session: "s1", Model: "magpie-7416897221.anthropic.claude-opus-5-5", Tokens: tk},
{Time: now.Add(-2 * time.Minute), Agent: "claude-desktop", Session: "s1", Model: "magpie-7416897221.anthropic.claude-opus-5-5[1m]", Tokens: tk},
{Time: now.Add(-3 * time.Minute), Agent: "claude-desktop", Session: "s1", Model: "mythos-magpie-0123456789", Tokens: tk},
{Time: now.Add(-4 * time.Minute), Agent: "claude-desktop", Session: "s1", Model: "anthropic/magpie-0123456789", Tokens: tk},
{Time: now.Add(-5 * time.Minute), Agent: "claude", Session: "s2", Model: "claude-sonnet-5-5", Requested: "claude-magpie-0123456789", Tokens: tk},
{Time: now.Add(-6 * time.Minute), Agent: "claude", Session: "s3", Model: "claude-opus-5-5", Tokens: tk}, // direct, never through the gateway
}
recs := []Record{{Time: now.Add(-time.Minute), Agent: "claude-desktop", Provider: "relay", Model: "claude-opus-5-5", Input: 120, Output: 40}}
rows, _, _, _ := ledgerWith(now.Add(-time.Hour), Filter{}, recs, logs)
if len(rows) != 2 {
t.Fatalf("the gateway's call and the one direct call, got %d rows: %+v", len(rows), rows)
}
for _, r := range rows {
if MagpieAlias(r.Model) || MagpieAlias(r.Requested) {
t.Errorf("a row under an alias: %+v", r)
}
}
old := LogCalls
LogCalls = func(time.Time) []sessions.Call { return logs }
t.Cleanup(func() { LogCalls = old })
d := Direct(Today)
if d.Calls != 1 || len(d.Models) != 1 || d.Models[0].Model != "claude-opus-5-5" {
t.Fatalf("Direct counts the one call outside the gateway: %d calls, models %+v", d.Calls, d.Models)
}
if got := ownCalls(logs); len(got) != 1 || got[0].Session != "s3" {
t.Fatalf("ownCalls: %+v", got)
}
}
func TestMagpieAliasForms(t *testing.T) {
for id, want := range map[string]bool{
"magpie-7416897221.anthropic.claude-opus-5-5": true,
"magpie-7416897221.anthropic.claude-opus-5[1m]": true,
"mythos-magpie-0123456789": true,
"anthropic/magpie-0123456789": true,
"claude-magpie-0123456789[1m]": true,
"magpie": false,
"magpie-123.anthropic.claude-opus-5-5": false, // not ten digits
"claude-opus-5-5": false,
"magpie-agent/claude-opus-5-5": false,
"": false,
} {
if got := MagpieAlias(id); got != want {
t.Errorf("MagpieAlias(%q) = %v, want %v", id, got, want)
}
}
}