Files
magpie/internal/gui/web_compress_test.go
T
yetone eb7199e120 gui: Routing is a plugin's page, its script, style and words in plugins/routing (P2)
P2 of docs/subsystems/plugin-runtime.md, the third page after Sessions
and Library.

- routing.js and routing.css move to assets/plugins/routing/ (client.js,
  routing.css). Its part's html is the page's head and boxes that were
  index.html's <main id="view-routing">; the rest is drawn by
  routingPage, run at mount since it builds on app.js. It keeps itself
  after that as it did (its own hidden, the language, focus), so its
  hooks are dirty and discard, a routing group being edited, which
  show() and the window's close ask through magpie.call:
  window.routingDirty and discardRouting are gone.
- plugins/routing: Essential, Requires window and gateway, in use while
  the user has a routing group (not Auto, not Hidden), since the page is
  where it is edited. The gateway routes the same with it off.
- With it off, what leads to the page goes too: the nav and panel tabs
  and the panel's section (data-feature="routing"), the guide's Open
  Routing, Providers' "Make a routing group of …", the Usage list's
  View routing and its time as a link, the context card's Latest
  request. A panel left on its Routing tab shows Agents.
- What other pages use of it stays on window: openRoute (context.js),
  kindName, swapWhy, routedWhy (Usage's Requests), newGroupWith
  (Providers), panelRoutingShown (the panel).
- The 505 strings only Routing says move from i18n.js to
  plugins/routing/i18n.js (4 languages); i18n.js 17048 → 15032 lines.
  The plugin's own name and line stay in i18n.js: the Plugins page
  says them with the plugin off (TestBuiltinsTranslated).
- Tests and comments that named routing.js name the new path (gateway's
  antigravity_rest and loop_guard, codeassist.go, usage.go,
  web_compress, group_member_info, assets_revalidate, font-cascade,
  win-fonts, three routing-*.test.cjs). builtins.test.cjs and
  builtins_test.go count seven built-ins and switch Routing off: its
  tabs go, the page shown stays.
- docs: plugin-runtime.md, gui-shell.md, gateway-routing.md.

Run, first on origin/main 3ec45e715, then after rebasing onto af27b70b4
(main's routing.js, css, app.js and i18n.js changes since were carried
into the moved files; its new Routing words, as the rules' conditions,
are in the plugin's i18n.js):
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows.
- go test -count=1 ./internal/gui ./internal/gateway ./internal/usage
  ./internal/pluginrt ./plugins/...: ok (gateway 1800s under load,
  -timeout 45m; gui with -skip TestMainWindowCorners, #1936).
- All 193 GUI test files, 3 at a time at load ~250, Chromium and WebKit,
  at the Usage commit on 743fcc035: 185 and 186 green. Every red run
  alone here and on a just-fetched origin/main c524bc018:
  - the same on main: routing-model-rest (#2115), routing-wb-restricted
    (#1905), usage-agents-hide (11 of 16 in both, #2211), usage-fold
    (#2163: 12 of 14 here, 9 of 14 on main over 7 runs, the same 4px),
    quota-count-wrap and quota-name-translated in WebKit (#2228);
  - red only under load, green alone on both: routing-flood (#2215),
    routing-log-icons, usage-ledger (#2211), and three plugin-* files
    whose tree was rebased under them mid-run.
- days-fit, failed-open, side-calls and burst-speed, red on main after
  cf455ebd5, are fixed before this commit (#2134, d692b2548).
2026-10-15 01:32:12 +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, Handler(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 := Handler(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)
}
}