Files
magpie/internal/gui/gatewaymode_test.go
T
yetone f44fa2ef6a sessions: gateway mode lists only the gateway's sessions, never this computer's agents' files
paultian on Discord: magpie web in gateway mode, on a machine with Claude
Code and Codex installed, listed that machine's own sessions beside the
ones other computers sent through it. Owner's call (via the lead): keep
Sessions in gateway mode, scope it.

- sessions.OwnOff (set by the gui to ownSessionsOff): while it holds,
  currentReads answers "none", Found and Dirs are empty, the trash lists
  and restores/purges only what the gateway kept, and moving Claude
  Code's folder is refused. Every route built on Reads follows: the list,
  one, transcript, markdown, export, stats, overview, search, manage,
  delete, carry, Usage's agent-file calls. Gateway-recorded sessions
  (ledger projection, recorded conversations) are not behind the gate.
- ownSessionsOff: magpie web's page, and Settings' gateway mode On, or
  Automatic with --gateway or no agents found (looked at once per 30s,
  and again on another HOME/PATH). A turn of the answer resets the
  sessions' caches and stats; the gateway-mode route does it at once.
- /api/sessions/reads says gateway:true: the page asks nothing, its empty
  state names the gateway's sessions (en/zh/zh-TW/ja/de), and Settings ›
  Privacy leaves out Agents' session history.
- The gateway-mode subtitle (5 languages), docs/reference.md and
  site docs/docker.html (en/zh/ja) say Sessions stays and what it lists
  (they said there was no Sessions tab, which was already wrong);
  docs/subsystems/gateway-sessions.md has the design.
- Tests: Handler marks the process as magpie web for good, and 13 tests
  served magpie web's page, so a later test's sandbox could be in gateway
  mode by itself. webPageHandler ends the mark with the test, and
  TestWebPageHandlerEveryWhere fails on a direct Handler(webHost{...}).

Ran (mote, at this tree rebased on origin/main):
- TestGatewayModeListsNoOwnSessions: red on the old code, green now.
- gateway-mode-sessions.test.cjs: chromium + webkit, en 900/390, zh,
  zh-TW, ja 420, de 390: 36/36; red on the old client.js (every case).
- GUI files the change reaches (gateway-mode*, sessions-*, session-*,
  privacy-*, usage-sessions-*, i18n-keys), both engines: 657/657.
- TestCoreScriptBudget ok.
- go test -shuffle=on ./internal/gui ./internal/sessions: sessions ok;
  gui ok but TestMainWindowCorners/macOS_26_SDK, red the same on
  origin/main 2f726b1f9 on mote (#3949).
- go vet, GOOS=linux go vet -tags nogui, GOOS=windows go vet of both
  packages; gofmt -l clean.

Broken by hand: OwnOff answering false (the Go test goes red on the list,
stats, manage, one, transcript); the Purge guard and the Restore guard
each (red on purge / restore); the old client.js and settings.js (the GUI
test red). An injected stale agents memo and leaked webPage did not
reproduce the one TestUsageRequestContent red seen in the first package
run; it did not come back in two more runs, one shuffled.

Not tried: a real magpie web on a server with real agents on other
computers; the Go test on Windows and Linux boxes; the 30s memo under
real use.
2026-11-04 20:20:41 +08:00

105 lines
3.5 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/settings"
)
// Gateway mode (Player on Discord): magpie web with no agents on its
// machine is in it by itself, and so is one started with --gateway;
// Settings' On or Off wins over both, and the Settings page's own save
// leaves it as it is. The app's windows are never in it. The page is told
// before it paints (boot.js), and the settings say why.
func TestGatewayMode(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
here := false
was := agentsHere
agentsHere = func() bool { return here }
t.Cleanup(func() { agentsHere = was; WebGateway.Store(false); webPage.Store(false) })
srv := webPageHandler(t, webHost{func() {}}, nil)
do := func(method, path, body string) *httptest.ResponseRecorder {
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, httptest.NewRequest(method, path, strings.NewReader(body)))
return rec
}
type state struct {
GatewayMode string
GatewayOn bool
GatewayWhy string
Web bool
}
check := func(when string, on bool, why string) {
t.Helper()
var s state
if rec := do("GET", "/api/settings", ""); rec.Code != http.StatusOK || json.Unmarshal(rec.Body.Bytes(), &s) != nil {
t.Fatalf("%s: settings %d %s", when, rec.Code, rec.Body)
}
if !s.Web || s.GatewayOn != on || s.GatewayWhy != why {
t.Fatalf("%s: settings say %+v, want on=%v why=%q", when, s, on, why)
}
boot := do("GET", "/boot.js", "").Body.String()
if strings.Contains(boot, `"gateway":true`) != on {
t.Fatalf("%s: boot.js %s", when, boot)
}
}
set := func(mode string) {
t.Helper()
if rec := do("POST", "/api/settings/gateway-mode", `{"mode":"`+mode+`"}`); rec.Code != http.StatusOK {
t.Fatalf("set %q: %d %s", mode, rec.Code, rec.Body)
}
}
check("no agents here", true, "no-agents")
here = true
check("an agent here", false, "")
WebGateway.Store(true)
check("--gateway", true, "flag")
set("off")
check("turned off with --gateway", false, "off")
if settings.Load().GatewayMode != "off" {
t.Fatalf("saved %q", settings.Load().GatewayMode)
}
// the Settings page's save, which sends what it was drawn with, leaves it
if rec := do("POST", "/api/settings", `{"theme":"dark","lang":"en","gatewayMode":"on"}`); rec.Code != http.StatusOK {
t.Fatalf("settings save: %d %s", rec.Code, rec.Body)
}
check("after the Settings page's save", false, "off")
WebGateway.Store(false)
set("on")
check("turned on with agents here", true, "on")
set("")
check("automatic again", false, "")
if rec := do("POST", "/api/settings/gateway-mode", `{"mode":"maybe"}`); rec.Code == http.StatusOK || settings.Load().GatewayMode != "" {
t.Fatalf("a mode not offered was taken: %d", rec.Code)
}
// the app's windows: never, whatever Settings says
set("on")
rec := httptest.NewRecorder()
Handler(&zoomWindows{}, nil).ServeHTTP(rec, httptest.NewRequest("GET", "/boot.js", nil))
if strings.Contains(rec.Body.String(), `"gateway":true`) {
t.Fatalf("the app's window was told gateway mode: %s", rec.Body)
}
if on, _ := gatewayMode(false); on {
t.Fatal("gatewayMode(false) is on")
}
// a sync or a restored backup never brings it from another computer
in := settings.Settings{GatewayMode: "off"}
in.KeepOwn(settings.Settings{GatewayMode: "on"})
if in.GatewayMode != "on" {
t.Fatalf("KeepOwn: %q", in.GatewayMode)
}
}