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/main3ec45e715, then after rebasing ontoaf27b70b4(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 on743fcc035: 185 and 186 green. Every red run alone here and on a just-fetched origin/mainc524bc018: - 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 aftercf455ebd5, are fixed before this commit (#2134,d692b2548).
123 lines
4.6 KiB
Go
123 lines
4.6 KiB
Go
package gui
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"io/fs"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// The page's files go out to be asked for again by their content's hash,
|
|
// so a browser or a cache in front of `magpie web` never keeps an older
|
|
// version's app.js under a newer page (Jorben on Discord); an unchanged
|
|
// one comes back as a 304, and boot.js and the API keep their own rules.
|
|
func TestPageFilesRevalidate(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, etag string) *httptest.ResponseRecorder {
|
|
r := httptest.NewRequest("GET", path, nil)
|
|
if etag != "" {
|
|
r.Header.Set("If-None-Match", etag)
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
srv.ServeHTTP(rec, r)
|
|
return rec
|
|
}
|
|
tags := map[string]string{}
|
|
for _, p := range []string{"/", "/app.js", "/app.css", "/i18n.js", "/icons/openai.svg"} {
|
|
rec := get(p, "")
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("%s: %d", p, rec.Code)
|
|
}
|
|
if cc := rec.Header().Get("Cache-Control"); cc != "no-cache" {
|
|
t.Errorf("%s: Cache-Control %q, want no-cache", p, cc)
|
|
}
|
|
etag := rec.Header().Get("ETag")
|
|
if etag == "" {
|
|
t.Errorf("%s: no ETag to ask for it again by", p)
|
|
continue
|
|
}
|
|
tags[p] = etag
|
|
if again := get(p, etag); again.Code != http.StatusNotModified {
|
|
t.Errorf("%s asked again with its ETag: %d, want 304", p, again.Code)
|
|
}
|
|
if again := get(p, `"another-version"`); again.Code != http.StatusOK || again.Body.Len() == 0 {
|
|
t.Errorf("%s asked with an older ETag: %d, %d bytes, want the file", p, again.Code, again.Body.Len())
|
|
}
|
|
}
|
|
if tags["/app.js"] != "" && tags["/app.js"] == tags["/app.css"] {
|
|
t.Errorf("app.js and app.css share the ETag %s: it isn't their content's", tags["/app.js"])
|
|
}
|
|
if cc := get("/boot.js", "").Header().Get("Cache-Control"); cc != "no-store" {
|
|
t.Errorf("boot.js: Cache-Control %q, want no-store", cc)
|
|
}
|
|
if rec := get("/wails/runtime.js", ""); rec.Code != http.StatusNotFound || rec.Header().Get("ETag") != "" {
|
|
t.Errorf("magpie web has no Wails runtime: %d, ETag %q", rec.Code, rec.Header().Get("ETag"))
|
|
}
|
|
}
|
|
|
|
// A cache in front of `magpie web` that kept a page file from before they
|
|
// were sent to be asked for again goes on serving it whatever magpie says
|
|
// now: a Docker user behind an HTTPS proxy had v0.1.630's page run
|
|
// v0.1.582's app.js and routing.js, in a private window too. app.js
|
|
// stopped at its first lines over an element the page no longer had
|
|
// (#moreSubs), routing.js then on app.js's pinOf, and the page was blank
|
|
// under its tabs. The page names each of its own scripts and styles by its
|
|
// content's hash, so a new version's page asks for new addresses, which no
|
|
// cache holds; boot.js, which the API writes each time, keeps its own.
|
|
func TestPageNamesItsFilesByContent(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, etag string) *httptest.ResponseRecorder {
|
|
r := httptest.NewRequest("GET", path, nil)
|
|
if etag != "" {
|
|
r.Header.Set("If-None-Match", etag)
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
srv.ServeHTTP(rec, r)
|
|
return rec
|
|
}
|
|
page := get("/", "")
|
|
if page.Code != http.StatusOK {
|
|
t.Fatalf("/: %d", page.Code)
|
|
}
|
|
if ct := page.Header().Get("Content-Type"); !strings.HasPrefix(ct, "text/html") {
|
|
t.Errorf("/: Content-Type %q", ct)
|
|
}
|
|
body := page.Body.String()
|
|
for _, f := range []string{"app.js", "plugins.js", "plugins/routing/client.js", "plugins/routing/routing.css", "plugins/sessions/client.js", "plugins/sessions/sessions.css", "plugins/library/client.js", "plugins/library/library.css", "context.js", "i18n.js", "compat.js", "app.css", "context.css"} {
|
|
b, err := fs.ReadFile(staticFS(), f)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sum := sha256.Sum256(b)
|
|
want := f + "?v=" + hex.EncodeToString(sum[:6])
|
|
if !strings.Contains(body, `"`+want+`"`) {
|
|
t.Errorf("the page doesn't name %s by its content (%s)", f, want)
|
|
continue
|
|
}
|
|
if rec := get("/"+want, ""); rec.Code != http.StatusOK || !bytes.Equal(rec.Body.Bytes(), b) {
|
|
t.Errorf("/%s: %d, %d bytes, want the file", want, rec.Code, rec.Body.Len())
|
|
}
|
|
}
|
|
if !strings.Contains(body, `src="boot.js"`) {
|
|
t.Error("boot.js is the API's, asked for afresh each time: it keeps its name")
|
|
}
|
|
if again := get("/", page.Header().Get("ETag")); again.Code != http.StatusNotModified {
|
|
t.Errorf("/ asked again with its ETag: %d, want 304", again.Code)
|
|
}
|
|
}
|