/** U2 Task 8: the two-finger hint (R-1) and the loupe of a finger's drag. */ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { act, cleanup, fireEvent, render, screen, waitFor, within, } from '@testing-library/react' import userEvent from '@testing-library/user-event' import type { Annotation, Point } from '@faunosteo/core-wasm/types' import AnnotationsTab from '@/components/editor/tabs/AnnotationsTab' import { resetWork } from '@/editor/annotations/workStore' import { useLocaleStore } from '@/state/localeStore' import { fr } from '@/i18n/fr' import { drawnText, insert, point } from '../helpers/editorFixtures' import { openReady, resetStore } from '../helpers/editorHarness' vi.mock('@/lib/supabase', () => ({ getSupabaseClient: () => ({}) })) vi.mock('@faunosteo/supabase', async (orig) => ({ ...(await orig()), getSheetVersion: vi.fn(), downloadSheetDrawing: vi.fn(), saveSheetDraft: vi.fn(), getOfficialSheetAdmin: vi.fn(), submitSheetReview: vi.fn(), })) const props = { readOnly: false, navigate: vi.fn(), reloadSheet: vi.fn(), section: 'drawing' as const, visited: ['drawing'] as const, onOpenSection: vi.fn(), onBack: vi.fn(), } const HINT = fr['editor.annotations.message.two_fingers'] function stubCtm(): void { const identity = { a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 } Object.defineProperty(window.SVGElement.prototype, 'getScreenCTM', { configurable: true, value: () => ({ ...identity, inverse: () => identity }), }) } beforeEach(() => { useLocaleStore.setState({ locale: 'fr' }) stubCtm() }) afterEach(() => { cleanup() resetStore() resetWork() }) const SQUARE: Point[] = [ [20, 20], [80, 20], [80, 80], [20, 80], ] const squareText = () => insert(drawnText(), { kind: 'annotation', value: { ...(point('z', 0, 0, 1).value as Annotation), kind: 'zone', geometry: { type: 'polygon', points: SQUARE }, } as Annotation, }) const tab = () => (
) async function show() { render(tab()) await waitFor(() => expect( screen.getByTestId('annotation-canvas-drawing').querySelector('svg'), ).not.toBeNull(), ) } async function mount() { await openReady({ text: squareText() }) await show() } async function select() { const list = await screen.findByRole('list', { name: 'Annotations de la vue', }) await userEvent.setup().click(within(list).getAllByRole('button')[0]) await screen.findByRole('button', { name: 'Propriétés' }) } const canvas = () => screen.getByTestId('annotation-canvas') const line = () => screen.getByTestId('gesture-line') const loupe = () => screen.queryByTestId('loupe') const hitOf = (name: string) => screen.getByRole('button', { name }).querySelector('[data-handle]')! const finger = ( pointerId: number, x: number, y: number, pointerType = 'touch', ) => ({ pointerId, isPrimary: pointerId === 1, button: 0, buttons: 1, pointerType, clientX: x, clientY: y, }) /** One pointer pressed at `from`, moved to `to` (past every slop), released there. */ function drag(el: Element, from: Point, to: Point, pointerType = 'touch') { fireEvent.pointerDown(el, finger(1, from[0], from[1], pointerType)) fireEvent.pointerMove(el, finger(1, to[0], to[1], pointerType)) fireEvent.pointerUp(el, { ...finger(1, to[0], to[1], pointerType), buttons: 0, }) } /** Two fingers on the box, the second one spread: the drawing zooms. */ function pinch() { fireEvent.pointerDown(canvas(), finger(1, 10, 10)) fireEvent.pointerDown(canvas(), finger(2, 60, 10)) fireEvent.pointerMove(canvas(), finger(2, 110, 10)) fireEvent.pointerUp(canvas(), { ...finger(2, 110, 10), buttons: 0 }) fireEvent.pointerUp(canvas(), { ...finger(1, 10, 10), buttons: 0 }) } describe('the two-finger hint (R-1)', () => { it('a finger dragged off the handles moves nothing and shows the hint', async () => { await mount() await select() drag(canvas(), [5, 5], [45, 5]) expect(line()).toHaveTextContent(HINT) }) it('a mouse drag pans: no hint', async () => { await mount() await select() drag(canvas(), [5, 5], [45, 5], 'mouse') expect(line()).not.toHaveTextContent(HINT) }) it('never replaces a message that shows', async () => { await mount() await select() drag(hitOf('Sommet 2'), [80, 20], [90, 10], 'mouse') expect(line()).toHaveTextContent('Sommet déplacé') drag(canvas(), [5, 5], [45, 5]) expect(line()).toHaveTextContent('Sommet déplacé') expect(line()).not.toHaveTextContent(HINT) }) it('never shows again once two fingers worked, even after a remount', async () => { await mount() await select() pinch() drag(canvas(), [5, 5], [45, 5]) expect(line()).not.toHaveTextContent(HINT) cleanup() await show() await select() drag(canvas(), [5, 5], [45, 5]) expect(line()).not.toHaveTextContent(HINT) }) }) describe('fix round 1: the two fingers counted in the pinch itself (Minor 2)', () => { it('a pinch whose framing change lands after the lift still suppresses the hint', async () => { await mount() await select() fireEvent.pointerDown(canvas(), finger(1, 10, 10)) fireEvent.pointerDown(canvas(), finger(2, 60, 10)) // One batch: the framing renders, and its effects run, only after both fingers lifted. act(() => { fireEvent.pointerMove(canvas(), finger(2, 110, 10)) fireEvent.pointerUp(canvas(), { ...finger(2, 110, 10), buttons: 0 }) fireEvent.pointerUp(canvas(), { ...finger(1, 10, 10), buttons: 0 }) }) drag(canvas(), [5, 5], [45, 5]) expect(line()).not.toHaveTextContent(HINT) }) }) describe('the loupe of a finger drag', () => { it('copies the drawing without its ids: no id appears twice (Minor 1)', async () => { await mount() await select() // The fixture drawing has no id: give it a few, as a real drawing has. const drawing = screen .getByTestId('annotation-canvas-drawing') .querySelector('svg')! drawing.setAttribute('id', 'drawing-root') drawing.firstElementChild?.setAttribute('id', 'drawing-part') const vertex = hitOf('Sommet 2') fireEvent.pointerDown(vertex, finger(1, 80, 20)) fireEvent.pointerMove(vertex, finger(1, 95, 35)) expect(loupe()).not.toBeNull() const ids = [...document.querySelectorAll('[id]')].map((el) => el.id) expect(ids).toContain('drawing-root') expect(ids.length).toBe(new Set(ids).size) fireEvent.pointerUp(vertex, { ...finger(1, 95, 35), buttons: 0 }) }) it('shows a picture of the drawing above the finger, then goes on the drop', async () => { await mount() await select() const vertex = hitOf('Sommet 2') fireEvent.pointerDown(vertex, finger(1, 80, 20)) fireEvent.pointerMove(vertex, finger(1, 95, 35)) const shown = loupe()! expect(shown).not.toBeNull() expect(shown).toHaveAttribute('aria-hidden', 'true') expect(shown.hasAttribute('inert')).toBe(true) expect(shown.querySelector('svg')).not.toBeNull() expect(shown.querySelector('[data-anno-id]')).toBeNull() expect(shown.querySelector('polygon')).not.toBeNull() fireEvent.pointerUp(vertex, { ...finger(1, 95, 35), buttons: 0 }) expect(loupe()).toBeNull() }) it('goes when the drag is cancelled by a second finger', async () => { await mount() await select() const vertex = hitOf('Sommet 2') fireEvent.pointerDown(vertex, finger(1, 80, 20)) fireEvent.pointerMove(vertex, finger(1, 95, 35)) expect(loupe()).not.toBeNull() fireEvent.pointerDown(canvas(), finger(2, 10, 10)) expect(loupe()).toBeNull() }) it('a mouse drag shows none', async () => { await mount() await select() const vertex = hitOf('Sommet 2') fireEvent.pointerDown(vertex, finger(1, 80, 20, 'mouse')) fireEvent.pointerMove(vertex, finger(1, 95, 35, 'mouse')) expect(loupe()).toBeNull() fireEvent.pointerUp(vertex, { ...finger(1, 95, 35, 'mouse'), buttons: 0 }) }) })