Files
faunosteo/apps/app/tests/unit/editor-point-edit.test.tsx
T
bchanot 44de465c6d fix(editor): no silent move loss, no empty commit, focus kept
- « Toutes les annotations » in the properties panel asks the drawing
  guard before it drops a started move (R-12); tests also cover a tool
  and « Ajouter un point ».
- A keyboard series back on its place, or a slider pinned at its stop,
  commits nothing.
- Escape that deselects from the handle or the slider moves the focus to
  the row (or the drawing) instead of losing it.
- A link circle keeps its handle, named « Accès {nom} », with no slider
  and no move mode (no label for it in REVUE).
- The move-mode drop notice waits until the body is shown (R-14).
- No layout shift: the strip's lead has a fixed width, the commands and
  the message pill get a new box per set or message.
- Doc: hit shapes use pointer-events auto; the fine « − » bubble is
  board 05's size.
- Tests: R-14 flip, A5 through the Point tool click, the repair e2e
  wrapped in expectNoShift.
2026-11-17 19:08:08 +01:00

589 lines
21 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.
/** U2 Task 4: the point handle, the size slider and the move mode in the Annotations tab. */
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 } from '@faunosteo/core-wasm/types'
import AnnotationsTab from '@/components/editor/tabs/AnnotationsTab'
import { useAnnotationWork, resetWork } from '@/editor/annotations/workStore'
import { useLocaleStore } from '@/state/localeStore'
import { useUndoShortcuts } from '@/editor/useUndoShortcuts'
import {
ASSET,
drawnText,
insert,
point,
svgBytes,
} from '../helpers/editorFixtures'
import {
USER,
VERSION,
openReady,
pressAt,
resetStore,
sb,
store,
versionAnswer,
} from '../helpers/editorHarness'
vi.mock('@/lib/supabase', () => ({ getSupabaseClient: () => ({}) }))
vi.mock('@faunosteo/supabase', async (orig) => ({
...(await orig<typeof import('@faunosteo/supabase')>()),
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(),
}
function UndoKeys() {
useUndoShortcuts(true)
return null
}
const annotations = () => store.getState().previewState!.document.annotations
const geometryOf = (id: string) =>
annotations().find((a: Annotation) => a.id === id)?.geometry
const depth = () => store.getState().undo.length
/** A change made elsewhere: the other point `b` moved through the store. */
const moveOther = () =>
store.getState().commit({
op: 'update',
key: { kind: 'annotation', id: 'b' },
element: point('b', 30, 30, 5),
})
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()
})
/** A link circle on the main view, opening the detail view `view-det` it adds first. */
const withLink = (text: string, id: string) => {
const withView = insert(text, {
kind: 'view',
value: {
kind: 'detail',
id: 'view-det',
name: { fr: 'Tête', en: 'Head' },
svg_asset_id: null,
view_box: { min_x: 10, min_y: 10, width: 80, height: 80 },
parent_view_id: 'view-main',
},
})
const value = {
id,
view_id: 'view-main',
category_id: 'cat-a',
laterality: 'unpaired',
geometry: { type: 'circle', cx: 70, cy: 70, r: 6 },
review_status: 'to_confirm',
color: null,
kind: 'detail_trigger',
name: null,
opens_view_id: 'view-det',
} as Annotation
return insert(withView, { kind: 'annotation', value })
}
const withPoints = (...points: ReturnType<typeof point>[]) =>
points.reduce((text, p) => insert(text, p), drawnText())
const tab = (readOnly = false) => (
// The Escape listener acts inside the editor workspace only.
<div data-editor-workspace>
<AnnotationsTab {...props} readOnly={readOnly} />
</div>
)
async function mount(text: string) {
await openReady({ text })
const view = render(tab())
await waitFor(() =>
expect(
screen.getByTestId('annotation-canvas-drawing').querySelector('svg'),
).not.toBeNull(),
)
return view
}
/** The same tab on a 4000 × 3000 drawing: the core gives the main view a minimum radius. */
async function mountLarge(text: string) {
const drawing = svgBytes('0 0 4000 3000')
sb.getSheetVersion.mockResolvedValue({
...versionAnswer(text, 3),
drawings: [{ asset_id: ASSET, sha256: drawing.sha256 }],
} as never)
sb.downloadSheetDrawing.mockResolvedValue(drawing.bytes)
store.getState().open(VERSION, USER, { canEdit: true })
await waitFor(() => expect(store.getState().status).toBe('ready'))
// The Escape listener acts inside the editor workspace only.
render(
<div data-editor-workspace>
<AnnotationsTab {...props} />
</div>,
)
await waitFor(() =>
expect(
screen.getByTestId('annotation-canvas-drawing').querySelector('svg'),
).not.toBeNull(),
)
}
async function select(index = 0) {
const list = await screen.findByRole('list', {
name: 'Annotations de la vue',
})
await userEvent.setup().click(within(list).getAllByRole('button')[index])
return screen.findByTestId('annotation-properties')
}
const slider = () =>
within(screen.getByTestId('size-slider')).getByRole('slider')
const handle = () => screen.getByRole('button', { name: 'Point N' })
const message = () => screen.getByTestId('gesture-line')
const moveButton = () =>
screen.getByRole('button', { name: 'Déplacer le point' })
const bar = () => screen.getByTestId('move-mode-bar')
const arrow = (name: string) =>
within(bar()).getByRole('button', { name: `Déplacer vers ${name}` })
describe('the size slider (R-10)', () => {
it('shows the view stops and the red mark at the core value', async () => {
await mountLarge(
withPoints(
point('a', 500, 500, 30),
point('b', 900, 500, 34),
point('c', 1500, 500, 34),
),
)
await select()
const minimum = store.getState().previewState!.min_point_radius['view-main']
expect(minimum).toBeGreaterThan(0)
expect(screen.getAllByTestId('size-stop')).toHaveLength(2)
expect(screen.getByTestId('size-minimum')).toHaveAttribute(
'data-value',
String(minimum),
)
const low = Math.round((Math.min(30, minimum) / 2) * 100) / 100
expect(slider()).toHaveAttribute('min', String(low))
expect(slider()).toHaveAttribute('max', '68')
})
it('has no red mark where the core gives no minimum', async () => {
await mount(withPoints(point('a', 50, 50, 5)))
await select()
expect(screen.queryByTestId('size-minimum')).toBeNull()
})
it('arrows and Page Up preview only; leaving the slider commits once', async () => {
await mount(withPoints(point('a', 50, 50, 5), point('b', 20, 20, 8)))
await select()
const before = depth()
fireEvent.keyDown(slider(), { key: 'ArrowRight' })
fireEvent.keyDown(slider(), { key: 'ArrowRight' })
expect(geometryOf('a')).toMatchObject({ r: 5 })
fireEvent.keyDown(slider(), { key: 'PageUp' })
expect(slider()).toHaveValue('8')
expect(depth()).toBe(before)
fireEvent.blur(slider())
expect(geometryOf('a')).toMatchObject({ r: 8 })
expect(depth()).toBe(before + 1)
expect(message()).toHaveTextContent('Taille modifiée')
})
it('a track click commits once at the release (WCAG 2.5.7)', async () => {
await mount(withPoints(point('a', 50, 50, 5)))
await select()
const before = depth()
fireEvent.change(slider(), { target: { value: '7' } })
expect(geometryOf('a')).toMatchObject({ r: 5 })
fireEvent.pointerUp(slider())
fireEvent.blur(slider())
expect(geometryOf('a')).toMatchObject({ r: 7 })
expect(depth()).toBe(before + 1)
})
it('Ctrl+Z on the slider cancels its preview, then undoes (R-13)', async () => {
await mount(withPoints(point('a', 50, 50, 5)))
render(<UndoKeys />)
await select()
fireEvent.change(slider(), { target: { value: '7' } })
fireEvent.keyDown(slider(), { key: 'z', ctrlKey: true })
expect(useAnnotationWork.getState().preview).toBeNull()
expect(slider()).toHaveValue('5')
fireEvent.change(slider(), { target: { value: '7' } })
fireEvent.pointerUp(slider())
expect(geometryOf('a')).toMatchObject({ r: 7 })
fireEvent.keyDown(slider(), { key: 'z', ctrlKey: true })
expect(geometryOf('a')).toMatchObject({ r: 5 })
})
it('a value under the mark is refused by the core and restored in red', async () => {
await mountLarge(withPoints(point('a', 500, 500, 30)))
await select()
const before = depth()
fireEvent.change(slider(), { target: { value: '8' } })
fireEvent.pointerUp(slider())
expect(geometryOf('a')).toMatchObject({ r: 30 })
expect(depth()).toBe(before)
expect(slider()).toHaveValue('30')
expect(within(message()).getByRole('alert')).toHaveTextContent(
'Taille refusée',
)
expect(screen.getByTestId('overlay-refused-place')).toBeInTheDocument()
})
})
describe('the move mode (A11, R-12, R-13)', () => {
const two = () => withPoints(point('a', 50, 50, 5), point('b', 20, 20, 5))
it('its arrows commit nothing until « Valider la position », which commits once', async () => {
await mount(two())
await select()
const before = depth()
await userEvent.setup().click(moveButton())
expect(document.activeElement).toBe(arrow('la gauche'))
expect(screen.getByTestId('edit-strip-title')).toHaveTextContent(
'Déplacer le point',
)
fireEvent.click(arrow('la droite'))
fireEvent.click(arrow('la droite'))
fireEvent.click(arrow('le bas'))
expect(geometryOf('a')).toMatchObject({ cx: 50, cy: 50 })
expect(screen.getByTestId('overlay-old-place')).toBeInTheDocument()
expect(depth()).toBe(before)
fireEvent.click(
within(bar()).getByRole('button', { name: 'Valider la position' }),
)
expect(geometryOf('a')).toMatchObject({ cx: 51, cy: 50.5 })
expect(depth()).toBe(before + 1)
expect(message()).toHaveTextContent('Point déplacé')
expect(screen.queryByTestId('move-mode-bar')).toBeNull()
})
it('« Annuler » restores the place, the focus back on « Déplacer le point »', async () => {
await mount(two())
await select()
await userEvent.setup().click(moveButton())
fireEvent.click(arrow('la droite'))
fireEvent.click(within(bar()).getByRole('button', { name: 'Annuler' }))
expect(useAnnotationWork.getState()).toMatchObject({
mode: null,
preview: null,
})
expect(geometryOf('a')).toMatchObject({ cx: 50 })
await waitFor(() => expect(document.activeElement).toBe(moveButton()))
})
it('Escape restores the place', async () => {
await mount(two())
await select()
await userEvent.setup().click(moveButton())
fireEvent.click(arrow('la droite'))
fireEvent.keyDown(arrow('la droite'), { key: 'Escape' })
expect(useAnnotationWork.getState().mode).toBeNull()
expect(screen.queryByTestId('move-mode-bar')).toBeNull()
expect(screen.getByTestId('annotation-properties')).toBeInTheDocument()
})
it('a click on another annotation selects nothing during the mode', async () => {
await mount(two())
await select()
await userEvent.setup().click(moveButton())
const other = screen
.getByTestId('annotation-canvas-drawing')
.querySelector('[data-anno-id="b"]')!
const init = {
pointerId: 1,
isPrimary: true,
button: 0,
buttons: 1,
pointerType: 'mouse',
}
fireEvent.pointerDown(other, { ...init, clientX: 20, clientY: 20 })
fireEvent.pointerUp(other, { ...init, clientX: 20, clientY: 20 })
expect(useAnnotationWork.getState().mode?.annotationId).toBe('a')
expect(screen.getByTestId('move-mode-bar')).toBeInTheDocument()
})
it('a drag anywhere moves the mode preview and commits nothing', async () => {
await mount(two())
await select()
await userEvent.setup().click(moveButton())
const box = screen.getByTestId('annotation-canvas')
const init = {
pointerId: 1,
isPrimary: true,
button: 0,
buttons: 1,
pointerType: 'touch',
}
fireEvent.pointerDown(box, { ...init, clientX: 80, clientY: 80 })
fireEvent.pointerMove(box, { ...init, clientX: 92, clientY: 80 })
fireEvent.pointerUp(box, { ...init, clientX: 92, clientY: 80 })
expect(useAnnotationWork.getState().preview?.geometry).toMatchObject({
cx: 62,
})
expect(geometryOf('a')).toMatchObject({ cx: 50 })
expect(useAnnotationWork.getState().mode?.started).toBe(true)
})
it('Ctrl+Z only cancels it; a later Enter on the handle commits nothing', async () => {
await mount(two())
render(<UndoKeys />)
await select()
await userEvent.setup().click(moveButton())
fireEvent.click(arrow('la droite'))
const before = depth()
fireEvent.keyDown(document.body, { key: 'z', ctrlKey: true })
expect(useAnnotationWork.getState()).toMatchObject({
mode: null,
preview: null,
})
expect(depth()).toBe(before)
fireEvent.keyDown(handle(), { key: 'Enter' })
expect(geometryOf('a')).toMatchObject({ cx: 50 })
expect(depth()).toBe(before)
})
it('a document change during the mode drops it and says so', async () => {
await mount(two())
await select()
await userEvent.setup().click(moveButton())
fireEvent.click(arrow('la droite'))
act(() => void moveOther())
expect(useAnnotationWork.getState().mode).toBeNull()
expect(
await screen.findByText('Déplacement abandonné : la fiche a changé.'),
).toBeInTheDocument()
expect(screen.queryByTestId('move-mode-bar')).toBeNull()
})
})
describe('the point handle (R-9, R-11)', () => {
it('is a focusable button outside any aria-hidden subtree, named « Point {nom} »', async () => {
await mount(withPoints(point('a', 50, 50, 5)))
await select()
expect(handle()).toHaveAttribute('tabindex', '0')
expect(handle().closest('[aria-hidden="true"]')).toBeNull()
})
it('arrows move a preview only (no pan), Enter commits once; Shift ×10', async () => {
await mount(withPoints(point('a', 50, 50, 5)))
await select()
const before = depth()
const transform = () =>
(screen.getByTestId('annotation-canvas').firstElementChild as HTMLElement)
.style.transform
const panned = transform()
handle().focus()
fireEvent.keyDown(handle(), { key: 'ArrowRight' })
fireEvent.keyDown(handle(), { key: 'ArrowDown', shiftKey: true })
expect(transform()).toBe(panned)
expect(geometryOf('a')).toMatchObject({ cx: 50, cy: 50 })
expect(depth()).toBe(before)
fireEvent.keyDown(handle(), { key: 'Enter' })
expect(geometryOf('a')).toMatchObject({ cx: 50.5, cy: 55 })
expect(depth()).toBe(before + 1)
})
it('leaving the handle commits the series (BDR-050); Escape drops it', async () => {
await mount(withPoints(point('a', 50, 50, 5)))
await select()
fireEvent.keyDown(handle(), { key: 'ArrowLeft' })
fireEvent.keyDown(handle(), { key: 'Escape' })
expect(useAnnotationWork.getState().preview).toBeNull()
fireEvent.blur(handle())
expect(geometryOf('a')).toMatchObject({ cx: 50 })
fireEvent.keyDown(handle(), { key: 'ArrowLeft' })
fireEvent.blur(handle())
expect(geometryOf('a')).toMatchObject({ cx: 49.5 })
})
it('a stale series never commits (R-13)', async () => {
await mount(withPoints(point('a', 50, 50, 5), point('b', 20, 20, 5)))
await select()
fireEvent.keyDown(handle(), { key: 'ArrowLeft' })
act(() => void moveOther())
fireEvent.keyDown(handle(), { key: 'Enter' })
expect(geometryOf('a')).toMatchObject({ cx: 50 })
})
})
describe('A5: a new point takes the most used size', () => {
const sized = () =>
withPoints(
point('a', 20, 20, 7),
point('b', 40, 40, 7),
point('c', 60, 60, 9),
)
it('the keyboard point and the Point tool click', async () => {
await mount(sized())
const user = userEvent.setup()
await user.click(screen.getByRole('button', { name: 'Ajouter un point' }))
const panel = await screen.findByTestId('annotation-creation')
expect(within(panel).getByLabelText('Rayon')).toHaveValue('7')
})
it('the Point tool shows the slider at that size; a new size feeds the click', async () => {
await mount(sized())
const user = userEvent.setup()
await user.click(screen.getByRole('button', { name: 'Point' }))
expect(slider()).toHaveValue('7')
fireEvent.change(slider(), { target: { value: '9' } })
expect(slider()).toHaveValue('9')
})
})
describe('review fixes: no silent loss, no empty commit, no lost focus', () => {
const two = () => withPoints(point('a', 50, 50, 5), point('b', 20, 20, 5))
const started = async () => {
const view = await mount(two())
await select()
await userEvent.setup().click(moveButton())
fireEvent.click(arrow('la droite'))
return view
}
const question = () => useAnnotationWork.getState().question?.work ?? null
it('« Tout afficher », a tool and « Ajouter un point » ask before a started move goes', async () => {
await started()
const panel = screen.getByTestId('annotation-properties')
fireEvent.click(
within(panel).getByRole('button', { name: 'Toutes les annotations' }),
)
expect(question()).toBe('move')
expect(useAnnotationWork.getState().mode?.started).toBe(true)
expect(screen.getByTestId('annotation-properties')).toBeInTheDocument()
act(() => useAnnotationWork.setState({ question: null }))
fireEvent.click(screen.getByRole('button', { name: 'Point' }))
expect(question()).toBe('move')
expect(screen.getByTestId('move-mode-bar')).toBeInTheDocument()
act(() => useAnnotationWork.setState({ question: null }))
fireEvent.click(screen.getByRole('button', { name: 'Ajouter un point' }))
expect(question()).toBe('move')
expect(screen.queryByTestId('annotation-creation')).toBeNull()
})
it('an unstarted mode ends at once on a tool change', async () => {
await mount(two())
await select()
await userEvent.setup().click(moveButton())
fireEvent.click(screen.getByRole('button', { name: 'Point' }))
expect(question()).toBeNull()
expect(useAnnotationWork.getState().mode).toBeNull()
})
it('a series back on its place and a slider pinned at its stop commit nothing', async () => {
await mount(two())
await select()
const before = depth()
fireEvent.keyDown(handle(), { key: 'ArrowRight' })
fireEvent.keyDown(handle(), { key: 'ArrowLeft' })
fireEvent.keyDown(handle(), { key: 'Enter' })
fireEvent.keyDown(slider(), { key: 'PageUp' })
fireEvent.blur(slider())
expect(depth()).toBe(before)
expect(message()).not.toHaveTextContent('Défaire')
})
it('Escape that deselects from the handle or the slider keeps the focus on the page', async () => {
await mount(two())
await select()
handle().focus()
fireEvent.keyDown(handle(), { key: 'Escape' })
expect(screen.queryByTestId('annotation-properties')).toBeNull()
expect(document.activeElement).not.toBe(document.body)
await select()
slider().focus()
fireEvent.keyDown(slider(), { key: 'Escape' })
expect(screen.queryByTestId('annotation-properties')).toBeNull()
expect(document.activeElement).not.toBe(document.body)
})
it('R-14: a read-only flip suspends a started mode; it resumes and commits after', async () => {
const view = await started()
const before = depth()
view.rerender(tab(true))
expect(useAnnotationWork.getState().mode?.started).toBe(true)
view.rerender(tab(false))
expect(screen.getByTestId('move-mode-bar')).toBeInTheDocument()
fireEvent.click(
within(bar()).getByRole('button', { name: 'Valider la position' }),
)
expect(geometryOf('a')).toMatchObject({ cx: 50.5 })
expect(depth()).toBe(before + 1)
})
it('a link circle keeps its handle (« Accès … ») but gets no size and no move mode', async () => {
await mount(withLink(two(), 'l'))
await select(2)
expect(screen.getByTestId('point-handle')).toHaveAccessibleName(/^Accès /)
expect(screen.queryByTestId('size-slider')).toBeNull()
expect(
screen.queryByRole('button', { name: 'Déplacer le point' }),
).toBeNull()
})
})
describe('A5 through the Point tool click', () => {
const sized = () =>
withPoints(
point('a', 20, 20, 7),
point('b', 40, 40, 7),
point('c', 60, 60, 9),
)
it('a click places the point at the most used size, or at the size the slider chose', async () => {
await mount(sized())
const user = userEvent.setup()
await user.click(screen.getByRole('button', { name: 'Point' }))
pressAt(screen.getByTestId('annotation-canvas'), 50, 80)
const panel = await screen.findByTestId('annotation-creation')
expect(within(panel).getByLabelText('Rayon')).toHaveValue('7')
})
it('a size chosen on the slider before the click feeds it', async () => {
await mount(sized())
const user = userEvent.setup()
await user.click(screen.getByRole('button', { name: 'Point' }))
fireEvent.change(slider(), { target: { value: '9' } })
pressAt(screen.getByTestId('annotation-canvas'), 50, 80)
const panel = await screen.findByTestId('annotation-creation')
expect(within(panel).getByLabelText('Rayon')).toHaveValue('9')
})
})