test(tests): C4 review round 3, helper locals
This commit is contained in:
1 parent
776692922e
commit
f7f0aa675d
1 file changed
+34
-25
@@ -200,13 +200,30 @@ async function touchSwipe(page: Page, cdp: CDPSession, travel: number) {
|
||||
}
|
||||
|
||||
const CALIBRATION_TRAVELS = [120, 200]
|
||||
/** The most scroll a calibration travel is expected to cause (measured gain about 1.5). */
|
||||
/**
|
||||
* 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 the scroll it caused. */
|
||||
/** 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 (await touchSwipe(page, cdp, travel)) - before
|
||||
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`)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -214,22 +231,15 @@ async function measuredSwipe(page: Page, cdp: CDPSession, travel: number) {
|
||||
* 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`)
|
||||
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)}`)
|
||||
}
|
||||
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 }
|
||||
return { line: { gain, offset: far.delta - gain * far.travel }, swipes }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -238,18 +248,17 @@ async function calibrate(page: Page, cdp: CDPSession, target: number) {
|
||||
* overshoot. Throws, with the trace, when 8 swipes do not land it.
|
||||
*/
|
||||
async function scrollToWorstCase(page: Page, cdp: CDPSession, trigger: Locator) {
|
||||
const target = -(await triggerRect(trigger)).gap
|
||||
const { gain, offset, trace } = await calibrate(page, cdp, target)
|
||||
for (let swipes = 0; swipes < 8; swipes++) {
|
||||
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=${gain.toFixed(2)} offset=${offset.toFixed(1)} swipes=${trace.join(' ')}`
|
||||
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) * offset) / gain)
|
||||
trace.push(`${travel}->${await measuredSwipe(page, cdp, travel)}`)
|
||||
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=${trace.join(' ')}`)
|
||||
throw new Error(`the trigger never landed within 4 px: swipes=${traceOf(swipes)}`)
|
||||
}
|
||||
|
||||
/** Whether the element is wholly on screen and the topmost thing at its centre. */
|
||||
|
||||
Reference in new issue
Block a user