From 5610f1e68a9856bab62e5273cb7167ce5e333f4f Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 6 Oct 2026 15:55:29 +0800 Subject: [PATCH] =?UTF-8?q?gui:=20Save=20shows=20a=20spinner=20and=20"Savi?= =?UTF-8?q?ng=E2=80=A6"=20while=20the=20provider's=20model=20list=20is=20f?= =?UTF-8?q?etched=20(zola=5Fxynb=20on=20X:=20"=E5=81=9A=E4=B8=AA=E8=BD=AC?= =?UTF-8?q?=E5=9C=88=E6=88=96=E8=80=85=E6=8F=90=E7=A4=BA=EF=BC=8C=E4=B8=8D?= =?UTF-8?q?=E7=84=B6=E8=AE=A9=E4=BA=BA=E8=A7=89=E5=BE=97=E6=B2=A1=E5=8F=8D?= =?UTF-8?q?=E5=BA=94")?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Save on a provider (and Add, Replace on import, and a subscription's Save) asks the vendor for its model list, which can take seconds. The button was only faded by .busy, which looked as if the click had done nothing. saving() now puts a spinning icon and what is under way ("Saving…", "Adding…", "Replacing…") in place of its label, at full opacity, aria-busy set and still not clickable twice; doneSaving() puts the label back when the save fails, so the error shows beside a Save that can be clicked again. Reduced motion stops the spin. zh/ja/de added. provider-save-busy.test.cjs (en/zh/ja/de, Chromium and WebKit, 440px and 900px; the fake save held until the test answers) fails on the old code (no button.saving) and passes, with a failed save restoring Save and a second save closing the editor; the 30 related GUI test files pass (175/175). Tried in a sandbox magpie (-tags dev, MAGPIE_DEV_UI) against a custom provider whose /v1/models answers after 6s: Save showed the spinner and "Saving…" in Chromium and WebKit until the save came back. --- internal/gui/assets/app.css | 3 + internal/gui/assets/app.js | 25 +++- internal/gui/assets/i18n.js | 6 + .../gui/tests/provider-save-busy.test.cjs | 109 ++++++++++++++++++ 4 files changed, 139 insertions(+), 4 deletions(-) create mode 100644 internal/gui/tests/provider-save-busy.test.cjs diff --git a/internal/gui/assets/app.css b/internal/gui/assets/app.css index 953a905a6..47b84d743 100644 --- a/internal/gui/assets/app.css +++ b/internal/gui/assets/app.css @@ -2286,6 +2286,9 @@ body.window .ag-sk-icon { width: 22px; height: 22px; border-radius: 6px; } .editor .editor-error { grid-column: 1 / -1; font-size: 12px; line-height: 1.45; color: var(--red); white-space: pre-wrap; overflow-wrap: anywhere; user-select: text; -webkit-user-select: text; } .editor .editor-error.warn { color: var(--amber); } .busy { opacity: .55; pointer-events: none; } +.saving { opacity: 1; display: inline-flex; align-items: center; gap: 5px; } +.saving svg { flex: none; animation: spin .8s linear infinite; } +@media (prefers-reduced-motion: reduce) { .saving svg { animation: none; } } /* agent chips: who is on this provider, click one to pick it a model */ .achips { display: flex; flex-wrap: wrap; gap: 5px; } diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 2f44ead88..2262721cf 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -7537,7 +7537,7 @@ function drawEditor(p, presetID) { const priceRate = priceRateOfDraft(); if (priceRate === undefined) return priceRateError(ed); if (priceTypedError(ed)) return; - saveBtn.classList.add("busy"); providerAction("save", { id: p.id, models: chosenIds(), unlisted: draft.unlisted, fallback: draft.fallback, contexts: cx.map, outputs: ox.map, compacts: cpx.map, proxy, accountProxies: own.map, maxConcurrency, ...queue, priceRate, modelPrefs: modelPrefsOfDraft(), pinUpstream: !!draft.pinUpstream, ...routingOfDraft(p) }, t("{name} saved", { name: p.name })); }; + saving(saveBtn, t("Saving…")); providerAction("save", { id: p.id, models: chosenIds(), unlisted: draft.unlisted, fallback: draft.fallback, contexts: cx.map, outputs: ox.map, compacts: cpx.map, proxy, accountProxies: own.map, maxConcurrency, ...queue, priceRate, modelPrefs: modelPrefsOfDraft(), pinUpstream: !!draft.pinUpstream, ...routingOfDraft(p) }, t("{name} saved", { name: p.name })); }; bar.append(cancel, saveBtn); ed.append(bar); return ed; @@ -7912,7 +7912,7 @@ function drawEditor(p, presetID) { if (isNew && custom && !body.chat && !body.anthropic && !body.responses && !body.decide) { url.focus(); return editorError(t("A base URL is needed"), "warn"); } if (endpoint && !body.chat && !body.responses) { endpoint.focus(); return editorError(t(pr.endpointNeeded || "Your resource's endpoint is needed"), "warn"); } editorError(""); - saveBtn.classList.add("busy"); + saving(saveBtn, t(isNew ? "Adding…" : "Saving…")); providerAction("save", body, t(isNew ? "{name} added" : "{name} saved", { name: draft.name || draft.id })); }; saveBtn.onclick = save; @@ -8335,7 +8335,7 @@ function renderImport(im) { const add = () => { const n = (im.name ?? p.name).trim(); if (!n) { name.focus(); return status(t("Give it a name"), "warn"); } - addBtn.classList.add("busy"); + saving(addBtn, t(im.replaces ? "Replacing…" : "Adding…")); providerAction("save", { ...p, name: n, key: (im.key ?? p.key ?? "").trim() }, t("{name} added", { name: n })); }; addBtn.onclick = add; @@ -11834,10 +11834,27 @@ async function providerAction(action, body, okMsg, base = "provider/") { } catch { /* the error below says enough */ } } if (!editorError(e.message, "err")) status(e.message, "err"); - document.querySelector(".editor .busy")?.classList.remove("busy"); + for (const b of document.querySelectorAll(".editor .busy")) doneSaving(b); } } +// saving shows a button's save under way, a spinner and what it is doing in +// place of its label (zola_xynb on X: Save asks the provider for its model +// list, which can take seconds, and a button only faded looked as if the +// click had done nothing); doneSaving puts the label back. +function saving(b, text) { + b.dataset.label = b.textContent; + b.classList.add("busy", "saving"); + b.setAttribute("aria-busy", "true"); + b.replaceChildren(svg(CLI_SPIN, 11, 1.8), el("span", "", text)); +} + +function doneSaving(b) { + b.classList.remove("busy", "saving"); + b.removeAttribute("aria-busy"); + if (b.dataset.label !== undefined) { b.textContent = b.dataset.label; delete b.dataset.label; } +} + // saidMoved says what was done, and which agents it moved off models it // took away (a provider switched off or removed, the last account signed // out: #200), each to the same model elsewhere or back to its default. diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.js index 4ee16db36..eb1679386 100644 --- a/internal/gui/assets/i18n.js +++ b/internal/gui/assets/i18n.js @@ -3336,6 +3336,8 @@ const I18N = { "Added": "已添加", "In the library as {name}": "已在资源库中,名为 {name}", "Adding…": "添加中…", + "Saving…": "保存中…", + "Replacing…": "替换中…", "Needs {what} to add": "添加时需要 {what}", "Add…": "添加…", "About {name}": "关于 {name}", @@ -6793,6 +6795,8 @@ const I18N = { "Added": "追加済み", "In the library as {name}": "ライブラリに {name} として登録済み", "Adding…": "追加中…", + "Saving…": "保存中…", + "Replacing…": "置き換え中…", "Needs {what} to add": "追加には {what} が必要です", "Add…": "追加…", "About {name}": "{name} について", @@ -10269,6 +10273,8 @@ const I18N = { "Added": "Hinzugefügt", "In the library as {name}": "In der Bibliothek als {name}", "Adding…": "Wird hinzugefügt…", + "Saving…": "Wird gespeichert…", + "Replacing…": "Wird ersetzt…", "Needs {what} to add": "Zum Hinzufügen wird {what} benötigt", "Add…": "Hinzufügen…", "About {name}": "Über {name}", diff --git a/internal/gui/tests/provider-save-busy.test.cjs b/internal/gui/tests/provider-save-busy.test.cjs new file mode 100644 index 000000000..6fb430650 --- /dev/null +++ b/internal/gui/tests/provider-save-busy.test.cjs @@ -0,0 +1,109 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// A provider's Save says it is under way (zola_xynb on X: Save asks the +// provider for its model list, which can take seconds, and the button only +// faded, as if the click had done nothing). While the save is out the +// button shows a spinner and "Saving…"; a save that fails puts "Save" back, +// with the error, for another try; one that goes through closes the editor. +// English, Chinese, Japanese and German, Chromium and WebKit, narrow and +// wide; no backend, the API is faked here. +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, saves) { + const models = ["deepseek-v4-flash", "deepseek-v4-pro"].map((id) => ({ id, name: id, on: true, context: 1000000 })); + const provider = { id: "deepseek", name: "DeepSeek", icon: "generic", chat: "https://api.deepseek.example/v1", responses: "", anthropic: "", models, agents: [], key: { set: true, masked: "sk-…1234" }, ready: true }; + const providers = { providers: [provider], presets: [], excluded: [], gateway: { running: true, window: true } }; + return async (route) => { + const req = route.request(), url = new URL(req.url()); + const json = (data, status = 200) => route.fulfill({ status, json: data }); + if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:${JSON.stringify(lang)},theme:"light",web:true};` }); + if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" }); + if (url.pathname === "/api/state") return json({ agents: [], profiles: [], settings: { lang, theme: "light" } }); + if (url.pathname === "/api/providers") return json(providers); + if (url.pathname === "/api/groups") return json({ groups: [], models: [] }); + if (url.pathname === "/api/provider/save") { + // held until the test answers it: ok, or the error to give + const err = await new Promise((answer) => saves.push(answer)); + return err ? json({ error: err }, 502) : json(providers); + } + if (url.pathname === "/api/settings") return json({ theme: "light", lang, tray: "panel", currency: "usd", version: "0.1.1080", lanURLs: [], redactWords: [] }); + if (url.pathname === "/api/plugins") return json({ plugins: [] }); + if (url.pathname === "/api/usage/quotas") return json([]); + if (url.pathname.startsWith("/api/")) return json({}); + const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname); + try { + await route.fulfill({ body: await fs.readFile(file), contentType: { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)] }); + } catch { + await route.fulfill({ status: 404, body: "" }); + } + }; +} + +const words = { + en: { save: "Save", saving: "Saving…" }, + zh: { save: "保存", saving: "保存中…" }, + ja: { save: "保存", saving: "保存中…" }, + de: { save: "Speichern", saving: "Wird gespeichert…" }, +}; + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + for (const lang of Object.keys(words)) { + for (const width of [440, 900]) { + const w = words[lang]; + test(`${engine} ${lang} ${width}px: a provider's Save shows it is under way`, async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: process.env.PLAYWRIGHT_CHANNEL || "chromium" })); + t.after(() => browser.close()); + const page = await (await browser.newContext({ viewport: { width, height: 720 } })).newPage(); + page.setDefaultTimeout(5000); + const errors = [], saves = []; + page.on("pageerror", (e) => errors.push(e.message)); + await page.route("**/*", serve(lang, saves)); + await page.goto("http://magpie.test/?view=providers"); + await page.locator('.row.provider[data-id="deepseek"]').click(); + const bar = page.locator(".editor .bar"); + const save = bar.getByRole("button", { name: w.save, exact: true }); + const top = await page.evaluate(() => document.scrollingElement.scrollTop); + await save.click(); + + // out: a spinner, what it is doing, not to be clicked again + const busy = bar.locator("button.saving"); + await busy.waitFor(); + await page.waitForFunction(() => document.querySelector(".editor .bar button.saving svg") && true); + assert.equal((await busy.textContent()).trim(), w.saving); + assert.equal(await busy.getAttribute("aria-busy"), "true"); + const look = await busy.evaluate((b) => { + const s = getComputedStyle(b), svg = b.querySelector("svg"), r = b.getBoundingClientRect(), label = b.querySelector("span").getBoundingClientRect(); + return { opacity: s.opacity, events: s.pointerEvents, spin: getComputedStyle(svg).animationName, svgW: svg.getBoundingClientRect().width, inside: label.right <= r.right + 0.5 && label.width > 0, clipped: b.scrollWidth > b.clientWidth + 1 }; + }); + assert.equal(look.opacity, "1", "readable, not faded as if off"); + assert.equal(look.events, "none", "not clicked twice"); + assert.equal(look.spin, "spin"); + assert(look.svgW > 0, "the spinner is shown"); + assert(look.inside && !look.clipped, `the label fits: ${JSON.stringify(look)}`); + assert.equal(saves.length, 1); + + // failed: Save again, with why + saves.shift()("upstream: model list timed out"); + await page.getByText("upstream: model list timed out").waitFor(); + await save.waitFor(); + assert.equal(await bar.locator("button.saving, button.busy").count(), 0); + assert.equal(await save.getAttribute("aria-busy"), null); + assert.equal((await save.textContent()).trim(), w.save); + + // again, and through: the editor closes + await save.click(); + await busy.waitFor(); + while (!saves.length) await new Promise((r) => setTimeout(r, 20)); + saves.shift()(null); + await page.waitForFunction(() => !document.querySelector(".editor .bar")); + assert.equal(await page.evaluate(() => document.scrollingElement.scrollTop), top, "saving scrolled the page"); + assert.deepEqual(errors, []); + }); + } + } +}