An open source design system that's fully customizable and agent ready
TypeScript
test(interaction-harness): mark touch listener starts before a story (#10633)
Christopher Chedeaufa30cff134c1
diff --git a/internal/interaction-harness/src/story.ts b/internal/interaction-harness/src/story.tsindex 1e6fdb8c2c..7c762f5ae3 100644--- a/internal/interaction-harness/src/story.ts+++ b/internal/interaction-harness/src/story.ts@@ -26,6 +26,7 @@ import {serveStorybook,type StaticServer,} from '@astryxdesign/a11y-spec/storybook';+import {installTouchStartBeforeLoad} from './touch';export const phone = {viewport: {width: 402, height: 714},@@ -63,6 +64,9 @@ export function withStorybook(test: TestType<any, any>): () => string {/** Pages whose Storybook highlight module `openStory` turned off. */const highlightOff = new WeakSet<Page>();+/** Pages that `openStory` gave the touch driver's listener-start mark. */+const touchStartMarked = new WeakSet<Page>();+export interface OpenStoryOptions {/*** Keeps Storybook's highlight module on. It is off by default: when on, it@@ -77,7 +81,9 @@ export interface OpenStoryOptions {* Loads a story in the page and waits for its first render, for Storybook to* finish that render, for its fonts and for two frames. Storybook's highlight* module is off (see `OpenStoryOptions`), and that is checked: its root- * element must be absent once the story renders.+ * element must be absent once the story renders. On WebKit, it adds the touch+ * driver's listener-start mark before the story loads (see+ * `installTouchStartBeforeLoad`).** Storybook finishes a render after the story first paints: it waits out the* story's animations (phase `completing`), runs `afterEach`, and only then@@ -109,6 +115,10 @@ export async function openStory().__STORYBOOK_HIGHLIGHT_INITIALIZED = true;});}+ if (!touchStartMarked.has(page)) {+ touchStartMarked.add(page);+ await installTouchStartBeforeLoad(page);+ }const params = new URLSearchParams({id: storyId, viewMode: 'story'});if (args != null) {params.set(diff --git a/internal/interaction-harness/src/storybook.interaction.browser.spec.ts b/internal/interaction-harness/src/storybook.interaction.browser.spec.tsindex 5bc308a777..f0d23c90fc 100644--- a/internal/interaction-harness/src/storybook.interaction.browser.spec.ts+++ b/internal/interaction-harness/src/storybook.interaction.browser.spec.ts@@ -8,13 +8,17 @@* on (the control), every insertion carries one. Both engines; Chromium's* trace also counts the style recalculations the scan forces. `openStory`* returns once Storybook has finished the story's render, so no listener- * Storybook adds to the document or the window lands after it+ * Storybook adds to the document or the window lands after it. A WebKit+ * touch driver created after `openStory` tells a page task's wait from the+ * page's listeners* @position Self-test for story.ts; it needs the built Storybook, as every* component proof does*/import {expect, test, type Page} from '@astryxdesign/a11y-spec/playwright';+import {drag} from './scripts';import {openStory, withStorybook} from './story';+import {touchDriver, TouchPacingError} from './touch';import {countBetween, traceRendering} from './trace';const storybook = withStorybook(test);@@ -167,3 +171,37 @@ test("openStory turns Storybook's highlight module off: an insertion in a story}await control.close();});++test("a WebKit touch driver created after openStory tells a page task's wait from the page's listeners", async ({+ page,+ browserName,+}) => {+ test.skip(+ browserName !== 'webkit',+ 'only WebKit stamps a touch when it arrives; Chromium carries the scripted times',+ );+ // Each pointer move queues a 60 ms task, as a component's timer can. The+ // listener itself returns at once, so the next move waits for the task+ // before the page's listeners begin. The driver comes after the story+ // loads, as in most proofs.+ await page.addInitScript(+ 'addEventListener("pointermove",()=>{setTimeout(()=>{const end=performance.now()+60;while(performance.now()<end){}},0)});',+ );+ await openStory(page, storybook(), 'core-button--primary');+ await page.addStyleTag({+ content: 'html, body { margin: 0; height: 3000px; touch-action: none; }',+ });+ const driver = await touchDriver(page);+ const outcome = await driver+ .play(drag({x: 200, y: 400}, {x: 200, y: 300}, {durationMs: 100, hz: 60}), {+ pacing: 'spacing',+ })+ .then(+ () => 'resolved',+ (error: unknown) => error,+ );+ expect(outcome).toBeInstanceOf(TouchPacingError);+ expect(String(outcome)).toMatch(+ /an event waited \d+ ms for the main thread before the page's listeners began/,+ );+});diff --git a/internal/interaction-harness/src/touch.ts b/internal/interaction-harness/src/touch.tsindex 02c3ececf3..d5715cc0bd 100644--- a/internal/interaction-harness/src/touch.ts+++ b/internal/interaction-harness/src/touch.ts@@ -55,6 +55,7 @@ import {armInputRun,HELD_BACK_MS,installInputStart,+ installInputStartBeforeLoad,type InputRun,} from './webInput';@@ -1326,6 +1327,18 @@ async function webkitDriver(page: Page): Promise<TouchDriver> {};}+/**+ * Adds the WebKit driver's listener-start mark to each page the tab loads+ * next, before the page's scripts. A driver created after that load then tells+ * a wait for the main thread from the page's listeners. `openStory` calls it.+ * Other engines read no mark, so it adds none there.+ */+export async function installTouchStartBeforeLoad(page: Page): Promise<void> {+ if (page.context().browser()?.browserType().name() === 'webkit') {+ await installInputStartBeforeLoad(page, TOUCH_TYPES);+ }+}+/** A touch driver for the page's engine. Firefox is not supported (no touch input path). */export async function touchDriver(page: Page): Promise<TouchDriver> {const name = page.context().browser()?.browserType().name();diff --git a/internal/interaction-harness/src/webInput.ts b/internal/interaction-harness/src/webInput.tsindex 41a1f5d678..cf1d61d9d6 100644--- a/internal/interaction-harness/src/webInput.ts+++ b/internal/interaction-harness/src/webInput.ts@@ -440,7 +440,24 @@ export async function installInputStart(page: Page,types: readonly string[],): Promise<void> {- const script = (first: boolean) => `(() => {+ await installInputStartBeforeLoad(page, types);+ await page.evaluate(inputStartScript(types, false));+}++/**+ * Adds the `installInputStart` mark to each page the tab loads next, before+ * the page's scripts. The page loaded now gets no mark. A driver created after+ * the next load then uses the mark.+ */+export async function installInputStartBeforeLoad(+ page: Page,+ types: readonly string[],+): Promise<void> {+ await page.addInitScript(inputStartScript(types, true));+}++function inputStartScript(types: readonly string[], first: boolean): string {+ return `(() => {if (window.__harnessInputStart) return;const starts = new WeakMap();window.__harnessInputStart = {starts, first: ${first}};@@ -450,8 +467,6 @@ export async function installInputStart(}, {capture: true, passive: true});}})()`;- await page.addInitScript(script(true));- await page.evaluate(script(false));}/**