From b34243823e894e8b36109db15fa87cda73ff3eb2 Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 16 Nov 2026 09:52:20 +0800 Subject: [PATCH] routing: accounts' resets stay in their column, and resizing over many accounts is smooth (lexrus on X) lexrus on X: on Routing's Accounts and keys, at some widths "resets in 3h" ran over the next column and the right column's words were cut, and with many accounts dragging the window's edge was janky. Overlap: the window parts' widths (--wk/--wv/--wr) were taken from the widest window of the whole list, so one provider's long reset pushed every row past its column. They are now per provider, and CSS caps each part to its row. Jank: the list was CSS columns, and the browser balanced every row again on each layout. packActs (plugins/routing/client.js) places each provider's box (and a long box's rows) itself under .rt-acts.packed with --px/--py: down a column and on into the next, ends as level as a binary search on the cap allows, a heading never last in a column. It runs on the window's resize, on render, and a frame after a box's height changes. Side mode (min1150, beside the requests) is unchanged: one scrolled column, nothing placed. Measured on mote with a 100-row fixture, per resize step: WebKit layout 26.6ms -> 5.9ms, frame p90 35 -> 18ms; Chromium layout 8.24ms -> 2.86ms. Test: routing-account-windows-fit.test.cjs, 4 and 20 Codex accounts, en and zh, Chromium and WebKit, widths 420-1600: no window part runs past or is cut, a provider's lines start together, the list isn't CSS columns, boxes sit inside the list in column order without overlapping, 2+ columns at 700-1099. Broken by hand: the old assets are red on the overlap; packing turned off is red ("the list isn't CSS columns"). gui-shell.md says how the columns are placed. Run on mote at this change on origin/main 7bdda0450 (pushed on 3b643f397, which changes internal/provider only): - the 209 GUI files that open Routing or read its files, plus i18n-keys and builtins: 3736 pass, 42 fail. Every red is red the same on a clean origin/main 7bdda0450 copy, or is filed: quota-bars-alike (#6700), ctx-session-line (#5801), quota-credits (#6807, filed now), routing-pin-away (#6512), routing-row-swap (#3799), routing-story-shrinks (#3954), mobile-web (needs .git for its origin/main baseline; #3952, #5013). routing-idle-frame's one WebKit red came under load (#3773) and passed 2 of 2 alone at this head. - go test ./internal/gui: ok. gofmt -l: clean (no Go changed). Not tried: the real Wails app, Linux WebKitGTK, Windows WebView2, and lexrus's own accounts. --- docs/subsystems/gui-shell.md | 15 +- internal/gui/assets/plugins/routing/client.js | 111 ++++++++++- .../gui/assets/plugins/routing/routing.css | 19 +- .../routing-account-windows-fit.test.cjs | 175 ++++++++++++++++++ 4 files changed, 305 insertions(+), 15 deletions(-) create mode 100644 internal/gui/tests/routing-account-windows-fit.test.cjs diff --git a/docs/subsystems/gui-shell.md b/docs/subsystems/gui-shell.md index 9d873a435..9044cbcde 100644 --- a/docs/subsystems/gui-shell.md +++ b/docs/subsystems/gui-shell.md @@ -353,7 +353,20 @@ its own only on a row with nothing else to say or one to act on; on the others (`.said`) it is in the tooltip with the models it answered, under its id. A folded row of keys or accounts names no models. Columns are `min(240px, 40vw)` wide, so a name and its Try now share a line -(`routing-acts-calm.test.cjs`). +(`routing-acts-calm.test.cjs`). They aren't CSS columns: `packActs` +places each provider's box (`.rt-pgrp`) with `--px`/`--py` under +`.rt-acts.packed`, in order down a column and on into the next, the +columns' ends as level as they go, and a `.long` box's rows run on into +the next column, its heading never last in one. CSS columns balanced +every row again on each layout, ~24ms a layout in WebKit with a hundred +rows, and dragging the window's edge stuttered (lexrus on X). It places +on the window's resize, before the frame is drawn, on a render, and a +frame after a box changes its height. Beside the requests (`min1150`, +not `solo`) the list is one scrolled column and nothing is placed. An +account's windows line up with the rows of its own provider (`--wk`, +`--wv`, `--wr` per provider), and no part runs past its row: a width the +whole list's widest window asked for ran the resets over the column +beside. See `routing-account-windows-fit.test.cjs`. `renderActs` in [`plugins/routing/client.js`](../../internal/gui/assets/plugins/routing/client.js) shows the first `ACTS_SHOWN` (8) rows of a longer list and puts the rest diff --git a/internal/gui/assets/plugins/routing/client.js b/internal/gui/assets/plugins/routing/client.js index 93754fd27..13dab05be 100644 --- a/internal/gui/assets/plugins/routing/client.js +++ b/internal/gui/assets/plugins/routing/client.js @@ -417,10 +417,93 @@ function routingPage(part) { inMetaFit = false; for (const [p, top] of kept) if (p.scrollTop !== top) p.scrollTop = top; } - if (side !== was) more.classList.toggle("min1150", side); + if (side !== was) { more.classList.toggle("min1150", side); packActs(); } }); }; let sideW = -1; new ResizeObserver(() => { const w = more.clientWidth; if (w && w !== sideW) { sideW = w; sideFit(); } }).observe(more); + // The accounts and keys in columns (under the requests, or alone): + // each provider's box placed in its column here, in order down one + // column and on into the next, the columns' ends as near level as they + // go, as CSS columns placed them. CSS columns balanced every row of the + // list again on each layout, ~24ms in WebKit for a hundred rows and + // several layouts a frame, so dragging the window's edge stuttered + // (lexrus on X); placed boxes are laid out as one column's rows are. + // A provider's rows long enough to run on into the next column (.long) + // go on there as they did, its heading never last in a column: the box + // is where its first rows are, and its rows past them are placed from + // it. Placed before the frame is laid out (on the window's resize, + // after a drawing), so the frame drawn has them where they go; a box + // that only changed its height is placed on the next frame. + const placed = new WeakMap(); + const placeAt = (e, left, top) => { + const spot = left + "|" + top; + if (placed.get(e) === spot) return; + placed.set(e, spot); + e.style.setProperty("--px", left); e.style.setProperty("--py", top); // read only while placed: a row is position: relative + }; + let packCols = 0, packing = 0; + const packActs = () => { + if (packing) { cancelAnimationFrame(packing); packing = 0; } + const kids = [...acts.children]; + const inCols = !more.classList.contains("min1150") || hist.classList.contains("solo"); + if (!inCols || !kids.length) { + if (acts.classList.contains("packed")) acts.classList.remove("packed"); + packCols = 0; + return; + } + const w = acts.clientWidth; + if (!w) return; // out of sight: placed as it was + // as many as CSS columns of min(240px, 40vw) fit + const n = Math.max(1, Math.floor(w / Math.min(240, innerWidth * .4))); + if (!acts.classList.contains("packed")) acts.classList.add("packed"); + if (n !== packCols) { packCols = n; acts.style.setProperty("--acols", String(n)); } + // what goes in a column whole: a box, or a long box's rows one by one + // (its heading with the row under it), the first with the box's top + const units = []; + for (const g of kids) { + if (!g.classList.contains("long") || !g.children.length) { units.push({ g, es: [], h: g.getBoundingClientRect().height }); continue; } + const pad = parseFloat(getComputedStyle(g).paddingTop) || 0; + let u = null; + for (const e of g.children) { + const h = e.getBoundingClientRect().height; + if (!u) units.push(u = { g, es: [e], hs: [h], h: pad + h, pad }); + else if (u.es.length === 1 && u.es[0].classList.contains("rt-prov") && u.g === g) { u.es.push(e); u.hs.push(h); u.h += h; } + else units.push(u = { g, es: [e], hs: [h], h, pad: 0 }); + } + } + const hs = units.map((u) => u.h); + // each column filled to at most cap: the least cap that takes n columns + const fill = (cap) => { + let c = 0, y = 0; + const at = hs.map((h) => { if (y > 0 && y + h > cap + .5) { c++; y = 0; } const p = [c, y]; y += h; return p; }); + return { at, cols: c + 1 }; + }; + let lo = Math.max(...hs), hi = hs.reduce((a, b) => a + b, 0); + while (hi - lo > .5) { const mid = (lo + hi) / 2; if (fill(mid).cols <= n) hi = mid; else lo = mid; } + const { at } = fill(fill(lo).cols <= n ? lo : hi); + const top = parseFloat(getComputedStyle(acts).paddingTop) || 0; + let tall = 0, from = null; // from: the long box's own column and top + at.forEach(([c, y], i) => { + const u = units[i]; + tall = Math.max(tall, y + u.h); + if (!u.es.length) { placeAt(u.g, c * 100 / n + "%", top + y + "px"); return; } + if (!from || from.g !== u.g) { + from = { g: u.g, c, y }; + placeAt(u.g, c * 100 / n + "%", top + y + "px"); + } + // the box is as tall as its rows in its own column + if (c === from.c) u.g.style.setProperty("--part-h", y + u.h - from.y + "px"); + let ey = y + u.pad - from.y; + u.es.forEach((e, j) => { placeAt(e, (c - from.c) * 100 + "%", ey + "px"); ey += u.hs[j]; }); + }); + if (acts.style.getPropertyValue("--acts-h") !== tall + "px") acts.style.setProperty("--acts-h", tall + "px"); + }; + // on the next frame: placed in the observer's call, the list's new + // height is one WebKit reports as a ResizeObserver loop + const packSoon = () => { if (!packing) packing = requestAnimationFrame(packActs); }; + const packSeen = new ResizeObserver(packSoon); + packSeen.observe(actHead); + addEventListener("resize", () => { if (shown()) packActs(); }); const path = () => { const p = document.createElementNS(NS, "path"); wires.appendChild(p); return p; }; const setText = (e, s) => { if (e.textContent !== s) e.textContent = s; }; @@ -5093,17 +5176,23 @@ function routingPage(part) { if (!u || a.last > u.last) inUse.set(a.w.provider, a); } // each subscription account's windows, laid out alike on every row - // (zwm_vincent on X): the short names' column as wide as the widest, - // so what is left and when it resets start at one place down the list. - // A wallet keeps its one line, with what of it lapses first + // of its provider (zwm_vincent on X): the short names' column as wide + // as the widest, so what is left and when it resets start at one place + // down its rows. A provider's own: as wide as the widest of the whole + // list, WorkBuddy's "deepseek-v4.1-flash tokens (est.)" made every + // window of every provider wider than a column, and the resets ran + // over the column beside (lexrus on X). A wallet keeps its one line, + // with what of it lapses first const winsBy = new Map(), chw = (s) => [...s].reduce((n, c) => n + (c.charCodeAt(0) > 0x2e7f ? 2 : 1), 0); - let wk = 0, wv = 0, wr = 0; + const widths = new Map(); // a provider โ†’ its windows' widest parts, in ch for (const a of list) { if (day || a.w.kind !== "account" || a.w.balance != null) continue; const ps = winParts(a.w); if (!ps.length) continue; winsBy.set(a, ps); - for (const p of ps) { wk = Math.max(wk, chw(p.k)); wv = Math.max(wv, chw(p.v)); wr = Math.max(wr, chw(p.r)); } + const m = widths.get(a.w.provider) || { k: 0, v: 0, r: 0 }; + for (const p of ps) { m.k = Math.max(m.k, chw(p.k)); m.v = Math.max(m.v, chw(p.v)); m.r = Math.max(m.r, chw(p.r)); } + widths.set(a.w.provider, m); } for (const a of list) { const w = a.w; @@ -5210,10 +5299,10 @@ function routingPage(part) { // there when a window has no bar or no reset. Where the row is too // narrow for the reset, it goes under, on every row alike. Each in // full on hover, with the day and time it resets - const box = el("div", "wq"); - box.style.setProperty("--wk", wk + 1 + "ch"); - box.style.setProperty("--wv", wv + "ch"); - box.style.setProperty("--wr", wr + "ch"); + const box = el("div", "wq"), wd = widths.get(w.provider); + box.style.setProperty("--wk", wd.k + 1 + "ch"); + box.style.setProperty("--wv", wd.v + "ch"); + box.style.setProperty("--wr", wd.r + "ch"); for (const p of grid) { const line = el("div"); let bar = el("span", "b"); @@ -5346,6 +5435,8 @@ function routingPage(part) { return g; }); patch(acts, boxes); + for (const g of boxes) { packSeen.observe(g); if (g.classList.contains("long")) for (const e of g.children) packSeen.observe(e); } + packActs(); } // signInAgain signs the account of seat w in again, in its provider's diff --git a/internal/gui/assets/plugins/routing/routing.css b/internal/gui/assets/plugins/routing/routing.css index c8cfa456b..b052e9eaf 100644 --- a/internal/gui/assets/plugins/routing/routing.css +++ b/internal/gui/assets/plugins/routing/routing.css @@ -503,6 +503,13 @@ button.rt-session { cursor: pointer; } :where(.rt-more:not(.min1150)) .rt-pgrp.long, .rt-cols.solo .rt-pgrp.long { break-inside: auto; } :where(.rt-more:not(.min1150)) .rt-pgrp.long > *, .rt-cols.solo .rt-pgrp.long > * { break-inside: avoid; } :where(.rt-more:not(.min1150)) .rt-pgrp.long > .rt-prov, .rt-cols.solo .rt-pgrp.long > .rt-prov { break-after: avoid; } +/* placed by client.js (packActs) in the columns CSS columns would have + them in: CSS columns balanced every row again on each layout, and a + window resized over many accounts stuttered (lexrus on X) */ +:where(.rt-more:not(.min1150)) .rt-acts.packed, .rt-cols.solo .rt-acts.packed { columns: auto; position: relative; box-sizing: content-box; height: var(--acts-h, auto); } +:where(.rt-more:not(.min1150)) .rt-acts.packed > *, .rt-cols.solo .rt-acts.packed > * { position: absolute; left: var(--px, 0); top: var(--py, 0); width: calc(100% / var(--acols, 1)); } +:where(.rt-more:not(.min1150)) .rt-acts.packed > .long, .rt-cols.solo .rt-acts.packed > .long { height: var(--part-h, auto); } +:where(.rt-more:not(.min1150)) .rt-acts.packed > .long > *, .rt-cols.solo .rt-acts.packed > .long > * { position: absolute; left: var(--px, 0); top: var(--py, 0); width: 100%; } .rt-cols.solo { grid-template-columns: minmax(0, 1fr); } .rt-reqs.none { display: block; background: none; border-color: transparent; } .rt-reqs.none .empty-state { padding: 14px 12px 18px; font-size: 12px; line-height: 1.6; } @@ -573,11 +580,15 @@ button.rt-session { cursor: pointer; } columns that start at one place down the list */ .rt-act .wq { display: grid; font-size: 11px; line-height: 17px; color: var(--fg-2); font-variant-numeric: tabular-nums; } .rt-act .wq > div { display: flex; flex-wrap: wrap; align-items: center; column-gap: 8px; } -.rt-act .wq span { min-width: 0; white-space: nowrap; } -.rt-act .wq .k { flex: 0 0 var(--wk, 3ch); overflow: hidden; } +.rt-act .wq span { min-width: 0; } +/* each part no wider than its row (lexrus on X: a width the provider's + widest window asked for, wider than a column, ran the reset over the + column beside); one wider than the row goes on a line of its own and + breaks between its words there, in full */ +.rt-act .wq .k { flex: 0 0 min(var(--wk, 3ch), 100%); overflow-wrap: anywhere; } .rt-act .wq .b { flex: 0 0 28px; } -.rt-act .wq .v { flex: 0 0 auto; min-width: var(--wv, 0); } -.rt-act .wq .r { flex: 1 0 auto; min-width: var(--wr, 0); max-width: 100%; overflow: hidden; text-overflow: ellipsis; text-align: right; } +.rt-act .wq .v { flex: 0 0 auto; min-width: min(var(--wv, 0px), 100%); max-width: 100%; } +.rt-act .wq .r { flex: 1 0 auto; min-width: min(var(--wr, 0px), 100%); max-width: 100%; text-align: right; } .rt-act .wq .k, .rt-act .wq .r { color: var(--muted); } .rt-act .wq .quota-track { height: 3px; } /* rows too narrow for a bar beside what is used (slim, set by diff --git a/internal/gui/tests/routing-account-windows-fit.test.cjs b/internal/gui/tests/routing-account-windows-fit.test.cjs new file mode 100644 index 000000000..812cb3a5a --- /dev/null +++ b/internal/gui/tests/routing-account-windows-fit.test.cjs @@ -0,0 +1,175 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// lexrus on X: on the Routing page's Accounts and keys, with many accounts +// laid out in columns, a window's reset ("resets in 3h") was drawn over +// the column beside it, and the right column's were cut at the card's +// edge. Each window's parts were as wide as the widest of the whole list +// (WorkBuddy's "deepseek-v4.1-flash tokens (est.)", its 200,000,000 / +// 200,000,000 ยท 100% used), wider than a column, and a part kept that +// width past its row. Now a window's line lines up with the rows of its +// own provider, and no part runs past its row at any width, every part +// in full. In English and Chinese, from a phone's width to a wide window, +// in both engines. And with that many accounts, dragging the window's +// edge stuttered: the list was CSS columns, balanced again on every +// layout; now its boxes are placed in their columns by the page. +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(process.env.MAGPIE_ASSETS || path.join(__dirname, "../assets")); +const now = Date.now(); +const iso = (ms) => new Date(ms).toISOString(); +const H = 3600e3; + +// the screenshot's providers: accounts with their Usage cards, and keys +const subs = [ + ["codex", "Codex", [["plus", "a@example.com", [{ name: "5 hours", used: 92, resetsAt: iso(now + 3 * H) }, { name: "7 days", used: 94, resetsAt: iso(now + 5 * 24 * H) }, { name: "Image generation", display: "Unknown" }]]]], + ["copilot", "Copilot", [["Pro", "b-user", [{ name: "Premium requests", used: 100, limit: 1500, amount: 1569.2, resetsAt: iso(now + 14 * 24 * H) }, { name: "Chat requests", unlimited: true }, { name: "Completions", unlimited: true }]]]], + ["cursor", "Cursor", [["Premium", "c@example.com", [{ name: "Weekly", used: 96.6, resetsAt: iso(now + 26 * H) }, { name: "Grok Bot", used: 96.6, resetsAt: iso(now + 26 * H) }, { name: "On-demand", display: "$0.00" }]]]], + ["workbuddy", "WorkBuddy", [["Free plan", "w-user", [ + { name: "Credits", used: 100, limit: 500, amount: 500, unit: "credits", resetsAt: iso(now + 20 * H) }, + { name: "Extra credits", used: 98.5, limit: 2517, amount: 2479.82, unit: "credits", resetsAt: iso(now + 9 * 24 * H) }, + { name: "Total credits", used: 98.8, limit: 3017, amount: 2979.82, unit: "credits", resetsAt: iso(now + 9 * 24 * H) }, + { name: "deepseek-v4.1-flash tokens (est.)", used: 100, limit: 200000000, amount: 200000000, resetsAt: iso(now + 30 * H) }, + ]]]], + ["workbuddy-ai", "WorkBuddy AI", [["Free plan", "x-user", [ + { name: "Credits", used: 100, limit: 100, amount: 100, unit: "credits", resetsAt: iso(now + 20 * H) }, + { name: "Extra credits", used: 100, limit: 90, amount: 90, unit: "credits", resetsAt: iso(now + 9 * 24 * H) }, + { name: "Total credits", used: 100, limit: 190, amount: 190, unit: "credits", resetsAt: iso(now + 9 * 24 * H) }, + ]]]], + ["zcode", "ZCode", [["GLM Coding Lite", "z@example.com", [{ name: "5 hours", used: 98, resetsAt: iso(now + 2 * H) }, { name: "Weekly", used: 10, resetsAt: iso(now + 6 * 24 * H) }]]]], + ["qoder", "Qoder", [["personal_pro", "q@example.com", [{ name: "Add-on credits", used: 35, limit: 100, amount: 35, unit: "credits", resetsAt: iso(now + 26 * H) }]]]], +]; +// many: the screenshot's list; long: a user with a score of Codex +// accounts, more rows than one column holds, which run on into the next +function fixture(codexes) { + const accts = [], cards = []; + for (const [p, name, as] of subs) { + for (const [plan, who, windows] of as) { + accts.push({ id: p + "@" + who, provider: p, name, who, kind: "account", agent: p, model: "m-" + p, known: false, plan }); + cards.push({ provider: p, name, kind: "subscription", user: who, plan, windows }); + } + } + for (let i = 2; i <= codexes; i++) { + const who = `codex${i}@example.com`; + accts.push({ id: "codex@" + who, provider: "codex", name: "Codex", who, kind: "account", agent: "codex", model: "m-codex", known: false, plan: "plus" }); + cards.push({ provider: "codex", name: "Codex", kind: "subscription", user: who, plan: "plus", windows: [{ name: "5 hours", used: 10 * (i % 10), resetsAt: iso(now + i * H) }, { name: "7 days", used: 9 * (i % 11), resetsAt: iso(now + i * 24 * H) }, { name: "Image generation", display: "Unknown" }] }); + } + const keys = ["Google Gemini", "NVIDIA NIM", "OpenAI", "OpenCode Zen Free", "OpenRouter", "Our Free Model"].map((name, i) => ({ + id: "key" + i, provider: "k" + i, name, kind: "provider", model: "deepseek-ai/deepseek-v4.1-flash-" + i, + })); + const order = [...accts, ...keys]; + const route = { + id: 7, seq: 7, time: iso(now - 2000), agent: "claude", model: "m", provider: "codex", done: true, status: 200, ms: 900, tokens: 1500, + order, tries: [{ id: order[0].id, model: "m-codex", start: iso(now - 2000), done: true, ms: 800, status: 200 }], + }; + return { cards, route }; +} + +function serve(lang, { cards, route }) { + const state = { agents: [{ id: "claude", name: "Claude Code", path: "/test/claude", fields: [] }], profiles: [], settings: { lang, theme: "light", quotaLeft: false } }; + 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/gateway/trace") { + if (url.searchParams.get("wait")) return new Promise(() => {}); // nothing more comes + return json({ mine: true, now: new Date().toISOString(), seq: 7, totals: { requests: 1, rerouted: 0, errors: 0 }, routes: [route] }); + } + if (url.pathname === "/api/usage/quotas") return json(cards); + if (url.pathname === "/api/usage/quotas/history") return json([]); + if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] }); + if (url.pathname === "/api/groups") return json({ models: [], groups: [], pools: [] }); + 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 }); + }; +} + +// every part of every window's line inside its row, and in full +const overruns = () => [...document.querySelectorAll(".rt-act .wq > div > span")].flatMap((s) => { + const row = s.closest(".rt-act"), rr = row.getBoundingClientRect(), r = s.getBoundingClientRect(); + if (!r.width) return []; + const pad = parseFloat(getComputedStyle(row).paddingRight); + const out = []; + if (r.right > rr.right - pad + 1 || r.left < rr.left - 1) out.push(`${s.textContent} [${Math.round(r.left)}โ€“${Math.round(r.right)}] past its row [${Math.round(rr.left)}โ€“${Math.round(rr.right - pad)}]`); + if (s.scrollWidth > s.clientWidth + 1) out.push(`${s.textContent} cut`); + return out; +}); + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + test(`${engine}: a window's line stays in its row at every width`, async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + t.after(() => browser.close()); + for (const [kind, codexes] of [["many", 4], ["long", 20]]) for (const lang of ["en", "zh"]) { + const fx = fixture(codexes); + await t.test(`${kind} ${lang}`, async () => { + const ctx = await browser.newContext({ viewport: { width: 1200, height: 2400 }, reducedMotion: "reduce" }); + try { + // every account in sight: the list whole, and Codex's opened + await ctx.addInitScript(() => { try { localStorage.setItem("magpie.routingActsAll", "1"); localStorage.setItem("magpie.routingKeysOpen", JSON.stringify(["codex/@"])); } catch {} }); + const page = await ctx.newPage(); + page.setDefaultTimeout(5000); + page.setDefaultNavigationTimeout(60000); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + await page.route("**/*", serve(lang, fx)); + await page.goto("http://magpie.test/?view=routing"); + await page.waitForFunction((n) => document.querySelectorAll(".rt-act .wq").length === n, fx.cards.length); + for (const width of [420, 560, 700, 765, 840, 960, 1100, 1300, 1600]) { + await page.setViewportSize({ width, height: 2400 }); + // the layout's classes are set a frame after the size is seen + await page.evaluate(() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(() => requestAnimationFrame(r))))); + const bad = await page.evaluate(overruns); + if (process.env.ARTIFACT_DIR) { + await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true }); + await page.locator(".rt-acts").first().screenshot({ path: path.join(process.env.ARTIFACT_DIR, `fit-${engine}-${kind}-${lang}-${width}.png`) }).catch(() => {}); + } + assert.deepEqual(bad, [], `${lang} at ${width}px`); + // a provider's rows still line up: each part starts at one + // place on every line of every row of it + const starts = await page.evaluate(() => [...document.querySelectorAll(".rt-pgrp")].map((g) => { + const ls = [...g.querySelectorAll(".rt-act .wq > div")]; + return [...new Set(ls.map((l) => { + const left = l.closest(".rt-act").getBoundingClientRect().left; + const [k, , v] = [...l.children].map((c) => c.getBoundingClientRect()); + return [k.left - left, v.left - left].map(Math.round).join(","); + }))]; + }).filter((s) => s.length > 1)); + assert.deepEqual(starts, [], `${lang} at ${width}px: a provider's lines start at one place`); + // and resizing stays smooth: the list isn't CSS columns, which + // balanced every row again on each layout (~24ms a layout in + // WebKit with this list), but boxes placed in order, none over + // another and all inside the list. A long box's rows run on + // into the next column in order, its heading never last in one + const lay = await page.evaluate(() => { + const acts = document.querySelector(".rt-acts"), ar = acts.getBoundingClientRect(); + const items = [...acts.children].flatMap((g) => g.classList.contains("long") ? [...g.children] : [g]); + const rs = items.map((e) => e.getBoundingClientRect()), bad = []; + const name = (i) => `${items[i].className.split(" ")[0]} ${i}`; + rs.forEach((r, i) => { + if (r.bottom > ar.top - acts.scrollTop + acts.scrollHeight + 1 || r.right > ar.right + 1 || r.left < ar.left - 1) bad.push(`${name(i)} outside the list`); + const p = rs[i - 1]; + if (p && !(r.left > p.left + 1 || (Math.abs(r.left - p.left) <= 1 && r.top >= p.bottom - 1))) bad.push(`${name(i)} out of order or over ${name(i - 1)}`); + if (p && items[i - 1].classList.contains("rt-prov") && Math.abs(r.left - p.left) > 1) bad.push(`${name(i - 1)} last in its column`); + }); + return { cols: getComputedStyle(acts).columnWidth, lefts: new Set(rs.map((r) => Math.round(r.left))).size, long: acts.querySelectorAll(".long").length, bad }; + }); + assert.equal(lay.cols, "auto", `${lang} at ${width}px: the list isn't CSS columns`); + assert.deepEqual(lay.bad, [], `${lang} at ${width}px`); + assert.equal(lay.long > 0, kind === "long", `${kind}: a box long enough to run on`); + if (width >= 700 && width < 1100) assert.ok(lay.lefts >= 2, `${lang} at ${width}px: ${lay.lefts} column(s)`); + } + assert.deepEqual(errors, []); + } finally { + await ctx.close(); + } + }); + } + }); +}