#1519 (fatkun): the Providers page asked /api/providers each time it opened, and after a start that read takes seconds (every account's sign-in, every agent's model), so the page showed skeletons that long. Now, as the Agents page draws the last state: - the app reads /api/providers once in the background after the start (prefetchProviders), and the page's first opening uses that read, or waits on it, instead of asking again. The sidebar's gateway line reuses the same read (sideGwRead), so a start still makes one read. - the server keeps the list it last built in the cache folder (providers-state.json, 0600; lastproviders.go) without the gateway's recent calls and lanes or any provider's headers, proxy or extra body. /api/providers?last=1 serves it at once; the page draws it while the fresh read is out, and the fresh one replaces it. - a row clicked on a kept list waits for the fresh one rather than opening an editor that would be drawn again under the reader's hands. Tests: - TestLastKnownProviders (internal/gui): red when the stripping of Headers/Proxy was broken by hand ("the kept list holds hdr-secret-1519 / proxy-secret-1519"), green restored. - providers-last-known.test.cjs (new): the kept list drawn before the fresh read answers, then replaced; a row click waits. With kept.last = false by hand the row-click case was red in all 5 languages. - sidebar.test.cjs: the narrow case now guards one /api/providers read in total, still one after widening to 1200px (the sidebar reuses it). Red on the old assets (0 reads), green on the new, both engines. Run: go test -timeout 45m ./internal/gui ok github.com/yetone/magpie/internal/gui 528.667s gofmt -l internal/: nothing; go vet ./internal/gui, GOOS=linux and GOOS=windows go vet -tags nogui ./...: exit 0 GUI, chromium and webkit, every language, wide and narrow: providers-last-known 20/20, sidebar 8/8, provider-proxy 4/4, agents-last-known 3/3, last-page 1/1 in each engine. provider-advanced-fold: 1-3 of 25 red per run, the same on the old assets (#2552). Chromium full GUI suite (before the kept.last and sidebar-test edits), under load ~200: 5681 tests, 55 red in 23 files; each was rerun alone and either passed or was as red on the old assets. routing-model-rest runs, old vs new: old 4, 2, 7 red, new 3, 3 red; routing-request- metrics two runs: old 1, 0 red, new 0, 2 red, each red at 23-29s (load ~240; the same test red on old). Not verified: a real cold start on Windows; the webkit full suite (59 provider/sidebar/start files run: 516 tests, the reds there pass alone or are #1863).
193 lines
13 KiB
JavaScript
193 lines
13 KiB
JavaScript
// Run with Node's test runner and Playwright on the module path; see README.md.
|
|
// The window's sidebar (app.js layoutNav): from 1024 points the pages are
|
|
// listed down the left in their groups, the open one's own tabs under it in
|
|
// place of the page's strip, Settings and the gateway's address at the foot
|
|
// and the header a breadcrumb; narrower, they go back into the header's row.
|
|
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs/promises");
|
|
const path = require("node:path");
|
|
const { test } = require("node:test");
|
|
const { chromium, webkit } = require("playwright");
|
|
|
|
const assets = path.resolve(__dirname, "../assets");
|
|
|
|
function serve(lang, reads) {
|
|
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 === "/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/groups") return json({ groups: [] });
|
|
if (url.pathname.startsWith("/api/")) return json({});
|
|
const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
|
|
const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)];
|
|
await route.fulfill({ body: await fs.readFile(file), contentType });
|
|
};
|
|
}
|
|
|
|
// each language's group names, and Settings' Models tab
|
|
const words = {
|
|
en: { groups: ["Work", "Models", "Insights", "Extend"], settings: "Settings", models: "Models" },
|
|
zh: { groups: ["使用", "模型", "观测", "扩展"], settings: "设置", models: "模型" },
|
|
"zh-TW": { groups: ["使用", "模型", "觀測", "擴充"], settings: "設定", models: "模型" },
|
|
ja: { groups: ["利用", "モデル", "観測", "拡張"], settings: "設定", models: "モデル" },
|
|
de: { groups: ["Arbeit", "Modelle", "Einblick", "Zusätze"], settings: "Einstellungen", models: "Modelle" },
|
|
};
|
|
const frame = (page) => page.evaluate(() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r))));
|
|
|
|
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
|
|
test(`${engine}: the window's sidebar`, async (t) => {
|
|
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
|
t.after(() => browser.close());
|
|
|
|
for (const [lang, w] of Object.entries(words)) await t.test(lang, async () => {
|
|
const reads = [];
|
|
const context = await browser.newContext({ viewport: { width: 1024, height: 760 }, reducedMotion: "reduce" });
|
|
const page = await context.newPage();
|
|
page.setDefaultTimeout(5000);
|
|
const errors = [];
|
|
page.on("pageerror", (e) => errors.push(e.message));
|
|
await page.route("**/*", serve(lang, reads));
|
|
await page.goto("http://magpie.test/");
|
|
await page.waitForSelector("#nav button.on");
|
|
|
|
// at 1024 points the pages are down the left, in their groups
|
|
assert.ok(await page.evaluate(() => document.body.classList.contains("side-nav")), "a sidebar from 1024 points");
|
|
assert.ok(await page.locator("#side .side-scroll #nav").isVisible(), "the pages are in the sidebar");
|
|
assert.equal(await page.locator(".top #nav").count(), 0, "and not in the header");
|
|
assert.deepEqual(await page.locator("#nav .nav-gl:visible").allTextContents(), w.groups);
|
|
assert.deepEqual(await page.locator("#nav button[data-view]").evaluateAll((bs) => bs.map((b) => b.dataset.view)),
|
|
["agents", "sessions", "library", "providers", "routing", "gateway", "usage", "plugins"]);
|
|
assert.ok(await page.locator("#side .side-foot #prefs").isVisible(), "Settings at the sidebar's foot");
|
|
assert.equal((await page.locator("#prefs .side-l").textContent()).trim(), w.settings);
|
|
// no page's name or group cut, and nothing scrolls sideways
|
|
const cut = await page.locator("#side :is(.nav-gl, button[data-view] > span, #prefs .side-l)").evaluateAll((es) => es.filter((e) => e.scrollWidth > e.clientWidth + 1).map((e) => e.textContent));
|
|
assert.deepEqual(cut, [], "names read whole");
|
|
assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= document.documentElement.clientWidth), "the window does not scroll sideways");
|
|
|
|
// the open page in the graphite of the text, not an accent colour
|
|
const [ic, fg] = await page.evaluate(() => [getComputedStyle(document.querySelector("#nav button.on .nav-ic")).color, getComputedStyle(document.body).color]);
|
|
assert.equal(ic, fg, "the open page's icon is the text's colour");
|
|
|
|
// a row pressed darkens and keeps its size: no button's squeeze
|
|
for (const sel of ['#nav button[data-view="usage"]', "#prefs"]) {
|
|
const r = page.locator(sel);
|
|
const bg = await r.evaluate((b) => getComputedStyle(b).backgroundColor);
|
|
const box = await r.boundingBox();
|
|
await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2);
|
|
await page.mouse.down();
|
|
await page.waitForTimeout(250);
|
|
const [tf, pressed] = await r.evaluate((b) => [getComputedStyle(b).transform, getComputedStyle(b).backgroundColor]);
|
|
await page.mouse.move(box.x + box.width + 400, box.y);
|
|
await page.mouse.up();
|
|
assert.equal(tf, "none", `${sel} pressed is not squeezed`);
|
|
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();
|
|
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");
|
|
|
|
// Settings: its tabs listed under it, its own strip hidden, and picked there
|
|
await page.locator("#prefs").click();
|
|
const subs = page.locator("#side .nav-sub:not(.leaving) .nav-s");
|
|
await subs.first().waitFor();
|
|
assert.equal(await subs.count(), 8);
|
|
assert.equal(await page.locator("#setTabs").isVisible(), false, "the page's own strip is hidden");
|
|
assert.deepEqual(await subs.evaluateAll((ss) => ss.map((s) => s.dataset.of)),
|
|
["setTab-general", "setTab-usage", "setTab-network", "setTab-models", "setTab-privacy", "setTab-otel", "setTab-sync", "setTab-about"]);
|
|
assert.deepEqual(await subs.evaluateAll((ss) => ss.filter((s) => s.scrollWidth > s.clientWidth + 1).map((s) => s.textContent)), [], "tab names read whole");
|
|
// the selection sets off as the row is pressed, ahead of the page,
|
|
// and goes back when the press leaves the row
|
|
const hlTo = () => page.evaluate(() => { const h = document.querySelector(".nav-sub:not(.leaving) .nav-hl"); return h.style.transform; });
|
|
const rowAt = (of) => page.evaluate((of) => `translateY(${document.querySelector(`.nav-s[data-of=${of}]`).offsetTop}px)`, of);
|
|
const startAt = await hlTo();
|
|
const net = await page.locator(".nav-s[data-of=setTab-network]").boundingBox();
|
|
await page.mouse.move(net.x + net.width / 2, net.y + net.height / 2);
|
|
await page.mouse.down();
|
|
assert.equal(await hlTo(), await rowAt("setTab-network"), "pressed, the selection is going to it");
|
|
assert.equal(await page.locator("#setTab-network").evaluate((b) => b.classList.contains("on")), false, "before the page has the tab");
|
|
// the selection is the one shape: the row pressed has no ground of its own
|
|
assert.equal(await page.locator(".nav-s[data-of=setTab-network]").evaluate((b) => getComputedStyle(b).backgroundColor), "rgba(0, 0, 0, 0)", "no block under the finger ahead of the selection");
|
|
await page.mouse.move(net.x + net.width / 2, net.y + 200);
|
|
assert.equal(await hlTo(), startAt, "the press left the row: back");
|
|
await page.mouse.up();
|
|
assert.equal(await page.locator("#setTab-network").evaluate((b) => b.classList.contains("on")), false, "nothing picked");
|
|
await page.locator(".nav-s[data-of=setTab-models]").click();
|
|
await page.waitForFunction(() => document.querySelector("#setTab-models").classList.contains("on"));
|
|
await frame(page);
|
|
assert.equal(await page.locator(".nav-s.cur").getAttribute("data-of"), "setTab-models");
|
|
assert.equal(await page.locator(".nav-s.cur").getAttribute("aria-selected"), "true");
|
|
const crumb = await page.locator("#crumb").evaluate((c) => [...c.querySelectorAll("span, b")].map((e) => e.textContent));
|
|
assert.deepEqual(crumb, [w.settings, w.models], "the header says where the reader is");
|
|
// the selection sits on the tab picked
|
|
const [hl, cur] = await page.evaluate(() => [document.querySelector(".nav-sub:not(.leaving) .nav-hl").getBoundingClientRect().top, document.querySelector(".nav-s.cur").getBoundingClientRect().top]);
|
|
assert.ok(Math.abs(hl - cur) < 1, `the selection is on the tab: ${hl} ${cur}`);
|
|
|
|
// another page without tabs of its own: the list folds away
|
|
await page.locator('#nav button[data-view="agents"]').click();
|
|
await page.waitForFunction(() => !document.querySelector(".nav-sub"));
|
|
|
|
// a point narrower, the header's row has them again, and no sidebar
|
|
await page.setViewportSize({ width: 1023, height: 760 });
|
|
await page.waitForFunction(() => !document.body.classList.contains("side-nav"));
|
|
await frame(page);
|
|
assert.ok(await page.locator(".top #nav").isVisible(), "the pages are in the header");
|
|
assert.equal(await page.locator("#side").isVisible(), false);
|
|
assert.equal(await page.locator("#crumb").isVisible(), false);
|
|
assert.equal(await page.locator("#nav .nav-gl:visible").count(), 0, "no group names in the header's row");
|
|
assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= document.documentElement.clientWidth), "the window does not scroll sideways");
|
|
// Settings' tabs are its own strip again
|
|
await page.locator("#prefs").click();
|
|
await page.locator("#setTabs").waitFor();
|
|
assert.equal(await page.locator(".nav-sub").count(), 0);
|
|
|
|
assert.deepEqual(errors, []);
|
|
await context.close();
|
|
});
|
|
|
|
await t.test("narrow from the start: the gateway is not read again for a sidebar", async () => {
|
|
const reads = [];
|
|
const context = await browser.newContext({ viewport: { width: 900, height: 700 }, reducedMotion: "reduce" });
|
|
const page = await context.newPage();
|
|
await page.route("**/*", serve("en", reads));
|
|
await page.goto("http://magpie.test/");
|
|
await page.waitForSelector("#nav button.on");
|
|
await page.waitForTimeout(2000);
|
|
// the providers list is read once after the start, for the
|
|
// Providers page to open on it (#1519), and nothing more with no
|
|
// sidebar to show its gateway
|
|
assert.equal(reads.length, 1, "read once, for the Providers page");
|
|
// made wide, the sidebar shows the gateway from that read
|
|
await page.setViewportSize({ width: 1200, height: 700 });
|
|
await page.locator("#sideGw").waitFor();
|
|
await page.waitForTimeout(500);
|
|
assert.equal(reads.length, 1, "the sidebar reads nothing more");
|
|
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();
|
|
await page.route("**/*", serve("en", []));
|
|
await page.goto("http://magpie.test/");
|
|
await page.waitForSelector("#nav button.on");
|
|
await page.locator("#prefs").click();
|
|
const box = page.locator("#side .nav-sub").first();
|
|
await box.waitFor();
|
|
assert.ok(await box.evaluate((b) => b.getAnimations().length > 0), "the list unrolls");
|
|
await page.locator('#nav button[data-view="agents"]').click();
|
|
// folding: out of reach at once, gone once it has played
|
|
assert.equal(await page.locator(".nav-sub:not(.leaving)").count(), 0);
|
|
assert.equal(await page.locator(".nav-s").count(), 0, "its rows are no tabs while they fold");
|
|
await page.waitForFunction(() => !document.querySelector(".nav-sub"));
|
|
await context.close();
|
|
});
|
|
});
|
|
}
|