From 94af7b6d1dbb0de9e5cedcc4999f90e2f5e6df6b Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 2 Oct 2026 15:30:33 +0800 Subject: [PATCH] =?UTF-8?q?gui:=20a=20sign-in=20for=20another=20account=20?= =?UTF-8?q?left=20unfinished=20can=20be=20closed,=20from=20beside=20Open?= =?UTF-8?q?=20again=20or=20with=20the=20editor=20(#526:=20=E5=BD=93?= =?UTF-8?q?=E6=88=91=E7=82=B9=E5=87=BB=E6=B7=BB=E5=8A=A0=E7=AC=AC=E4=BA=8C?= =?UTF-8?q?=E4=B8=AA=E8=B4=A6=E5=8F=B7=EF=BC=8C=E4=BD=86=E6=9C=80=E5=90=8E?= =?UTF-8?q?=E6=B2=A1=E6=B7=BB=E5=8A=A0=E6=97=B6=EF=BC=8C=E8=BF=99=E4=B8=AA?= =?UTF-8?q?=E8=AF=B7=E5=9C=A8=E6=B5=8F=E8=A7=88=E5=99=A8=E4=B8=AD=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E7=99=BB=E5=BD=95=E7=9A=84=E7=AA=97=E5=8F=A3=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E4=B8=8D=E4=BC=9A=E9=87=8A=E6=94=BE=EF=BC=8C=E6=97=A0?= =?UTF-8?q?=E8=AE=BA=E6=88=91=E7=82=B9=E5=87=BB=E5=BA=95=E9=83=A8=E7=9A=84?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=E3=80=81=E5=8F=96=E6=B6=88=E3=80=81=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E9=83=BD=E6=B2=A1=E5=8A=9E=E6=B3=95=E3=80=82=E5=B8=8C?= =?UTF-8?q?=E6=9C=9B=E5=9C=A8"=E9=87=8D=E6=96=B0=E6=89=93=E5=BC=80"?= =?UTF-8?q?=E6=8C=89=E9=92=AE=E6=97=81=E8=BE=B9=E5=86=8D=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=B8=80=E4=B8=AA"=E5=85=B3=E9=97=AD"=E6=8C=89=E9=92=AE)=20?= =?UTF-8?q?=E2=80=94=20the=20waiting=20box=20did=20have=20a=20Cancel,=20at?= =?UTF-8?q?=20its=20right,=20but=20the=20account=20list=20(.accts)=20is=20?= =?UTF-8?q?a=20grid=20with=20an=20auto=20column,=20so=20Qoder's=20device?= =?UTF-8?q?=20link,=20one=20long=20unbroken=20line,=20widened=20the=20box?= =?UTF-8?q?=20past=20the=20list,=20which=20clips:=20the=20link=20ran=20off?= =?UTF-8?q?=20the=20edge=20with=20its=20copy=20button,=20and=20Cancel=20wi?= =?UTF-8?q?th=20it,=20out=20of=20sight=20(as=20in=20the=20issue's=20screen?= =?UTF-8?q?shot;=20the=20"In=20use"=20pill=20of=20the=20account=20above=20?= =?UTF-8?q?went=20too).=20The=20list's=20one=20column=20is=20now=20minmax(?= =?UTF-8?q?0,=201fr),=20so=20the=20link=20is=20cut=20short=20with=20an=20e?= =?UTF-8?q?llipsis=20inside=20the=20box,=20as=20.signlink=20meant=20it=20t?= =?UTF-8?q?o=20be,=20and=20Cancel=20and=20the=20copy=20button=20are=20in?= =?UTF-8?q?=20sight.=20Beside=20Open=20again=20there=20is=20now=20Close=20?= =?UTF-8?q?(=E5=85=B3=E9=97=AD,=20titled=20"Stop=20waiting=20for=20this=20?= =?UTF-8?q?sign-in"=20/=20=E4=B8=8D=E5=86=8D=E7=AD=89=E5=BE=85=E8=BF=99?= =?UTF-8?q?=E6=AC=A1=E7=99=BB=E5=BD=95),=20which=20drops=20the=20sign-in?= =?UTF-8?q?=20as=20Cancel=20does:=20signin//cancel=20is=20posted,=20th?= =?UTF-8?q?e=20polling=20stops,=20and=20Add=20another=20is=20back.=20And?= =?UTF-8?q?=20the=20editor's=20footer=20no=20longer=20leaves=20a=20waiting?= =?UTF-8?q?=20sign-in=20behind:=20Cancel,=20Escape=20or=20a=20click=20outs?= =?UTF-8?q?ide=20(cancelEdit)=20and=20a=20Save=20or=20Remove=20that=20went?= =?UTF-8?q?=20through=20(providerAction)=20cancel=20a=20sign-in=20of=20the?= =?UTF-8?q?=20edited=20provider's=20account,=20so=20the=20editor=20opened?= =?UTF-8?q?=20again=20is=20as=20it=20was,=20not=20still=20waiting;=20an=20?= =?UTF-8?q?import=20being=20read=20is=20left=20to=20finish,=20and=20leavin?= =?UTF-8?q?g=20the=20Providers=20page=20(a=20tab=20switch)=20keeps=20the?= =?UTF-8?q?=20sign-in=20going,=20as=20before.=20signin-close.test.cjs=20(n?= =?UTF-8?q?ew;=20WebKit,=20en=20and=20zh,=20at=20the=20issue's=20970x845):?= =?UTF-8?q?=20the=20box=20and=20its=20buttons=20inside=20the=20account=20l?= =?UTF-8?q?ist,=20Close=20beside=20Open=20again,=20Close=20and=20the=20box?= =?UTF-8?q?'s=20Cancel=20each=20posting=20cancel=20and=20stopping=20the=20?= =?UTF-8?q?polling=20without=20moving=20the=20page,=20the=20editor's=20Can?= =?UTF-8?q?cel=20and=20Save=20taking=20the=20sign-in=20with=20them,=20and?= =?UTF-8?q?=20no=20other=20POST;=20it=20fails=20without=20the=20change=20(?= =?UTF-8?q?Cancel=20and=20the=20copy=20button=20outside=20the=20list;=20no?= =?UTF-8?q?=20Close).=20signin-link,=20signin-again,=20signin-callback,=20?= =?UTF-8?q?signin-closes-sheet,=20signin-paste-codex,=20signin-paste-key,?= =?UTF-8?q?=20plugin-signin,=20plugin-moved-signin,=20routing-signin,=20lo?= =?UTF-8?q?gin-import,=20account-return,=20provider-remove,=20own-signin-r?= =?UTF-8?q?emove,=20saved-signedout-remove=20and=20click-scroll=20pass=20i?= =?UTF-8?q?n=20WebKit=20(one=20signin-callback=20case=20launches=20Chromiu?= =?UTF-8?q?m=20alone,=20which=20can't=20start=20here=20without=20its=20ICU?= =?UTF-8?q?=20data).=20go=20vet=20./internal/...,=20go=20build=20-tags=20n?= =?UTF-8?q?ogui=20(darwin,=20GOOS=3Dlinux),=20GOOS=3Dwindows=20go=20build,?= =?UTF-8?q?=20go=20test=20-tags=20nogui=20./...=20pass=20(TestFXInState,?= =?UTF-8?q?=20TestUserPath=20and=20TestCodexVersion=20failed=20once=20unde?= =?UTF-8?q?r=20load,=20pass=20on=20the=20rerun;=20untouched).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/gui/assets/app.css | 2 +- internal/gui/assets/app.js | 28 +++- internal/gui/assets/i18n.js | 1 + internal/gui/tests/README.md | 10 ++ internal/gui/tests/signin-close.test.cjs | 166 +++++++++++++++++++++++ 5 files changed, 203 insertions(+), 4 deletions(-) create mode 100644 internal/gui/tests/signin-close.test.cjs diff --git a/internal/gui/assets/app.css b/internal/gui/assets/app.css index da07b2e12..6e9111993 100644 --- a/internal/gui/assets/app.css +++ b/internal/gui/assets/app.css @@ -1572,7 +1572,7 @@ body.window .ag-sk-icon { width: 22px; height: 22px; border-radius: 6px; } .acct .n { font-weight: 500; } .acct .plan { color: var(--muted); font-size: 11.5px; padding: 1px 6px; border-radius: 5px; background: var(--pill-hover); } /* a subscription's accounts: the one in use first, a way to add another */ -.accts { display: grid; border: 1px solid var(--line); border-radius: 10px; background: var(--card); overflow: hidden; } +.accts { display: grid; grid-template-columns: minmax(0, 1fr); border: 1px solid var(--line); border-radius: 10px; background: var(--card); overflow: hidden; } .acc { display: flex; align-items: center; gap: 9px; min-height: 38px; padding: 5px 8px 5px 11px; font-size: 12.5px; text-align: left; } .acc + .acc, .accts .signing { border-top: 1px solid var(--line-2); } .editor .accts label.keep-login { min-height: 34px; padding: 6px 11px; font-size: 12px; border-top: 1px solid var(--line-2); } diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 728938df1..4f1485451 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -4324,7 +4324,12 @@ function input(value, placeholder, type = "text") { i.onkeydown = (e) => { e.stopPropagation(); if (e.key === "Escape") cancelEdit(); }; return i; } -function cancelEdit() { editing = null; draft = null; importing = null; importingApps = null; renderProviders(); } +// keep: leaving the page, a sign-in under way in the editor goes on; the +// editor put away (Cancel, Escape, a click outside) takes it with it (#526) +function cancelEdit(keep) { + if (keep !== true) dropSigningIn(editing); + editing = null; draft = null; importing = null; importingApps = null; renderProviders(); +} // proxyPicker: the proxy one provider's requests go through (#237) — the // one in Settings, none, or its own — so Codex can go through a proxy @@ -6564,6 +6569,17 @@ function cancelSignIn() { renderProviders(); } +// dropSigningIn: the editor of provider id closing (Cancel, Save, Remove) +// leaves no sign-in of its account waiting behind it, to come back with the +// editor (#526: 无论我点击底部的移除、取消、保存都没办法); an import being +// read is left to finish. +function dropSigningIn(id) { + const p = typeof id === "string" && providers?.providers?.find((x) => x.id === id); + if (!p?.account || !signing || signing.agent !== p.account.agent || signing.state === "importing") return; + if (signing.id) api("signin/" + signing.id + "/cancel", {}).catch(() => {}); + signing = null; +} + // renderSigning: where a sign-in stands, in place of the button that // started it — waiting on the browser, or what went wrong. function renderSigning(sub) { @@ -6652,7 +6668,11 @@ function renderSigning(sub) { const acts = el("span", "acts"); const open = el("button", "link", t("Open again")); open.onclick = () => api("open", { url: signing.url }).catch(() => {}); - acts.append(open); + // a sign-in left unfinished is put away from beside the link too (#526) + const close = el("button", "link", t("Close")); + close.title = t("Stop waiting for this sign-in"); + close.onclick = cancelSignIn; + acts.append(open, close); tt.append(acts); } if (signing.pasteCallback || signing.pasteCode || signing.pasteKey) { @@ -7747,7 +7767,9 @@ function renderMove(p) { async function providerAction(action, body, okMsg, base = "provider/") { try { + const was = editing; providers = await api(base + action, body); + dropSigningIn(was); // saved or removed, the editor takes its sign-in with it editing = null; draft = null; importing = null; @@ -12725,7 +12747,7 @@ function show(v) { requestAnimationFrame(back); closePicker(); closeAgentModels(); - if (v !== "providers" && editing !== null) cancelEdit(); + if (v !== "providers" && editing !== null) cancelEdit(true); if (v === "gateway") loadGatewayKeys(); if (v === "providers" || v === "gateway" || v === "routing") loadProviders().then(back, (e) => status(e.message, "err")); if (v === "usage") loadUsage(true).then(back, (e) => status(e.message, "err")); diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.js index abec3adc7..dcdd4e504 100644 --- a/internal/gui/assets/i18n.js +++ b/internal/gui/assets/i18n.js @@ -576,6 +576,7 @@ const I18N = { "{name} is used through its own CLI, which isn't on this computer yet. magpie is installing it with the official installer; the sign-in page opens as soon as it's done.": "{name} 需要通过它自己的 CLI 使用,这台电脑上还没有安装。magpie 正在用官方安装脚本安装,装好后会立即打开登录页面。", "magpie opened the sign-in page. The account shows up here as soon as you're done.": "magpie 已打开登录页面,完成后账号会立即出现在这里。", "Open again": "重新打开", + "Stop waiting for this sign-in": "不再等待这次登录", "Copy link": "复制链接", "Sign-in link": "登录链接", "Account in use": "正在使用的账号", diff --git a/internal/gui/tests/README.md b/internal/gui/tests/README.md index b1128d8dd..57b802c6f 100644 --- a/internal/gui/tests/README.md +++ b/internal/gui/tests/README.md @@ -957,6 +957,16 @@ with a click, with no left-border accent. The copy button beside it says it was copied. Open again is still there, and nothing scrolls. The checks run in English and Chinese, in Chromium and WebKit. +`signin-close.test.cjs` puts away a sign-in for another account left +unfinished (#526). In Qoder's editor at the issue's window size, Add another +→ Sign in anyway waits with Qoder's long device link: the box and every +button in it stay inside the account list (the link is cut short, Cancel in +sight), and Close sits beside Open again. Close posts signin//cancel, +stops the polling and brings Add another back without moving the page; the +box's Cancel does the same, and so do the editor's Cancel and Save, so the +editor opened again has no box. English and Chinese, Chromium and WebKit; +the API is faked. + `signin-paste-codex.test.cjs` checks a ChatGPT sign-in finished from its pasted address, for a magpie the browser can't reach (on a server, in Docker). While the sign-in waits, the box says to copy the address of the diff --git a/internal/gui/tests/signin-close.test.cjs b/internal/gui/tests/signin-close.test.cjs new file mode 100644 index 000000000..8cc69a63b --- /dev/null +++ b/internal/gui/tests/signin-close.test.cjs @@ -0,0 +1,166 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// A sign-in for another account, left unfinished, can be put away (#526: +// 当我点击添加第二个账号,但最后没添加时,这个请在浏览器中完成登录的窗口状态不会释放, +// 无论我点击底部的移除、取消、保存都没办法 — 希望在"重新打开"按钮旁边再添加一个"关闭"按钮). +// In Qoder's editor, Add another account → Sign in anyway waits on the +// browser with Qoder's long device link. Close sits beside Open again: it +// tells magpie to drop the sign-in (signin//cancel), stops asking after +// it, and the Add another row is back; the box's Cancel stays inside the +// account list, the link never pushing it out of sight. The editor's own +// Cancel and Save put a waiting sign-in away too, so the editor opened again +// is as it was. +// No click moves the page. English and Chinese, Chromium and WebKit; 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"); +const LINK = "https://qoder.com/device/" + "a1b2c3d4e5".repeat(14) + "LprwDu"; +const account = { + id: "qoder", name: "Qoder", icon: "qoder", chat: "", responses: "", anthropic: "", catalog: "", + models: [{ id: "qwen3.8-max", name: "Qwen3.8-Max", on: true }], agents: [], fallback: [], headers: {}, keyList: [], + account: { agent: "qoder", agentName: "Qoder", user: "alice@example.com", logins: [{ user: "alice@example.com", active: true, on: true, own: true, plan: "personal_standard" }] }, +}; + +function serve(lang, ctl) { + return async (route) => { + const req = route.request(), url = new URL(req.url()); + const json = (data) => route.fulfill({ json: data }); + if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${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({ agents: [], profiles: [], settings: { lang, theme: "light" } }); + if (url.pathname === "/api/providers") return json({ providers: [account], presets: [], excluded: [], gateway: { running: true, window: true } }); + if (url.pathname === "/api/plugins") return json({ plugins: [] }); + if (url.pathname === "/api/groups") return json({ groups: [], pools: [] }); + if (url.pathname === "/api/gateway/trace") return json({ routes: [] }); + if (url.pathname === "/api/usage/quotas") return json([]); + if (url.pathname === "/api/signin" && req.method() === "POST") { + ctl.started++; + return json({ id: "q" + ctl.started, agent: "qoder", state: "waiting", url: LINK, instructions: "" }); + } + const m = /^\/api\/signin\/([^/]+)(\/cancel)?$/.exec(url.pathname); + if (m && m[2]) { ctl.canceled.push(m[1]); return route.fulfill({ status: 204 }); } + if (m) { + ctl.polled.push(m[1]); + return json({ id: m[1], agent: "qoder", state: ctl.canceled.includes(m[1]) ? "canceled" : "waiting", url: LINK }); + } + if (url.pathname === "/api/provider/save") { ctl.saved.push(req.postDataJSON().id); return json({ providers: [account], presets: [], excluded: [], gateway: { running: true, window: true } }); } + if (url.pathname === "/api/open") { ctl.opened.push(req.postDataJSON().url); return json({}); } + if (url.pathname.startsWith("/api/")) { ctl.other.push(req.method() + " " + url.pathname); 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)]; + try { await route.fulfill({ body: await fs.readFile(file), contentType }); } catch { await route.fulfill({ status: 404, body: "" }); } + }; +} + +const W = { + en: { add: "Add another Qoder (international) account", anyway: "Sign in anyway", wait: "Finish signing in to Qoder (international) in your browser", again: "Open again", close: "Close", cancel: "Cancel" }, + zh: { add: "添加另一个 Qoder 国际版 (qoder.com) 账号", anyway: "仍然登录", wait: "请在浏览器中完成 Qoder 国际版 (qoder.com) 登录", again: "重新打开", close: "关闭", cancel: "取消" }, +}; + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + for (const lang of ["en", "zh"]) { + test(`${engine} ${lang}: a sign-in left unfinished can be closed`, async (t) => { + const w = W[lang]; + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + const page = await (await browser.newContext({ viewport: { width: 970, height: 845 }, reducedMotion: "reduce" })).newPage(); + t.after(async () => { + if (process.env.ARTIFACT_DIR) { + await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true }); + await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-${lang}-signin-close.png`) }); + } + await browser.close(); + }); + page.setDefaultTimeout(5000); + const errors = []; + const ctl = { started: 0, canceled: [], polled: [], opened: [], other: [], saved: [] }; + page.on("pageerror", (e) => errors.push(e.message)); + page.on("dialog", (d) => { errors.push("dialog: " + d.message()); d.dismiss(); }); + await page.route("**/*", serve(lang, ctl)); + await page.goto("http://magpie.test/?view=providers"); + const open = async () => { + if (!await page.locator(".editor .accts").count()) await page.locator(".row.provider", { hasText: "Qoder" }).first().click(); + const add = page.locator(".editor .accts .acc.add", { hasText: w.add }); + await add.waitFor(); + return add; + }; + const begin = async () => { + const add = await open(); + await add.click(); + await page.locator(".editor .accts .signing button", { hasText: w.anyway }).click(); + const box = page.locator(".editor .accts .signing"); + await box.locator(".n", { hasText: w.wait }).waitFor(); + return box; + }; + const scrolls = () => page.evaluate(() => [window.scrollY, ...[...document.querySelectorAll("*")].filter((e) => e.scrollTop).map((e) => e.scrollTop)]); + + const box = await begin(); + await page.waitForTimeout(100); + if (process.env.ARTIFACT_DIR) { + await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true }); + await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-${lang}-signin-waiting.png`) }); + } + // the box and every button in it are inside the account list, in + // sight: the long link doesn't push them out + const fit = await box.evaluate((b) => { + const r = b.closest(".accts").getBoundingClientRect(); + return [b, ...b.querySelectorAll("button")].map((x) => { + const q = x.getBoundingClientRect(); + return { t: x.textContent.slice(0, 30), inside: q.width > 0 && q.left >= r.left - 0.5 && q.right <= r.right + 0.5 }; + }); + }); + assert(fit.every((f) => f.inside), JSON.stringify(fit)); + // Close beside Open again + const acts = box.locator(".acts"); + assert.deepEqual(await acts.locator("button").allTextContents(), [w.again, w.close]); + for (let i = 0; i < 40 && !ctl.polled.length; i++) await page.waitForTimeout(50); + assert(ctl.polled.length, "the sign-in is followed"); + const before = await scrolls(); + await acts.getByRole("button", { name: w.close, exact: true }).click(); + await page.locator(".editor .accts .acc.add", { hasText: w.add }).waitFor(); + assert.equal(await page.locator(".editor .accts .signing").count(), 0, "the box is gone"); + for (let i = 0; i < 40 && !ctl.canceled.length; i++) await page.waitForTimeout(25); + assert.deepEqual(ctl.canceled, ["q1"], "magpie is told to drop it"); + assert.deepEqual(await scrolls(), before, "the click moved the page"); + // no more asking after it + const polled = ctl.polled.length; + await page.waitForTimeout(2000); + assert(ctl.polled.length <= polled + 1, `still polled: ${ctl.polled.length - polled}`); + + // the box's own Cancel does the same + const box2 = await begin(); + await box2.locator(":scope > button", { hasText: w.cancel }).click(); + await page.locator(".editor .accts .acc.add", { hasText: w.add }).waitFor(); + assert.deepEqual(ctl.canceled, ["q1", "q2"]); + + // 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 .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"]); + await open(); + assert.equal(await page.locator(".editor .accts .signing").count(), 0, "the box came back with the editor"); + + // so does Save + await begin(); + await page.locator(".editor .bar button.primary").last().click(); + await page.locator(".editor .accts").waitFor({ state: "detached" }); + for (let i = 0; i < 40 && ctl.canceled.length < 4; i++) await page.waitForTimeout(25); + assert.deepEqual(ctl.canceled, ["q1", "q2", "q3", "q4"]); + assert.deepEqual(ctl.saved, ["qoder"]); + await open(); + await page.waitForTimeout(1000); + assert.equal(await page.locator(".editor .accts .acc.add", { hasText: w.add }).count(), 1, "the editor stays open, with Add another"); + + const missing = await page.evaluate(() => ["Close", "Stop waiting for this sign-in"].filter((k) => !I18N.zh[k])); + assert.deepEqual(missing, []); + assert.deepEqual(ctl.other.filter((o) => o.startsWith("POST") && !o.includes("/api/open")), [], "nothing else was changed"); + assert.deepEqual(errors, []); + }); + } +}