Files
faunosteo/apps/app/e2e/support/measure-drag.ts
T
bchanot 85231a2680 feat(editor): two-finger pan, touch hint, loupe, touch-sized handles
useFraming gains twoPointerPan (the editor's pinch also pans by the
midpoint and scales from the spread since the pinch began; the
catalogue passes nothing). A futile one-finger drag shows the R-1
hint unless two fingers already worked in this editor visit (work
store flag). A finger drag of a handle shows the loupe (technique A:
the drawing copied once, data-anno-id stripped, outline on top).
editor-touch.spec covers 1 024, 1 180 and 1 280 px with CDP touch.
measure-drag.ts is the hand-run smoothness measure (MEASURE_DRAG=1).
equineShape moved from editor-zones.spec to support/editor.ts, no
assertion changed.
2026-11-18 10:54:46 +01:00

185 lines
6.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* The smoothness measure of U2 Task 8 (plan Task 8 Step 1), run by hand, never by a suite: a
* timing assertion would flake on the shared sandbox. Chromium touch at 1 180 px, CPU × 4, a
* 2 s drag of a vertex of « Vertèbres thoraciques » on 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). It prints the median and the p95, in ms, from each
* pointermove to the end of the next frame (after its style, layout and paint), loupe
* included; the same drag with a mouse (no loupe) is the control.
*
* Run: MEASURE_DRAG=1 pnpm db:sandbox e2e-shell editor-measure-drag.spec.ts
*/
import type { BrowserContext, Page } from '@playwright/test'
import { fr } from '@/i18n/fr'
import { expect } from './browser'
import {
editorActor,
equineDrawing,
equineShape,
openEditor,
pickAnnotation,
seedDraft,
toolButton,
} from './editor'
const DURATION_MS = 2_000
const STEP_MS = 16
const RADIUS_PX = 40
const CPU_RATE = 4
export interface DragFigures {
kind: 'touch' | 'mouse'
samples: number
/** Samples whose frame showed the loupe. */
withLoupe: number
/** From the event's own time stamp (input queueing included) to the frame's end. */
fromEvent: { median: number; p95: number }
/** From the first listener of the event to the frame's end: the frame's own work. */
fromHandler: { median: number; p95: number }
}
interface Sample {
fromEvent: number
fromHandler: number
/** The loupe was on screen at the frame's end. */
loupe: boolean
}
/** A capture listener on the document: it runs before the application's own. */
function startRecording(page: Page, kind: string) {
return page.evaluate((pointerType) => {
const scope = window as unknown as { __drag: Sample[] }
scope.__drag = []
const record = (event: PointerEvent) => {
if (event.pointerType !== pointerType || event.buttons === 0) return
const start = performance.now()
requestAnimationFrame(() => {
// The message runs after this frame's rendering: style, layout and paint.
const channel = new MessageChannel()
channel.port1.onmessage = () => {
const end = performance.now()
scope.__drag.push({
fromEvent: end - event.timeStamp,
fromHandler: end - start,
loupe: document.querySelector('[data-testid="loupe"]') !== null,
})
}
channel.port2.postMessage(0)
})
}
document.addEventListener('pointermove', record, true)
}, kind)
}
const percentile = (sorted: number[], p: number) =>
sorted[Math.min(sorted.length - 1, Math.floor((sorted.length - 1) * p))]
function summary(values: number[]) {
const sorted = [...values].sort((a, b) => a - b)
const round = (v: number) => Math.round(v * 10) / 10
return {
median: round(percentile(sorted, 0.5)),
p95: round(percentile(sorted, 0.95)),
}
}
async function figures(page: Page, kind: DragFigures['kind']) {
const samples = await page.evaluate(
() => (window as unknown as { __drag: Sample[] }).__drag,
)
return {
kind,
samples: samples.length,
withLoupe: samples.filter((s) => s.loupe).length,
fromEvent: summary(samples.map((s) => s.fromEvent)),
fromHandler: summary(samples.map((s) => s.fromHandler)),
}
}
/** The point of the circle the drag follows, `elapsed` ms after its start. */
function pathAt(from: { x: number; y: number }, elapsed: number) {
const angle = (2 * Math.PI * elapsed) / DURATION_MS
return {
x: from.x + RADIUS_PX * Math.sin(angle),
y: from.y - RADIUS_PX * (1 - Math.cos(angle)),
}
}
type Mover = (at: { x: number; y: number }) => Promise<unknown>
/** Moves along the path for DURATION_MS, one move per STEP_MS at most. */
async function follow(from: { x: number; y: number }, moveTo: Mover) {
const start = Date.now()
for (let elapsed = 0; elapsed < DURATION_MS; elapsed = Date.now() - start) {
await moveTo(pathAt(from, elapsed))
await new Promise((done) => setTimeout(done, STEP_MS))
}
}
async function vertexCentre(page: Page) {
const box = (await page
.getByRole('button', { name: 'Sommet 1', exact: true })
.locator('[data-handle]')
.boundingBox())!
return { x: box.x + box.width / 2, y: box.y + box.height / 2 }
}
async function touchDrag(page: Page, context: BrowserContext) {
const cdp = await context.newCDPSession(page)
const from = await vertexCentre(page)
const send = (type: string, points: { x: number; y: number }[]) =>
cdp.send('Input.dispatchTouchEvent', {
type: type as 'touchStart',
touchPoints: points.map((p) => ({ ...p, id: 0 })),
})
await send('touchStart', [from])
await follow(from, (at) => send('touchMove', [at]))
await send('touchEnd', [])
}
async function mouseDrag(page: Page) {
const from = await vertexCentre(page)
await page.mouse.move(from.x, from.y)
await page.mouse.down()
await follow(from, (at) => page.mouse.move(at.x, at.y))
await page.mouse.up()
}
/** One measured drag of the same vertex, by `kind`, CPU × 4 while it runs. */
async function measure(
page: Page,
context: BrowserContext,
kind: DragFigures['kind'],
): Promise<DragFigures> {
const cdp = await context.newCDPSession(page)
await page.getByTestId('annotation-canvas').scrollIntoViewIfNeeded()
await startRecording(page, kind)
await cdp.send('Emulation.setCPUThrottlingRate', { rate: CPU_RATE })
try {
if (kind === 'touch') await touchDrag(page, context)
else await mouseDrag(page)
} finally {
await cdp.send('Emulation.setCPUThrottlingRate', { rate: 1 })
}
return figures(page, kind)
}
/** Seeds its own equine-sized draft, opens it, selects the zone, measures touch then mouse. */
export async function measureDrag(
page: Page,
context: BrowserContext,
): Promise<DragFigures[]> {
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/)
const touch = await measure(page, context, 'touch')
// The drop committed the vertex; the mouse drags the same vertex from there.
const mouse = await measure(page, context, 'mouse')
return [touch, mouse]
}