test(tests): C4 round 4, deterministic touch proof

This commit is contained in:
bchanot committed 2026-11-13 14:42:29 +01:00
1 parent c0da36da36
commit 78fb2d076e
1 file changed
+47 -66
+47 -66
View File
@@ -199,66 +199,41 @@ async function touchSwipe(page: Page, cdp: CDPSession, travel: number) {
return stableScrollY(page)
}
const CALIBRATION_TRAVELS = [120, 200]
/**
* Bounds the scroll one px of finger travel causes (measured 1.08 to 1.27); it is not the
* fitted slope of `delta` on `travel` (about 1.54, which includes the offset).
*/
const CALIBRATION_GAIN_CEILING = 1.3
/** One swipe, answering its travel and the scroll it caused. */
async function measuredSwipe(page: Page, cdp: CDPSession, travel: number) {
const before = await page.evaluate(() => window.scrollY)
return { travel, delta: (await touchSwipe(page, cdp, travel)) - before }
}
const traceOf = (swipes: { travel: number; delta: number }[]) =>
swipes.map((swipe) => `${swipe.travel}->${swipe.delta}`).join(' ')
/** Throws unless the calibration swipes fit before the target and the end of the page. */
async function expectCalibrationRoom(page: Page, target: number) {
const room = await page.evaluate(
() => document.documentElement.scrollHeight - window.innerHeight - window.scrollY,
)
const needed = CALIBRATION_TRAVELS.reduce((sum, t) => sum + t, 0) * CALIBRATION_GAIN_CEILING
if (needed >= target || needed >= room) {
throw new Error(`calibration needs ~${needed} px, target ${target} px, scroll room ${room} px`)
}
}
/** Finger travel of each reach swipe, well above the touch slop (a swipe <= ~60 px moves 0). */
const REACH_TRAVEL = 200
const REACH_MAX_SWIPES = 12
/**
* 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.
* Swipes by touch only until the trigger is wholly on screen and hit-testable. Answers the
* swipe count; throws with the scrollY trace after REACH_MAX_SWIPES swipes.
*/
async function calibrate(page: Page, cdp: CDPSession, target: number) {
await expectCalibrationRoom(page, target)
const swipes = []
for (const travel of CALIBRATION_TRAVELS) swipes.push(await measuredSwipe(page, cdp, travel))
const [near, far] = swipes
const gain = (far.delta - near.delta) / (far.travel - near.travel)
if (near.delta <= 0 || far.delta <= 0 || !(gain > 0.2)) {
throw new Error(`touch calibration failed: gain=${gain}, swipes=${traceOf(swipes)}`)
async function swipeUntilReached(page: Page, cdp: CDPSession, trigger: Locator) {
const trace: number[] = []
for (let swipes = 0; swipes <= REACH_MAX_SWIPES; swipes++) {
const view = await probeInView(trigger)
if (view.inside && view.hit) return swipes
if (swipes < REACH_MAX_SWIPES) trace.push(await touchSwipe(page, cdp, REACH_TRAVEL))
}
return { line: { gain, offset: far.delta - gain * far.travel }, swipes }
throw new Error(`the trigger never came into view: scrollY after each swipe ${trace.join(' ')}`)
}
/**
* 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 { line, swipes } = await calibrate(page, cdp, -(await triggerRect(trigger)).gap)
for (let landing = 0; landing < 8; landing++) {
const { gap } = await triggerRect(trigger)
if (gap >= 0 && gap <= 4) {
return `gain=${line.gain.toFixed(2)} offset=${line.offset.toFixed(1)} swipes=${traceOf(swipes)}`
}
const wanted = -gap - 2
const travel = Math.round((wanted - Math.sign(wanted) * line.offset) / line.gain)
swipes.push(await measuredSwipe(page, cdp, travel))
}
throw new Error(`the trigger never landed within 4 px: swipes=${traceOf(swipes)}`)
/** Scrolls at once so the trigger's bottom sits on the viewport bottom (clamped to the page). */
async function scrollToWorstCase(page: Page, trigger: Locator) {
await trigger.evaluate((el) => {
const max = document.documentElement.scrollHeight - window.innerHeight
const want = window.scrollY + el.getBoundingClientRect().bottom - window.innerHeight
window.scrollTo({ top: Math.min(Math.max(Math.ceil(want), 0), max), behavior: 'instant' })
})
return stableScrollY(page)
}
/** Taps the trigger: the panel opens with heading and close in view, the page does not scroll. */
async function tapOpensInPlace(page: Page, trigger: Locator, scrollY: number) {
expect(await probeInView(trigger), 'trigger in view').toEqual({ inside: true, hit: true })
await trigger.tap()
await expect(page.locator('#editor-citations-title')).toBeFocused()
expect(await stableScrollY(page), 'no scroll once the panel is open').toBe(scrollY)
await expectPanelReachable(page)
}
/** Whether the element is wholly on screen and the topmost thing at its centre. */
@@ -592,7 +567,7 @@ test.describe('citations panel at 375 px, read-only (O4-2)', () => {
expect(await drawingBox()).toEqual(drawing)
})
test('667 x 375 by touch: the trigger is reached, the panel stays in view @phone', async ({
test('667 x 375: reached by touch; at the worst case the panel stays in view @phone', async ({
page,
}) => {
test.setTimeout(150_000)
@@ -602,24 +577,30 @@ test.describe('citations panel at 375 px, read-only (O4-2)', () => {
const trigger = page.locator('[data-citations-trigger]:visible')
await expect(trigger).toBeVisible()
await page.evaluate(() => window.scrollTo(0, 0))
await settle(page)
expect(await stableScrollY(page), 'start at the top').toBe(0)
const start = await triggerRect(trigger)
// A. Reachability by touch swipes only (CDP touch events, no keyboard, no programmatic scroll).
const cdp = await page.context().newCDPSession(page)
const log = await scrollToWorstCase(page, cdp, trigger)
const end = await triggerRect(trigger)
const swipes = await swipeUntilReached(page, cdp, trigger)
const reached = await stableScrollY(page)
expect(reached, 'the page scrolled by touch').toBeGreaterThan(0)
await tapOpensInPlace(page, trigger, reached)
const close = page.getByRole('button', { name: dict['editor.references.close'] })
await close.tap()
await expect(place(page, 'citations')).toHaveCount(0)
// B. Worst case: the trigger's bottom exactly on the viewport bottom. Set by window.scrollTo
// because touch has a slop floor and a load-dependent gain, so a pixel-exact touch landing
// is not deterministic; A already proves the trigger is reachable by touch.
const worst = await scrollToWorstCase(page, trigger)
const end = await triggerRect(trigger)
test.info().annotations.push({
type: 'c4-measure',
description: `top=${start.top} bottom=${start.bottom}; ${log}; gap=${end.gap} y=${reached}`,
description: `top=${start.top} bottom=${start.bottom}; A swipes=${swipes} y=${reached}; ` +
`B gap=${end.gap} y=${worst}`,
})
expect(end.gap, 'trigger gap to the viewport bottom').toBeGreaterThanOrEqual(0)
expect(end.gap, 'trigger gap to the viewport bottom').toBeLessThanOrEqual(4)
expect(end.top, 'trigger top on screen').toBeGreaterThanOrEqual(0)
expect(await probeInView(trigger), 'trigger hit-testable').toMatchObject({ hit: true })
await trigger.tap()
await expect(page.locator('#editor-citations-title')).toBeFocused()
expect(await stableScrollY(page), 'no scroll once the panel is open').toBe(reached)
await expectPanelReachable(page)
expect(end.gap, 'trigger gap to the viewport bottom').toBeLessThanOrEqual(1)
await tapOpensInPlace(page, trigger, worst)
})
test('the document size does not change while the panel opens and closes @phone', async ({