From bcdb92ac6875beb7e0418c1447701ce9440f2f9f Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Mon, 26 Oct 2026 19:59:54 +0100 Subject: [PATCH] Web page: Load and Unload in the header (#3550) One button in the header of every tab: Load while the model is unloaded, Unload otherwise. It calls POST /load and POST /unload (the server already had both, for its own page only). Unload asks first and never interrupts a turn: the button is off while an answer is being written, an image is made or a request is queued. The pill and the Monitor say Unloaded / Loading / Unloading, a message or an image on an unloaded model shows Loading, and a poll that started before the POST cannot flip the button back (the POST's state is held until a later poll agrees). On a phone the pill's text is the first thing the header row gives up (it measures the row, not a fixed width), so Load/Unload and the theme button stay on the screen from 320 px. Browser tests (tools/webtest/browser_test.mjs): unload with its question, load back, declining, not while answering, a message that loads, the header at 320 to 640 px. Ported by hand from PR #3550 by Sakurai Momoka (sakuumomo); the texts now go through the page's translation catalog. Co-authored-by: Sakurai Momoka <159397306+sakuumomo@users.noreply.github.com> --- serve/test_web_load_unload.py | 68 +++++++++++ serve/web/app.css | 22 +++- serve/web/app.js | 214 +++++++++++++++++++++++++++++++-- serve/web/index.html | 2 + tools/webtest/browser_test.mjs | 73 +++++++++++ 5 files changed, 363 insertions(+), 16 deletions(-) create mode 100644 serve/test_web_load_unload.py diff --git a/serve/test_web_load_unload.py b/serve/test_web_load_unload.py new file mode 100644 index 00000000..f6118e7d --- /dev/null +++ b/serve/test_web_load_unload.py @@ -0,0 +1,68 @@ +"""serve/test_web_load_unload.py - the web page's header carries the Load / Unload control (#3550, by sakuumomo). + +Pins, as text: index.html holds the header button and the Monitor's "Unloaded" badge; app.js wires the button to POST /load +and POST /unload, holds the POST's state until a later poll agrees, answers a 401 with the key copy, and refits the header +row only when its text changed (the per-second poll does not force a layout). The behaviour itself, in a real browser, is in +tools/webtest/browser_test.mjs ("load/unload: ..."). + + python -m unittest serve.test_web_load_unload -v +""" +from __future__ import annotations + +import shutil +import subprocess +import unittest +from pathlib import Path + +WEB = Path(__file__).resolve().parent / "web" + + +class WebLoadUnloadMarkup(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.html = (WEB / "index.html").read_text(encoding="utf-8") + cls.js = (WEB / "app.js").read_text(encoding="utf-8") + cls.css = (WEB / "app.css").read_text(encoding="utf-8") + + def test_header_button_and_monitor_badge(self): + self.assertIn('id="unload-btn"', self.html) + self.assertIn('data-s="unloaded"', self.html) + self.assertLess(self.html.index('id="unload-btn"'), self.html.index('id="theme-btn"')) # in the header row + + def test_button_posts_load_and_unload(self): + self.assertIn('$("unload-btn").onclick', self.js) + self.assertIn('fetch(loadingNow ? "load" : "unload", {method: "POST", headers: headers(true), body: "{}"})', self.js) + + def test_unload_asks_first_and_never_interrupts_a_turn(self): + self.assertIn("confirm(tr(\"model.confirmUnload\"", self.js) + self.assertIn("turnBlocksUnload(live)", self.js) + self.assertIn("function turnBlocksUnload(live)", self.js) + + def test_401_gets_the_key_copy(self): + # The control POST answers a 401 the way poll and send do, not with a bare HTTP line. + self.assertIn('r.status === 401 ? tr("toast.keyAbout2"', self.js) + + def test_poll_hold_until_a_poll_agrees(self): + self.assertIn("metricsHold = {state, after: metricsEpoch};", self.js) + self.assertIn("function metricsAgree(live, state)", self.js) + + def test_header_refits_only_on_changed_text(self): + # The per-second poll re-sets the pill; the fit runs when its text actually changed. + self.assertIn("if (text !== lastPillText) { lastPillText = text; fitHeader(); }", self.js) + + def test_the_header_row_keeps_the_buttons_on_a_phone(self): + self.assertIn("#unload-btn", self.css) + self.assertIn(".st-header--clip #pill-text", self.css) + for width in ("955px", "457px", "382px"): + self.assertIn(f"@media (max-width: {width})", self.css) + + +@unittest.skipUnless(shutil.which("node"), "node is not installed") +class WebLoadUnloadParses(unittest.TestCase): + def test_app_js_parses(self): + r = subprocess.run(["node", "--check", str(WEB / "app.js")], capture_output=True, text=True) + self.assertEqual(r.returncode, 0, r.stderr) + + +if __name__ == "__main__": + unittest.main() diff --git a/serve/web/app.css b/serve/web/app.css index fb17d537..9c40e420 100644 --- a/serve/web/app.css +++ b/serve/web/app.css @@ -11,6 +11,8 @@ main { flex: 1; min-height: 0; display: flex; } .card-title { font-weight: var(--st-fw-bold); font-size: var(--st-fs-base); color: var(--st-ink); } .st-header { position: relative; z-index: 2; flex: none; } .st-header .st-tabs { margin-left: var(--st-s-4); } +#unload-btn { flex: none; height: 32px; padding: 0 12px; min-width: 7.75rem; } +.st-header--clip #pill-text { display: none; } #drawer-close .st-icon { transform: rotate(-90deg); } /* ---------------------------------------------------------------- chat */ @@ -181,12 +183,26 @@ main { flex: 1; min-height: 0; display: flex; } @media (max-width: 900px) { .monitor-row { grid-template-columns: minmax(0, 1fr); } } -@media (max-width: 640px) { +/* The tab strip keeps a width it can scroll. The pill's text hides only when the header row overflows (fitHeader in app.js). + Load / Unload (PR #3550) sits in the row: 9rem of tabs plus its 7.75rem fit from 458px; below that the tighter floors keep + it and the theme button in the viewport. */ +@media (max-width: 955px) { .st-header { padding: 0 var(--st-s-3); gap: var(--st-s-2); } - .st-header .st-tabs { margin-left: 0; } + .st-header__brand, #theme-btn, #pill, #unload-btn { flex: none; } + .st-header .st-tabs { margin-left: 0; flex: 1 1 auto; min-width: 9rem; overflow-x: auto; } .st-tab { padding: 0 10px; } .st-tab .st-icon { display: none; } - #pill-text { display: none; } +} +@media (max-width: 457px) { + #unload-btn { min-width: 0; padding: 0 8px; } + .st-header .st-tabs { min-width: 5.5rem; } +} +/* the name plus "Unloading…" still overflows at 374px in Firefox 158 */ +@media (max-width: 382px) { + .st-header__brand { display: none; } + .st-header .st-tabs { min-width: 4.75rem; } +} +@media (max-width: 640px) { .chat { padding: var(--st-s-5) var(--st-s-4); } .composer-wrap { padding: 0 var(--st-s-3) var(--st-s-3); } .monitor, .about { padding: var(--st-s-4); } diff --git a/serve/web/app.js b/serve/web/app.js index 310c0b90..4b0477fd 100644 --- a/serve/web/app.js +++ b/serve/web/app.js @@ -164,22 +164,38 @@ function setMetric(key, value, unit, sub) { } let lastMetrics = null, metricsFailures = 0, keyWarned = false, mcpTick = 0; +let metricsEpoch = 0; +// After Load or Unload returns, a poll that started earlier can still say the opposite. Hold the POST's state until a later +// poll agrees, or until the first poll that started after the POST arrives. (PR #3550 by sakuumomo) +let metricsHold = null; let reqShowAll = false; // the Monitor's request table: the last 12, or every one the server keeps (issue #35) +function metricsAgree(live, state) { + const s = (live && live.state) || ""; + if (state === "unloaded") return s === "unloaded"; + return s !== "" && s !== "unloaded"; +} async function poll() { + const epoch = ++metricsEpoch; try { const r = await fetch(reqShowAll ? "metrics?requests=all" : "metrics", {headers: headers()}); if (r.status === 401) { - setPill("error", "API key needed"); - if (!keyWarned) { keyWarned = true; toast("warn", "API key needed", "This server needs a key: add it under About > Settings.", 6000); } + setPill("error", tr("pill.keyNeeded", "API key needed")); + if (!keyWarned) { keyWarned = true; toast("warn", tr("pill.keyNeeded", "API key needed"), tr("toast.keyAbout", "This server needs a key: add it under About > Settings."), 6000); } } else if (r.ok) { - lastMetrics = await r.json(); + const m = await r.json(); + if (metricsHold && epoch <= metricsHold.after && !metricsAgree(m.live, metricsHold.state)) { + setTimeout(poll, 1000); + return; + } + if (metricsHold && (metricsAgree(m.live, metricsHold.state) || epoch > metricsHold.after)) metricsHold = null; + lastMetrics = m; metricsFailures = 0; render(lastMetrics); } else { throw new Error(`HTTP ${r.status}`); } } catch (e) { - if (++metricsFailures === 3) setPill("error", "Server not reachable"); + if (++metricsFailures === 3) setPill("error", tr("pill.unreachable", "Server not reachable")); } if (tab === "monitor" && ++mcpTick % 10 === 0) loadMcp(); // server states change rarely: every 10 s setTimeout(poll, 1000); @@ -188,21 +204,164 @@ async function poll() { function setPill(state, text) { $("pill").dataset.state = state === "error" ? "queued" : state; $("pill-text").textContent = text; + // a poll re-sets the sentence every second; refit only when its text changed + if (text !== lastPillText) { lastPillText = text; fitHeader(); } } +let lastPillText = null; +// The pill's sentence is the first thing the header row can spare when it runs out of room. +function fitHeader() { + const header = document.querySelector(".st-header"); + if (!header) return; + header.classList.remove("st-header--clip"); + if (header.scrollWidth > header.clientWidth) header.classList.add("st-header--clip"); +} +window.addEventListener("resize", fitHeader); + +// ------------------------------------------------------------------ Load / Unload (POST /load, /unload: the header button) +// Metrics stay "unloaded" until the engine is READY, so a turn or an image job on this page has to paint Loading itself. +// modelOp covers the button's own POST, which returns before the next poll can agree. +let modelOp = null; +let imageBusy = false; +function pageBusy() { return busy != null || imageBusy; } +// "generating an image" is the job a refresh can see. imageBusy is this page's POST before that phase is published. A chat +// phase already on the metrics wins, because Stop still stops that reply. +function imageLive(live) { + if (live && live.phase === "generating an image") return true; + const state = live && live.state; + if (state === "reading" || state === "generating") return false; + return imageBusy; +} +function paintLive() { if (lastMetrics) render(lastMetrics); } +function turnBlocksUnload(live) { + const state = (live && live.state) || ""; + return pageBusy() || state === "reading" || state === "generating" || (live && live.queued > 0); +} +function setModelButton(text, title, disabled) { + const btn = $("unload-btn"); + btn.textContent = text; btn.title = title; btn.disabled = disabled; + fitHeader(); +} +function modelButton(live) { + if (modelOp === "load") return setModelButton(tr("model.loading", "Loading…"), tr("model.loadingTitle", "The model is loading"), true); + if (modelOp === "unload") return setModelButton(tr("model.unloading", "Unloading…"), tr("model.unloadingTitle", "The model is unloading"), true); + const state = (live && live.state) || ""; + const metricsBusy = state === "reading" || state === "generating" || (live && live.queued > 0); + const imageNow = imageLive(live); + const unload = tr("model.unload", "Unload"); + if (state === "unloaded") { + if (imageNow) return setModelButton(tr("model.loading", "Loading…"), tr("model.imageLoading", "An image is already loading the model"), true); + if (pageBusy()) return setModelButton(tr("model.loading", "Loading…"), tr("model.replyLoading", "A reply is already loading the model"), true); + return setModelButton(tr("model.load", "Load"), tr("model.loadTitle", "Load the model now. A message also loads it."), false); + } + if (imageNow) return setModelButton(unload, tr("model.imageBusy", "An image is generating. It finishes on its own."), true); + if (!state) return setModelButton("…", tr("model.waiting", "Waiting for the server"), true); + if (metricsBusy || pageBusy()) return setModelButton(unload, tr("model.stopFirst", "Stop the reply, then unload"), true); + return setModelButton(unload, tr("model.unloadTitle", "Unload the model and free the GPU"), false); +} +$("unload-btn").onclick = async () => { + if (modelOp) return; + const live = (lastMetrics && lastMetrics.live) || {}; + const loading = live.state === "unloaded"; + const busyCopy = (sample, stillLoading) => stillLoading + ? (imageLive(sample) ? tr("model.imageLoadingDot", "An image is already loading the model.") : tr("model.replyLoadingDot", "A reply is already loading the model.")) + : (imageLive(sample) ? tr("model.imageBusy", "An image is generating. It finishes on its own.") : tr("model.stopFirstDot", "Stop the reply, then unload.")); + if (loading ? pageBusy() : turnBlocksUnload(live)) { + toast("warn", tr("model.busy", "Model is busy"), busyCopy(live, loading)); + return; + } + if (!loading && !confirm(tr("model.confirmUnload", "Unload the model and free the GPU? Load, or the next message, brings it back."))) return; + // confirm() blocks the poll. Yield so a sample that arrived during the dialog is applied. A confirmed Unload stays an unload. + if (!loading) { + modelOp = "unload"; + modelButton(live); + await new Promise((resolve) => setTimeout(resolve, 0)); + } + const now = (lastMetrics && lastMetrics.live) || {}; + const loadingNow = loading; + if (loadingNow ? pageBusy() : turnBlocksUnload(now)) { + modelOp = null; + if (lastMetrics) render(lastMetrics); else modelButton(now); + toast("warn", tr("model.busy", "Model is busy"), busyCopy(now, loadingNow)); + return; + } + modelOp = loadingNow ? "load" : "unload"; + modelButton(now); + if (loadingNow) toast("info", tr("model.loadingToast", "Loading the model"), tr("model.fewMinutes", "This can take a few minutes.")); + const remember = (state) => { + if (!lastMetrics) lastMetrics = {}; + lastMetrics.live = Object.assign({}, lastMetrics.live, {state, queued: 0}); + metricsHold = {state, after: metricsEpoch}; + }; + try { + const r = await fetch(loadingNow ? "load" : "unload", {method: "POST", headers: headers(true), body: "{}"}); + let body = {}; + try { body = await r.json(); } catch (e) { /* the connection dropped */ } + const status = body.status || ""; + const msg = (body.error && body.error.message) || + (r.status === 401 ? tr("toast.keyAbout2", "API key needed: add it under About > Settings.") : tr("toast.httpAnswer", "The server answered HTTP {code}.", {code: r.status})); + const failTitle = loadingNow ? tr("model.loadFailed", "Load failed") : tr("model.unloadFailed", "Unload failed"); + if (loadingNow) { + if (r.ok && status === "loaded") { + remember("idle"); + toast("success", tr("model.loaded", "Model loaded"), tr("model.gpuInUse", "The GPU is in use.")); + } else { + toast("error", failTitle, msg); + } + } else if (status === "unloaded" || status === "not loaded") { + remember("unloaded"); + toast(status === "unloaded" ? "success" : "info", + status === "unloaded" ? tr("model.unloaded", "Model unloaded") : tr("model.alreadyUnloaded", "Already unloaded"), + tr("model.gpuFree", "The GPU is free. Load, or the next message, brings it back.")); + } else if (r.status === 409 || status === "busy") { + let sample = (lastMetrics && lastMetrics.live) || {}; + try { + const mr = await fetch("metrics", {headers: headers()}); + if (mr.ok) { + const m = await mr.json(); + if (m && m.live) { sample = m.live; lastMetrics = m; } + } + } catch (e) { /* the 409 body has no phase */ } + toast("warn", tr("model.busy", "Model is busy"), imageLive(sample) + ? tr("model.imageBusy", "An image is generating. It finishes on its own.") : tr("model.stopFirstDot", "Stop the reply, then unload.")); + } else if (status === "unsupported") { + toast("error", tr("model.unsupported", "Unload is not available"), ""); + } else { + toast("error", failTitle, msg); + } + } catch (e) { + toast("error", loadingNow ? tr("model.loadFailed", "Load failed") : tr("model.unloadFailed", "Unload failed"), tr("model.noAnswer", "The server did not answer.")); + } finally { + modelOp = null; + if (lastMetrics) render(lastMetrics); else modelButton({}); + } +}; function render(m) { const live = m.live || {}, hw = m.hardware || {}, st = m.hardware_static || {}, eng = m.engine || {}, h = m.history || {}; const last = (m.requests || [])[0]; - // the header pill - if (live.state === "reading") { + // the header pill. modelOp and a local load win over "N queued": with parallel requests, queued stays up through + // ensure_loaded(), and metrics stay "unloaded" until the engine is READY. An image POST in that window is still a load. + const localLoad = live.state === "unloaded" && (busy != null || imageBusy); + if (modelOp === "load") { + setPill("idle", tr("model.loading", "Loading…")); + } else if (modelOp === "unload") { + setPill("idle", tr("model.unloading", "Unloading…")); + } else if (localLoad) { + setPill("idle", tr("model.loading", "Loading…")); + } else if (imageLive(live)) { + setPill("reading", tr("pill.image", "Generating image…")); + } else if (live.state === "reading") { const pct = live.prompt_total ? Math.round((100 * live.prompt_read) / live.prompt_total) : null; - setPill("reading", pct != null ? `Reading prompt · ${pct}%` : "Reading prompt"); + setPill("reading", pct != null ? tr("pill.readingPct", "Reading prompt · {pct}%", {pct}) : tr("pill.reading", "Reading prompt")); } else if (live.state === "generating") { - setPill("generating", `Generating · ${fmt(live.tok_s, 1)} tok/s`); + setPill("generating", tr("pill.generating", "Generating · {rate} tok/s", {rate: fmt(live.tok_s, 1)})); + } else if (live.state === "unloaded") { + setPill("idle", tr("pill.unloaded", "Unloaded")); } else { - setPill("idle", "Idle"); + setPill("idle", tr("pill.idle", "Idle")); } - if (live.queued > 0) setPill("queued", `${live.queued} queued`); + modelButton(live); + if (!modelOp && !localLoad && live.queued > 0) setPill("queued", tr("pill.queued", "{n} queued", {n: live.queued})); if (tab === "monitor") renderMonitor(live, hw, st, eng, h, last, m.requests || [], m.totals, m.requests_kept); if (tab === "monitor") renderConvCache(m.conversation_cache); if (tab === "about") renderAbout(eng, hw, st); @@ -252,11 +411,29 @@ function renderTotals(t) { } function renderMonitor(live, hw, st, eng, h, last, requests, totals, kept) { // model state - const on = live.queued > 0 ? "queued" : live.state; - for (const b of document.querySelectorAll("#state-badges .st-badge")) b.classList.toggle("on", b.dataset.s === on || b.dataset.s === live.state); + // A local turn keeps metrics on "unloaded" for the whole engine start. + const imageNow = imageLive(live); + const starting = modelOp === "load" || imageNow || (busy != null && live.state === "unloaded"); + const on = !modelOp && live.queued > 0 ? "queued" : live.state; + for (const b of document.querySelectorAll("#state-badges .st-badge")) { + const lit = (b.dataset.s === on || b.dataset.s === live.state) && !(starting && b.dataset.s === "unloaded"); + b.classList.toggle("on", lit); + } const prog = $("state-progress"); let label = "Waiting for a request", detail = "", pct = 0; - if (live.state === "reading") { + if (modelOp === "load" || (busy != null && live.state === "unloaded" && !imageNow)) { + label = "Loading"; + delete prog.dataset.tone; + detail = "The model is loading."; + } else if (imageNow && (live.state === "unloaded" || live.state === "reading" || live.state === "generating" || live.state === "idle")) { + label = live.state === "unloaded" ? "Loading" : "Generating image"; + delete prog.dataset.tone; + detail = live.state === "unloaded" ? "The model is loading." : "An image is generating. It finishes on its own."; + } else if (modelOp === "unload") { + label = "Unloading"; + delete prog.dataset.tone; + detail = "Freeing the GPU."; + } else if (live.state === "reading") { label = "Reading prompt"; prog.dataset.tone = "info"; if (live.prompt_total) { @@ -270,6 +447,10 @@ function renderMonitor(live, hw, st, eng, h, last, requests, totals, kept) { delete prog.dataset.tone; pct = live.max_tokens ? Math.min(100, (100 * live.generated) / live.max_tokens) : 0; detail = `${fmt(live.generated)} tokens · ${fmt(live.tok_s, 1)} tok/s`; + } else if (live.state === "unloaded") { + label = "Unloaded"; + delete prog.dataset.tone; + detail = "Load brings it back. A message does too."; } else if (last) { delete prog.dataset.tone; detail = `last: ${fmt(last.output_tokens)} tokens${last.decode_tok_s ? ` at ${fmt(last.decode_tok_s, 1)} tok/s` : ""}`; @@ -859,6 +1040,7 @@ async function send() { const controller = new AbortController(); busy = {controller, msg: m}; setBusy(true); + paintLive(); const body = {model: health.model, messages: apiMessages(), stream: true, reasoning_effort: settings.thinking}; @@ -937,6 +1119,7 @@ async function send() { if (m.limit) m.meta = `${m.meta || ""} · stopped at the limit of ${m.limit} tool rounds (mcp.max_rounds)`; busy = null; setBusy(false); + paintLive(); if (frame) cancelAnimationFrame(frame); updateAssistant(el, m, false); saveChat(); @@ -1184,6 +1367,7 @@ async function start() { loadHealth().then(loadMcp).then(() => { if (startQuestion) { $("input").value = startQuestion; send(); } }); showTab(location.hash.slice(1) || "chat"); poll(); + requestAnimationFrame(fitHeader); } start(); @@ -1194,7 +1378,9 @@ $("img-form").onsubmit = async (ev) => { if (!prompt) return; const body = {prompt, size: $("img-size").value, response_format: "b64_json"}; if ($("img-seed").value !== "") body.seed = Number($("img-seed").value); + imageBusy = true; $("img-go").disabled = true; + paintLive(); const t0 = performance.now(); $("img-hint").textContent = "Generating\u2026 (a minute or more on a small GPU)"; try { @@ -1216,6 +1402,8 @@ $("img-form").onsubmit = async (ev) => { $("img-hint").textContent = ""; toast("error", "No image", String(e.message || e), 8000); } finally { + imageBusy = false; $("img-go").disabled = false; + paintLive(); } }; diff --git a/serve/web/index.html b/serve/web/index.html index 704417c7..be6f6d36 100644 --- a/serve/web/index.html +++ b/serve/web/index.html @@ -33,6 +33,7 @@ Connecting… + @@ -102,6 +103,7 @@ Model state Idle + Unloaded Reading Generating Queued diff --git a/tools/webtest/browser_test.mjs b/tools/webtest/browser_test.mjs index 8fe01ec2..5949db2e 100644 --- a/tools/webtest/browser_test.mjs +++ b/tools/webtest/browser_test.mjs @@ -180,6 +180,79 @@ test("math: off by default, drawn when switched on, source kept", async ({ brows await page.close(); }); +// ------------------------------------------------------------------------------------------------ load / unload (#3550) +const btnText = (page) => page.$eval("#unload-btn", (b) => b.textContent); +const waitBtn = (page, text) => page.waitForFunction((t) => document.getElementById("unload-btn").textContent === t, { timeout: 8000 }, text); +const posts = (mock, what) => mock.state.calls.filter((c) => c === "POST " + what).length; + +test("load/unload: the header button unloads after a confirmation and loads again", async ({ browser, mock }) => { + mock.state.loaded = true; mock.state.calls.length = 0; + const page = await openPage(browser, mock); + await waitBtn(page, "Unload"); + await page.click("#unload-btn"); + assert.match(page.lastDialog, /Unload the model and free the GPU/); + await waitBtn(page, "Load"); + assert.equal(posts(mock, "/unload"), 1); + assert.equal(await page.$eval("#pill-text", (e) => e.textContent), "Unloaded"); + await sleep(2500); // the poll must agree, not flip the button back + assert.equal(await btnText(page), "Load"); + await page.click("#unload-btn"); + await waitBtn(page, "Unload"); + assert.equal(posts(mock, "/load"), 1); + assert.equal(await page.$eval("#pill-text", (e) => e.textContent), "Idle"); + await page.close(); +}); + +test("load/unload: declining the question changes nothing", async ({ browser, mock }) => { + mock.state.loaded = true; mock.state.calls.length = 0; + const page = await openPage(browser, mock, { dialogs: "dismiss" }); + await waitBtn(page, "Unload"); + await page.click("#unload-btn"); + await sleep(500); + assert.equal(posts(mock, "/unload"), 0); + assert.equal(await btnText(page), "Unload"); + await page.close(); +}); + +test("load/unload: not while an answer is being written", async ({ browser, mock }) => { + mock.state.loaded = true; + const page = await openPage(browser, mock); + await waitBtn(page, "Unload"); + await say(page, "#hold please"); + await page.waitForFunction(() => busy !== null); + assert.equal(await page.$eval("#unload-btn", (b) => b.disabled), true); + assert.match(await page.$eval("#unload-btn", (b) => b.title), /Stop the reply/); + await page.click("#stop-btn"); + await idle(page); + await page.waitForFunction(() => !document.getElementById("unload-btn").disabled, { timeout: 8000 }); + await page.close(); +}); + +test("load/unload: an unloaded model is loaded by a message and the button follows", async ({ browser, mock }) => { + mock.state.loaded = false; + const page = await openPage(browser, mock); + await waitBtn(page, "Load"); + await say(page, "hello"); + await idle(page); + await waitBtn(page, "Unload"); + await page.close(); +}); + +test("load/unload: the header fits a phone", async ({ browser, mock }) => { + mock.state.loaded = true; + for (const width of [320, 360, 390, 640]) { + const page = await openPage(browser, mock, { width, height: 700 }); + await waitBtn(page, "Unload"); + const r = await page.evaluate(() => { + const box = (id) => { const b = document.getElementById(id).getBoundingClientRect(); return [b.left, b.right]; }; + return { unload: box("unload-btn"), theme: box("theme-btn"), scroll: document.documentElement.scrollWidth, width: innerWidth }; + }); + for (const [l, rt] of [r.unload, r.theme]) assert.ok(l >= 0 && rt <= r.width, `${width}px: ${JSON.stringify(r)}`); + assert.ok(r.scroll <= r.width, `${width}px: the page scrolls sideways: ${JSON.stringify(r)}`); + await page.close(); + } +}); + const mock = await startMock(); const browser = await launch(); await run(async () => ({ browser, mock, close: async () => { await browser.close(); await mock.stop(); } }));