/** * The no-shift probe of U2 (adjustment A14, plan Task 3): a state change of the annotations * tab moves no box. The boxes of the canvas, the strip, the message row, the aside and the * draft header, the page's scroll height, its scrollbar gutter and its client width are read * before and after `act`; a layout-shift observer adds up every entry in between, the ones following * an input included (a message appearing after a drop is still a shift). */ import { expect, type Page } from '@playwright/test' interface Box { x: number y: number width: number height: number } interface Snapshot { boxes: Record scrollHeight: number gutter: string clientWidth: number } const PARTS = { canvas: '[data-testid="annotation-canvas"]', strip: '[data-testid="edit-strip"]', line: '[data-testid="gesture-line"]', aside: '[data-testid="editor-tab-annotations"] aside', header: 'header:has(> nav + div > h1)', } as const function snapshot(page: Page): Promise { return page.evaluate((parts) => { const shown = (selector: string) => [...document.querySelectorAll(selector)].find( (element) => (element as HTMLElement).offsetParent !== null, ) const boxes: Record = {} for (const [name, selector] of Object.entries(parts)) { const rect = shown(selector)?.getBoundingClientRect() boxes[name] = rect ? { x: rect.x, y: rect.y, width: rect.width, height: rect.height } : null } const root = document.documentElement return { boxes, scrollHeight: root.scrollHeight, gutter: getComputedStyle(root).scrollbarGutter, clientWidth: root.clientWidth, } }, PARTS) } /** The page side of the probe: the summed shift and what moved, for the failure message. */ interface ShiftScope { __shift: { total: number; moved: string[] } __shiftObserver?: PerformanceObserver /** The browser's own requestAnimationFrame, kept before `page.clock` replaces it. */ __nativeRaf?: (callback: FrameRequestCallback) => number } /** * Keeps the native requestAnimationFrame: call it before `page.clock.install()`, whose fake * frames would let the two-frame wait end before the real layout (and its shift) happens. */ export async function keepNativeFrames(page: Page): Promise { await page.addInitScript(() => { const scope = window as unknown as ShiftScope const raf = window.requestAnimationFrame // Kept only when this script ran before the clock's (else the probe would wait on fakes). if (String(raf).includes('[native code]')) scope.__nativeRaf = raf.bind(window) else throw new Error('keepNativeFrames ran after page.clock.install()') }) } /** Starts summing layout-shift entries; read back with `shiftTotal`. */ function observeShifts(page: Page): Promise { return page.evaluate(() => { const scope = window as unknown as ShiftScope scope.__shiftObserver?.disconnect() scope.__shift = { total: 0, moved: [] } const observer = new PerformanceObserver((list) => { for (const entry of list.getEntries()) { const shift = entry as unknown as { value: number sources: { node?: Node }[] } scope.__shift.total += shift.value for (const source of shift.sources) { const node = source.node as Element | undefined scope.__shift.moved.push( node?.outerHTML?.slice(0, 160) ?? String(source.node), ) } } }) observer.observe({ type: 'layout-shift' }) scope.__shiftObserver = observer }) } /** Two frames for layout to settle, then the pending entries and the total. */ function shiftTotal(page: Page): Promise { return page.evaluate(async () => { const scope = window as unknown as ShiftScope const faked = !String(window.requestAnimationFrame).includes( '[native code]', ) if (faked && !scope.__nativeRaf) { throw new Error( 'page.clock fakes the frames: call keepNativeFrames first', ) } const frame = scope.__nativeRaf ?? window.requestAnimationFrame.bind(window) await new Promise((done) => frame(() => frame(done))) const observer = scope.__shiftObserver! const pending = observer.takeRecords() observer.disconnect() for (const entry of pending) { scope.__shift.total += (entry as unknown as { value: number }).value } return scope.__shift }) } /** * Runs `act` and asserts nothing moved: the boxes of the canvas, the strip, the message * row, the aside and the header, the scroll height, and no layout shift anywhere (U2 Task * 6: the aside always shows the overview, so no exemption is left). */ export async function expectNoShift( page: Page, act: () => Promise, ): Promise { const before = await snapshot(page) // A selector that stops matching would compare null with null: every part must be there. for (const [name, box] of Object.entries(before.boxes)) { expect(box, `${name} box before the act`).not.toBeNull() } await observeShifts(page) await act() const shift = await shiftTotal(page) const after = await snapshot(page) expect(after).toEqual(before) expect(shift.total, `layout shift of ${shift.moved.join(' | ')}`).toBe(0) }