diff --git a/internal/gui/assets/plugins/routing/client.js b/internal/gui/assets/plugins/routing/client.js index 14332f58f..cc5f21a7b 100644 --- a/internal/gui/assets/plugins/routing/client.js +++ b/internal/gui/assets/plugins/routing/client.js @@ -6457,7 +6457,16 @@ function routingPage(part) { const edit = el("button", "text", t("Edit")); edit.onclick = (e) => { e.stopPropagation(); open(); }; const open = () => openGroupEditor(g); - row.onclick = open; + // words dragged across on the card are being selected to copy + // (jiabubu on Discord), not a click that opens the editor; a click + // with something else selected still opens it + let selAtDown = ""; + row.onpointerdown = () => { selAtDown = String(getSelection() || ""); }; + row.onclick = () => { + const s = getSelection(); + if (s && !s.isCollapsed && row.contains(s.focusNode) && String(s) !== selAtDown) return; + open(); + }; row.oncontextmenu = (e) => { e.preventDefault(); groupMenu(ics, g); }; row.append(ics, main, tags, edit); // a group of the user's switches off as a whole, kept as it is (PAMI diff --git a/internal/gui/assets/plugins/routing/routing.css b/internal/gui/assets/plugins/routing/routing.css index e65799b71..57e097b53 100644 --- a/internal/gui/assets/plugins/routing/routing.css +++ b/internal/gui/assets/plugins/routing/routing.css @@ -590,6 +590,12 @@ button.rt-session { cursor: pointer; } .rt-gedit button.primary.busy { opacity: .75; display: inline-flex; align-items: center; gap: 6px; } .rt-gedit button.primary.busy::before { content: ""; width: 10px; height: 10px; border-radius: 50%; border: 1.6px solid currentColor; border-right-color: transparent; animation: spin .7s linear infinite; } @media (prefers-reduced-motion: reduce) { .rt-gedit button.primary.busy::before { animation-duration: 2s; } } +/* what a group's card and its editor say can be selected and copied + (jiabubu on Discord): its name and id, its models, its patterns, what + is left of its accounts; a drag across them selects, a click still + opens the editor (groupRow). Its controls stay controls. */ +.rt-groups:not(.sorting) > .rt-group :is(.nm b, .nm code.mdl, .mem), +.rt-gedit .fallback:not(.rt-rules) > .fbl:not(.sorting) > .fbrow > .n, .rt-gaccts > .fbrow > .n { user-select: text; -webkit-user-select: text; } .rt-group .mem { margin-top: 2px; font-size: 11.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } /* a pattern the group finds models by, and how many it matches now */ .rt-group .mem.pat code, .rt-pats code { font: var(--code-style, normal) 500 11px/1.5 var(--code-font, var(--code)); font-stretch: var(--code-stretch, 100%); color: var(--fg-2); } diff --git a/internal/gui/tests/group-select-copy.test.cjs b/internal/gui/tests/group-select-copy.test.cjs new file mode 100644 index 000000000..26b757afa --- /dev/null +++ b/internal/gui/tests/group-select-copy.test.cjs @@ -0,0 +1,125 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// What a routing group's card says can be selected and copied (jiabubu on +// Discord): its patterns, its models, its id. A drag across them selects +// and leaves the editor shut; a click still opens it, and the page doesn't +// move. In the editor its models' names and its patterns can be selected +// too. Its controls (the routing, Edit, the handle) can't. In both engines, +// in English and Chinese. +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 models = [ + { id: "or/a:free", name: "a:free", providerName: "OpenRouter", icon: "generic" }, + { id: "or/c:free", name: "c:free", providerName: "OpenRouter", icon: "generic" }, + { id: "an/claude-opus-5-5", name: "claude-opus-5-5", providerName: "Anthropic", icon: "generic" }, +]; +const members = ["an/claude-opus-5-5", "or/a:free", "or/c:free"]; +const groups = () => ({ + models, pools: [], + groups: [{ id: "free", name: "Free", members, match: ["or/*:free"], matched: ["or/a:free", "or/c:free"], fast: [], off: [], routing: "order", ready: true, + patterns: [{ pattern: "or/*:free", models: 2 }], + memberInfo: members.map((id) => ({ id, ready: true })) }, + { id: "two", name: "Two", members: ["an/claude-opus-5-5"], match: [], matched: [], fast: [], off: [], routing: "order", ready: true, patterns: [], + memberInfo: [{ id: "an/claude-opus-5-5", ready: true }] }], +}); + +function serve(lang, posts) { + const state = { agents: [{ id: "codex", name: "Codex", path: "/test/codex", fields: [] }], profiles: [], settings: { lang, theme: "light" } }; + return async (r) => { + const url = new URL(r.request().url()); + const json = (data) => r.fulfill({ json: data }); + if (url.pathname === "/boot.js") return r.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` }); + if (url.pathname === "/wails/runtime.js") return r.fulfill({ contentType: "text/javascript", body: "export const Window = {};" }); + if (url.pathname === "/api/state") return json(state); + if (url.pathname === "/api/plugins") return json({ plugins: [] }); + if (url.pathname === "/api/gateway/trace") { + if (url.searchParams.get("wait")) return new Promise(() => {}); // nothing more comes + return json({ mine: true, now: new Date().toISOString(), seq: 1, totals: { requests: 0, rerouted: 0, errors: 0 }, routes: [] }); + } + if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] }); + if (url.pathname === "/api/groups") return json(groups()); + if (url.pathname.startsWith("/api/groups/")) { + posts.push({ path: url.pathname, body: JSON.parse(r.request().postData() || "{}") }); + return json(groups()); + } + if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true, url: "http://127.0.0.1:3999" } }); + if (url.pathname.startsWith("/api/")) 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)]; + await r.fulfill({ body: await fs.readFile(file), contentType }); + }; +} + +// drag drags the mouse across loc's text, as a reader selecting it does, +// scrolled to with the wheel as a reader does, clear of the window's foot, +// and gives what ended up selected +async function drag(page, loc) { + await page.evaluate(() => getSelection().removeAllRanges()); + let b = await loc.boundingBox(); + for (let i = 0; i < 20 && (b.y < 120 || b.y + b.height > 1200); i++) { + await page.mouse.move(10 + b.width / 2, 600); + await page.mouse.wheel(0, b.y - 600); + await page.waitForTimeout(120); + b = await loc.boundingBox(); + } + const y = b.y + b.height / 2; + await page.mouse.move(b.x + 1, y); + await page.mouse.down(); + await page.mouse.move(b.x + b.width / 2, y, { steps: 4 }); + await page.mouse.move(b.x + b.width - 1, y, { steps: 4 }); + await page.mouse.up(); + await page.waitForTimeout(100); // a click the drag ended in has run + return page.evaluate(() => String(getSelection())); +} +const selectOf = (loc) => loc.evaluate((e) => { const s = getComputedStyle(e); return s.webkitUserSelect || s.userSelect; }); +const scrolls = (page) => page.evaluate(() => [...document.querySelectorAll("*")].filter((e) => e.scrollTop).map((e) => e.scrollTop).concat(scrollY).join(",")); + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + for (const lang of ["en", "zh"]) { + test(`${engine} ${lang}: a group's card and editor can be selected and copied`, async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + const page = await (await browser.newContext({ viewport: { width: 1100, height: 1400 }, reducedMotion: "reduce" })).newPage(); + t.after(() => browser.close()); + page.setDefaultTimeout(5000); + page.setDefaultNavigationTimeout(60000); + const errors = [], posts = []; + page.on("pageerror", (e) => errors.push(e.message)); + await page.route("**/*", serve(lang, posts)); + await page.goto("http://magpie.test/?view=routing"); + const card = page.locator(".rt-group", { hasText: "Free" }); + await card.waitFor(); + const ed = page.locator(".rt-gedit"); + const at = await scrolls(page); + + // the pattern, the models, the id: dragged across, selected, and the editor stays shut + assert.equal(await drag(page, card.locator(".mem.pat code")), "or/*:free", "the pattern is selected"); + assert.equal(await ed.count(), 0, "a drag across the pattern doesn't open the editor"); + const mem = card.locator(".mem:not(.pat)").first(); + const said = (await mem.textContent()).trim(); + assert.equal((await drag(page, mem)).trim(), said, "the models are selected"); + assert.equal(await ed.count(), 0); + assert.equal(await drag(page, card.locator("code.mdl")), "group/free", "its id is selected"); + assert.equal(await ed.count(), 0); + assert.equal(await scrolls(page), at, "selecting moves nothing"); + // its controls stay controls + for (const c of [card.locator(".rt-groute"), card.locator(".rt-ghandle"), card.locator("button.text").last()]) assert.equal(await selectOf(c), "none"); + + // a click, with words still selected on the card, opens the editor + await card.locator(".mem.pat").click({ position: { x: 3, y: 3 } }); + await ed.waitFor(); + assert.equal(await scrolls(page), at, "the click moves nothing either"); + // in the editor, a model's name and a pattern are selected too + const name = ed.locator(".fallback:not(.rt-pats) .fbrow .n > span:first-child", { hasText: /^claude-opus-5-5$/ }); + assert.equal(await drag(page, name), "claude-opus-5-5"); + const pat = ed.locator(".rt-pats .fbrow code"); + assert.equal(await drag(page, pat), "or/*:free"); + assert.equal(await selectOf(ed.locator(".rt-pats button", { hasText: lang === "zh" ? "移除" : "Remove" }).first()), "none"); + assert.deepEqual(posts, [], "selecting saves nothing"); + assert.deepEqual(errors, []); + }); + } +}