Files
magpie/internal/gui/tests/ledger-swipe.test.cjs
T
yetone 9d4c338a67 gui: magpie web on a phone no longer scrolls sideways or keeps a band at its foot (#4943)
precis1onz (iOS Safari, dark, English, Usage) saw the page move sideways
and a dark band stay at the screen's foot.

- Sideways: Usage → Requests' "Split by" row (label and five choices) is
  one unwrapped span, 393 to 558px wide at 375 to 430px. It was the only
  thing wider than a phone in any view or language. On the phone layout
  it now wraps inside the screen (app.css, phone block only).
- The band: the Requests table's stand-in scrollbar (#799) is sticky at
  the view's foot, and a touch screen draws no bar in it, so it stood
  there as an empty 12px band. ledHScroll now leaves it hidden on
  (hover: none) and (pointer: coarse); a finger moves the table itself.
  A desktop keeps it as before.

New test, phone-no-sideways.test.cjs: every view (Agents, Sessions,
Providers, Routing, Groups, Gateway, Library, Plugins, Masking, each
Usage tab, Settings' tabs) on an iPhone at 375 in en, zh, zh-TW, ja, de
and at 390 and 430 in en, in Chromium and WebKit, with long ids, emails
and paths; nothing may be wider than the screen and no empty band may
stay sticky at a view's foot. ledger-swipe now expects no bar on a
phone, and that the swipe moves the table.

Run (mote, through mote-run):
- phone-no-sideways: red on the old assets (803d7747e) in every run,
  led-split too wide plus the empty #ledHScroll at the foot; green on
  the new in both engines, again at the rebased head with ledger-swipe
  and usage-ledger-leave.
- mobile-web 14/14 in both engines with a real git base in the copy,
  so the desktop layout matches the base.
- Every GUI file the change can reach (1485 tests, both engines): the
  reds are all red on base and filed: mobile-web desktop subtests in a
  copy without .git (#5013), phone-connect-menu 420 (#4706),
  usage-agents-hide (#2276), usage-computers sync tick in Chromium
  (#5224), usage-fold (#3415), usage-ledger Requests (#5048).
  ledger-swipe in WebKit flakes on base too (#5223).
- go test -tags nogui ./internal/gui ok (size budgets), also at the
  rebased head; gofmt clean; go vet for darwin, linux and windows ok.

Not run: the whole GUI and Go suites. The run was queued over two
hours behind mote's suite.lock and never started; the change touches
no Go code.
2026-11-10 03:00:30 +08:00

116 lines
6.9 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// A swipe across the Requests table on a phone (Pluviophile on Discord: 左右
// 滑动震颤, 粘滞卡顿). The table and the scrollbar kept at the window's foot
// (#799) follow each other, and the scroll a follow sets off is told a frame
// later in WebKit; a phone's finger has moved the table on by then, so the
// bar's echo wrote the table back where it was, every other frame. Here the
// table is moved between frames, as a phone's own scrolling moves it, and
// the page never writes it back. A phone shows no such bar at all (#4943:
// it stood as an empty band at the screen's foot); a finger moves the
// table. English and Chinese, Chromium and WebKit at a phone's size; no
// backend, the API is faked.
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, devices } = require("playwright");
const assets = path.resolve(process.env.MAGPIE_LEDGER_ASSETS || path.join(__dirname, "../assets"));
const now = Date.now();
const ROWS = Array.from({ length: 40 }, (_, i) => ({
route_id: 500 + i, t: new Date(now - (i + 1) * 60e3).toISOString(), agent: "trae", agentName: "Trae CN", icon: "generic",
provider: "deepseek", providerName: "DeepSeek Harness", req: "group/auto-deepseek-v4-1-flash", model: "deepseek-v4-1-flash", served: "deepseek-v4-1-flash",
in: 1200 + i, out: 80, ms: 2400, ttft_ms: 600, status: [200, 400, 499][i % 3], rid: "req_" + i, ep: "/v1/chat/completions", cost: 0.001, priced: true,
}));
function server(lang) {
return async (route) => {
const url = new URL(route.request().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:true};` });
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" }, fx: { rate: 7.2, at: new Date().toISOString() } });
if (url.pathname === "/api/usage/requests") return json({ period: url.searchParams.get("period"), rows: ROWS, offset: 0, total: ROWS.length, calls: ROWS.length, errors: 0, input: 1, output: 1, cost: 0.04, unpriced: 0, agents: [{ id: "trae", name: "Trae CN", icon: "generic" }] });
if (url.pathname === "/api/usage/quotas") return json([]);
if (url.pathname === "/api/usage") return json({ calls: 40, errors: 0, input: 1, output: 1, reasoning: 0, unpriced: 0, cost: 0.04, bucket: "day", series: [], agents: [], models: [], path: "" });
if (url.pathname === "/api/sessions") return json({ sessions: [], dirs: [] });
if (url.pathname === "/api/sessions/stats") return json({ from: "", to: "", days: [], agents: {} });
if (url.pathname === "/api/groups") return json({ groups: [], models: [] });
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 route.fulfill({ body: await fs.readFile(file), contentType });
};
}
// every write the page makes to either box's scrollLeft is counted; the
// test's own moves go past the count, as a phone's scrolling does
function count() {
const d = Object.getOwnPropertyDescriptor(Element.prototype, "scrollLeft");
window.__writes = { ledWrap: 0, ledHScroll: 0 };
Object.defineProperty(Element.prototype, "scrollLeft", {
configurable: true,
get() { return d.get.call(this); },
set(v) { if (this.id in window.__writes) window.__writes[this.id]++; d.set.call(this, v); },
});
// moves one box 2.5px every 7ms, off the frame, for 60 frames, and says
// where it stood each frame and how often the page wrote either box
window.__swipe = (id) => new Promise((done) => {
const box = document.getElementById(id), seen = [];
let x = 0;
d.set.call(box, 0);
const at = () => d.get.call(box);
requestAnimationFrame(() => {
window.__writes.ledWrap = window.__writes.ledHScroll = 0;
const move = setInterval(() => { x += 2.5; d.set.call(box, x); }, 7);
const frame = () => {
seen.push(at());
if (seen.length < 60) return requestAnimationFrame(frame);
clearInterval(move);
done({ seen, writes: { ...window.__writes } });
};
requestAnimationFrame(frame);
});
});
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(engine + ": a swipe across the Requests table on a phone", async (t) => {
assert(["chromium", "webkit"].includes(engine), "BROWSER must be chromium or webkit");
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const phone = { ...devices["iPhone 13"] };
delete phone.defaultBrowserType;
for (const lang of ["en", "zh"]) {
await t.test(lang, async () => {
const ctx = await browser.newContext({ ...phone, reducedMotion: "reduce" });
t.after(() => ctx.close());
const p = await ctx.newPage();
p.setDefaultTimeout(5000);
p.setDefaultNavigationTimeout(60000);
const errors = [];
p.on("pageerror", (e) => errors.push(e.message));
await p.addInitScript(count);
await p.route("**/*", server(lang));
await p.goto("http://magpie.test/?view=usage");
await p.waitForSelector("#view-usage", { state: "visible" });
await p.locator(":is(#usageTab .opt, .nav-s):visible").nth(1).click();
await p.locator("#ledWrap .led tbody tr").first().waitFor();
await p.waitForTimeout(300);
assert(await p.locator("#ledWrap").evaluate((e) => e.scrollWidth > e.clientWidth + 100), "the table is wider than the phone");
assert.equal(await p.locator("#ledHScroll").isHidden(), true, "a phone keeps no stand-in scrollbar at the foot (#4943)");
// the table swiped: it is never written back, and never stands still
// a frame while the finger moves it
const table = await p.evaluate(() => window.__swipe("ledWrap"));
const stood = table.seen.filter((x, i) => i && x <= table.seen[i - 1]).length;
assert.equal(table.writes.ledWrap, 0, "the page wrote the swiped table back: " + JSON.stringify(table));
assert.equal(stood, 0, "the table stood still or went back under the finger: " + JSON.stringify(table.seen));
await p.waitForTimeout(100);
assert(await p.locator("#ledWrap").evaluate((e) => e.scrollLeft > 100), "the table stayed where the swipe left it");
assert.deepEqual(errors, []);
});
}
});
}