Files
magpie/internal/gui/sessions_gateway_keys_test.go
T
yetone 371b2b63a0 sessions: gateway mode lists the sessions the gateway counted
Usage › Sessions lists only the stats' keys, and the keys named only the
agent files this computer reads. A Docker magpie in gateway mode reads none,
so the sessions that came through the gateway were counted and not listed.
MergeExternalStats now puts them in the keys too, in the list's order.

Run (mote): go test ./internal/sessions ./internal/gui -run
'TestGatewayStats|TestGatewayMode' ok; GOOS=linux go vet -tags nogui ./...
and GOOS=windows go vet ./... clean, gofmt -l clean.
Broken by hand: on the old code the unit test's keys are [claude:a] and the
gui test's [] (gateway mode off, days=0).
Not tried: a real Docker magpie; the full suite (mote rebooted mid-run).

Reported-by: linkaww on Discord
2026-11-08 02:37:09 +08:00

67 lines
1.9 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"
"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)
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().Add(-time.Hour).Truncate(time.Second)
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)
}
}
}
}