diff --git a/internal/gui/tests/reader.cjs b/internal/gui/tests/reader.cjs index a747d5a81..b5eaa111a 100644 --- a/internal/gui/tests/reader.cjs +++ b/internal/gui/tests/reader.cjs @@ -31,4 +31,36 @@ async function click(page, locator) { await locator.click(); } -module.exports = { inView, click }; +// Settings is drawn from each read of the settings, and opening the page +// reads them twice: the page's own read, then the window's load() once the +// state is in. Every row is replaced by the second draw, so a row found +// after the first is detached under whatever the test does to it next +// (#1653: scrollIntoViewIfNeeded in WebKit on a busy machine). Call +// watchSettingsReads before page.goto; settingsDrawn then waits until load() +// has asked for its read and every read asked for has been drawn. +async function watchSettingsReads(page) { + await page.addInitScript(() => { + const reads = window.__settingsReads = { asked: 0, drawn: 0 }; + const fetch0 = window.fetch; + window.fetch = function (input, init) { + const p = fetch0.apply(this, arguments); + const url = new URL(typeof input === "string" ? input : input.url, location.href); + if (url.pathname !== "/api/settings" || (init?.method || "GET") !== "GET") return p; + reads.asked++; + // drawn a task after its body is in: loadSettings draws in the + // microtasks that follow + const drawn = () => setTimeout(() => reads.drawn++); + return p.then((res) => { + const text = res.text; + res.text = function () { return text.call(this).finally(drawn); }; + return res; + }, (e) => { drawn(); throw e; }); + }; + }); +} + +// load.done is set in the same run of load() that then asks for the +// settings, with no wait between, so once it is set the read is counted. +const settingsDrawn = (page) => page.waitForFunction(() => load.done && __settingsReads.asked === __settingsReads.drawn); + +module.exports = { inView, click, watchSettingsReads, settingsDrawn }; diff --git a/internal/gui/tests/redact-kinds.test.cjs b/internal/gui/tests/redact-kinds.test.cjs index bc20aeb5e..6c575d070 100644 --- a/internal/gui/tests/redact-kinds.test.cjs +++ b/internal/gui/tests/redact-kinds.test.cjs @@ -10,6 +10,7 @@ const fs = require("node:fs/promises"); const path = require("node:path"); const { test } = require("node:test"); const { chromium, webkit } = require("playwright"); +const { watchSettingsReads, settingsDrawn } = require("./reader.cjs"); const assets = path.resolve(__dirname, "../assets"); const ids = ["ssn", "passport", "iban", "birthday", "mac", "serial", "home", "userhost", "ip", "bucket"]; @@ -64,8 +65,10 @@ async function open(engine, lang, width, personal, saved) { const errors = []; page.on("pageerror", (e) => errors.push(e.message)); await page.route("**/*", serve(lang, personal, saved)); + await watchSettingsReads(page); await page.goto("http://magpie.test/?view=settings&tab=privacy"); await page.locator("#redactList .row").first().waitFor(); + await settingsDrawn(page); // the rows found are the ones that stay (#1653) return { browser, page, errors }; } diff --git a/internal/gui/tests/redact-rule-edit.test.cjs b/internal/gui/tests/redact-rule-edit.test.cjs index 15e1ffd6e..c7536a81c 100644 --- a/internal/gui/tests/redact-rule-edit.test.cjs +++ b/internal/gui/tests/redact-rule-edit.test.cjs @@ -12,6 +12,7 @@ const fs = require("node:fs/promises"); const path = require("node:path"); const { test } = require("node:test"); const { chromium, webkit } = require("playwright"); +const { watchSettingsReads, settingsDrawn } = require("./reader.cjs"); const assets = path.resolve(__dirname, "../assets"); const words = { @@ -64,9 +65,11 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", const errors = [], calls = []; page.on("pageerror", (e) => errors.push(e.message)); await page.route("**/*", serve(lang, calls)); + await watchSettingsReads(page); await page.goto("http://magpie.test/?view=settings&tab=privacy"); const head = page.locator("#redactList .row.rule-row").first(); await head.waitFor(); + await settingsDrawn(page); // the rows found are the ones that stay (#1653) // the rules, in order, from the row that adds one const listed = () => head.evaluate((h) => { const out = []; diff --git a/internal/gui/tests/redact-rule-row.test.cjs b/internal/gui/tests/redact-rule-row.test.cjs index 7c852c1bd..2a9762e9c 100644 --- a/internal/gui/tests/redact-rule-row.test.cjs +++ b/internal/gui/tests/redact-rule-row.test.cjs @@ -9,6 +9,7 @@ const fs = require("node:fs/promises"); const path = require("node:path"); const { test } = require("node:test"); const { chromium, webkit } = require("playwright"); +const { watchSettingsReads, settingsDrawn } = require("./reader.cjs"); const assets = path.resolve(__dirname, "../assets"); const settings = (lang) => ({ lang, theme: "light", redact: true, redactRules: [{ kind: "GATEWAY_KEY", prefix: "oc_sk_" }] }); @@ -47,9 +48,11 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", const errors = []; page.on("pageerror", (e) => errors.push(e.message)); await page.route("**/*", serve(lang)); + await watchSettingsReads(page); await page.goto("http://magpie.test/?view=settings&tab=privacy"); const row = page.locator("#redactList .row.rule-row"); await row.waitFor(); + await settingsDrawn(page); // the row found is the one that stays (#1653) await row.scrollIntoViewIfNeeded(); const m = await row.evaluate((r) => {