diff --git a/internal/gui/api.go b/internal/gui/api.go index b29f80658..70f603df5 100644 --- a/internal/gui/api.go +++ b/internal/gui/api.go @@ -845,6 +845,13 @@ func Handler(w Windows, gw *gateway.Server) http.Handler { // before the first paint rather than after the settings come list := builtinsState() boot["featuresOff"] = builtinsOff(list) + // and where the gateway listens, so the sidebar's line at its foot + // is drawn from the start, not held back until providers has come + // in (#1519's read waits for the agents); only this process's own, + // for one served by another magpie is the providers' read to find + if served.Load() != nil { + boot["sideGateway"] = map[string]any{"url": gateway.URL(), "running": true, "boot": true} + } // and the settings of those the page draws (kernel.js) boot["pluginConfig"] = builtinConfig(list) // on Omarchy the page takes its theme's look before it paints diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 4cadf6a49..c2ca2b045 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -24382,10 +24382,10 @@ function renderCrumb(items) { if (on) parts.push(on.textContent.trim().split(" ยท ")[0]); c.replaceChildren(...parts.flatMap((p, i) => i ? [el("i", "", "/"), el(i === parts.length - 1 ? "b" : "span", "", p)] : [el(parts.length === 1 ? "b" : "span", "", p)])); } -// the gateway at the sidebar's foot: where it listens, or that it doesn't -var sideGwAt = null; +// the gateway at the sidebar's foot, boot.js's until providers says (#3429) +var sideGwAt = window.bootPrefs?.sideGateway; function sideGwRead() { - if (!(sideGwRead.due && sideNav() && !sideGwAt)) return; + if (!(sideGwRead.due && sideNav() && (!sideGwAt || sideGwAt.boot))) return; // the list is not read yet: the read the Providers page is made ready // with (#1519) serves both, one read, not two if (!providers && mode !== "panel") { diff --git a/internal/gui/builtins_test.go b/internal/gui/builtins_test.go index b431efa81..c8d17c151 100644 --- a/internal/gui/builtins_test.go +++ b/internal/gui/builtins_test.go @@ -9,6 +9,7 @@ import ( "strings" "testing" + "github.com/yetone/magpie/internal/gateway" "github.com/yetone/magpie/internal/pluginrt" "github.com/yetone/magpie/internal/settings" _ "github.com/yetone/magpie/plugins/all" @@ -137,6 +138,37 @@ func TestBootSaysBuiltinsOff(t *testing.T) { } } +// boot.js says where the gateway listens when this process serves it, so +// the sidebar's line is drawn before the first paint (#3429), and nothing +// when it doesn't: one served by another magpie is the providers' read to +// find, never this process's guess. +func TestBootSaysWhereItsGatewayListens(t *testing.T) { + h := builtinHome(t) + t.Setenv("MAGPIE_ADDR", "127.0.0.1:3481") + boot := func() string { + rec := httptest.NewRecorder() + h.ServeHTTP(rec, httptest.NewRequest("GET", "/boot.js", nil)) + return rec.Body.String() + } + prev := served.Swap(nil) + t.Cleanup(func() { served.Store(prev) }) + if b := boot(); strings.Contains(b, "sideGateway") { + t.Fatalf("no gateway served here: %s", b) + } + served.Store(gateway.New()) + if b := boot(); !strings.Contains(b, `"sideGateway":{"boot":true,"running":true,"url":"http://127.0.0.1:3481"}`) { + t.Fatalf("served here: %s", b) + } + t.Setenv("MAGPIE_ADDR", "0.0.0.0:3483") + if b := boot(); !strings.Contains(b, `"sideGateway":{"boot":true,"running":true,"url":"http://127.0.0.1:3483"}`) { + t.Fatalf("every interface, reached on loopback: %s", b) + } + served.Store(nil) + if b := boot(); strings.Contains(b, "sideGateway") { + t.Fatalf("stopped: %s", b) + } +} + // A plugin's settings are set only as their schema allows, kept in // plugins.yml, and handed to the page in boot.js; a request says on or // config, not both. diff --git a/internal/gui/tests/sidebar.test.cjs b/internal/gui/tests/sidebar.test.cjs index c244d07ee..0b2a06325 100644 --- a/internal/gui/tests/sidebar.test.cjs +++ b/internal/gui/tests/sidebar.test.cjs @@ -11,15 +11,15 @@ const { chromium, webkit } = require("playwright"); const assets = path.resolve(__dirname, "../assets"); -function serve(lang, reads) { +function serve(lang, reads, gateway = { url: "http://127.0.0.1:3425", running: true, window: true }) { const state = { agents: [], profiles: [], settings: { lang, theme: "light" } }; return async (route) => { const url = new URL(route.request().url()); const json = (data) => route.fulfill({ json: data }); - if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:${JSON.stringify(lang)},theme:"light",web:false};` }); + if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:${JSON.stringify(lang)},theme:"light",web:false,sideGateway:{url:"http://127.0.0.1:3425",running:true,boot:true}};` }); if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" }); if (url.pathname === "/api/state") return json(state); - if (url.pathname === "/api/providers") { reads.push(performance.now()); return json({ providers: [], presets: [], excluded: [], gateway: { url: "http://127.0.0.1:3425", running: true, window: true } }); } + if (url.pathname === "/api/providers") { reads.push(performance.now()); return json({ providers: [], presets: [], excluded: [], gateway }); } if (url.pathname === "/api/groups") return json({ groups: [] }); if (url.pathname.startsWith("/api/")) return json({}); const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname); @@ -89,8 +89,9 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", if (sel === "#prefs") assert.notEqual(pressed, bg, `${sel} pressed darkens`); } - // the gateway's address at the foot, read once the window has settled - await page.locator("#sideGw").waitFor(); + // the gateway's address at the foot, drawn from boot.js ahead of the + // read, not held back until the providers have come in + assert.ok(await page.locator("#sideGw").isVisible(), "the gateway's line from boot.js"); assert.match(await page.locator("#sideGw").textContent(), /127\.0\.0\.1:3425/); assert.ok(await page.locator("#sideGw span").evaluate((s) => s.scrollWidth <= s.clientWidth), "the address is read whole"); assert.equal(reads.length, 1, "read once for the sidebar"); @@ -173,6 +174,25 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", await context.close(); }); + await t.test("the line boot.js drew is read again: a gateway gone since says so", async () => { + // boot.js is written once this process holds the gateway, before its + // listen is known to have worked; the read after the start has the + // last word on the line it drew (#3429) + const reads = []; + const context = await browser.newContext({ viewport: { width: 1200, height: 700 }, reducedMotion: "reduce" }); + const page = await context.newPage(); + page.setDefaultTimeout(5000); + await page.route("**/*", serve("en", reads, { url: "http://127.0.0.1:3490", running: false })); + await page.goto("http://magpie.test/"); + await page.waitForSelector("#nav button.on"); + assert.match(await page.locator("#sideGw").textContent(), /127\.0\.0\.1:3425/, "boot.js's line first"); + await page.waitForFunction(() => document.querySelector("#sideGw span")?.textContent === "Gateway stopped"); + assert.ok(await page.locator("#sideGw").evaluate((b) => b.classList.contains("off"))); + assert.match(await page.locator("#sideGw").getAttribute("title"), /3490/); + assert.equal(reads.length, 1, "the read the Providers page is made ready with, no other"); + await context.close(); + }); + await t.test("the list opens and folds as it plays", async () => { const context = await browser.newContext({ viewport: { width: 1200, height: 760 } }); const page = await context.newPage();