diff --git a/apps/app/e2e/shell/editor-measure-commit.spec.ts b/apps/app/e2e/shell/editor-measure-commit.spec.ts new file mode 100644 index 00000000..315b23e9 --- /dev/null +++ b/apps/app/e2e/shell/editor-measure-commit.spec.ts @@ -0,0 +1,60 @@ +import { test } from '../support/browser' +import { measureCommits } from '../support/measure-commit' +import { + PREVIEW_ORIGIN, + previewMeasure, + startPreview, +} from '../support/measure-preview' +import { + dropLateReplays, + FAKE_SUPABASE_ORIGIN, + routeToSandbox, +} from '../support/sandbox' + +// U2 Task 10: the hand-run commit-latency measure (support/measure-commit.ts). Skipped unless +// MEASURE_COMMIT=1: a timing figure, never a suite assertion. With MEASURE_BUILD=preview it +// runs on a production build (support/measure-preview.ts) instead of the dev server. + +const preview = previewMeasure() + +test.describe('commit latency measure (hand-run)', () => { + test.use({ + locale: 'fr-FR', + actionTimeout: 15_000, + ...(preview + ? { + baseURL: PREVIEW_ORIGIN, + allowedOrigins: [PREVIEW_ORIGIN, FAKE_SUPABASE_ORIGIN], + } + : {}), + }) + let stopPreview: (() => void) | null = null + test.beforeEach(async ({ context }, info) => { + test.skip( + process.env.MEASURE_COMMIT !== '1', + 'run by hand: MEASURE_COMMIT=1', + ) + test.skip(info.project.name !== 'chromium-desktop', 'one run, at 1 280 px') + test.setTimeout(600_000) + if (preview) stopPreview = await startPreview() + await routeToSandbox(context) + }) + test.afterEach(async ({ context }) => { + await dropLateReplays(context) + stopPreview?.() + stopPreview = null + }) + + test('vertex drag, « + » and « − » on the equine zone, three runs each', async ({ + page, + context, + }, info) => { + test.setTimeout(600_000) + const build = preview ? 'production build (vite preview)' : 'dev server' + for (const figures of await measureCommits(page, context)) { + const line = JSON.stringify({ build, ...figures }) + console.log(`MEASURE ${line}`) + info.annotations.push({ type: 'measure', description: line }) + } + }) +}) diff --git a/apps/app/e2e/support/measure-commit.ts b/apps/app/e2e/support/measure-commit.ts new file mode 100644 index 00000000..dc4fd21c --- /dev/null +++ b/apps/app/e2e/support/measure-commit.ts @@ -0,0 +1,208 @@ +/** + * The commit-latency measure of U2 Task 10 (plan Task 10 Step 3, budget 500 ms), run by hand, + * never by a suite: a timing assertion would flake on the shared sandbox. Chromium mouse at + * 1 280 × 800, CPU × 4 while each gesture runs, on « Vertèbres thoraciques » of the equine + * main view (the curated draft's drawing and document, seeded per run as a new draft of its + * own: no existing sheet or working copy is opened). Three gestures, three runs each: a + * vertex drag, a « + » insert and a « − » removal, each undone before the next run. + * + * Start: the `pointerup` that releases the drag or activates the handle (its time stamp, so + * input queueing counts). End: the end of the first frame, after its style, layout and paint, + * whose DOM shows the committed state: the selected polygon's `points` differ from the stored + * ones, no old-place outline (no preview pending), and « Défaire la dernière modification » + * enabled (the undo entry exists; the undo stack is empty before each gesture). + * + * Run: MEASURE_COMMIT=1 pnpm db:sandbox e2e-shell editor-measure-commit.spec.ts + * (MEASURE_BUILD=preview: on a production build, support/measure-preview.ts). + */ +import type { BrowserContext, Page } from '@playwright/test' +import { fr } from '@/i18n/fr' +import { expect } from './browser' +import { + clientOf, + editorActor, + equineDrawing, + equineShape, + openEditor, + pickAnnotation, + scrollToTop, + seedDraft, + shownPoints, + toolButton, +} from './editor' + +const RUNS = 3 +const CPU_RATE = 4 +const BUDGET_MS = 500 +/** A commit not rendered within this is reported as a failure, not as a figure. */ +const GIVE_UP_MS = 5_000 +/** Past the press machine's double-click window (400 ms): a press repeated at the same place + * sooner would open the properties dialog instead of acting. */ +const APART_MS = 600 + +export type Gesture = 'drag' | 'insert' | 'remove' + +export interface CommitFigures { + gesture: Gesture + /** One latency per run, ms, in run order. */ + runs: number[] + median: number + max: number + budget: number +} + +const undoName = fr['editor.history.undo_title'] +const undoButton = (page: Page) => + page.getByTestId('history-buttons').getByRole('button', { name: undoName }) +const handle = (page: Page, name: string) => + page.getByRole('button', { name, exact: true }) + +/** Arms a capture listener for the next mouse `pointerup`; resolves with the latency. */ +function armCommit(page: Page, stored: string) { + return page.evaluate( + ({ stored, undoName, giveUp }) => { + const scope = window as unknown as { __commit: Promise } + const committed = () => { + const shape = document.querySelector( + '[data-testid="overlay-selection"] polygon', + ) + const undo = document.querySelector( + `[data-testid="history-buttons"] button[aria-label="${undoName}"]`, + ) + return ( + shape !== null && + shape.getAttribute('points') !== stored && + document.querySelector('[data-testid="overlay-old-place"]') === + null && + undo?.getAttribute('aria-disabled') === 'false' + ) + } + scope.__commit = new Promise((done, fail) => { + const onUp = (event: PointerEvent) => { + if (event.pointerType !== 'mouse') return + document.removeEventListener('pointerup', onUp, true) + const start = event.timeStamp + const frame = () => + requestAnimationFrame(() => { + if (performance.now() - start > giveUp) + return fail(new Error('commit not rendered')) + if (!committed()) return frame() + // The message runs after this frame's rendering: style, layout and paint. + const channel = new MessageChannel() + channel.port1.onmessage = () => done(performance.now() - start) + channel.port2.postMessage(0) + }) + frame() + } + document.addEventListener('pointerup', onUp, true) + }) + }, + { stored, undoName, giveUp: GIVE_UP_MS }, + ) +} + +const latency = (page: Page) => + page.evaluate( + () => (window as unknown as { __commit: Promise }).__commit, + ) + +async function pressHandle(page: Page, name: string) { + const box = (await handle(page, name).locator('[data-handle]').boundingBox())! + await page.mouse.click(box.x + box.width / 2, box.y + box.height / 2) +} + +/** The vertex drag of editor-zones: vertex 2 of « Thoraciques », 61 drawing units down. */ +async function dragVertex(page: Page) { + const start = await clientOf(page, 1103, 558.9) + const end = await clientOf(page, 1103, 620) + await page.mouse.move(start.x, start.y) + await page.mouse.down() + await page.mouse.move(end.x, end.y, { steps: 6 }) + await page.mouse.up() +} + +/** Vertex 3 chosen, the « − » shown: setup, outside the measured window. */ +async function chooseVertex3(page: Page) { + const detail = page.getByTestId('edit-strip-detail') + if ((await detail.textContent())?.includes('sommet 3 choisi')) return + await pressHandle(page, 'Sommet 3') + await expect(detail).toContainText('sommet 3 choisi') +} + +const GESTURES: Record Promise> = { + drag: dragVertex, + insert: (page) => pressHandle(page, 'Ajouter un sommet après le sommet 2'), + remove: (page) => pressHandle(page, 'Retirer le sommet 3'), +} + +/** Back to the stored zone with an empty undo stack, checked. */ +async function undoBack(page: Page, stored: number[][]) { + await page.keyboard.press('Control+z') + await expect.poll(() => shownPoints(page)).toEqual(stored) + await expect(undoButton(page)).toHaveAttribute('aria-disabled', 'true') +} + +/** One measured gesture, CPU × 4 from its start to its rendered commit. */ +async function measureOnce( + page: Page, + context: BrowserContext, + gesture: Gesture, +) { + await page.waitForTimeout(APART_MS) + if (gesture === 'remove') await chooseVertex3(page) + await page.waitForTimeout(APART_MS) + const stored = await page + .getByTestId('overlay-selection') + .locator('polygon') + .getAttribute('points') + const cdp = await context.newCDPSession(page) + await armCommit(page, stored ?? '') + await cdp.send('Emulation.setCPUThrottlingRate', { rate: CPU_RATE }) + try { + await GESTURES[gesture](page) + return await latency(page) + } finally { + await cdp.send('Emulation.setCPUThrottlingRate', { rate: 1 }) + } +} + +function figures(gesture: Gesture, runs: number[]): CommitFigures { + const sorted = [...runs].sort((a, b) => a - b) + const round = (v: number) => Math.round(v * 10) / 10 + return { + gesture, + runs: runs.map(round), + median: round(sorted[Math.floor(sorted.length / 2)]), + max: round(sorted[sorted.length - 1]), + budget: BUDGET_MS, + } +} + +/** Seeds its own equine-sized draft, opens it, selects the zone, measures each gesture. */ +export async function measureCommits( + page: Page, + context: BrowserContext, +): Promise { + await page.setViewportSize({ width: 1280, height: 800 }) + const actor = await editorActor('fr') + const seed = await seedDraft(actor, { + drawing: equineDrawing(), + shape: equineShape, + }) + await openEditor(page, fr, actor, seed.sheetId) + await expect(toolButton(page, fr, 'point')).toBeEnabled() + await pickAnnotation(page, fr, /Vertèbres thoraciques/) + await scrollToTop(page) + await expect(undoButton(page)).toHaveAttribute('aria-disabled', 'true') + const stored = await shownPoints(page) + const result: CommitFigures[] = [] + for (const gesture of Object.keys(GESTURES) as Gesture[]) { + const runs: number[] = [] + for (let run = 0; run < RUNS; run++) { + runs.push(await measureOnce(page, context, gesture)) + await undoBack(page, stored) + } + result.push(figures(gesture, runs)) + } + return result +} diff --git a/apps/app/e2e/support/measure-preview.ts b/apps/app/e2e/support/measure-preview.ts index c94be988..bd2e5def 100644 --- a/apps/app/e2e/support/measure-preview.ts +++ b/apps/app/e2e/support/measure-preview.ts @@ -1,6 +1,7 @@ /** - * The production build of the hand-run measure (support/measure-drag.ts), opt-in with - * MEASURE_DRAG=1 MEASURE_BUILD=preview: the app built with the same sandbox environment + * The production build of the hand-run measures (support/measure-drag.ts and + * support/measure-commit.ts), opt-in with MEASURE_DRAG=1 or MEASURE_COMMIT=1 and + * MEASURE_BUILD=preview: the app built with the same sandbox environment * the shell config gives its dev server (the fake Supabase origin, the sandbox anon key), * written under the git-ignored e2e/test-results, then served by `vite preview` on a * loopback port of its own (`--strictPort`, never reused). No suite and no Docker config @@ -16,7 +17,8 @@ const OUT = join(APP, 'e2e/test-results/measure-build') const VITE = join(APP, 'node_modules/.bin/vite') export const previewMeasure = () => - process.env.MEASURE_DRAG === '1' && process.env.MEASURE_BUILD === 'preview' + (process.env.MEASURE_DRAG === '1' || process.env.MEASURE_COMMIT === '1') && + process.env.MEASURE_BUILD === 'preview' function viteEnv() { const anonKey = process.env.SANDBOX_ANON_KEY