An open source design system that's fully customizable and agent ready
TypeScript
test(interaction-harness): name a wheel gap with no page frame across it (#10594)
Christopher Chedeaued586883ddb1
diff --git a/internal/interaction-harness/README.md b/internal/interaction-harness/README.mdindex 529e9c9304..987c5e5fae 100644--- a/internal/interaction-harness/README.md+++ b/internal/interaction-harness/README.md@@ -616,24 +616,30 @@ as it does a trackpad's.WebKit dispatches a step only once the page's main thread is free, and thesteps 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 first-listener received it (`wheelArrivals`). Two things can split a run past+listener received it (`wheelArrivals`). Three things can split a run past`maxGapMs`. When a step reached the page within one frame of its own wheellisteners finishing the step before, and the gap is more than a frame widerthan the harness's own spacing of those sends, those listeners held it back:the burst throws `WheelPageStallError`, the page split the gesture, and theproof fails. A page can only widen a gap, so one no wider than the sendspacing 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. A proof never re-takes itself: a-`WheelPacingError` stays red, named as the runner's. Where one shows in CI's-profile, fix the wheel's delivery, not the proof. The burst times the+When the page drew no frame across nearly all of a widened gap, engine or page+work held the main thread: the burst records the page's frames and throws+`WheelPageStallError` with the frame gap, the arrival gap and the listeners'+time. A starved runner's renderer draws no frame either, so judge such a stall+from a run with CI's CPU limits. Otherwise the dispatch stalled before the+page (a loaded runner) and it throws `WheelPacingError` with the gaps. A proof+never re-takes itself: a `WheelPacingError` stays red, named as the runner's.+Where one shows in CI's profile, fix the wheel's delivery, not the proof. The+burst times thepage's handling with a listener on the window that runs after the page's own.A step whose propagation the page stops is marked by a task queued when itarrived, and never later than the next step's arrival. So its handling, withany work queued before that task, counts as the page's. Other main-thread work-between steps (timers, frames) still counts as the dispatch's stall. Chromium-stamps a step with its dispatch time, so a page that holds its main thread does-not widen the gaps there, and every gap past `maxGapMs` is a `WheelPacingError`.+between steps that leaves frames running still counts as the dispatch's stall.+Chromium stamps a step with its dispatch time, so a page that holds its main+thread does not widen the gaps there, and every gap past `maxGapMs` is a+`WheelPacingError`.The WebKit input command is reached through Playwright's in-process serverobject, so it needs a locally launched browser (the default), not `connect()`.diff --git a/internal/interaction-harness/src/harness.interaction.browser.spec.ts b/internal/interaction-harness/src/harness.interaction.browser.spec.tsindex 4d165fb60a..819b789e3c 100644--- a/internal/interaction-harness/src/harness.interaction.browser.spec.ts+++ b/internal/interaction-harness/src/harness.interaction.browser.spec.ts@@ -3573,6 +3573,61 @@ test.describe('wheel and pen input', () => {}});+ test("a page that draws no frame across a burst's gap splits it itself, with its listeners idle: WheelPageStallError names the frame gap", async ({+ browser,+ browserName,+ }) => {+ test.skip(+ browserName !== 'webkit',+ 'only WebKit dispatches a wheel step once the page is free',+ );+ const context = await browser.newContext({+ viewport: {width: 402, height: 714},+ isMobile: false,+ hasTouch: false,+ });+ try {+ const page = await context.newPage();+ // The first step queues a 150 ms task, as engine or page work can hold+ // the main thread. The listener itself returns at once, and the page+ // draws no frame until the task ends.+ await page.route('http://wheel-task-page.test/', route =>+ route.fulfill({+ contentType: 'text/html',+ body: '<!doctype html><body style="margin:0;height:3000px"><script>let queued=false;addEventListener("wheel",()=>{if(queued)return;queued=true;setTimeout(()=>{const end=performance.now()+150;while(performance.now()<end){}},0)},{passive:true});</script>',+ }),+ );+ await page.goto('http://wheel-task-page.test/');+ const driver = await webInputDriver(page);+ test.skip(!driver.wheels, WHEEL_SKIP);+ const outcome = await driver+ .wheelBurst({+ x: 200,+ y: 300,+ steps: Array.from({length: 6}, () => ({dx: 10})),+ maxGapMs: 100,+ })+ .then(+ () => 'resolved',+ (error: unknown) => error,+ );+ expect(outcome).toBeInstanceOf(WheelPageStallError);+ const {stall} = outcome as WheelPageStallError;+ expect(stall.frameGapMs).toBeGreaterThan(100);+ expect(stall.handlingMs).toBeLessThan(stall.gapMs);+ expect(String(outcome)).toMatch(+ /the page drew no frame for \d+ ms around a \d+ ms gap between two wheel steps, over its 100 ms \(engine or page work\)/,+ );+ // A starved runner's renderer draws no frame either, and the error+ // says so.+ expect(String(outcome)).toMatch(+ /On a starved runner the renderer draws no frame either/,+ );+ } finally {+ await context.close();+ }+ });+test('a page whose wheel listener stops the step and holds its main thread still splits a burst itself', async ({browser,browserName,diff --git a/internal/interaction-harness/src/webInput.pacing.test.ts b/internal/interaction-harness/src/webInput.pacing.test.tsindex f9f548e117..cda7c73c96 100644--- a/internal/interaction-harness/src/webInput.pacing.test.ts+++ b/internal/interaction-harness/src/webInput.pacing.test.ts@@ -99,6 +99,32 @@ describe('wheelGapCauses', () => {).toEqual({pageStalls: [], dispatchStalls: [152]});});+ it('gives a WebKit gap to the page, naming its frame gap, when the page drew no frame across it with its listeners idle', () => {+ // The listeners took 1 ms; the page then drew no frame from 20 to 380,+ // across the 360 ms gap between the steps.+ const frames = [4, 20, 380, 396];+ expect(+ wheelGapCauses('webkit', [16, 376], [17, 377], 100, [16], frames),+ ).toEqual({+ pageStalls: [{gapMs: 360, handlingMs: 1, frameGapMs: 360}],+ dispatchStalls: [],+ });+ // With frames running through the same gap, it is the dispatch's.+ const running = Array.from({length: 26}, (_, k) => 4 + 16 * k);+ expect(+ wheelGapCauses('webkit', [16, 376], [17, 377], 100, [16], running),+ ).toEqual({pageStalls: [], dispatchStalls: [360]});+ // A gap no wider than the harness's own send spacing stays the+ // dispatch's, frames or not.+ expect(+ wheelGapCauses('webkit', [16, 376], [17, 377], 100, [360], frames),+ ).toEqual({pageStalls: [], dispatchStalls: [360]});+ // Chromium's stamps are its dispatch's.+ expect(+ wheelGapCauses('chromium', [16, 376], [17, 377], 100, [16], frames),+ ).toEqual({pageStalls: [], dispatchStalls: [360]});+ });+it('counts a step whose handling was never seen as the dispatch\u2019s', () => {expect(wheelGapCauses('webkit', [0, 150], [Number.NaN, 151], 100)).toEqual({pageStalls: [],diff --git a/internal/interaction-harness/src/webInput.ts b/internal/interaction-harness/src/webInput.tsindex 7cd3e639f3..41a1f5d678 100644--- a/internal/interaction-harness/src/webInput.ts+++ b/internal/interaction-harness/src/webInput.ts@@ -69,10 +69,11 @@ export interface WheelBurst {readonly ctrl?: boolean;/*** The longest gap the page may see between two received steps, ms (see- * `wheelArrivals`): the gap after which the proof's component would treat- * the run as more than one gesture. Past it `wheelBurst` throws `WheelPageStallError` when the- * page's own handling of the step before caused the gap, and- * `WheelPacingError` otherwise.+ * `wheelArrivals`). Past it, the proof's component would treat the run as+ * more than one gesture. Past it `wheelBurst` throws+ * `WheelPageStallError` when the page held the gap, and `WheelPacingError`+ * otherwise. The page holds it while it handles the step before, or while+ * it draws no frame.*/readonly maxGapMs?: number;}@@ -84,9 +85,9 @@ export interface WheelBurst {* 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. Such a- * stall is the runner's or that work's, never the wheel listeners'.+ * splits what the driver sent as one gesture. Other work between steps that+ * leaves the page's frames running also lands here. Such a stall is the+ * runner's or that work's, never the wheel listeners'.*/export class WheelPacingError extends Error {constructor(@@ -102,11 +103,13 @@ export class WheelPacingError extends Error {/*** The page received a wheel burst's steps further apart than the proof's- * `maxGapMs` while it was still handling the step before. Its own wheel- * listeners held the main thread, and WebKit dispatches the next step only- * once it is free. The handling time is page wall time, which on a starved- * runner includes the page's wait for a CPU. Judge a page stall from a run- * with CI's CPU limits: there it is the proof's own failure.+ * `maxGapMs` while its main thread was held. Its own wheel listeners were+ * still handling the step before, or the page drew no frame across the gap.+ * WebKit dispatches the next step only once the thread is free. Both+ * times are page wall time. On a starved runner they include the page's wait+ * for a CPU, and a renderer held from outside draws no frame either. Judge a+ * page stall from a run with CI's CPU limits: there it is the proof's own+ * failure.*/export class WheelPageStallError extends Error {constructor(@@ -114,16 +117,23 @@ export class WheelPageStallError extends Error {readonly stall: WheelPageStall,) {super(- `webInputDriver: the page's own wheel listeners took ${stall.handlingMs.toFixed(0)} ms of page wall time to handle a step, so the next reached it ${stall.gapMs.toFixed(0)} ms later, over its ${maxGapMs} ms: the page split the run. On a starved runner that time includes the page's wait for a CPU: judge a page stall from a run with CI's CPU limits`,+ stall.frameGapMs == null+ ? `webInputDriver: the page's own wheel listeners took ${stall.handlingMs.toFixed(0)} ms of page wall time to handle a step, so the next reached it ${stall.gapMs.toFixed(0)} ms later, over its ${maxGapMs} ms: the page split the run. On a starved runner that time includes the page's wait for a CPU: judge a page stall from a run with CI's CPU limits`+ : `webInputDriver: the page drew no frame for ${stall.frameGapMs.toFixed(0)} ms around a ${stall.gapMs.toFixed(0)} ms gap between two wheel steps, over its ${maxGapMs} ms (engine or page work), while its own wheel listeners took ${stall.handlingMs.toFixed(0)} ms: the page split the run. On a starved runner the renderer draws no frame either: judge a page stall from a run with CI's CPU limits`,);this.name = 'WheelPageStallError';}}-/** A gap the page caused: how long it was, and how long the page spent handling the step before it. */+/**+ * A gap the page caused: how long it was, and how long the page spent+ * handling the step before it. `frameGapMs` is set when the page drew no+ * frame across the gap instead.+ */export interface WheelPageStall {readonly gapMs: number;readonly handlingMs: number;+ readonly frameGapMs?: number;}/**@@ -198,22 +208,25 @@ export function wheelSendGaps(/*** 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 (for a step whose propagation- * the page stopped, when a task queued at its arrival ran; NaN unseen);+ * received steps reached the page (`wheelArrivals`). `handledAt` is when the+ * page's wheel listeners had finished with each, NaN unseen. For a step whose+ * propagation the page stopped, it is when a task queued at its arrival ran.* `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, unless it ran- * before a stopped step's marking task.+ * (`wheelSendGaps`; `null` where unknown). WebKit dispatches a step only once+ * the page's main thread is free. There a gap is the page's when the step+ * reached it within one frame of the listeners finishing the step before. It+ * waited on them. The gap must also be 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. That holds even when a+ * starved renderer also held the step, as a runner whose CPU quota holds the+ * page right after its listeners does. A WebKit gap is the page's too when+ * `frames`, the page's frame times, show none across nearly all of it. Then+ * engine or page work held the main thread, or a starved runner held the+ * renderer, not the page's wheel listeners.+ * Every other gap is the dispatch's. In Chromium, which stamps a step with+ * its dispatch time, the page cannot move its stamps, so every gap is. Other+ * work between steps that leaves frames running counts as the dispatch's,+ * unless it ran before a stopped step's marking task.*/export function wheelGapCauses(engine: Engine,@@ -221,6 +234,7 @@ export function wheelGapCauses(handledAt: readonly number[],maxGapMs: number,sendGapsMs: readonly (number | null)[] = [],+ frames: readonly number[] = [],): {pageStalls: WheelPageStall[]; dispatchStalls: number[]} {const pageStalls: WheelPageStall[] = [];const dispatchStalls: number[] = [];@@ -230,13 +244,25 @@ export function wheelGapCauses(continue;}const sendGapMs = sendGapsMs[i - 1];+ const widened = sendGapMs == null || gapMs > sendGapMs + HELD_BACK_MS;+ const handlingMs = handledAt[i - 1] - stamps[i - 1];const heldBack =engine === 'webkit' &&Number.isFinite(handledAt[i - 1]) &&stamps[i] - handledAt[i - 1] <= HELD_BACK_MS &&- (sendGapMs == null || gapMs > sendGapMs + HELD_BACK_MS);+ widened;+ const frameGap =+ engine === 'webkit' && widened+ ? frameGapAcross(frames, stamps[i - 1], stamps[i])+ : null;if (heldBack) {- pageStalls.push({gapMs, handlingMs: handledAt[i - 1] - stamps[i - 1]});+ pageStalls.push({gapMs, handlingMs});+ } else if (frameGap != null && frameGap.covered >= gapMs - HELD_BACK_MS) {+ pageStalls.push({+ gapMs,+ handlingMs: Number.isFinite(handlingMs) ? handlingMs : 0,+ frameGapMs: frameGap.gapMs,+ });} else {dispatchStalls.push(gapMs);}@@ -244,6 +270,26 @@ export function wheelGapCauses(return {pageStalls, dispatchStalls};}+/**+ * The page's longest stretch without a frame inside `from` to `to`. It gives+ * how much of that span the stretch covers, and the whole gap between its two+ * frames. Null with fewer than two frames.+ */+function frameGapAcross(+ frames: readonly number[],+ from: number,+ to: number,+): {covered: number; gapMs: number} | null {+ let best: {covered: number; gapMs: number} | null = null;+ for (let k = 1; k < frames.length; k++) {+ const covered = Math.min(frames[k], to) - Math.max(frames[k - 1], from);+ if (covered > 0 && (best == null || covered > best.covered)) {+ best = {covered, gapMs: frames[k] - frames[k - 1]};+ }+ }+ return best;+}+/** A point of a pen stroke. */export interface PenPoint {readonly x: number;@@ -273,9 +319,10 @@ export interface WebInputDriver {* handles one step when the next arrives, the engine coalesces them, as it* does a trackpad's: the page can receive fewer, larger steps with the same* total. Returns how many steps the page received and the gaps between- * their arrivals (`wheelArrivals`), ms; with `maxGapMs`, a longer gap throws- * `WheelPageStallError` when the page's own handling of the step before- * caused it, and `WheelPacingError` otherwise.+ * their arrivals (`wheelArrivals`), ms. With `maxGapMs`, a longer gap throws+ * `WheelPageStallError` when the page held it, and `WheelPacingError`+ * otherwise. The page holds a gap while it handles the step before, or+ * while it draws no frame.*/wheelBurst(burst: WheelBurst): Promise<{readonly receivedSteps: number;@@ -353,6 +400,8 @@ export interface InputRun {stamps: number[];/** When the page's first listener received each event. */receivedAt: number[];+ /** When the page drew each frame during the run, with `frames` asked. */+ frames: number[];/*** True while every event of the run carries the mark `installInputStart`* added before the page's scripts. Then `receivedAt` comes before every@@ -416,9 +465,10 @@ export async function installInputStart(export function armInputRun(page: Page,types: readonly string[],+ {frames = false}: {readonly frames?: boolean} = {},): Promise<JSHandle<InputRun>> {return page.evaluateHandle(- eventTypes => {+ ([eventTypes, recordFrames]) => {const first = {capture: true, passive: true};const last = {capture: false, passive: true};const index = new WeakMap<Event, number>();@@ -433,11 +483,13 @@ export function armInputRun(};}).__harnessInputStart;+ let drawing = recordFrames;const run = {types: [...eventTypes],stamps: [] as number[],receivedAt: [] as number[],startsFirst: mark?.first === true,+ frames: [] as number[],handledAt: [] as number[],eventTypes: [] as string[],pointerTypes: [] as string[],@@ -445,6 +497,7 @@ export function armInputRun(dx: 0,dy: 0,stop: () => {+ drawing = false;for (const type of eventTypes) {window.removeEventListener(type, received, first);window.removeEventListener(type, handled, last);@@ -499,9 +552,27 @@ export function armInputRun(window.addEventListener(type, received, first);window.addEventListener(type, handled, last);}+ // The page's own frames, on the real frame loop: a frozen timer gate+ // (story.ts) holds only the page's.+ const frame =+ (+ window as unknown as {+ __harnessTimerGate?: {frame?: typeof requestAnimationFrame};+ }+ ).__harnessTimerGate?.frame ??+ window.requestAnimationFrame.bind(window);+ const draw = () => {+ if (drawing) {+ run.frames.push(performance.now());+ frame(draw);+ }+ };+ if (recordFrames) {+ frame(draw);+ }return run;},- [...types],+ [[...types], frames] as const,);}@@ -525,6 +596,7 @@ async function untilRunReceived(new Promise<{stamps: number[];receivedAt: number[];+ frames: number[];handledAt: number[];deltas: [number, number][];dx: number;@@ -553,6 +625,7 @@ async function untilRunReceived(resolve({stamps: run.stamps.slice(),receivedAt: run.receivedAt.slice(),+ frames: run.frames.slice(),handledAt: run.handledAt.slice(),deltas: run.deltas.slice(),dx: run.dx,@@ -584,6 +657,7 @@ async function untilRunReceived(got.handledAt,maxGapMs,wheelSendGaps(steps, sentAt, got.deltas),+ got.frames,);if (pageStalls.length > 0) {throw new WheelPageStallError(maxGapMs, pageStalls[0]);@@ -755,7 +829,7 @@ async function webkitWebInput(page: Page): Promise<WebInputDriver> {await page.keyboard.down('Control');}try {- const armed = await armInputRun(page, ['wheel']);+ const armed = await armInputRun(page, ['wheel'], {frames: true});const sentAt = await dispatchOnSchedule(steps,everyMs,