import { expect, test } from '../support/browser' import type { Page } from '@playwright/test' import probe from '../fixtures/interior-probe.json' // Owner brief « V1-2.2 : fermeture du dernier MEDIUM » (2026-11-01): a zone or a trigger drawn // as a polygon or a path must fill a disc of 4 CSS px on the phone preview, its stroke left // out. The core's verdict (e2e/fixtures/interior-probe.json, crates/core/tests/interior_probe.rs) // against the browser's own hit test, in the preview's 286 × 310 box (one unit per CSS px): // an accepted shape takes real presses in the disc the core found and its fill alone holds a // 4 px disc; a refused one fills none. Then the equine draft's zones, in the editor canvas. // Run on Chromium, Firefox and WebKit when installed. type Point = { x: number; y: number } type Disc = { x: number; y: number; r: number } /** The threshold: a disc of 4 CSS px across. */ const MIN_RADIUS_PX = 2 /** The browser's mask is sampled every quarter pixel: its radius is within that of the fill. */ const MASK_STEP_PX = 0.25 async function press(page: Page, at: Point, touch: boolean) { if (touch) await page.touchscreen.tap(at.x, at.y) else await page.mouse.click(at.x, at.y) } async function recorded(page: Page): Promise { const text = (await page.getByTestId('clicks').textContent()) ?? '' return text.split(' ').filter(Boolean) } async function clickRecords(page: Page, at: Point, touch: boolean) { const before = (await recorded(page)).length await press(page, at, touch) await page.waitForTimeout(50) return (await recorded(page)).slice(before) } test.describe('interior clicks', () => { for (const c of probe.cases) { test(`${c.accepted ? 'accepted' : 'refused'}: ${c.name}`, async ({ page, }, info) => { await page.goto(`/e2e/harness/interior.html?case=${c.name}`) await expect(page.locator('[data-anno-id="shape"]')).toBeAttached() const browser = await page.evaluate(widestFill, MASK_STEP_PX) if (c.widest) { // The core never claims more than the browser fills (it may claim less: its disc // crosses no edge, where a nonzero fill may run on past one). expect(browser.r, JSON.stringify(browser)).toBeGreaterThan( c.widest.r - 0.5, ) } if (!c.accepted) { expect(browser.r, JSON.stringify(browser)).toBeLessThan(MIN_RADIUS_PX) return } if (!c.widest) throw new Error(`${c.name} accepted without a disc`) expect(browser.r).toBeGreaterThanOrEqual(MIN_RADIUS_PX) const disc = c.widest for (const fillOnly of [true, false]) { const ring = await page.evaluate(ringHits, { disc, fillOnly }) expect(ring, `fillOnly ${String(fillOnly)}`).toEqual({ missed: [], covered: 0, }) } const centre = await page.evaluate(toScreen, { x: disc.x, y: disc.y }) const touch = info.project.use.hasTouch === true expect(await clickRecords(page, centre, touch)).toEqual(['shape']) }) } test('every equine zone holds the disc the core found and takes a real press', async ({ page, }, info) => { await page.goto('/e2e/harness/canvas.html') await expect( page.locator('[data-testid="anatomy-canvas"] [data-anno-id="caudal"]'), ).toBeAttached() const touch = info.project.use.hasTouch === true for (const zone of probe.equine) { if (!zone.widest) throw new Error(`${zone.id} without a disc`) const disc = zone.widest // A point painted over the zone takes its taps (« the visible wins »): the samples under // a visible disc are left out, at most a quarter of them. const { missed, covered } = await page.evaluate(ringHits, { disc, fillOnly: false, id: zone.id, clearOfDiscs: true, }) expect(missed, zone.id).toEqual([]) expect(covered, zone.id).toBeLessThanOrEqual(8) // Chromium's touch adjustment gives a point the taps up to about 10 px around its disc // (measured 2026-11-01): the real press goes where the zone's fill lies farthest from // every disc, inside its edges. const at = await page.evaluate(clearestFill, { id: zone.id, disc }) expect(await clickRecords(page, at, touch), zone.id).toEqual([zone.id]) } }) }) /** * In the page: the point of the shape's fill, on a half CSS px grid, its own on the hit test and * inside its edges by 2 px or 60 % of the core's disc radius if less (the caudal zone's disc is * 4.95 px across at 320), farthest from every visible disc. */ function clearestFill(args: { id: string; disc: Disc }): Point { const { id, disc } = args const svg = document.querySelector( '[data-testid="anatomy-canvas"] svg', ) as SVGSVGElement const ctm = svg.getScreenCTM() if (!ctm) throw new Error('no CTM') const shape = svg.querySelector( `[data-anno-id="${id}"]`, ) as SVGGeometryElement const twins = ( Array.from( svg.querySelectorAll('circle[pointer-events="visiblePainted"]'), ) as SVGCircleElement[] ).map((t) => ({ centre: new DOMPoint( t.cx.baseVal.value, t.cy.baseVal.value, ).matrixTransform(ctm), r: t.r.baseVal.value * ctm.a, })) const inverse = ctm.inverse() const filled = (x: number, y: number) => shape.isPointInFill(new DOMPoint(x, y).matrixTransform(inverse)) const margin = Math.min(2, 0.6 * disc.r * ctm.a) const box = shape.getBoundingClientRect() let best = { x: 0, y: 0, d: -1 } for (let x = Math.ceil(box.left); x <= box.right; x += 0.5) { for (let y = Math.ceil(box.top); y <= box.bottom; y += 0.5) { const steps = [-margin, 0, margin] const inside = steps.every((dx) => steps.every((dy) => filled(x + dx, y + dy)), ) if (!inside) continue const hit = document.elementFromPoint(x, y)?.closest('[data-anno-id]') if (hit?.getAttribute('data-anno-id') !== id) continue const d = Math.min( ...twins.map((t) => Math.hypot(x - t.centre.x, y - t.centre.y) - t.r), ) if (d > best.d) best = { x, y, d } } } if (best.d < 0) throw new Error(`${id}: no fill clear of the discs`) return { x: best.x, y: best.y } } /** In the page: a point of the layer's user space on screen. */ function toScreen(at: Point): Point { const svg = document.querySelector('svg') as SVGSVGElement const ctm = svg.getScreenCTM() if (!ctm) throw new Error('no CTM') const p = new DOMPoint(at.x, at.y).matrixTransform(ctm) return { x: p.x, y: p.y } } /** * In the page: the browser's hit test at the disc's centre and 32 points at 90 % of its radius. * With `fillOnly` the shape takes `pointer-events="fill"` meanwhile, its stroke out of the test; * with `clearOfDiscs` the points within a visible disc (its stroke and half a pixel included) * are counted apart. Answers the points that hit another element, and the points left out. */ function ringHits(args: { disc: Disc fillOnly: boolean id?: string clearOfDiscs?: boolean }): { missed: string[]; covered: number } { const { disc, fillOnly } = args const id = args.id ?? 'shape' const svg = document.querySelector('svg') as SVGSVGElement const ctm = svg.getScreenCTM() if (!ctm) throw new Error('no CTM') const shapes = Array.from(svg.querySelectorAll(`[data-anno-id="${id}"]`)) const kept = shapes.map((s) => s.getAttribute('pointer-events')) if (fillOnly) shapes.forEach((s) => s.setAttribute('pointer-events', 'fill')) const reach = disc.r * 0.9 const points: Point[] = [{ x: disc.x, y: disc.y }] for (let k = 0; k < 32; k++) { const a = (k * 2 * Math.PI) / 32 points.push({ x: disc.x + reach * Math.cos(a), y: disc.y + reach * Math.sin(a), }) } const twins = Array.from( svg.querySelectorAll('circle[pointer-events="visiblePainted"]'), ) as SVGCircleElement[] const underDisc = (at: Point) => twins.some( (t) => Math.hypot(at.x - t.cx.baseVal.value, at.y - t.cy.baseVal.value) <= t.r.baseVal.value + 0.75 + 0.5 / ctm.a, ) const missed: string[] = [] let covered = 0 for (const at of points) { if (args.clearOfDiscs && underDisc(at)) { covered++ continue } const p = new DOMPoint(at.x, at.y).matrixTransform(ctm) const hit = document .elementFromPoint(p.x, p.y) ?.closest('[data-anno-id]') ?.getAttribute('data-anno-id') if (hit !== id) missed.push(`${at.x.toFixed(2)},${at.y.toFixed(2)}: ${String(hit)}`) } shapes.forEach((s, i) => { const value = kept[i] if (value === null || value === undefined) s.removeAttribute('pointer-events') else s.setAttribute('pointer-events', value) }) return { missed, covered } } /** * In the page: the widest disc of the shape's fill as the browser hits it (`pointer-events` * `fill`, the stroke out), from a mask sampled every `step` CSS px over its box and 3 px around * it, by an exact Euclidean distance transform (Felzenszwalb and Huttenlocher). The radius is * the distance to the nearest sample outside the fill, less half a step. */ function widestFill(step: number): Disc { const svg = document.querySelector('svg') as SVGSVGElement const shape = svg.querySelector( '[data-anno-id="shape"]', ) as SVGGraphicsElement const kept = shape.getAttribute('pointer-events') shape.setAttribute('pointer-events', 'fill') const box = shape.getBoundingClientRect() const x0 = box.left - 3 const y0 = box.top - 3 const w = Math.ceil((box.width + 6) / step) + 1 const h = Math.ceil((box.height + 6) / step) + 1 const far = 1e20 const grid = new Float64Array(w * h) for (let j = 0; j < h; j++) { for (let i = 0; i < w; i++) { const hit = document.elementFromPoint(x0 + i * step, y0 + j * step) grid[j * w + i] = hit === shape ? far : 0 } } if (kept === null) shape.removeAttribute('pointer-events') else shape.setAttribute('pointer-events', kept) const line = (f: Float64Array): Float64Array => { const n = f.length const d = new Float64Array(n) const v = new Int32Array(n) const z = new Float64Array(n + 1) let k = 0 v[0] = 0 z[0] = -Infinity z[1] = Infinity for (let q = 1; q < n; q++) { let s = (f[q] + q * q - (f[v[k]] + v[k] * v[k])) / (2 * q - 2 * v[k]) while (s <= z[k]) { k-- s = (f[q] + q * q - (f[v[k]] + v[k] * v[k])) / (2 * q - 2 * v[k]) } k++ v[k] = q z[k] = s z[k + 1] = Infinity } k = 0 for (let q = 0; q < n; q++) { while (z[k + 1] < q) k++ d[q] = (q - v[k]) ** 2 + f[v[k]] } return d } for (let i = 0; i < w; i++) { const column = new Float64Array(h) for (let j = 0; j < h; j++) column[j] = grid[j * w + i] const done = line(column) for (let j = 0; j < h; j++) grid[j * w + i] = done[j] } let best = { x: 0, y: 0, r: 0 } for (let j = 0; j < h; j++) { const done = line(grid.subarray(j * w, (j + 1) * w).slice()) for (let i = 0; i < w; i++) { const r = Math.sqrt(done[i]) * step - step / 2 if (r > best.r) best = { x: x0 + i * step, y: y0 + j * step, r } } } return best }