From f53c4a1a1451a10a589b9ff02fa29ff33fa9b501 Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 14 Nov 2026 08:31:52 +0800 Subject: [PATCH] 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. --- internal/usage/gateway_alias_test.go | 2 +- internal/usage/loop_count_test.go | 2 +- internal/usage/retried_requests_test.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/usage/gateway_alias_test.go b/internal/usage/gateway_alias_test.go index b93d07ec8..9f373c269 100644 --- a/internal/usage/gateway_alias_test.go +++ b/internal/usage/gateway_alias_test.go @@ -14,7 +14,7 @@ import ( // 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 := time.Now() + 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}, diff --git a/internal/usage/loop_count_test.go b/internal/usage/loop_count_test.go index 41d54cc90..5ef144768 100644 --- a/internal/usage/loop_count_test.go +++ b/internal/usage/loop_count_test.go @@ -19,7 +19,7 @@ func TestLoopsCountedPerModel(t *testing.T) { t.Setenv("USERPROFILE", home) t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache")) - now := time.Now() + now := holdClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local)) // midnight never falls between a call and Today (#6486) at := func(m int) string { return now.Add(time.Duration(m-10) * time.Second).Format(time.RFC3339Nano) } lines := []string{ // two replies of kimi ended for looping, as the gateway logs them: diff --git a/internal/usage/retried_requests_test.go b/internal/usage/retried_requests_test.go index cf8d6bede..c9564f53c 100644 --- a/internal/usage/retried_requests_test.go +++ b/internal/usage/retried_requests_test.go @@ -21,7 +21,7 @@ func TestRequestsCountedOnceWithTheirRetries(t *testing.T) { t.Setenv("USERPROFILE", home) t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache")) - now := time.Now() + now := holdClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local)) // midnight never falls between a call and Today (#6486) at := func(m int) string { return now.Add(time.Duration(m-10) * time.Second).Format(time.RFC3339Nano) } lines := []string{ // written before handed_on: a safety refusal that a group's next