From 776692922e285463c0255a80a518a0597eb9fa82 Mon Sep 17 00:00:00 2001 From: bchanot Date: Fri, 13 Nov 2026 14:16:30 +0100 Subject: [PATCH] test(tests): C4 review round 2, calibration guard --- .../app/e2e/shell/editor-shell-layout.spec.ts | 62 +++++++++++++------ 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/apps/app/e2e/shell/editor-shell-layout.spec.ts b/apps/app/e2e/shell/editor-shell-layout.spec.ts index b1273343..ebdd5fda 100644 --- a/apps/app/e2e/shell/editor-shell-layout.spec.ts +++ b/apps/app/e2e/shell/editor-shell-layout.spec.ts @@ -199,31 +199,57 @@ async function touchSwipe(page: Page, cdp: CDPSession, travel: number) { return stableScrollY(page) } +const CALIBRATION_TRAVELS = [120, 200] +/** The most scroll a calibration travel is expected to cause (measured gain about 1.5). */ +const CALIBRATION_GAIN_CEILING = 1.3 + +/** One swipe, answering the scroll it caused. */ +async function measuredSwipe(page: Page, cdp: CDPSession, travel: number) { + const before = await page.evaluate(() => window.scrollY) + return (await touchSwipe(page, cdp, travel)) - before +} + /** - * Scrolls by touch until the trigger's bottom is 0 to 4 px above the viewport bottom. Two - * swipes of known travel give the line `delta = gain * travel + offset` (touch slop and - * Chromium's scroll gain); each next swipe inverts it for the distance left, and a swipe back - * corrects an overshoot. + * Two swipes of known travel give the line `delta = gain * travel + offset` (touch slop and + * Chromium's scroll gain). The page must have room for them before the target. + */ +async function calibrate(page: Page, cdp: CDPSession, target: number) { + const [near, far] = CALIBRATION_TRAVELS + const room = await page.evaluate( + () => document.documentElement.scrollHeight - window.innerHeight - window.scrollY, + ) + const needed = (near + far) * CALIBRATION_GAIN_CEILING + if (needed >= target || needed >= room) { + throw new Error(`calibration needs ~${needed} px, target ${target} px, scroll room ${room} px`) + } + const deltaNear = await measuredSwipe(page, cdp, near) + const deltaFar = await measuredSwipe(page, cdp, far) + const trace = [`${near}->${deltaNear}`, `${far}->${deltaFar}`] + const gain = (deltaFar - deltaNear) / (far - near) + if (deltaNear <= 0 || deltaFar <= 0 || !(gain > 0.2)) { + throw new Error(`touch calibration failed: gain=${gain}, swipes=${trace.join(' ')}`) + } + return { gain, offset: deltaFar - gain * far, trace } +} + +/** + * Scrolls by touch until the trigger's bottom is 0 to 4 px above the viewport bottom: each + * swipe inverts the calibration for the distance left (aim: 2 px), a swipe back corrects an + * overshoot. Throws, with the trace, when 8 swipes do not land it. */ async function scrollToWorstCase(page: Page, cdp: CDPSession, trigger: Locator) { - const trace: string[] = [] - const swipe = async (travel: number) => { - const before = await page.evaluate(() => window.scrollY) - const delta = (await touchSwipe(page, cdp, travel)) - before - trace.push(`${travel}->${delta}`) - return delta - } - const [near, far] = [120, 200] - const [deltaNear, deltaFar] = [await swipe(near), await swipe(far)] - const gain = (deltaFar - deltaNear) / (far - near) - const offset = deltaFar - gain * far + const target = -(await triggerRect(trigger)).gap + const { gain, offset, trace } = await calibrate(page, cdp, target) for (let swipes = 0; swipes < 8; swipes++) { const { gap } = await triggerRect(trigger) - if (gap >= 0 && gap <= 4) break + if (gap >= 0 && gap <= 4) { + return `gain=${gain.toFixed(2)} offset=${offset.toFixed(1)} swipes=${trace.join(' ')}` + } const wanted = -gap - 2 - await swipe(Math.round((wanted - Math.sign(wanted) * offset) / gain)) + const travel = Math.round((wanted - Math.sign(wanted) * offset) / gain) + trace.push(`${travel}->${await measuredSwipe(page, cdp, travel)}`) } - return `gain=${gain.toFixed(2)} offset=${offset.toFixed(1)} swipes=${trace.join(' ')}` + throw new Error(`the trigger never landed within 4 px: swipes=${trace.join(' ')}`) } /** Whether the element is wholly on screen and the topmost thing at its centre. */