Fail npm test when a page test reads a style or scrolls through a raw locator.evaluate, and document the CI lessons from PR 16: cargo fmt and clippy before pushing app changes, the test concurrency cap, and the redraw-safe page-test helpers

AI Assisted
This commit is contained in:
jparkerweb committed 2026-10-26 22:46:16 -07:00
1 parent 77ddcc048d
commit d5d8278cfe
7 files changed
+67 -6

No files matched your search

+2 -2
View File
@@ -14,7 +14,7 @@ import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import { pageSuite, openSession, tempProject, textOf, itemCard } from "./page-test-helpers.mjs";
import { pageSuite, openSession, tempProject, textOf, itemCard, scrollCard } from "./page-test-helpers.mjs";
import { until } from "./web-console-test-helpers.mjs";
const suite = pageSuite(test);
@@ -74,7 +74,7 @@ test("each git tab keeps its own scroll place, across a tab switch and an agent
const column = () => page.locator("#main").evaluate((el) => el.scrollTop);
await tab("Changes").click();
await page.getByRole("tabpanel").getByText("src/app.js", { exact: true }).waitFor();
await page.locator("#main").evaluate((el) => (el.scrollTop = 120));
await scrollCard(page.locator("#main"), 120);
const changesAt = await column();
assert.ok(changesAt > 0, "the board's column scrolls");
// Another tab opens at its own place, not where Changes was left.
+46
View File
@@ -335,3 +335,49 @@ test("shape: the docs-table check names a file with no row, and passes one that
fs.rmSync(dir, { recursive: true, force: true });
}
});
// A page test that reads a style or sets a scroll through locator.evaluate
// acts on whatever element the locator found, and a redraw can replace that
// element before the call runs: one off the page reads "" for every style and
// scrolls nothing anyone sees. On 3 and 4 core CI runners that failed a
// different page test at random on each run until the reads went through
// page-test-helpers.mjs's styleOf(), stylesOf() and scrollCard(), which retry
// until the element is still on the page. page.evaluate and waitForFunction
// look the element up afresh, so they are not flagged.
const RACY_STYLE = /(?<!\bpage)\.evaluate\(\s*\((\w+)\)\s*=>[^\n]*getComputedStyle\(\1\)/;
const RACY_SCROLL = /(?<!\bpage)\.evaluate\(\s*\((\w+)\)\s*=>\s*\(?\s*\1\.scrollTop\s*=[^=]/;
function racyPageReads(name, text) {
const problems = [];
text.split("\n").forEach((line, i) => {
if (RACY_STYLE.test(line)) problems.push(`${name}:${i + 1}: read the style with styleOf() or stylesOf() from page-test-helpers.mjs`);
if (RACY_SCROLL.test(line)) problems.push(`${name}:${i + 1}: scroll with scrollCard() from page-test-helpers.mjs`);
});
return problems;
}
test("shape: page tests read styles and set scrolls through the redraw-safe helpers", () => {
const dir = path.join(ROOT, "scripts");
const pageTests = fs.readdirSync(dir).filter((f) => /^test-web-console-page-.*\.mjs$/.test(f));
assert.ok(pageTests.length > 0, "the page tests were found");
const problems = pageTests.flatMap((f) => racyPageReads(f, fs.readFileSync(path.join(dir, f), "utf8")));
assert.deepEqual(problems, []);
});
test("shape: the redraw-safe check names a locator's style read and scroll, and passes page-wide ones", () => {
const racy = [
'await card.evaluate((el) => (el.scrollTop = 400));',
'const c = await chip.evaluate((b) => getComputedStyle(b).borderTopStyle);',
].join("\n");
assert.deepEqual(racyPageReads("x.mjs", racy), [
"x.mjs:1: scroll with scrollCard() from page-test-helpers.mjs",
"x.mjs:2: read the style with styleOf() or stylesOf() from page-test-helpers.mjs",
]);
const fine = [
'await s.page.evaluate(() => { document.getElementById("main").scrollTop = 4000; });',
'const top = await card.evaluate((el) => el.scrollTop === 0);',
'await s.page.waitForFunction(() => getComputedStyle(document.body).color);',
'const color = await s.page.evaluate((el) => getComputedStyle(el).color);',
].join("\n");
assert.deepEqual(racyPageReads("x.mjs", fine), []);
});