diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 94ff7567f..84e1c4b06 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -9432,6 +9432,13 @@ function duplicateProvider(p) { renderProviders(); } +// closeWhenClean: an editor's Cancel says Close while nothing waits for its +// Save; its switches and pills that act at once are done already +function closeWhenClean(ed, cancel) { + const say = () => { cancel.textContent = t(providerDirty() ? "Cancel" : "Close"); }; + for (const ev of ["input", "change", "click"]) ed.addEventListener(ev, () => setTimeout(say)); + say(); +} // renderEditor: an existing provider (p), a new preset (presetID), or custom. function renderEditor(p, presetID) { // its own icons, not the page's kept ones, which the rows after it take back @@ -9443,6 +9450,8 @@ function renderEditor(p, presetID) { providerDraftRef = draft; providerDraftBase = providerDraftValue(); } + const cancel = p && editor.querySelector(":scope > .bar .ed-cancel"); + if (cancel) closeWhenClean(editor, cancel); return editor; } finally { keptIcons = kept; } } @@ -9748,7 +9757,7 @@ function drawEditor(p, presetID) { del.title = t("{agent} stays signed in; magpie just stops offering it", { agent: a.agentName }); del.onclick = () => providerAction("delete", { id: p.id }, t("{name} removed", { name: p.name })); bar.append(del, el("span", "grow")); - const cancel = el("button", "text", t("Cancel")); + const cancel = el("button", "text ed-cancel", t("Cancel")); cancel.onclick = cancelEdit; const saveBtn = el("button", "text primary", t("Save")); saveBtn.onclick = () => { @@ -10421,7 +10430,7 @@ function drawEditor(p, presetID) { bar.append(dup); } bar.append(el("span", "grow")); - const cancel = el("button", "text", t("Cancel")); + const cancel = el("button", "text ed-cancel", t("Cancel")); cancel.onclick = cancelEdit; const saveBtn = el("button", "text primary", t(isNew ? "Add" : "Save")); const save = () => { diff --git a/internal/gui/tests/balance-url-first.test.cjs b/internal/gui/tests/balance-url-first.test.cjs index d1a684a2d..d10565510 100644 --- a/internal/gui/tests/balance-url-first.test.cjs +++ b/internal/gui/tests/balance-url-first.test.cjs @@ -87,7 +87,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", assert.deepEqual(fit, { inside: true, url: true }); // one with no Balance URL keeps Advanced shut, as before - await page.locator(".editor .bar").getByRole("button", { name: await tr("Cancel"), exact: true }).click(); + await page.locator(".editor .bar .ed-cancel").click(); await page.locator(".row.provider", { hasText: "Plain" }).click(); assert.equal(await page.locator(".editor details.advanced").evaluate((d) => d.open), false, "nothing set, nothing opened"); diff --git a/internal/gui/tests/lane-limit.test.cjs b/internal/gui/tests/lane-limit.test.cjs index 9384124d6..76459dbc2 100644 --- a/internal/gui/tests/lane-limit.test.cjs +++ b/internal/gui/tests/lane-limit.test.cjs @@ -73,12 +73,12 @@ function serve(lang, posts, lanes) { const words = { en: { busy: "1/1 running · 2 queued", running: "3 running", one: "1 at once", two: "2 at once", three: "3 at once", four: "4 at once", five: "5 at once", seven: "7 at once", none: "No limit at once", - menu: "Requests at once", def: "Provider's", other: "Other…", save: "Save", queue: "Queue size", wait: "Queue wait", + menu: "Requests at once", def: "Provider's", other: "Other…", save: "Save", cancel: "Cancel", close: "Close", queue: "Queue size", wait: "Queue wait", badQueue: "Queue size: a whole number from 0 to 10000", }, zh: { busy: "1/1 运行中 · 2 排队", running: "3 运行中", one: "并发 1", two: "并发 2", three: "并发 3", four: "并发 4", five: "并发 5", seven: "并发 7", none: "并发不限", - menu: "同时请求数", def: "沿用提供商", other: "其他…", save: "保存", queue: "排队上限", wait: "排队等待", + menu: "同时请求数", def: "沿用提供商", other: "其他…", save: "保存", cancel: "取消", close: "关闭", queue: "排队上限", wait: "排队等待", badQueue: "排队上限:0 到 10000 的整数", }, }; @@ -210,6 +210,8 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", await item(page, "2").click(); const p = await posted(posts, n); assert.deepEqual(p.body, { id: "codex", account: "me@example.com", limit: 2 }); + await page.waitForTimeout(250); + assert.equal(await page.locator(".editor > .bar .ed-cancel").textContent(), w.close, "a signed-in provider's editor says Close too"); assert.deepEqual(errors, []); }); @@ -247,6 +249,9 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", await box.fill("5"); assert.equal(await pill(page, "k2").textContent(), w.five, "the provider's limit as typed"); assert.equal(await pill(page, "k1").textContent(), w.one, "its own limit stays"); + // brought into sight first: a scroll that lands after the click closes the menu + await pill(page, "k2").scrollIntoViewIfNeeded(); + await page.waitForTimeout(250); await pill(page, "k2").click(); await menu(page).waitFor(); assert.match(await item(page, w.def).textContent(), new RegExp(w.five), "the menu's provider's limit as typed"); @@ -259,6 +264,34 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", assert.deepEqual(errors, []); }); + test(`${engine} ${lang}: the editor says Close while nothing waits for Save, and Cancel once something does`, async (t) => { + // a key's pill acts at once, so after it there is nothing to cancel + const lanes = { now: {}, reads: 0 }; + const { page, errors, posts } = await open(t, "Relay", lanes); + const bar = page.locator(".editor > .bar"); + const says = () => bar.locator(".ed-cancel").textContent(); + assert.equal(await says(), w.close); + await pill(page, "k2").click(); + await menu(page).waitFor(); + const n = posts.length; + await item(page, "3").click(); + await posted(posts, n); + await page.waitForTimeout(250); + assert.equal(await says(), w.close, "a pill that acts at once leaves nothing to cancel"); + const box = page.locator(".editor input.concurrency"); + await box.fill("5"); + await page.waitForTimeout(50); + assert.equal(await says(), w.cancel, "a typed Concurrency waits for Save"); + await box.fill("2"); + await page.waitForTimeout(50); + assert.equal(await says(), w.close, "typed back as saved"); + await bar.getByRole("button", { name: w.close, exact: true }).click(); + await page.locator(".editor").waitFor({ state: "detached" }); + assert.equal(await page.locator(".action-confirm").count(), 0, "nothing to discard, nothing asked"); + assert.deepEqual(await page.evaluate(() => ["zh", "zh-TW", "ja", "de"].filter((l) => !I18N[l].Close)), []); + assert.deepEqual(errors, []); + }); + test(`${engine} ${lang}: the queue's length and wait are saved with the editor, and refused out of range`, async (t) => { const lanes = { now: {}, reads: 0 }; const { page, errors, posts } = await open(t, "Relay", lanes); diff --git a/internal/gui/tests/nokey-editor.test.cjs b/internal/gui/tests/nokey-editor.test.cjs index c8cb3690b..48176795c 100644 --- a/internal/gui/tests/nokey-editor.test.cjs +++ b/internal/gui/tests/nokey-editor.test.cjs @@ -70,7 +70,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", const link = ed.locator(".ehead .link"); assert.equal(await link.count() ? await link.textContent() : null, links[name], `${name}: its head's link`); assert.deepEqual(errors, [], `${name}: nothing throws`); - await ed.locator(".bar").getByRole("button", { name: w.cancel, exact: true }).click(); + await ed.locator(".bar .ed-cancel").click(); await ed.waitFor({ state: "detached" }); } }); diff --git a/internal/gui/tests/preset-name.test.cjs b/internal/gui/tests/preset-name.test.cjs index e34e6cb1c..cfb997031 100644 --- a/internal/gui/tests/preset-name.test.cjs +++ b/internal/gui/tests/preset-name.test.cjs @@ -92,7 +92,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", ed = await open("My Relay"); assert.equal(await nameBox(ed).count(), 1, "custom: one Name box"); assert.equal(await ed.locator("input.provider-name").count(), 0); - await ed.locator(".bar").getByRole("button", { name: w.cancel, exact: true }).click(); + await ed.locator(".bar .ed-cancel").click(); await ed.waitFor({ state: "detached" }); assert.deepEqual(errors, [], "nothing throws"); }); diff --git a/internal/gui/tests/qianfan-plan.test.cjs b/internal/gui/tests/qianfan-plan.test.cjs index 1ae57d00a..fe6571b7b 100644 --- a/internal/gui/tests/qianfan-plan.test.cjs +++ b/internal/gui/tests/qianfan-plan.test.cjs @@ -63,7 +63,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", await page.locator(".row.provider", { hasText: "Qianfan API" }).click(); await page.locator(".editor").waitFor(); assert.equal(await page.locator(".editor .stepplan").count(), 0); - await page.locator(".editor .bar").getByRole("button", { name: words[lang].cancel, exact: true }).click(); + await page.locator(".editor .bar .ed-cancel").click(); await page.locator(".editor").waitFor({ state: "detached" }); // the one not signed in takes a paste, for its own id @@ -77,7 +77,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", assert.equal(posts[0].body.text, "magpie-qianfan:{}"); // the one signed in signs out, for its own id - await page.locator(".editor .bar").getByRole("button", { name: words[lang].cancel, exact: true }).click(); + await page.locator(".editor .bar .ed-cancel").click(); await page.locator(".editor").waitFor({ state: "detached" }); await page.locator(".row.provider", { hasText: "Qianfan Plan" }).click(); await page.locator(".editor .stepplan").getByRole("button", { name: words[lang].out, exact: true }).click(); @@ -87,7 +87,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", assert.equal(await page.locator(".editor .stepplan .ended").count(), 0); // the one Baidu ended says so and takes a new paste, for its own id - await page.locator(".editor .bar").getByRole("button", { name: words[lang].cancel, exact: true }).click(); + await page.locator(".editor .bar .ed-cancel").click(); await page.locator(".editor").waitFor({ state: "detached" }); await page.locator(".row.provider", { hasText: "Qianfan Old" }).click(); assert.match(await page.locator(".editor .stepplan .ended").innerText(), new RegExp(words[lang].ended)); diff --git a/internal/gui/tests/signin-close.test.cjs b/internal/gui/tests/signin-close.test.cjs index d9ebcee70..de36d94bf 100644 --- a/internal/gui/tests/signin-close.test.cjs +++ b/internal/gui/tests/signin-close.test.cjs @@ -146,7 +146,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", // the editor's Cancel puts a waiting sign-in away: opened again, the // editor has Add another, not the box await begin(); - await page.locator(".editor .bar button", { hasText: new RegExp("^" + w.cancel + "$") }).last().click(); + await page.locator(".editor .bar .ed-cancel").click(); await page.locator(".editor .accts").waitFor({ state: "detached" }); for (let i = 0; i < 40 && ctl.canceled.length < 3; i++) await page.waitForTimeout(25); assert.deepEqual(ctl.canceled, ["q1", "q2", "q3"]); diff --git a/internal/gui/tests/stepfun-per-provider.test.cjs b/internal/gui/tests/stepfun-per-provider.test.cjs index fd4e7e781..444060e8e 100644 --- a/internal/gui/tests/stepfun-per-provider.test.cjs +++ b/internal/gui/tests/stepfun-per-provider.test.cjs @@ -68,7 +68,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", assert.equal(posts[0].body.text, "magpie-stepfun:{}"); // the one signed in signs out, for its own id - await page.locator(".editor .bar").getByRole("button", { name: words[lang].cancel, exact: true }).click(); + await page.locator(".editor .bar .ed-cancel").click(); await page.locator(".editor").waitFor({ state: "detached" }); await page.locator(".row.provider", { hasText: "StepFun" }).filter({ hasNotText: "Work" }).click(); await page.locator(".editor .stepplan").getByRole("button", { name: words[lang].out, exact: true }).click();