Files
magpie/internal/gui/web_compress_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

143 lines
5.5 KiB
Go

package gui
import (
"bytes"
"compress/gzip"
"crypto/sha256"
"encoding/hex"
"io"
"io/fs"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
)
// `magpie web` sent its page's ~4 MB of scripts and styles, and the API's
// JSON, as they are: over a 3 Mbps link to a server every load waited for
// all of it (akic404 on Discord). A browser that takes gzip gets them
// gzipped, a client that doesn't gets the bytes as they are, pictures
// already compressed are left alone, and a file asked for again by its
// ETag is still a 304.
func TestWebCompresses(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"))
srv := webHandler("3430", "0123456789abcdef-key", 0, webPageHandler(t, webHost{}, nil))
cookie := &http.Cookie{Name: "magpie_web_3430", Value: "0123456789abcdef-key"}
get := func(path, enc, etag string) *httptest.ResponseRecorder {
r := httptest.NewRequest("GET", path, nil)
r.AddCookie(cookie)
if enc != "" {
r.Header.Set("Accept-Encoding", enc)
}
if etag != "" {
r.Header.Set("If-None-Match", etag)
}
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, r)
return rec
}
// what Chrome and Safari send over plain http
const browser = "gzip, deflate"
for _, f := range []string{"i18n.js", "app.js", "plugins/routing/client.js", "app.css", "index.html"} {
want, err := fs.ReadFile(staticFS(), f)
if err != nil {
t.Fatal(err)
}
path := "/" + f
if f == "index.html" {
path, want = "/", versionedPage(want)
}
rec := get(path, browser, "")
if rec.Code != http.StatusOK || rec.Header().Get("Content-Encoding") != "gzip" {
t.Errorf("%s to a browser that takes gzip: %d, Content-Encoding %q", path, rec.Code, rec.Header().Get("Content-Encoding"))
continue
}
if rec.Body.Len()*2 > len(want) {
t.Errorf("%s: %d bytes gzipped of %d", path, rec.Body.Len(), len(want))
}
zr, err := gzip.NewReader(bytes.NewReader(rec.Body.Bytes()))
if err != nil {
t.Fatalf("%s: %v", path, err)
}
got, err := io.ReadAll(zr)
if err != nil || !bytes.Equal(got, want) {
t.Errorf("%s unzipped isn't the file (%d bytes of %d): %v", path, len(got), len(want), err)
}
if !strings.Contains(rec.Header().Get("Vary"), "Accept-Encoding") {
t.Errorf("%s: Vary %q, a cache would hand gzip to a client that can't read it", path, rec.Header().Get("Vary"))
}
etag := rec.Header().Get("ETag")
if !strings.HasSuffix(etag, `-gzip"`) {
t.Errorf("%s gzipped keeps the ETag of other bytes: %q", path, etag)
}
if again := get(path, browser, etag); again.Code != http.StatusNotModified {
t.Errorf("%s asked again with its gzipped ETag: %d, want 304", path, again.Code)
}
plain := get(path, "", "")
if plain.Header().Get("Content-Encoding") != "" || !bytes.Equal(plain.Body.Bytes(), want) {
t.Errorf("%s to a client that takes no gzip: Content-Encoding %q, %d bytes of %d", path, plain.Header().Get("Content-Encoding"), plain.Body.Len(), len(want))
}
}
if rec := get("/api/state", browser, ""); rec.Code != http.StatusOK || rec.Header().Get("Content-Encoding") != "gzip" {
t.Errorf("/api/state: %d, Content-Encoding %q, want its JSON gzipped", rec.Code, rec.Header().Get("Content-Encoding"))
}
if rec := get("/icons/crush.png", browser, ""); rec.Code != http.StatusOK || rec.Header().Get("Content-Encoding") != "" {
t.Errorf("a PNG: %d, Content-Encoding %q, want it as it is", rec.Code, rec.Header().Get("Content-Encoding"))
}
if rec := get("/icons/openai.svg", browser, ""); rec.Header().Get("Content-Encoding") != "gzip" {
t.Errorf("an SVG is text: Content-Encoding %q, want gzip", rec.Header().Get("Content-Encoding"))
}
// and still behind the key
r := httptest.NewRequest("GET", "/api/state", nil)
r.Header.Set("Accept-Encoding", browser)
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, r)
if rec.Code != http.StatusUnauthorized {
t.Errorf("no key: %d", rec.Code)
}
}
// A script or stylesheet asked for by the address the page names it by,
// its content's hash, is kept by the browser for good and not asked for
// again at each load; the same file under another version's hash, or
// none, is still asked for again (TestPageFilesRevalidate).
func TestVersionedFilesKept(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"))
srv := webPageHandler(t, webHost{}, nil)
get := func(path string) *httptest.ResponseRecorder {
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, httptest.NewRequest("GET", path, nil))
return rec
}
page := get("/").Body.String()
for _, f := range []string{"app.js", "plugins/routing/client.js", "i18n.js", "app.css"} {
b, err := fs.ReadFile(staticFS(), f)
if err != nil {
t.Fatal(err)
}
sum := sha256.Sum256(b)
named := "/" + f + "?v=" + hex.EncodeToString(sum[:6])
if !strings.Contains(page, `"`+named[1:]+`"`) {
t.Fatalf("the page doesn't name %s", named)
}
if cc := get(named).Header().Get("Cache-Control"); cc != "max-age=31536000, immutable" {
t.Errorf("%s: Cache-Control %q, want it kept for good", named, cc)
}
if cc := get("/" + f + "?v=000000000000").Header().Get("Cache-Control"); cc != "no-cache" {
t.Errorf("%s under another version's hash: Cache-Control %q, want no-cache", f, cc)
}
}
if cc := get("/").Header().Get("Cache-Control"); cc != "no-cache" {
t.Errorf("the page: Cache-Control %q, want no-cache", cc)
}
}