From 817f74ba184a3f9c3f264d1941dac4494329fe80 Mon Sep 17 00:00:00 2001 From: bchanot Date: Fri, 13 Nov 2026 14:13:03 +0100 Subject: [PATCH] test(tests): C4 review round 1, worst-case touch distance --- .../app/e2e/shell/editor-shell-layout.spec.ts | 220 +++++++++--------- 1 file changed, 112 insertions(+), 108 deletions(-) diff --git a/apps/app/e2e/shell/editor-shell-layout.spec.ts b/apps/app/e2e/shell/editor-shell-layout.spec.ts index d2b24613..b1273343 100644 --- a/apps/app/e2e/shell/editor-shell-layout.spec.ts +++ b/apps/app/e2e/shell/editor-shell-layout.spec.ts @@ -1,5 +1,5 @@ import { fr } from '@/i18n/fr' -import type { Page, TestInfo } from '@playwright/test' +import type { CDPSession, Locator, Page, TestInfo } from '@playwright/test' import { stepUpAdmin } from '../support/admin' import { expect, @@ -155,6 +155,104 @@ async function settle(page: Page) { ) } +/** Opens the finding by taps only: the caret of a closed row opens the branch. */ +async function tapFinding(page: Page, name: string) { + await openTab(page, dict, 'findings') + const item = page.getByRole('treeitem', { name }) + for (let depth = 0; depth < 4 && (await item.count()) === 0; depth++) { + const closed = page.getByRole('treeitem', { expanded: false }).first() + await closed.locator('span').first().tap() + } + await item.tap() +} + +/** The window's scrollY once two animation frames in a row agree (no fling left). */ +async function stableScrollY(page: Page) { + let last = -1 + for (let tries = 0; tries < 60; tries++) { + await settle(page) + const now = await page.evaluate(() => window.scrollY) + if (now === last) return now + last = now + } + throw new Error('the scroll never settled') +} + +/** The trigger's box; `gap` is the room between its bottom and the viewport bottom. */ +function triggerRect(trigger: Locator) { + return trigger.evaluate((el) => { + const box = el.getBoundingClientRect() + return { top: box.top, bottom: box.bottom, gap: window.innerHeight - box.bottom } + }) +} + +/** One finger swipe on the page: `travel` > 0 moves the finger up (the page goes down). */ +async function touchSwipe(page: Page, cdp: CDPSession, travel: number) { + const from = 340 + const point = (y: number) => [{ x: 200, y }] + await cdp.send('Input.dispatchTouchEvent', { type: 'touchStart', touchPoints: point(from) }) + for (let step = 1; step <= 10; step++) { + const y = from - (travel * step) / 10 + await cdp.send('Input.dispatchTouchEvent', { type: 'touchMove', touchPoints: point(y) }) + } + await cdp.send('Input.dispatchTouchEvent', { type: 'touchEnd', touchPoints: [] }) + return stableScrollY(page) +} + +/** + * Scrolls by touch until the trigger's bottom is 0 to 4 px above the viewport bottom. Two + * swipes of known travel give the line `delta = gain * travel + offset` (touch slop and + * Chromium's scroll gain); each next swipe inverts it for the distance left, and a swipe back + * corrects an overshoot. + */ +async function scrollToWorstCase(page: Page, cdp: CDPSession, trigger: Locator) { + const trace: string[] = [] + const swipe = async (travel: number) => { + const before = await page.evaluate(() => window.scrollY) + const delta = (await touchSwipe(page, cdp, travel)) - before + trace.push(`${travel}->${delta}`) + return delta + } + const [near, far] = [120, 200] + const [deltaNear, deltaFar] = [await swipe(near), await swipe(far)] + const gain = (deltaFar - deltaNear) / (far - near) + const offset = deltaFar - gain * far + for (let swipes = 0; swipes < 8; swipes++) { + const { gap } = await triggerRect(trigger) + if (gap >= 0 && gap <= 4) break + const wanted = -gap - 2 + await swipe(Math.round((wanted - Math.sign(wanted) * offset) / gain)) + } + return `gain=${gain.toFixed(2)} offset=${offset.toFixed(1)} swipes=${trace.join(' ')}` +} + +/** Whether the element is wholly on screen and the topmost thing at its centre. */ +function probeInView(target: Locator) { + return target.evaluate((el) => { + const box = el.getBoundingClientRect() + const at = document.elementFromPoint(box.x + box.width / 2, box.y + box.height / 2) + const inside = + box.top >= 0 && box.left >= 0 && + box.bottom <= window.innerHeight && box.right <= window.innerWidth + return { inside, hit: !!at && el.contains(at) } + }) +} + +/** The open panel's heading and close button, in view and hit-testable, no scroll needed. */ +async function expectPanelReachable(page: Page) { + const panel = page.locator('#editor-citations-panel') + const heading = page.locator('#editor-citations-title') + const close = panel.getByRole('button', { name: dict['editor.references.close'] }) + const visible = await panel.evaluate( + (el) => Math.min(el.getBoundingClientRect().bottom, window.innerHeight) - + Math.max(el.getBoundingClientRect().top, 0), + ) + test.info().annotations.push({ type: 'c4-measure', description: `panel visible ${visible}` }) + expect(visible).toBeGreaterThan(0) + expect(await probeInView(heading), 'heading').toEqual({ inside: true, hit: true }) + expect(await probeInView(close), 'close button').toEqual({ inside: true, hit: true }) +} + test.describe('editor shell layout (H)', () => { test.use({ locale: 'fr-FR', actionTimeout: 15_000 }) test.beforeEach(async ({ context }, info) => { @@ -459,128 +557,34 @@ test.describe('citations panel at 375 px, read-only (O4-2)', () => { expect(await drawingBox()).toEqual(drawing) }) - test('667 x 375 by touch: the trigger scrolls into reach, the panel heading and close stay in view @phone', async ({ + test('667 x 375 by touch: the trigger is reached, the panel stays in view @phone', async ({ page, }) => { test.setTimeout(150_000) await open(page) await page.setViewportSize({ width: 667, height: 375 }) - await openTab(page, dict, 'findings') - const item = page.getByRole('treeitem', { name: 'Constat A' }) - for (let depth = 0; depth < 4 && (await item.count()) === 0; depth++) { - const closed = page.getByRole('treeitem', { expanded: false }).first() - await closed.locator('span').first().tap() // the caret opens the branch - } - await item.tap() + await tapFinding(page, 'Constat A') const trigger = page.locator('[data-citations-trigger]:visible') await expect(trigger).toBeVisible() await page.evaluate(() => window.scrollTo(0, 0)) await settle(page) - // The worst distance: the trigger's bottom lands on the viewport's bottom. - const before = await trigger.evaluate((el) => ({ - bottom: el.getBoundingClientRect().bottom, - top: el.getBoundingClientRect().top, - height: window.innerHeight, - })) - const distance = Math.max(0, Math.ceil(before.bottom - before.height)) + const start = await triggerRect(trigger) const cdp = await page.context().newCDPSession(page) - // A real finger swipe (CDP touch events; `synthesizeScrollGesture` with touch scrolls - // nothing in this Chromium): up to 200 px at a time, a pause before the lift so no - // fling carries on, repeated until the trigger's bottom is on screen. - const swipe = async (step: number) => { - const from = 340 - await cdp.send('Input.dispatchTouchEvent', { - type: 'touchStart', - touchPoints: [{ x: 200, y: from }], - }) - for (let i = 1; i <= 10; i++) { - await cdp.send('Input.dispatchTouchEvent', { - type: 'touchMove', - touchPoints: [{ x: 200, y: from - (step * i) / 10 }], - }) - } - await page.waitForTimeout(150) - await cdp.send('Input.dispatchTouchEvent', { - type: 'touchEnd', - touchPoints: [], - }) - await settle(page) - } - const gap = async () => - Math.ceil( - (await trigger.evaluate((el) => el.getBoundingClientRect().bottom)) - - before.height, - ) - let swipes = 0 - for (let left = distance; left > 0 && swipes < 12; swipes++) { - await swipe(Math.min(Math.max(left, 60), 200)) // under ~60 px a swipe is below the touch slop - left = await gap() - } - await settle(page) - const reached = await page.evaluate(() => window.scrollY) - const hit = await trigger.evaluate((el) => { - const box = el.getBoundingClientRect() - const at = document.elementFromPoint( - box.x + box.width / 2, - box.y + box.height / 2, - ) - return { - covered: !at || !el.contains(at), - bottom: box.bottom, - top: box.top, - scrollMax: document.documentElement.scrollHeight - window.innerHeight, - at: at ? `${at.tagName}.${String(at.className).slice(0, 60)}` : null, - } - }) + const log = await scrollToWorstCase(page, cdp, trigger) + const end = await triggerRect(trigger) + const reached = await stableScrollY(page) test.info().annotations.push({ type: 'c4-measure', - description: `trigger top=${before.top} bottom=${before.bottom} at scroll 0, distance=${distance}, swipes=${swipes}, scrollY=${reached}, bottom after=${hit.bottom}`, + description: `top=${start.top} bottom=${start.bottom}; ${log}; gap=${end.gap} y=${reached}`, }) - expect(hit.bottom, 'trigger bottom on screen').toBeLessThanOrEqual(before.height) - expect(hit.covered, 'trigger hit-testable after the touch scroll').toBe(false) + expect(end.gap, 'trigger gap to the viewport bottom').toBeGreaterThanOrEqual(0) + expect(end.gap, 'trigger gap to the viewport bottom').toBeLessThanOrEqual(4) + expect(end.top, 'trigger top on screen').toBeGreaterThanOrEqual(0) + expect(await probeInView(trigger), 'trigger hit-testable').toMatchObject({ hit: true }) await trigger.tap() await expect(page.locator('#editor-citations-title')).toBeFocused() - await settle(page) - const inView = await page.evaluate((label) => { - const probe = (selector: string) => { - const el = document.querySelector(selector)! - const box = el.getBoundingClientRect() - const at = document.elementFromPoint( - box.x + box.width / 2, - box.y + box.height / 2, - ) - return { - inside: - box.top >= 0 && - box.bottom <= window.innerHeight && - box.left >= 0 && - box.right <= window.innerWidth, - hit: !!at && el.contains(at), - } - } - const panel = document.querySelector('#editor-citations-panel')! - const rect = panel.getBoundingClientRect() - const visible = - Math.min(rect.bottom, window.innerHeight) - Math.max(rect.top, 0) - return { - heading: probe('#editor-citations-title'), - close: probe(`#editor-citations-panel button[aria-label="${label}"]`), - visible, - scrollY: window.scrollY, - } - }, dict['editor.references.close']) - test.info().annotations.push({ - type: 'c4-measure', - description: `panel visible height=${inView.visible}, scrollY after open=${inView.scrollY}`, - }) - expect(inView.heading, 'heading in view and hit-testable').toEqual({ - inside: true, - hit: true, - }) - expect(inView.close, 'close button in view and hit-testable').toEqual({ - inside: true, - hit: true, - }) + expect(await stableScrollY(page), 'no scroll once the panel is open').toBe(reached) + await expectPanelReachable(page) }) test('the document size does not change while the panel opens and closes @phone', async ({