fix(app): annotations tab split, editor-wide undo keys, keyboard point fits, typed radius message

This commit is contained in:
bchanot committed 2026-11-04 14:00:57 +01:00
1 parent 447d0a4272
commit 3f15762d95
25 files changed
+1398 -653

No files matched your search

@@ -277,6 +277,22 @@ describe('a frame change (rule 7)', () => {
expect(store.getState().documentText).toBe(before)
})
it('is refused when it makes a point too_small, nothing kept', async () => {
await openWith(
insert(drawnText(), point('anno-a', 2500, 2500, 400)),
'0 0 5000 5000',
)
const before = store.getState().documentText
const outcome = crop({ min_x: 0, min_y: 0, width: 200000, height: 200000 })
expect(outcome).toMatchObject({ ok: false, reason: 'refused' })
if (outcome.ok || outcome.reason !== 'refused')
throw new Error('not refused')
expect(outcome.refusal.issues.map((i) => i.code)).toContain(
'annotation.geometry.too_small',
)
expect(store.getState().documentText).toBe(before)
})
it('is accepted when only an already-invalid point is affected', async () => {
await openReady({ text: insert(drawnText(), point('anno-a', 150, 50, 5)) })
expect(crop({ min_x: 0, min_y: 0, width: 80, height: 80 }).ok).toBe(true)
@@ -1,6 +1,7 @@
/** Lot H: the Annotations tab (creation, corrected position, gestures, read-only, repair). */
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import {
act,
cleanup,
fireEvent,
render,
@@ -22,6 +23,7 @@ import {
import {
USER,
VERSION,
commitMove,
openReady,
resetStore,
sb,
@@ -693,13 +695,11 @@ describe('polygons and paths', () => {
})
const panel = await selectFromList()
expect(within(panel).queryByText(/M 20 20/)).toBeNull()
await userEvent
.setup()
.click(
within(panel).getByRole('button', {
name: 'Remplacer par un polygone',
}),
)
await userEvent.setup().click(
within(panel).getByRole('button', {
name: 'Remplacer par un polygone',
}),
)
const canvas = screen.getByTestId('annotation-canvas')
triangle.forEach(([x, y]) =>
fireEvent.click(canvas, { clientX: x, clientY: y }),
@@ -714,25 +714,232 @@ describe('polygons and paths', () => {
})
})
describe('keyboard shortcuts', () => {
it('undoes with Ctrl+Z outside a text field and not inside one', async () => {
await mount()
await selectFromList()
const user = userEvent.setup()
const cx = within(
screen.getByTestId('annotation-properties'),
).getByLabelText('Centre x')
await user.clear(cx)
await user.type(cx, '60{Enter}')
fireEvent.keyDown(cx, { key: 'z', ctrlKey: true })
expect(geometryOf('anno-a')).toMatchObject({ cx: 60 })
fireEvent.keyDown(document.body, { key: 'z', ctrlKey: true })
expect(geometryOf('anno-a')).toMatchObject({ cx: 50 })
fireEvent.keyDown(document.body, {
key: 'z',
ctrlKey: true,
shiftKey: true,
describe('keys while tracing (H3)', () => {
const clickAt = (x: number, y: number) =>
fireEvent.click(screen.getByTestId('annotation-canvas'), {
clientX: x,
clientY: y,
})
expect(geometryOf('anno-a')).toMatchObject({ cx: 60 })
async function traced() {
await mount({ text: drawnText() })
await waitFor(() =>
expect(screen.getByRole('button', { name: 'Zone' })).toBeEnabled(),
)
const user = userEvent.setup()
await user.click(screen.getByRole('button', { name: 'Zone' }))
triangle.forEach(([x, y]) => clickAt(x, y))
return user
}
it('Enter on « Annuler le tracé » cancels the tracing instead of closing it', async () => {
const user = await traced()
screen.getByRole('button', { name: 'Annuler le tracé' }).focus()
await user.keyboard('{Enter}')
expect(
screen.queryByRole('button', { name: 'Annuler le tracé' }),
).toBeNull()
expect(screen.queryByTestId('annotation-creation')).toBeNull()
})
it('Enter on a view chip or a list row never closes the tracing', async () => {
await traced()
const views = screen.getByRole('group', { name: 'Vues' })
fireEvent.keyDown(within(views).getAllByRole('button')[0], {
key: 'Enter',
})
expect(screen.queryByTestId('annotation-creation')).toBeNull()
expect(
screen.getByRole('button', { name: 'Annuler le tracé' }),
).toBeInTheDocument()
})
})
describe('a second click with the point tool (H6)', () => {
it('moves the pending point and keeps what was typed', async () => {
await mount({ text: drawnText() })
await waitFor(() =>
expect(screen.getByRole('button', { name: 'Point' })).toBeEnabled(),
)
const user = userEvent.setup()
await user.click(screen.getByRole('button', { name: 'Point' }))
fireEvent.click(screen.getByTestId('annotation-canvas'), {
clientX: 50,
clientY: 50,
})
const panel = await fillCreation(user)
fireEvent.click(screen.getByTestId('annotation-canvas'), {
clientX: 60,
clientY: 40,
})
await waitFor(() =>
expect(within(panel).getByLabelText('Centre x')).toHaveValue('60'),
)
expect(within(panel).getByLabelText('Nom (FR)')).toHaveValue('Nuque')
expect(within(panel).getByLabelText('Latéralité')).toHaveValue('unpaired')
expect(screen.getByTestId('annotation-creation')).toBe(panel)
})
})
describe('a typed radius (H5)', () => {
it('shows the core issue, not a corrected position', async () => {
const drawing = svgBytes('0 0 5000 5000')
sb.getSheetVersion.mockResolvedValue({
...versionAnswer(drawnText(), 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'))
render(<AnnotationsTab {...props} />)
await waitFor(() =>
expect(
screen.getByRole('button', { name: 'Ajouter un point' }),
).toBeEnabled(),
)
const user = userEvent.setup()
await user.click(screen.getByRole('button', { name: 'Ajouter un point' }))
const panel = await fillCreation(user)
const r = within(panel).getByLabelText('Rayon')
await user.clear(r)
await user.type(r, '1{Enter}')
await user.click(within(panel).getByRole('button', { name: 'Créer' }))
expect(screen.queryByTestId('correction-notice')).toBeNull()
expect(within(panel).getByRole('alert')).toBeInTheDocument()
expect(annotations()).toHaveLength(0)
})
})
describe('the frame follows the view (H7)', () => {
it('holds the keyboard point at the centre of the view shown, after a switch', async () => {
const withView = insert(drawnText(), {
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: 20, width: 40, height: 60 },
parent_view_id: 'view-main',
},
})
await mount({ text: withView })
const user = userEvent.setup()
await user.click(screen.getByRole('button', { name: 'Tête' }))
await waitFor(() =>
expect(
screen.getByRole('button', { name: 'Ajouter un point' }),
).toBeEnabled(),
)
await user.click(screen.getByRole('button', { name: 'Ajouter un point' }))
const panel = await screen.findByTestId('annotation-creation')
expect(within(panel).getByLabelText('Centre x')).toHaveValue('30')
expect(within(panel).getByLabelText('Centre y')).toHaveValue('50')
})
})
describe('refusals through the tab (H8)', () => {
const zoneText = (geometry: object) =>
insert(drawnText(), { kind: 'annotation', value: zoneAnnotation(geometry) })
it('a refused « Remplacer par un polygone » drops the tracing, shows the core message', async () => {
const path = { type: 'path', d: 'M 20 20 L 80 20 L 50 80 Z' }
await mount({ text: zoneText(path) })
const panel = await selectFromList()
await userEvent.setup().click(
within(panel).getByRole('button', {
name: 'Remplacer par un polygone',
}),
)
const canvas = screen.getByTestId('annotation-canvas')
;[
[20, 20],
[500, 20],
[50, 80],
].forEach(([x, y]) => fireEvent.click(canvas, { clientX: x, clientY: y }))
fireEvent.keyDown(canvas, { key: 'Enter' })
expect(await screen.findByRole('alert')).toBeInTheDocument()
expect(
screen.queryByRole('button', { name: 'Annuler le tracé' }),
).toBeNull()
expect(geometryOf('zone-a')).toEqual(path)
})
it('a refused vertex add and a refused remove stay « non appliqué », document unchanged', async () => {
await mount({
text: zoneText({ type: 'polygon', points: [...triangle, [20, 60]] }),
})
const panel = await selectFromList()
const user = userEvent.setup()
const before = store.getState().documentText
const x = within(panel).getByLabelText('x du sommet 1')
await user.clear(x)
await user.type(x, '500{Enter}')
await user.click(
within(panel).getByRole('button', { name: 'Ajouter un sommet' }),
)
expect(store.getState().documentText).toBe(before)
expect(within(panel).getAllByLabelText(/^x du sommet/)).toHaveLength(5)
expect(
within(panel).getByText(/Valeurs non appliquées/),
).toBeInTheDocument()
await user.click(within(panel).getByRole('button', { name: 'Retirer' }))
expect(store.getState().documentText).toBe(before)
expect(
within(panel).getByText(/Valeurs non appliquées/),
).toBeInTheDocument()
})
})
describe('the drag and the canvas follow the handle (H8)', () => {
const down = (el: Element, x: number, y: number) =>
fireEvent.pointerDown(el, {
clientX: x,
clientY: y,
button: 0,
pointerId: 1,
})
it('lostpointercapture drops the drag with no command', async () => {
await mount()
await selectFromList()
const hit = marker('anno-a')
const version = store.getState().handleVersion
down(hit, 50, 50)
fireEvent.pointerMove(hit, { clientX: 70, clientY: 50, pointerId: 1 })
fireEvent.lostPointerCapture(hit, { pointerId: 1 })
fireEvent.pointerUp(hit, { clientX: 70, clientY: 50, pointerId: 1 })
expect(store.getState().handleVersion).toBe(version)
})
it('a switch to read-only mid-drag drops it', async () => {
await openReady({ text: text() })
const view = render(<AnnotationsTab {...props} />)
await waitFor(() =>
expect(
screen.getByTestId('annotation-canvas-drawing').querySelector('svg'),
).not.toBeNull(),
)
await selectFromList()
const hit = marker('anno-a')
const version = store.getState().handleVersion
down(hit, 50, 50)
fireEvent.pointerMove(hit, { clientX: 70, clientY: 50, pointerId: 1 })
view.rerender(<AnnotationsTab {...props} readOnly />)
fireEvent.pointerUp(hit, { clientX: 70, clientY: 50, pointerId: 1 })
expect(store.getState().handleVersion).toBe(version)
expect(geometryOf('anno-a')).toEqual({
type: 'circle',
cx: 50,
cy: 50,
r: 5,
})
})
it('rebuilds the drawing when the handle changes under it', async () => {
await mount()
const first = marker('anno-a')
act(() => void commitMove(60))
await waitFor(() => expect(marker('anno-a')).not.toBe(first))
})
})
+95 -10
View File
@@ -147,24 +147,27 @@ describe('correctionFor', () => {
it('offers C for a refused B or an A refused out_of_frame, never for A too_small', () => {
const reduced = { cx: 13.19, cy: 15, r: 13.19 }
expect(
correctionFor(frame, reduced, ['annotation.geometry.too_small']),
correctionFor(frame, reduced, ['annotation.geometry.too_small'], 13.19),
).not.toBeNull()
const edge = { cx: 5, cy: 15, r: 13.2 }
expect(
correctionFor(frame, edge, ['annotation.geometry.out_of_frame'])?.circle
.cx,
correctionFor(frame, edge, ['annotation.geometry.out_of_frame'], 13.2)
?.circle.cx,
).toBe(13.2)
expect(
correctionFor(frame, { cx: 20, cy: 15, r: 13.2 }, [
'annotation.geometry.too_small',
]),
correctionFor(
frame,
{ cx: 20, cy: 15, r: 13.2 },
['annotation.geometry.too_small'],
13.2,
),
).toBeNull()
})
it('offers nothing when C is the circle that was just refused', () => {
const circle = { cx: 13.2, cy: 13.2, r: 13.2 }
expect(
correctionFor(frame, circle, ['annotation.geometry.out_of_frame']),
correctionFor(frame, circle, ['annotation.geometry.out_of_frame'], 13.2),
).toBeNull()
})
})
@@ -211,7 +214,12 @@ describe.each(FRAMES)('a point placed in the frame %s', (viewBox) => {
expect(proposal.kind).not.toBe('outside')
if (proposal.kind === 'outside') continue
const insert = circleInserter(b)
const attempt = attemptPoint(frame, proposal.circle, insert)
const attempt = attemptPoint(
frame,
proposal.circle,
insert,
proposal.circle.r,
)
expect(attempt).toMatchObject({ status: 'created' })
expect(currentErrors(b).size).toBe(0)
}
@@ -247,7 +255,12 @@ describe('a very elongated frame', () => {
const frame = frameOf(b)!
const proposal = proposePoint(frame, { x: 500, y: 15 })
if (proposal.kind !== 'circle') throw new Error('proposal')
const attempt = attemptPoint(frame, proposal.circle, circleInserter(b))
const attempt = attemptPoint(
frame,
proposal.circle,
circleInserter(b),
proposal.circle.r,
)
expect(attempt.status).toBe('refused')
expect(b.preview.document).toBe(b.text)
})
@@ -267,7 +280,9 @@ describe('a detail crop', () => {
const inside = proposePoint(frame, { x: 150, y: 350 })
if (inside.kind !== 'circle') throw new Error('proposal')
const insert = circleInserter(b, 'view-det')
expect(attemptPoint(frame, inside.circle, insert)).toMatchObject({
expect(
attemptPoint(frame, inside.circle, insert, inside.circle.r),
).toMatchObject({
status: 'created',
})
expect(proposePoint(frame, { x: 99, y: 350 })).toEqual({ kind: 'outside' })
@@ -276,3 +291,73 @@ describe('a detail crop', () => {
expect(currentErrors(b).size).toBe(0)
})
})
describe('a radius the user typed (H5)', () => {
const frame = f(0, 0, 40, 30)
it('is never replaced by a corrected position: the core issue is shown', () => {
const typed = { cx: 20, cy: 15, r: 1 }
const codes = ['annotation.geometry.too_small']
expect(correctionFor(frame, typed, codes, 13.2)).toBeNull()
expect(correctionFor(frame, typed, codes, undefined)).toBeNull()
})
it('on the core: a typed too-small radius creates nothing and keeps the message', () => {
const b = bed('0 0 5000 5000')
const big = frameOf(b)!
const attempt = attemptPoint(
big,
{ cx: 2500, cy: 2500, r: 1 },
circleInserter(b),
defaultRadius(big),
)
expect(attempt).toMatchObject({ status: 'refused' })
expect(b.preview.document).toBe(b.text)
})
})
describe('the keyboard point (H4)', () => {
it.each([
'0.003 0.004 20 20',
'0 0 20 20',
'-50 20 40 30',
'0.003 0.004 1000 30',
])('fits the frame %s by construction, with no correction', (viewBox) => {
const b = bed(viewBox)
const frame = frameOf(b)!
const circle = centreCircle(frame)
const slack = 1e-9 // float noise of x + w / 2 − r only; the core decides below
expect(circle.cx - circle.r).toBeGreaterThanOrEqual(frame.x - slack)
expect(circle.cy - circle.r).toBeGreaterThanOrEqual(frame.y - slack)
expect(circle.cx + circle.r).toBeLessThanOrEqual(frame.x + frame.w + slack)
expect(circle.cy + circle.r).toBeLessThanOrEqual(frame.y + frame.h + slack)
const attempt = attemptPoint(frame, circle, circleInserter(b), circle.r)
expect(attempt.status).not.toBe('correction')
if (viewBox.endsWith('1000 30')) return
expect(attempt).toMatchObject({ status: 'created' })
})
})
describe.each(['0 0 1000 30', '0 0 4000 200'])(
'the elongated frame %s (H8)',
(viewBox) => {
it('gives the core the verdict on every edge and corner click, keeping nothing invalid', () => {
const frame = frameOf(bed(viewBox))!
for (const click of clicksOf(frame)) {
const b = bed(viewBox)
const proposal = proposePoint(frame, click)
if (proposal.kind === 'outside') continue
const attempt = attemptPoint(
frame,
proposal.circle,
circleInserter(b),
proposal.circle.r,
)
expect(['created', 'refused', 'correction']).toContain(attempt.status)
expect(currentErrors(b).size).toBe(0)
if (attempt.status !== 'created')
expect(b.preview.document).toBe(b.text)
}
})
},
)
@@ -0,0 +1,79 @@
/** Lot H fix H2: Ctrl+Z and Ctrl+Shift+Z are editor-wide, writable only, not in a dialog. */
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { act, cleanup, fireEvent, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import {
baseText,
commitMove,
resetStore,
serverHas,
store,
} from '../helpers/editorHarness'
import { mountDraft, setWidth } from '../helpers/editorRoute'
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(),
}))
beforeEach(() => serverHas(baseText(), 3))
afterEach(() => {
cleanup()
resetStore()
setWidth(1024)
})
const ready = () =>
screen.findByRole('heading', { level: 1, name: 'Brouillon, version 4' })
const cx = () => store.getState().previewState?.document.annotations[0].geometry
const ctrlZ = (target: Element, shift = false) =>
fireEvent.keyDown(target, { key: 'z', ctrlKey: true, shiftKey: shift })
describe('editor-wide undo', () => {
it('undoes and redoes from another tab', async () => {
mountDraft()
await ready()
act(() => void commitMove(40))
await userEvent.setup().click(screen.getByRole('tab', { name: 'Général' }))
ctrlZ(document.body)
expect(cx()).toMatchObject({ cx: 50 })
ctrlZ(document.body, true)
expect(cx()).toMatchObject({ cx: 40 })
})
it('does nothing in a text field', async () => {
mountDraft()
await ready()
act(() => void commitMove(40))
await userEvent.setup().click(screen.getByRole('tab', { name: 'Général' }))
ctrlZ(document.querySelector('input') as Element)
expect(cx()).toMatchObject({ cx: 40 })
})
it('does nothing while a modal dialog is open', async () => {
mountDraft()
await ready()
act(() => void commitMove(40))
const dialog = document.createElement('dialog')
dialog.setAttribute('open', '')
document.body.append(dialog)
ctrlZ(document.body)
expect(cx()).toMatchObject({ cx: 40 })
dialog.remove()
})
it('is off in a read-only mode', async () => {
mountDraft({ width: 800 })
await ready()
store.getState().setCanEdit(true)
act(() => void commitMove(40))
store.getState().setCanEdit(false)
ctrlZ(document.body)
expect(cx()).toMatchObject({ cx: 40 })
})
})