Files
magpie/internal/gui/sessions_gateway_keys_test.go
T
yetoneandskygunner 28a79bc33a gui: the gateway-mode stats test holds its clock at a local 00:30 (#5690)
TestGatewayModeStatsNameTheGatewaySessions wrote its record at
time.Now()-1h, which in the first hour of a local day is yesterday, so
days=1 left it out: red on macOS and ubuntu CI at 4167419ab (00:31 UTC)
and on mote at 00:40 PST (#5690). The test now runs in a testing/synctest
bubble whose clock is moved to the next local 00:30, writes the record at
that moment, and checks the clock didn't move during the queries. A usage
test pins GatewaySessionWindow's edge: a local midnight is left out, one
nanosecond after it is in, on two consecutive days.

skygunner's patch (1554065a on their fork, posted on #5690 on 11-08),
applied as written on origin/main e12244aca; it waited three days unported.

Run on mote at this tree:
- The old fixture (-1h) inside the bubble: red at days=1 (TZ=UTC).
- -race -count=3 of both tests in UTC, America/Los_Angeles,
  Asia/Shanghai and Australia/Sydney: ok.
- go test -tags nogui ./internal/gui ./internal/usage: ok; go vet ok;
  gofmt -l clean.
Not run: the full suite (test-only change).

Co-authored-by: skygunner <skygunner@users.noreply.github.com>
2026-11-11 08:51:15 +08:00

79 lines
2.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"slices"
"strings"
"testing"
"testing/synctest"
"time"
"github.com/yetone/magpie/internal/sessions"
"github.com/yetone/magpie/internal/usage"
)
// linkaww on Discord, a magpie in Docker in gateway mode: Usage › Sessions
// was all but empty. Its list is the sessions whose keys the stats name
// (st.keys), and those were only the sessions read from this computer's
// files: one that came through the gateway alone, which in gateway mode
// is every one, was counted in the totals and never listed.
func TestGatewayModeStatsNameTheGatewaySessions(t *testing.T) {
sandboxHome(t)
synctest.Test(t, func(t *testing.T) {
now := time.Now().In(time.Local)
midnight := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
if !now.Before(midnight.Add(30 * time.Minute)) {
midnight = midnight.AddDate(0, 0, 1)
}
time.Sleep(midnight.Add(30 * time.Minute).Sub(now))
sessions.Reset()
forgetStats()
t.Cleanup(func() {
webPage.Store(false)
ownWas.Store(0)
sessions.Reset()
forgetStats()
})
const remote = "8c1f0e7a-1111-4222-8333-444455556666"
at := time.Now()
usage.Append(usage.Record{Time: at, Agent: "claude", Session: remote, Provider: "fake", Model: "m", Input: 50, Output: 5, Status: 200})
mux := http.NewServeMux()
sessionRoutes(mux, folderOnly{})
gatewayModeRoutes(mux)
ok := func(method, path, body string) []byte {
t.Helper()
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest(method, path, strings.NewReader(body)))
if w.Code != 200 {
t.Fatalf("%s %s: %d %s", method, path, w.Code, w.Body)
}
return w.Body.Bytes()
}
keys := func(days string) []string {
t.Helper()
var st struct{ Keys []string }
if err := json.Unmarshal(ok("GET", "/api/sessions/stats?days="+days, ""), &st); err != nil {
t.Fatal(err)
}
return st.Keys
}
for _, mode := range []string{"off", "on"} {
if mode == "on" {
webPage.Store(true)
ok("POST", "/api/settings/gateway-mode", `{"mode":"on"}`)
}
for _, days := range []string{"0", "7", "1"} {
if k := keys(days); !slices.Contains(k, "claude:"+remote) {
t.Fatalf("gateway mode %s, days=%s: keys %v leave out the gateway's session", mode, days, k)
}
if now := time.Now(); !now.Equal(at) {
t.Fatalf("virtual clock advanced during stats query: %v to %v", at, now)
}
}
}
})
}