diff --git a/scripts/page-test-helpers.mjs b/scripts/page-test-helpers.mjs index 8ad5a5f..d322bb4 100644 --- a/scripts/page-test-helpers.mjs +++ b/scripts/page-test-helpers.mjs @@ -116,9 +116,38 @@ export async function openSession(browser, { t, workflow, payload: first, projec s.page.on("pageerror", (e) => s.errors.push(e.message)); await s.page.goto(s.url, { waitUntil: "domcontentloaded" }); await drawn(s.page); + // Redraws that follow the first one (the workspace frame, a scan, the update check) land a + // moment later, and on a busy runner a test that has already scrolled, typed or opened + // something loses it to them. Hand the test a page that has gone quiet. A fake clock freezes + // the page's own timers, so those sessions are left as they are. + if (!clock) await quiet(s.page); return s; } +// Waits until the document has not changed for `quietMs`, or `maxMs` at most (a page that never +// goes quiet must not block a test). Counts mutations in the page and polls the count from +// here, so it needs no timer in the page. +export async function quiet(page, { quietMs = 300, maxMs = 4000 } = {}) { + await page + .evaluate(() => { + if (window.__quiet) return; + window.__quiet = { n: 0 }; + new MutationObserver(() => window.__quiet.n++).observe(document.body, { subtree: true, childList: true, attributes: true, characterData: true }); + }) + .catch(() => {}); + const end = Date.now() + maxMs; + let last = -1; + let since = Date.now(); + while (Date.now() < end) { + const n = await page.evaluate(() => (window.__quiet ? window.__quiet.n : -1)).catch(() => -1); + if (n !== last) { + last = n; + since = Date.now(); + } else if (Date.now() - since >= quietMs) return; + await new Promise((r) => setTimeout(r, 50)); + } +} + // The page has adopted a state frame: app.js has replaced the boot pane the // markup ships with. export async function drawn(page) { diff --git a/scripts/test-web-console-page-session.mjs b/scripts/test-web-console-page-session.mjs index 1aa4c53..463a7d2 100644 --- a/scripts/test-web-console-page-session.mjs +++ b/scripts/test-web-console-page-session.mjs @@ -904,14 +904,23 @@ test("a workflow page uses its modules: the full footer, the session meter, @ su // The @ list is the workspace frame, which can land after the page draws; // the footer has one label per width tier once it has. await s.page.waitForFunction(() => (document.getElementById("footer-where").dataset.key || "").includes("\n")); - // A redraw can replace the box while it is typed in, and the typing is lost with it (seen on - // slow runners): clear the box and type again until the list shows, up to four times. + // A redraw can replace the box while it is typed in: the typing is lost, or the list closes + // with the box it belonged to (both seen on slow runners). Clear the box and type again until + // the list is up and names the project, up to four times, then assert it for real. const pop = s.page.locator("#mention-pop"); + const listed = async () => { + try { + await pop.waitFor({ timeout: 3000 }); + return new RegExp(name).test(await pop.innerText({ timeout: 1500 })); + } catch { + return false; + } + }; for (let attempt = 0; attempt < 4; attempt++) { const ed = itemCard(s.page, "q1").locator(".md-host .cm-content").first(); await ed.click(); await ed.pressSequentially("See @"); - if (await pop.waitFor({ timeout: 3000 }).then(() => true, () => false)) break; + if (await listed()) break; await fillBox(itemCard(s.page, "q1").locator(".md-host"), ""); } await pop.waitFor();