diff --git a/internal/gui/assets/plugins/routing/client.js b/internal/gui/assets/plugins/routing/client.js index d3e92cc28..62d25a4e5 100644 --- a/internal/gui/assets/plugins/routing/client.js +++ b/internal/gui/assets/plugins/routing/client.js @@ -2010,40 +2010,43 @@ function routingPage(part) { bodies.replaceChildren(grid); } // holdStory keeps the story of the live request, its head and its lines, - // as tall as the tallest of the last few requests answered, at this + // as tall as the tallest of the last few requests told, at this // width: a request under way is told in fewer lines than one answered // ("is answering…", then "answered in 4.0 s…" over two lines at a // phone's width), and its head gets View usage and Replay, taller than // the words, once it is done. So each new request moved the context // window under it up and back down (dumplings on Discord: the card - // jittered on every request). Only the last STORY_HELD answered count: one + // jittered on every request). Only the last STORY_HELD told count: one // told in many lines (a request handed on from member to member) no // longer holds the box that tall for good, and it shrinks back once // that many shorter ones have come after it (夜松云 on Discord: the box - // grew and never shrank back). A request picked, a replay or a past day - // is drawn at its own height. A change of width measures it again at + // grew and never shrank back). Each request told counts, answered or + // under way, at the height it has now: on a busy gateway a request is + // answered as the next starts, the two come in one trace, and the + // newest is always under way, so counting only those told answered + // held the long one for good (shensmart on Discord, a remote magpie in + // a local browser: the room grew with use). A request picked, a replay + // or a past day is drawn at its own height. A change of width measures it again at // once, as it is then (the list coming in under it can take a // scrollbar's width off it before the first live request) const STORY_HELD = 3; - let storyHold = { key: "", done: new Map() }, storyDrawn = ""; + let storyHold = { key: "", told: new Map() }, storyDrawn = ""; function holdStory() { const follow = !pinned && !rp && !day; const w = steps.clientWidth; const key = JSON.stringify([follow, w, document.documentElement.lang]); if (key === storyHold.key && storyDrawn === headKey + stepsKey) return; - if (key !== storyHold.key) storyHold = { key, done: new Map() }; + if (key !== storyHold.key) storyHold = { key, told: new Map() }; storyDrawn = headKey + stepsKey; [logHead, steps].forEach((e) => { e.style.minHeight = ""; }); if (!follow || !w || !logR) return; const hs = [logHead, steps].map((e) => e.getBoundingClientRect().height); - // an answered request's own height, kept by its id: the last few only - if (logR.done) { - storyHold.done.set(logR.id, hs); - const ids = [...storyHold.done.keys()].sort((x, y) => y - x); - for (const id of ids.slice(STORY_HELD)) storyHold.done.delete(id); - } + // a request's own height as told now, kept by its id: the last few only + storyHold.told.set(logR.id, hs); + const ids = [...storyHold.told.keys()].sort((x, y) => y - x); + for (const id of ids.slice(STORY_HELD)) storyHold.told.delete(id); [logHead, steps].forEach((e, i) => { - const held = Math.max(0, ...[...storyHold.done.values()].map((h) => h[i])); + const held = Math.max(0, ...[...storyHold.told.values()].map((h) => h[i])); if (held > hs[i]) e.style.minHeight = held + "px"; }); } diff --git a/internal/gui/tests/routing-story-shrinks.test.cjs b/internal/gui/tests/routing-story-shrinks.test.cjs index 5863ecdc3..24d5c3026 100644 --- a/internal/gui/tests/routing-story-shrinks.test.cjs +++ b/internal/gui/tests/routing-story-shrinks.test.cjs @@ -44,7 +44,7 @@ function serve(lang, feed) { const trace = (routes) => json({ mine: true, now: new Date().toISOString(), seq: routes.length ? routes[routes.length - 1].seq : Number(url.searchParams.get("after")), totals: { requests: 0, rerouted: 0, errors: 0 }, routes }); if (!url.searchParams.get("wait")) return trace(first); const r = await new Promise((res) => { feed.next = res; }); - return trace([r]); + return trace([].concat(r)); } if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] }); if (url.pathname === "/api/groups") return json({ groups: [], models: [], pools: [] }); @@ -114,6 +114,53 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", feed.next?.(req(999, 0)); await context.close(); }); + + // A busy gateway (shensmart on Discord, a remote magpie in a local + // browser: the room under the story grew and never shrank back): + // each request is answered as the next one starts, so the two come + // in one trace and the newest is always under way. None of them is + // ever drawn answered, yet the long one stops holding the box once + // as many requests have been told after it. + await t.test(`${lang} ${width}px, the newest always under way`, async () => { + const context = await browser.newContext({ viewport: { width, height: 900 }, reducedMotion: "reduce" }); + const page = await context.newPage(); + page.setDefaultTimeout(5000); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + const feed = { next: null }; + await page.route("**/*", serve(lang, feed)); + await page.goto("http://magpie.test/?view=routing"); + await page.locator(".rt-req").nth(first.length - 1).waitFor(); + await page.locator(".rt-log .rt-steps li").first().waitFor(); + await page.waitForTimeout(800); + const send = async (r) => { + for (let i = 0; i < 60 && !feed.next; i++) await page.waitForTimeout(50); + const next = feed.next; + assert(next, "the page asks for the next trace"); + feed.next = null; + next(r); + await page.waitForTimeout(400); + }; + const story = () => page.evaluate(() => { + const s = document.querySelector(".rt-log .rt-steps"); + const lines = [...s.children].reduce((b, li) => Math.max(b, li.getBoundingClientRect().bottom), s.getBoundingClientRect().top); + return { h: s.getBoundingClientRect().height, content: lines - s.getBoundingClientRect().top, held: s.style.minHeight }; + }); + + await send(req(103, 4)); + const tall = await story(); + await send([req(104, 0, "start")]); + const going = await story(); + assert(going.h >= tall.h - 0.5, `held while the next is under way: ${going.h} vs ${tall.h}`); + for (let id = 105; id <= 110; id++) await send([req(id - 1, 0), req(id, 0, "start")]); + const after = await story(); + assert(after.h < tall.h - 40, `the box shrank back with the newest always under way: ${after.h} vs ${tall.h} (min-height ${after.held || "none"})`); + assert(after.h - after.content <= 24, `no empty room under its lines: ${after.h} tall, lines to ${after.content}`); + + assert.deepEqual(errors, []); + feed.next?.(req(999, 0)); + await context.close(); + }); } }); }