An open source design system that's fully customizable and agent ready
TypeScript
test(harness): a wheel gap is the page's only when the page widened it past the send spacing (#9215)
Christopher Chedeau57688d767e1b
diff --git a/internal/interaction-harness/README.md b/internal/interaction-harness/README.mdindex b36f7456d9..e258459a33 100644--- a/internal/interaction-harness/README.md+++ b/internal/interaction-harness/README.md@@ -290,8 +290,11 @@ steps it holds meanwhile reach the page coalesced into one, stamped like thestep before it, so the burst times each WebKit step by when the page's firstlistener received it (`wheelArrivals`). Two things can split a run past`maxGapMs`. When a step reached the page within one frame of its own wheel-listeners finishing the step before, those listeners held it back: the burst-throws `WheelPageStallError`, the page split the gesture, and the proof fails.+listeners finishing the step before, and the gap is more than a frame wider+than the harness's own spacing of those sends, those listeners held it back:+the burst throws `WheelPageStallError`, the page split the gesture, and the+proof fails. A page can only widen a gap, so one no wider than the send+spacing is the dispatch's even when a starved renderer also held the step.Otherwise the dispatch stalled before the page (a loaded runner) and it throws`WheelPacingError` with the gaps. Wrap such aproof in `retakeOnWheelPacing(testInfo, attempt)`: one re-take, only ondiff --git a/internal/interaction-harness/src/harness.interaction.browser.spec.ts b/internal/interaction-harness/src/harness.interaction.browser.spec.tsindex 4b4fc08fd7..d52f74c92a 100644--- a/internal/interaction-harness/src/harness.interaction.browser.spec.ts+++ b/internal/interaction-harness/src/harness.interaction.browser.spec.ts@@ -1527,7 +1527,11 @@ test.describe('wheel and pen input', () => {browser,}) => {// The page is free well before each next step arrives, so the gap is the- // dispatch's, not the page's.+ // dispatch's, not the page's. The steps leave 400 ms apart, four times+ // the 100 ms bound with the 30 ms listener long done when the next one+ // arrives, so the case still has to throw: a runner whose CPU quota holds+ // the engine for 100-300 ms could deliver steps 125 ms apart back to back,+ // leaving no gap past the bound to classify.const context = await browser.newContext({viewport: {width: 402, height: 714},isMobile: false,@@ -1549,7 +1553,7 @@ test.describe('wheel and pen input', () => {x: 200,y: 300,steps: Array.from({length: 4}, () => ({dx: 10})),- everyMs: 125,+ everyMs: 400,maxGapMs: 100,}),).rejects.toBeInstanceOf(WheelPacingError);diff --git a/internal/interaction-harness/src/webInput.pacing.test.ts b/internal/interaction-harness/src/webInput.pacing.test.tsindex 23d93ce03a..ff57cec75d 100644--- a/internal/interaction-harness/src/webInput.pacing.test.ts+++ b/internal/interaction-harness/src/webInput.pacing.test.ts@@ -5,12 +5,13 @@* @input webInput.ts's wheel pacing error and re-take* @output Unit cases: a wheel burst's steps arrive when the page received* them in WebKit and at their stamps in Chromium; its gaps past its bound- * are the page's when its own handling of the step before caused them, and- * the dispatch's otherwise; each error names its gaps; and a proof is+ * are the page's when its own handling of the step before caused them and+ * widened them past the harness's own send spacing, and the dispatch's+ * otherwise; each error names its gaps; and a proof is* re-taken once, only on a dispatch stall, with the re-take listed.* @position Node unit tests beside the harness's browser self-tests.*- * SYNC: When modified, update webInput.ts (wheelArrivals, wheelGapCauses,+ * SYNC: When modified, update webInput.ts (wheelArrivals, wheelSendGaps, wheelGapCauses,* WheelPacingError, WheelPageStallError, retakeOnWheelPacing).*/@@ -19,6 +20,7 @@ import {retakeOnWheelPacing,wheelArrivals,wheelGapCauses,+ wheelSendGaps,WheelPacingError,WheelPageStallError,} from './webInput';@@ -107,6 +109,96 @@ describe('wheelGapCauses', () => {});});+describe('wheelSendGaps', () => {+ it('spaces each received gap by when the harness sent the two steps', () => {+ expect(+ wheelSendGaps(+ Array.from({length: 4}, () => ({dx: 10})),+ [0, 400, 801, 1200],+ Array.from({length: 4}, (): [number, number] => [10, 0]),+ ),+ ).toEqual([400, 401, 399]);+ });++ it('spaces a coalesced step from the first sent step it carries', () => {+ // WebKit held three steps and delivered them as one 30 px step: the page+ // could have held the first of them, sent 16 ms after the step before.+ expect(+ wheelSendGaps(+ Array.from({length: 4}, () => ({dy: 10})),+ [0, 16, 32, 48],+ [+ [0, 10],+ [0, 30],+ ],+ ),+ ).toEqual([16]);+ });++ it('leaves a gap unknown when the received deltas match no sent step', () => {+ expect(+ wheelSendGaps(+ [{dx: 10}, {dx: 10}],+ [0, 16],+ [+ [10, 0],+ [7, 0],+ ],+ ),+ ).toEqual([null]);+ });+});++describe('wheelGapCauses with the send spacing', () => {+ it('gives a held-back WebKit gap to the dispatch when it is no wider than the harness sent the steps', () => {+ // Steps sent 400 ms apart: the first reached the page 290 ms late, a+ // starved renderer then held the page 64 ms past its 30 ms listener, and+ // the second arrived as the page came free, 111 ms after the first.+ expect(wheelGapCauses('webkit', [0, 111], [96, 141], 100, [400])).toEqual({+ pageStalls: [],+ dispatchStalls: [111],+ });+ });++ it('gives the page a stall behind a late first step, whose coalesced steps were sent at the burst pace', () => {+ // 19 steps sent 16 ms apart; the first reached the page 290 ms late, the+ // listener then held the page 150 ms, and steps 1 to 18 arrived coalesced+ // as it finished.+ const steps = Array.from({length: 19}, () => ({dy: 10}));+ const sentAt = steps.map((_, i) => 16 * i);+ const sendGaps = wheelSendGaps(steps, sentAt, [+ [0, 10],+ [0, 180],+ ]);+ expect(sendGaps).toEqual([16]);+ expect(+ wheelGapCauses('webkit', [290, 442], [440, 443], 100, sendGaps),+ ).toEqual({+ pageStalls: [{gapMs: 152, handlingMs: 150}],+ dispatchStalls: [],+ });+ });++ it('still gives the page a gap its listener widened past the send spacing', () => {+ // A 150 ms listener on steps sent 16 ms apart.+ expect(+ wheelGapCauses('webkit', [0, 152, 168], [150, 153, 169], 100, [16, 16]),+ ).toEqual({+ pageStalls: [{gapMs: 152, handlingMs: 150}],+ dispatchStalls: [],+ });+ });++ it('keeps the one-frame rule where the send spacing is unknown', () => {+ expect(wheelGapCauses('webkit', [0, 152], [150, 153], 100, [null])).toEqual(+ {+ pageStalls: [{gapMs: 152, handlingMs: 150}],+ dispatchStalls: [],+ },+ );+ });+});+describe('WheelPageStallError', () => {it("names the page's handling, the gap and the bound, and is not a pacing error", () => {const error = new WheelPageStallError(100, {gapMs: 152, handlingMs: 150});diff --git a/internal/interaction-harness/src/webInput.ts b/internal/interaction-harness/src/webInput.tsindex 4a9c25baf2..539edb2eef 100644--- a/internal/interaction-harness/src/webInput.ts+++ b/internal/interaction-harness/src/webInput.ts@@ -80,9 +80,11 @@ export interface WheelBurst {/*** The page received a wheel burst's steps further apart than the proof's* `maxGapMs`, and its wheel listeners did not hold the step back: the step- * reached the page later than one frame after they finished the one before.- * The dispatch stalled before the page, so a loaded runner splits what the- * driver sent as one gesture. Other work on the page's main+ * reached the page later than one frame after they finished the one before,+ * or the gap was no wider than the harness's own spacing of the sends (the+ * page can only widen a gap, so a starved renderer that also held the step+ * lands here). The dispatch stalled before the page, so a loaded runner+ * splits what the driver sent as one gesture. Other work on the page's main* thread between steps (timers, frame callbacks) also lands here, so a* re-taken run is the runner's or that work's, never the wheel listeners'.*/@@ -146,22 +148,76 @@ export function wheelArrivals(return engine === 'webkit' ? receivedAt : stamps;}+/**+ * How far apart the harness sent the steps behind each received gap, ms+ * (`sendGapsMs[i - 1]` for the gap before received step `i`), or `null` where+ * the received steps cannot be matched to the sent ones. A received step is+ * matched to the sent steps its deltas reach: WebKit coalesces the steps it+ * holds into one that carries their summed deltas. The gap is spaced from the+ * last sent step the step before carried to the first sent step this one+ * carries, the send the page could have held: spacing it by the last carried+ * step would let a late first step lend the gap its delay.+ */+export function wheelSendGaps(+ steps: readonly {readonly dx?: number; readonly dy?: number}[],+ sentAt: readonly number[],+ deltas: readonly (readonly [number, number])[],+): (number | null)[] {+ const close = (a: number, b: number) => Math.abs(a - b) < 0.01;+ // What the page has received after each sent step, if it received them all.+ const sentSums: [number, number][] = [];+ let sx = 0;+ let sy = 0;+ for (const {dx = 0, dy = 0} of steps) {+ sx += dx;+ sy += dy;+ sentSums.push([sx, sy]);+ }+ let rx = 0;+ let ry = 0;+ let from = 0;+ const lastSent = deltas.map(([dx, dy]) => {+ rx += dx;+ ry += dy;+ for (let j = from; j < sentSums.length; j++) {+ if (close(sentSums[j][0], rx) && close(sentSums[j][1], ry)) {+ from = j + 1;+ return j;+ }+ }+ return null;+ });+ return lastSent.slice(1).map((j, i) => {+ const before = lastSent[i];+ return j == null || before == null || j <= before+ ? null+ : sentAt[before + 1] - sentAt[before];+ });+}+/*** Sorts a burst's gaps past `maxGapMs` by cause. `stamps` are when the* received steps reached the page (`wheelArrivals`); `handledAt` is when the- * page's wheel listeners had finished with each (NaN where that was not seen). In WebKit, which- * dispatches a step only once the page's main thread is free, a gap is the- * page's when the step reached the page within one frame of the listeners- * finishing the step before: it was waiting on them. Every other gap is the- * dispatch's, and in Chromium, which stamps a step with its dispatch time, so- * the page cannot move its stamps, every gap is. Other work on the page's main- * thread between steps (timers, frame callbacks) counts as the dispatch's.+ * page's wheel listeners had finished with each (NaN where that was not seen);+ * `sendGapsMs` is how far apart the harness sent the steps behind each gap+ * (`wheelSendGaps`; `null` where unknown). In WebKit, which dispatches a step+ * only once the page's main thread is free, a gap is the page's when the step+ * reached the page within one frame of the listeners finishing the step+ * before, so it was waiting on them, and the gap is more than a frame wider+ * than the harness's own spacing of those steps: the page can only widen a+ * gap, so one no wider than the send spacing is the dispatch's, even when a+ * starved renderer also held the step (a runner whose CPU quota holds the+ * page right after its listeners). Every other gap is the dispatch's, and in+ * Chromium, which stamps a step with its dispatch time, so the page cannot+ * move its stamps, every gap is. Other work on the page's main thread between+ * steps (timers, frame callbacks) counts as the dispatch's.*/export function wheelGapCauses(engine: Engine,stamps: readonly number[],handledAt: readonly number[],maxGapMs: number,+ sendGapsMs: readonly (number | null)[] = [],): {pageStalls: WheelPageStall[]; dispatchStalls: number[]} {const pageStalls: WheelPageStall[] = [];const dispatchStalls: number[] = [];@@ -170,10 +226,12 @@ export function wheelGapCauses(if (gapMs <= maxGapMs) {continue;}+ const sendGapMs = sendGapsMs[i - 1];const heldBack =engine === 'webkit' &&Number.isFinite(handledAt[i - 1]) &&- stamps[i] - handledAt[i - 1] <= HELD_BACK_MS;+ stamps[i] - handledAt[i - 1] <= HELD_BACK_MS &&+ (sendGapMs == null || gapMs > sendGapMs + HELD_BACK_MS);if (heldBack) {pageStalls.push({gapMs, handlingMs: handledAt[i - 1] - stamps[i - 1]});} else {@@ -319,6 +377,8 @@ interface WheelRun {receivedAt: number[];/** When the page's wheel listeners had finished with each step; NaN until seen. */handledAt: number[];+ /** Each received step's deltas, CSS px: a coalesced step carries the sum. */+ deltas: [number, number][];dx: number;dy: number;stop: () => void;@@ -339,6 +399,7 @@ function armWheelRun(page: Page): Promise<JSHandle<WheelRun>> {stamps: [] as number[],receivedAt: [] as number[],handledAt: [] as number[],+ deltas: [] as [number, number][],dx: 0,dy: 0,stop: () => {@@ -351,6 +412,7 @@ function armWheelRun(page: Page): Promise<JSHandle<WheelRun>> {run.stamps.push(wheel.timeStamp);run.receivedAt.push(performance.now());run.handledAt.push(Number.NaN);+ run.deltas.push([wheel.deltaX, wheel.deltaY]);run.dx += wheel.deltaX;run.dy += wheel.deltaY;};@@ -372,6 +434,7 @@ async function untilRunReceived(engine: Engine,armed: JSHandle<WheelRun>,{steps, maxGapMs}: Pick<WheelBurst, 'steps' | 'maxGapMs'>,+ sentAt: readonly number[],): Promise<{receivedSteps: number; receivedGapsMs: number[]}> {const want = steps.reduce<{dx: number; dy: number}>((sum, {dx = 0, dy = 0}) => ({dx: sum.dx + dx, dy: sum.dy + dy}),@@ -383,6 +446,7 @@ async function untilRunReceived(stamps: number[];receivedAt: number[];handledAt: number[];+ deltas: [number, number][];dx: number;dy: number;}>(resolve => {@@ -405,6 +469,7 @@ async function untilRunReceived(stamps: run.stamps.slice(),receivedAt: run.receivedAt.slice(),handledAt: run.handledAt.slice(),+ deltas: run.deltas.slice(),dx: run.dx,dy: run.dy,});@@ -433,6 +498,7 @@ async function untilRunReceived(arrivals,got.handledAt,maxGapMs,+ wheelSendGaps(steps, sentAt, got.deltas),);if (pageStalls.length > 0) {throw new WheelPageStallError(maxGapMs, pageStalls[0]);@@ -446,23 +512,27 @@ async function untilRunReceived(/*** Sends each step at its scheduled time without awaiting the one before, then- * waits until every dispatch is answered.+ * waits until every dispatch is answered. Resolves with when each step left+ * (`Date.now()`, ms).*/async function dispatchOnSchedule<T>(steps: readonly T[],everyMs: number,send: (step: T) => Promise<unknown>,-): Promise<void> {+): Promise<number[]> {const start = Date.now();const sent: Promise<unknown>[] = [];+ const sentAt: number[] = [];for (const [i, step] of steps.entries()) {const wait = start + i * everyMs - Date.now();if (wait > 0) {await sleep(wait);}+ sentAt.push(Date.now());sent.push(send(step));}await Promise.all(sent);+ return sentAt;}async function chromiumWebInput(page: Page): Promise<WebInputDriver> {@@ -491,17 +561,26 @@ async function chromiumWebInput(page: Page): Promise<WebInputDriver> {() => window.devicePixelRatio * (window.visualViewport?.scale ?? 1),);const armed = await armWheelRun(page);- await dispatchOnSchedule(steps, everyMs, ({dx = 0, dy = 0}) =>- cdp.send('Input.dispatchMouseEvent', {- type: 'mouseWheel',- x,- y,- deltaX: dx * scale,- deltaY: dy * scale,- modifiers: ctrl ? 2 : 0,- }),+ const sentAt = await dispatchOnSchedule(+ steps,+ everyMs,+ ({dx = 0, dy = 0}) =>+ cdp.send('Input.dispatchMouseEvent', {+ type: 'mouseWheel',+ x,+ y,+ deltaX: dx * scale,+ deltaY: dy * scale,+ modifiers: ctrl ? 2 : 0,+ }),+ );+ return untilRunReceived(+ page,+ 'chromium',+ armed,+ {steps, maxGapMs},+ sentAt,);- return untilRunReceived(page, 'chromium', armed, {steps, maxGapMs});},async pen(points) {if (points.length === 0) {@@ -592,13 +671,18 @@ async function webkitWebInput(page: Page): Promise<WebInputDriver> {}try {const armed = await armWheelRun(page);- await dispatchOnSchedule(steps, everyMs, ({dx = 0, dy = 0}) =>- page.mouse.wheel(dx, dy),- );- return await untilRunReceived(page, 'webkit', armed, {+ const sentAt = await dispatchOnSchedule(steps,- maxGapMs,- });+ everyMs,+ ({dx = 0, dy = 0}) => page.mouse.wheel(dx, dy),+ );+ return await untilRunReceived(+ page,+ 'webkit',+ armed,+ {steps, maxGapMs},+ sentAt,+ );} finally {if (ctrl) {await page.keyboard.up('Control');