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, []); + }); + } +}