feat(app): editor Annotations tab, core-decided placement, repair rules
This commit is contained in:
1 parent
92c595caa1
commit
26097852ff
37 files changed
+5001
-6
No files matched your search
@@ -0,0 +1,356 @@
|
||||
/**
|
||||
* Lot H: every geometry commit goes through the store under the geometry `accept` (C26,
|
||||
* C32, C33): proof on the Node core that no page action leaves an annotation with a
|
||||
* geometry error it did not already have, and that an invalid one can be repaired.
|
||||
*/
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { Annotation, Geometry, ViewBox } from '@faunosteo/core-wasm/types'
|
||||
import {
|
||||
insertAnnotation,
|
||||
updateGeometry,
|
||||
type CommitContext,
|
||||
} from '@/editor/annotations/annotationCommit'
|
||||
import { translatedGeometry } from '@/editor/annotations/gesture'
|
||||
import {
|
||||
geometryAccept,
|
||||
geometryErrorsById,
|
||||
unmeasuredViews,
|
||||
} from '@/editor/issues'
|
||||
import {
|
||||
ASSET,
|
||||
drawnText,
|
||||
insert,
|
||||
point,
|
||||
svgBytes,
|
||||
} from '../helpers/editorFixtures'
|
||||
import {
|
||||
USER,
|
||||
VERSION,
|
||||
openReady,
|
||||
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 context: CommitContext = { t: (key) => key, locale: 'fr' }
|
||||
afterEach(resetStore)
|
||||
|
||||
const state = () => store.getState().previewState!
|
||||
const annotation = (id: string): Annotation => {
|
||||
const found = state().document.annotations.find((a) => a.id === id)
|
||||
if (!found) throw new Error(`no ${id}`)
|
||||
return found
|
||||
}
|
||||
const errors = () => geometryErrorsById(state())
|
||||
const circle = (cx: number, cy: number, r: number): Geometry => ({
|
||||
type: 'circle',
|
||||
cx,
|
||||
cy,
|
||||
r,
|
||||
})
|
||||
const asAnnotation = (element: ReturnType<typeof point>) =>
|
||||
element.value as Annotation
|
||||
|
||||
function zoneText(id: string, geometry: Geometry, base = drawnText()): string {
|
||||
const value = {
|
||||
...asAnnotation(point(id, 0, 0, 1)),
|
||||
kind: 'zone',
|
||||
geometry,
|
||||
} as Annotation
|
||||
return insert(base, { kind: 'annotation', value })
|
||||
}
|
||||
|
||||
/** Opens a draft whose main drawing has `viewBox`, or none (a missing drawing). */
|
||||
async function openWith(text: string, viewBox: string | null): Promise<void> {
|
||||
const drawing = svgBytes(viewBox ?? '0 0 100 100')
|
||||
const answer = versionAnswer(text, 3, viewBox !== null)
|
||||
sb.getSheetVersion.mockResolvedValue({
|
||||
...answer,
|
||||
drawings:
|
||||
viewBox === null ? [] : [{ asset_id: ASSET, sha256: drawing.sha256 }],
|
||||
} as never)
|
||||
sb.downloadSheetDrawing.mockResolvedValue(drawing.bytes)
|
||||
store.getState().open(VERSION, USER, { canEdit: true })
|
||||
await vi.waitFor(() => expect(store.getState().status).not.toBe('loading'))
|
||||
}
|
||||
|
||||
describe('a new point (C26)', () => {
|
||||
it('is refused out of frame, the store and the handle unchanged', async () => {
|
||||
await openReady({ text: drawnText() })
|
||||
const version = store.getState().handleVersion
|
||||
const result = insertAnnotation(
|
||||
(id) => asAnnotation(point(id, 150, 50, 5)),
|
||||
context,
|
||||
)
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
codes: ['annotation.geometry.out_of_frame'],
|
||||
})
|
||||
expect(store.getState().handleVersion).toBe(version)
|
||||
expect(store.getState().documentText).toBe(drawnText())
|
||||
expect(store.getState().unsaved).toBe(false)
|
||||
})
|
||||
|
||||
it('is kept when valid, with no geometry error', async () => {
|
||||
await openReady({ text: drawnText() })
|
||||
expect(
|
||||
insertAnnotation((id) => asAnnotation(point(id, 50, 50, 5)), context),
|
||||
).toMatchObject({ ok: true })
|
||||
expect(state().document.annotations).toHaveLength(1)
|
||||
expect(errors().size).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('a move or a typed value on a valid point', () => {
|
||||
const open = () =>
|
||||
openReady({ text: insert(drawnText(), point('anno-a', 50, 50, 5)) })
|
||||
|
||||
it('rolls back a drag that drops it half out of the frame', async () => {
|
||||
await open()
|
||||
const before = store.getState().documentText
|
||||
const dropped = translatedGeometry(circle(50, 50, 5), { x: 48, y: 0 })!
|
||||
const result = updateGeometry(annotation('anno-a'), dropped, context)
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
codes: ['annotation.geometry.out_of_frame'],
|
||||
})
|
||||
expect(store.getState().documentText).toBe(before)
|
||||
expect(errors().size).toBe(0)
|
||||
})
|
||||
|
||||
it('commits a drag that stays inside, rounded to two decimals', async () => {
|
||||
await open()
|
||||
const moved = translatedGeometry(circle(50, 50, 5), {
|
||||
x: 10.004,
|
||||
y: -3.333,
|
||||
})!
|
||||
expect(updateGeometry(annotation('anno-a'), moved, context)).toEqual({
|
||||
ok: true,
|
||||
})
|
||||
expect(annotation('anno-a').geometry).toEqual(circle(60, 46.67, 5))
|
||||
})
|
||||
|
||||
it('refuses a typed radius the core reports too_small', async () => {
|
||||
await openWith(
|
||||
insert(drawnText(), point('anno-a', 2500, 2500, 400)),
|
||||
'0 0 5000 5000',
|
||||
)
|
||||
expect(errors().size).toBe(0)
|
||||
const result = updateGeometry(
|
||||
annotation('anno-a'),
|
||||
circle(2500, 2500, 1),
|
||||
context,
|
||||
)
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
codes: ['annotation.geometry.too_small'],
|
||||
})
|
||||
expect(annotation('anno-a').geometry).toEqual(circle(2500, 2500, 400))
|
||||
})
|
||||
})
|
||||
|
||||
describe('repairing an annotation that is already invalid (C33)', () => {
|
||||
it('refuses cx alone, keeps the error visible, applies the coherent set', async () => {
|
||||
await openReady({ text: insert(drawnText(), point('anno-a', 150, 150, 5)) })
|
||||
expect(errors().has('anno-a')).toBe(true)
|
||||
const before = store.getState().documentText
|
||||
const half = updateGeometry(
|
||||
annotation('anno-a'),
|
||||
circle(50, 150, 5),
|
||||
context,
|
||||
)
|
||||
expect(half).toMatchObject({
|
||||
ok: false,
|
||||
codes: ['annotation.geometry.out_of_frame'],
|
||||
})
|
||||
expect(store.getState().documentText).toBe(before)
|
||||
expect(errors().has('anno-a')).toBe(true)
|
||||
expect(
|
||||
updateGeometry(annotation('anno-a'), circle(50, 50, 5), context),
|
||||
).toEqual({ ok: true })
|
||||
expect(errors().size).toBe(0)
|
||||
})
|
||||
|
||||
it('repairs a polygon the same way, one vertex then the whole set', async () => {
|
||||
const bad: Geometry = {
|
||||
type: 'polygon',
|
||||
points: [
|
||||
[150, 150],
|
||||
[190, 150],
|
||||
[170, 190],
|
||||
],
|
||||
}
|
||||
await openReady({ text: zoneText('zone-a', bad) })
|
||||
expect(errors().has('zone-a')).toBe(true)
|
||||
const partial: Geometry = {
|
||||
type: 'polygon',
|
||||
points: [
|
||||
[20, 20],
|
||||
[190, 150],
|
||||
[170, 190],
|
||||
],
|
||||
}
|
||||
expect(updateGeometry(annotation('zone-a'), partial, context).ok).toBe(
|
||||
false,
|
||||
)
|
||||
const whole: Geometry = {
|
||||
type: 'polygon',
|
||||
points: [
|
||||
[20, 20],
|
||||
[80, 20],
|
||||
[50, 80],
|
||||
],
|
||||
}
|
||||
expect(updateGeometry(annotation('zone-a'), whole, context)).toEqual({
|
||||
ok: true,
|
||||
})
|
||||
expect(errors().size).toBe(0)
|
||||
})
|
||||
|
||||
it('repairs an invalid point that came back from a restored copy', async () => {
|
||||
const invalid = JSON.parse(insert(drawnText(), point('anno-a', 150, 50, 5)))
|
||||
sessionStorage.setItem(
|
||||
`osteo:sheet-draft:${USER}:${VERSION}`,
|
||||
JSON.stringify({
|
||||
v: 1,
|
||||
user_id: USER,
|
||||
version_id: VERSION,
|
||||
sheet_id: 'sheet-1',
|
||||
sheet_name: 'Fiche',
|
||||
revision: 3,
|
||||
written_at: '2026-11-03T10:00:00Z',
|
||||
document: invalid,
|
||||
drawings: [{ asset_id: ASSET, sha256: svgBytes().sha256 }],
|
||||
}),
|
||||
)
|
||||
await openReady({
|
||||
text: insert(drawnText(), point('anno-a', 50, 50, 5)),
|
||||
revision: 3,
|
||||
})
|
||||
expect(await store.getState().restoreCopy()).toEqual({ ok: true })
|
||||
expect(errors().has('anno-a')).toBe(true)
|
||||
expect(
|
||||
updateGeometry(annotation('anno-a'), circle(50, 50, 5), context),
|
||||
).toEqual({ ok: true })
|
||||
expect(errors().size).toBe(0)
|
||||
})
|
||||
|
||||
it('refuses a drop that leaves an already-invalid point invalid', async () => {
|
||||
await openReady({ text: insert(drawnText(), point('anno-a', 150, 50, 5)) })
|
||||
const result = updateGeometry(
|
||||
annotation('anno-a'),
|
||||
circle(140, 50, 5),
|
||||
context,
|
||||
)
|
||||
expect(result.ok).toBe(false)
|
||||
expect(annotation('anno-a').geometry).toEqual(circle(150, 50, 5))
|
||||
})
|
||||
})
|
||||
|
||||
describe('a frame change (rule 7)', () => {
|
||||
const crop = (box: ViewBox) => {
|
||||
const main = state().document.views.find((v) => v.kind === 'main')!
|
||||
const command = {
|
||||
op: 'update' as const,
|
||||
key: { kind: 'view' as const, id: main.id },
|
||||
element: { kind: 'view' as const, value: { ...main, view_box: box } },
|
||||
}
|
||||
const accept = geometryAccept({ before: state(), changedIds: [] })
|
||||
return store.getState().commit(command, { accept })
|
||||
}
|
||||
|
||||
it('is refused when it pushes a valid point out of the frame', async () => {
|
||||
await openReady({ text: insert(drawnText(), point('anno-a', 50, 50, 5)) })
|
||||
const before = store.getState().documentText
|
||||
expect(crop({ min_x: 0, min_y: 0, width: 40, height: 40 }).ok).toBe(false)
|
||||
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)
|
||||
})
|
||||
})
|
||||
|
||||
describe('an unmeasured view (C32)', () => {
|
||||
const valid = () => insert(drawnText(), point('anno-a', 50, 50, 5))
|
||||
|
||||
it('refuses a creation and a move when the drawing is missing', async () => {
|
||||
await openWith(valid(), null)
|
||||
expect(unmeasuredViews(state()).has('view-main')).toBe(true)
|
||||
const created = insertAnnotation(
|
||||
(id) => asAnnotation(point(id, 40, 40, 5)),
|
||||
context,
|
||||
)
|
||||
expect(created).toEqual({
|
||||
ok: false,
|
||||
messages: ['editor.annotations.unmeasured.hint'],
|
||||
codes: [],
|
||||
})
|
||||
const moved = updateGeometry(
|
||||
annotation('anno-a'),
|
||||
circle(60, 50, 5),
|
||||
context,
|
||||
)
|
||||
expect(moved.ok).toBe(false)
|
||||
expect(annotation('anno-a').geometry).toEqual(circle(50, 50, 5))
|
||||
})
|
||||
|
||||
it('refuses them on a crop of span 0.5', async () => {
|
||||
const view = {
|
||||
kind: 'view' as const,
|
||||
value: {
|
||||
kind: 'detail' as const,
|
||||
id: 'view-det',
|
||||
name: { fr: 'D', en: 'D' },
|
||||
svg_asset_id: null,
|
||||
view_box: { min_x: 0, min_y: 0, width: 0.5, height: 0.5 },
|
||||
parent_view_id: 'view-main',
|
||||
},
|
||||
}
|
||||
await openReady({ text: insert(drawnText(), view) })
|
||||
expect(unmeasuredViews(state()).has('view-det')).toBe(true)
|
||||
const created = insertAnnotation(
|
||||
(id) => asAnnotation(point(id, 0.25, 0.25, 0.1, 'view-det')),
|
||||
context,
|
||||
)
|
||||
expect(created.ok).toBe(false)
|
||||
expect(state().document.annotations).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('refuses them on a drawing without viewBox', async () => {
|
||||
const bare =
|
||||
'<svg xmlns="http://www.w3.org/2000/svg"><rect width="4" height="4"/></svg>'
|
||||
const { cleanSvg } = await import('@/editor/core/cleaning')
|
||||
const cleaned = cleanSvg(new TextEncoder().encode(bare))
|
||||
if (!cleaned.ok || !cleaned.value.stored || !cleaned.value.sha256)
|
||||
throw new Error('svg')
|
||||
sb.getSheetVersion.mockResolvedValue({
|
||||
...versionAnswer(valid(), 3),
|
||||
drawings: [{ asset_id: ASSET, sha256: cleaned.value.sha256 }],
|
||||
} as never)
|
||||
sb.downloadSheetDrawing.mockResolvedValue(
|
||||
new TextEncoder().encode(cleaned.value.stored),
|
||||
)
|
||||
store.getState().open(VERSION, USER, { canEdit: true })
|
||||
await vi.waitFor(() => expect(store.getState().status).not.toBe('loading'))
|
||||
expect(unmeasuredViews(state()).has('view-main')).toBe(true)
|
||||
const moved = updateGeometry(
|
||||
annotation('anno-a'),
|
||||
circle(60, 50, 5),
|
||||
context,
|
||||
)
|
||||
expect(moved.ok).toBe(false)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,738 @@
|
||||
/** Lot H: the Annotations tab (creation, corrected position, gestures, read-only, repair). */
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import {
|
||||
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 { useLocaleStore } from '@/state/localeStore'
|
||||
import {
|
||||
ASSET,
|
||||
drawnText,
|
||||
insert,
|
||||
point,
|
||||
svgBytes,
|
||||
} from '../helpers/editorFixtures'
|
||||
import {
|
||||
USER,
|
||||
VERSION,
|
||||
openReady,
|
||||
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, onSelectTab: vi.fn(), reloadSheet: vi.fn() }
|
||||
const named = (id: string, cx: number, cy: number, r: number) =>
|
||||
point(id, cx, cy, r)
|
||||
const annotations = () => store.getState().previewState!.document.annotations
|
||||
const geometryOf = (id: string) =>
|
||||
annotations().find((a: Annotation) => a.id === id)?.geometry
|
||||
|
||||
/** Screen = drawing: the overlay's inverse CTM is the identity in the page under test. */
|
||||
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()
|
||||
})
|
||||
|
||||
const text = () => insert(drawnText(), named('anno-a', 50, 50, 5))
|
||||
|
||||
async function mount(options: { text?: string; readOnly?: boolean } = {}) {
|
||||
await openReady({ text: options.text ?? text() })
|
||||
render(<AnnotationsTab {...props} readOnly={options.readOnly ?? false} />)
|
||||
const drawing = options.readOnly
|
||||
? 'catalogue-canvas-drawing'
|
||||
: 'annotation-canvas-drawing'
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId(drawing).querySelector('svg')).not.toBeNull(),
|
||||
)
|
||||
}
|
||||
|
||||
async function selectFromList(name: RegExp | string = /N/) {
|
||||
const list = await screen.findByRole('list', {
|
||||
name: 'Annotations de la vue',
|
||||
})
|
||||
await userEvent.setup().click(within(list).getAllByRole('button')[0])
|
||||
void name
|
||||
return screen.findByTestId('annotation-properties')
|
||||
}
|
||||
|
||||
describe('selection and the list', () => {
|
||||
it('lists the view annotations, selects one and shows its exact stored numbers', async () => {
|
||||
await mount({
|
||||
text: insert(drawnText(), named('anno-a', 743.0451049804688, 50, 5)),
|
||||
})
|
||||
const panel = await selectFromList()
|
||||
expect(within(panel).getByLabelText('Centre x')).toHaveValue(
|
||||
'743.0451049804688',
|
||||
)
|
||||
expect(screen.getByTestId('annotation-selection-tag')).toHaveTextContent(
|
||||
'Sélection : point',
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('a numeric edit', () => {
|
||||
it('commits only when the typed number differs from the stored one', async () => {
|
||||
await mount()
|
||||
const panel = await selectFromList()
|
||||
const user = userEvent.setup()
|
||||
const version = store.getState().handleVersion
|
||||
const cx = within(panel).getByLabelText('Centre x')
|
||||
await user.click(cx)
|
||||
await user.keyboard('{Enter}')
|
||||
expect(store.getState().handleVersion).toBe(version)
|
||||
await user.clear(cx)
|
||||
await user.type(cx, '60{Enter}')
|
||||
expect(geometryOf('anno-a')).toEqual({
|
||||
type: 'circle',
|
||||
cx: 60,
|
||||
cy: 50,
|
||||
r: 5,
|
||||
})
|
||||
expect(store.getState().handleVersion).toBe(version + 1)
|
||||
})
|
||||
|
||||
it('keeps a refused value as « non appliqué » with the document unchanged, then applies a coherent set', async () => {
|
||||
await mount({ text: insert(drawnText(), named('anno-a', 150, 150, 5)) })
|
||||
const panel = await selectFromList()
|
||||
const user = userEvent.setup()
|
||||
const before = store.getState().documentText
|
||||
expect(
|
||||
within(panel).getByTestId('annotation-geometry-issues'),
|
||||
).toBeInTheDocument()
|
||||
const cx = within(panel).getByLabelText('Centre x')
|
||||
await user.clear(cx)
|
||||
await user.type(cx, '50{Enter}')
|
||||
expect(store.getState().documentText).toBe(before)
|
||||
expect(within(panel).getByText('non appliqué')).toBeInTheDocument()
|
||||
expect(
|
||||
within(panel).getByText(/Valeurs non appliquées/),
|
||||
).toBeInTheDocument()
|
||||
expect(
|
||||
within(panel).getByRole('button', {
|
||||
name: 'Revenir aux valeurs enregistrées',
|
||||
}),
|
||||
).toBeInTheDocument()
|
||||
expect(within(panel).getByLabelText('Centre x')).toHaveValue('50')
|
||||
const cy = within(panel).getByLabelText('Centre y')
|
||||
await user.clear(cy)
|
||||
await user.type(cy, '50{Enter}')
|
||||
expect(geometryOf('anno-a')).toEqual({
|
||||
type: 'circle',
|
||||
cx: 50,
|
||||
cy: 50,
|
||||
r: 5,
|
||||
})
|
||||
expect(screen.queryByTestId('annotation-geometry-issues')).toBeNull()
|
||||
})
|
||||
|
||||
it('reverts the group to the saved values', async () => {
|
||||
await mount({ text: insert(drawnText(), named('anno-a', 150, 150, 5)) })
|
||||
const panel = await selectFromList()
|
||||
const user = userEvent.setup()
|
||||
const cx = within(panel).getByLabelText('Centre x')
|
||||
await user.clear(cx)
|
||||
await user.type(cx, '50{Enter}')
|
||||
await user.click(
|
||||
within(panel).getByRole('button', {
|
||||
name: 'Revenir aux valeurs enregistrées',
|
||||
}),
|
||||
)
|
||||
expect(within(panel).getByLabelText('Centre x')).toHaveValue('150')
|
||||
})
|
||||
})
|
||||
|
||||
async function fillCreation(user: ReturnType<typeof userEvent.setup>) {
|
||||
const panel = await screen.findByTestId('annotation-creation')
|
||||
await user.type(within(panel).getByLabelText('Nom (FR)'), 'Nuque')
|
||||
await user.type(within(panel).getByLabelText('Nom (EN)'), 'Poll')
|
||||
await user.selectOptions(
|
||||
within(panel).getByLabelText('Latéralité'),
|
||||
'unpaired',
|
||||
)
|
||||
return panel
|
||||
}
|
||||
|
||||
describe('the keyboard creation path', () => {
|
||||
it('holds a point at the frame centre, asks the required fields, then inserts once', async () => {
|
||||
await mount({ text: drawnText() })
|
||||
const user = userEvent.setup()
|
||||
await waitFor(() =>
|
||||
expect(
|
||||
screen.getByRole('button', { name: 'Ajouter un point' }),
|
||||
).toBeEnabled(),
|
||||
)
|
||||
await user.click(screen.getByRole('button', { name: 'Ajouter un point' }))
|
||||
const panel = await fillCreation(user)
|
||||
expect(within(panel).getByLabelText('Centre x')).toHaveValue('50')
|
||||
expect(within(panel).getByLabelText('Latéralité')).toHaveValue('unpaired')
|
||||
expect(annotations()).toHaveLength(0)
|
||||
await user.click(within(panel).getByRole('button', { name: 'Créer' }))
|
||||
expect(annotations()).toHaveLength(1)
|
||||
expect(annotations()[0]).toMatchObject({
|
||||
kind: 'point',
|
||||
name: { fr: 'Nuque', en: 'Poll' },
|
||||
laterality: 'unpaired',
|
||||
geometry: { type: 'circle', cx: 50, cy: 50 },
|
||||
})
|
||||
expect(
|
||||
await screen.findByTestId('annotation-properties'),
|
||||
).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('refuses a creation without laterality and creates nothing', async () => {
|
||||
await mount({ text: drawnText() })
|
||||
const user = userEvent.setup()
|
||||
await waitFor(() =>
|
||||
expect(
|
||||
screen.getByRole('button', { name: 'Ajouter une zone' }),
|
||||
).toBeEnabled(),
|
||||
)
|
||||
await user.click(screen.getByRole('button', { name: 'Ajouter une zone' }))
|
||||
const panel = await screen.findByTestId('annotation-creation')
|
||||
await user.type(within(panel).getByLabelText('Nom (FR)'), 'A')
|
||||
await user.type(within(panel).getByLabelText('Nom (EN)'), 'B')
|
||||
await user.click(within(panel).getByRole('button', { name: 'Créer' }))
|
||||
expect(within(panel).getByRole('alert')).toHaveTextContent(
|
||||
'Choisissez la catégorie et la latéralité',
|
||||
)
|
||||
expect(annotations()).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('applies the geometry group to a pending zone, the core deciding at « Créer »', async () => {
|
||||
await mount({ text: drawnText() })
|
||||
const user = userEvent.setup()
|
||||
await waitFor(() =>
|
||||
expect(
|
||||
screen.getByRole('button', { name: 'Ajouter une zone' }),
|
||||
).toBeEnabled(),
|
||||
)
|
||||
await user.click(screen.getByRole('button', { name: 'Ajouter une zone' }))
|
||||
const panel = await fillCreation(user)
|
||||
const x = within(panel).getByLabelText('x du sommet 1')
|
||||
await user.clear(x)
|
||||
await user.type(x, '500{Enter}')
|
||||
expect(within(panel).getByLabelText('x du sommet 1')).toHaveValue('500')
|
||||
await user.click(within(panel).getByRole('button', { name: 'Créer' }))
|
||||
expect(annotations()).toHaveLength(0)
|
||||
expect(within(panel).getByRole('alert')).toBeInTheDocument()
|
||||
expect(within(panel).getByLabelText('x du sommet 1')).toHaveValue('500')
|
||||
})
|
||||
})
|
||||
|
||||
describe('the point tool and the corrected position (C26)', () => {
|
||||
async function clickCorner() {
|
||||
const user = userEvent.setup()
|
||||
await user.click(screen.getByRole('button', { name: 'Point' }))
|
||||
fireEvent.click(screen.getByTestId('annotation-canvas'), {
|
||||
clientX: 0,
|
||||
clientY: 0,
|
||||
})
|
||||
return user
|
||||
}
|
||||
|
||||
it('proposes C with the click marked apart, inserts it only on confirmation', async () => {
|
||||
await mount({ text: drawnText() })
|
||||
await waitFor(() =>
|
||||
expect(screen.getByRole('button', { name: 'Point' })).toBeEnabled(),
|
||||
)
|
||||
const user = await clickCorner()
|
||||
const notice = await screen.findByTestId('correction-notice')
|
||||
expect(notice).toHaveTextContent(
|
||||
'Position corrigée proposée : x = 15, y = 15.',
|
||||
)
|
||||
expect(screen.getByTestId('overlay-click-cross')).toBeInTheDocument()
|
||||
const panel = await fillCreation(user)
|
||||
expect(annotations()).toHaveLength(0)
|
||||
await user.click(
|
||||
within(notice).getByRole('button', { name: 'Utiliser cette position' }),
|
||||
)
|
||||
expect(geometryOf(annotations()[0].id)).toEqual({
|
||||
type: 'circle',
|
||||
cx: 15,
|
||||
cy: 15,
|
||||
r: 15,
|
||||
})
|
||||
expect(panel).not.toBeInTheDocument()
|
||||
expect(screen.queryByTestId('overlay-click-cross')).toBeNull()
|
||||
const props = await screen.findByTestId('annotation-properties')
|
||||
expect(within(props).getByLabelText('Centre x')).toHaveValue('15')
|
||||
})
|
||||
|
||||
it('keeps the pending point at the click when the correction is cancelled', async () => {
|
||||
await mount({ text: drawnText() })
|
||||
await waitFor(() =>
|
||||
expect(screen.getByRole('button', { name: 'Point' })).toBeEnabled(),
|
||||
)
|
||||
const user = await clickCorner()
|
||||
const notice = await screen.findByTestId('correction-notice')
|
||||
await user.click(within(notice).getByRole('button', { name: 'Annuler' }))
|
||||
expect(screen.queryByTestId('correction-notice')).toBeNull()
|
||||
expect(screen.queryByTestId('overlay-click-cross')).toBeNull()
|
||||
const panel = screen.getByTestId('annotation-creation')
|
||||
expect(within(panel).getByLabelText('Centre x')).toHaveValue('0')
|
||||
expect(annotations()).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('offers the correction when the core refuses a pending point', async () => {
|
||||
await mount({ text: drawnText() })
|
||||
const user = userEvent.setup()
|
||||
await waitFor(() =>
|
||||
expect(
|
||||
screen.getByRole('button', { name: 'Ajouter un point' }),
|
||||
).toBeEnabled(),
|
||||
)
|
||||
await user.click(screen.getByRole('button', { name: 'Ajouter un point' }))
|
||||
const panel = await fillCreation(user)
|
||||
const cx = within(panel).getByLabelText('Centre x')
|
||||
await user.clear(cx)
|
||||
await user.type(cx, '98{Enter}')
|
||||
await user.click(within(panel).getByRole('button', { name: 'Créer' }))
|
||||
expect(await screen.findByTestId('correction-notice')).toHaveTextContent(
|
||||
'x = 85',
|
||||
)
|
||||
expect(annotations()).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('says to click inside the drawing, and proposes nothing, outside the frame', 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: -5,
|
||||
clientY: 50,
|
||||
})
|
||||
expect(
|
||||
await screen.findByText("Cliquez à l'intérieur du dessin."),
|
||||
).toBeInTheDocument()
|
||||
expect(screen.queryByTestId('annotation-creation')).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
function marker(id: string): Element {
|
||||
const found = screen
|
||||
.getByTestId('annotation-canvas-drawing')
|
||||
.querySelector(`[data-anno-id="${id}"]`)
|
||||
if (!found) throw new Error(`no marker ${id}`)
|
||||
return found
|
||||
}
|
||||
|
||||
describe('select and move gestures (C34)', () => {
|
||||
const down = (el: Element, x: number, y: number) =>
|
||||
fireEvent.pointerDown(el, {
|
||||
clientX: x,
|
||||
clientY: y,
|
||||
button: 0,
|
||||
pointerId: 1,
|
||||
})
|
||||
const moveTo = (el: Element, x: number, y: number) =>
|
||||
fireEvent.pointerMove(el, { clientX: x, clientY: y, pointerId: 1 })
|
||||
const up = (el: Element, x: number, y: number) =>
|
||||
fireEvent.pointerUp(el, { clientX: x, clientY: y, pointerId: 1 })
|
||||
|
||||
async function selected() {
|
||||
await mount()
|
||||
await selectFromList()
|
||||
return marker('anno-a')
|
||||
}
|
||||
|
||||
it('treats a press and release under 4 px as a click: nothing is committed', async () => {
|
||||
const hit = await selected()
|
||||
const version = store.getState().handleVersion
|
||||
down(hit, 50, 50)
|
||||
moveTo(hit, 52, 51)
|
||||
up(hit, 52, 51)
|
||||
expect(store.getState().handleVersion).toBe(version)
|
||||
expect(geometryOf('anno-a')).toEqual({
|
||||
type: 'circle',
|
||||
cx: 50,
|
||||
cy: 50,
|
||||
r: 5,
|
||||
})
|
||||
})
|
||||
|
||||
it('moves the selected circle as a block after 4 px, in one update rounded to 2 decimals', async () => {
|
||||
const hit = await selected()
|
||||
const version = store.getState().handleVersion
|
||||
down(hit, 50, 50)
|
||||
moveTo(hit, 58, 50)
|
||||
expect(screen.getByTestId('annotation-overlay')).toBeInTheDocument()
|
||||
up(hit, 60.004, 40)
|
||||
expect(geometryOf('anno-a')).toEqual({
|
||||
type: 'circle',
|
||||
cx: 60,
|
||||
cy: 40,
|
||||
r: 5,
|
||||
})
|
||||
expect(store.getState().handleVersion).toBe(version + 1)
|
||||
})
|
||||
|
||||
it('rolls a drop half out of the frame back, with the core message', async () => {
|
||||
const hit = await selected()
|
||||
const before = store.getState().documentText
|
||||
down(hit, 50, 50)
|
||||
moveTo(hit, 90, 50)
|
||||
up(hit, 98, 50)
|
||||
expect(store.getState().documentText).toBe(before)
|
||||
expect(await screen.findByRole('alert')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('drops the drag on pointer cancel with no command', async () => {
|
||||
const hit = await selected()
|
||||
const version = store.getState().handleVersion
|
||||
down(hit, 50, 50)
|
||||
moveTo(hit, 70, 50)
|
||||
fireEvent.pointerCancel(hit, { pointerId: 1 })
|
||||
up(hit, 70, 50)
|
||||
expect(store.getState().handleVersion).toBe(version)
|
||||
})
|
||||
|
||||
it('does not move an annotation that is not selected', async () => {
|
||||
await mount()
|
||||
const hit = marker('anno-a')
|
||||
const version = store.getState().handleVersion
|
||||
down(hit, 50, 50)
|
||||
moveTo(hit, 70, 50)
|
||||
up(hit, 70, 50)
|
||||
expect(store.getState().handleVersion).toBe(version)
|
||||
})
|
||||
})
|
||||
|
||||
describe('an unmeasured view (C32)', () => {
|
||||
async function mountMissing() {
|
||||
const drawing = svgBytes()
|
||||
sb.getSheetVersion.mockResolvedValue({
|
||||
...versionAnswer(text(), 3),
|
||||
drawings: [],
|
||||
} 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} />)
|
||||
void ASSET
|
||||
}
|
||||
|
||||
it('disables every tool and add button, shows the hint, keeps the annotation selectable read-only', async () => {
|
||||
vi.spyOn(console, 'warn').mockImplementation(() => undefined)
|
||||
await mountMissing()
|
||||
for (const name of [
|
||||
'Point',
|
||||
'Zone',
|
||||
'Accès à une vue détaillée',
|
||||
'Ajouter un point',
|
||||
'Ajouter une zone',
|
||||
'Ajouter un accès',
|
||||
]) {
|
||||
expect(screen.getByRole('button', { name })).toBeDisabled()
|
||||
}
|
||||
expect(
|
||||
screen.getAllByText(/Ce dessin n'est pas mesurable/).length,
|
||||
).toBeGreaterThan(0)
|
||||
const panel = await selectFromList()
|
||||
expect(
|
||||
within(panel).queryByRole('textbox', { name: 'Centre x' }),
|
||||
).toBeNull()
|
||||
expect(
|
||||
within(panel).getByText('Centre x', { exact: false }),
|
||||
).toBeInTheDocument()
|
||||
expect(
|
||||
within(panel).getByText(/Ce dessin n'est pas mesurable/),
|
||||
).toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
|
||||
describe('tools and categories', () => {
|
||||
it('disables the drawing tools until an annotation category exists', async () => {
|
||||
const bare = JSON.parse(drawnText())
|
||||
bare.categories = []
|
||||
await mount({ text: JSON.stringify(bare) })
|
||||
expect(screen.getByRole('button', { name: 'Point' })).toBeDisabled()
|
||||
expect(
|
||||
screen.getByText(
|
||||
"Créez d'abord une catégorie d'annotation (onglet Général).",
|
||||
),
|
||||
).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('enables the link tool only when the view has a direct detail child', async () => {
|
||||
await mount({ text: drawnText() })
|
||||
expect(
|
||||
screen.getByRole('button', { name: 'Accès à une vue détaillée' }),
|
||||
).toBeDisabled()
|
||||
expect(
|
||||
screen.getByRole('button', { name: 'Ajouter un accès' }),
|
||||
).toBeDisabled()
|
||||
})
|
||||
|
||||
it('undoes and redoes through the tool column', 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}')
|
||||
await user.click(screen.getByRole('button', { name: 'Annuler' }))
|
||||
expect(geometryOf('anno-a')).toMatchObject({ cx: 50 })
|
||||
await user.click(screen.getByRole('button', { name: 'Rétablir' }))
|
||||
expect(geometryOf('anno-a')).toMatchObject({ cx: 60 })
|
||||
})
|
||||
})
|
||||
|
||||
describe('read-only', () => {
|
||||
it('browses the annotations with no tool, no add button and the fields as text', async () => {
|
||||
await mount({ readOnly: true })
|
||||
expect(screen.queryByRole('button', { name: 'Point' })).toBeNull()
|
||||
expect(
|
||||
screen.queryByRole('button', { name: 'Ajouter un point' }),
|
||||
).toBeNull()
|
||||
const panel = await selectFromList()
|
||||
expect(within(panel).queryByRole('textbox')).toBeNull()
|
||||
expect(
|
||||
within(panel).queryByRole('button', { name: 'Supprimer' }),
|
||||
).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
const triangle = [
|
||||
[20, 20],
|
||||
[80, 20],
|
||||
[50, 80],
|
||||
]
|
||||
const zoneAnnotation = (geometry: object): Annotation =>
|
||||
({
|
||||
...(point('zone-a', 0, 0, 1).value as Annotation),
|
||||
kind: 'zone',
|
||||
geometry,
|
||||
}) as Annotation
|
||||
|
||||
describe('tracing a zone or a link', () => {
|
||||
const clickAt = (x: number, y: number) =>
|
||||
fireEvent.click(screen.getByTestId('annotation-canvas'), {
|
||||
clientX: x,
|
||||
clientY: y,
|
||||
})
|
||||
|
||||
it('closes on Enter, holds the polygon pending, inserts it on « Créer »', async () => {
|
||||
await mount({ text: drawnText() })
|
||||
const user = userEvent.setup()
|
||||
await waitFor(() =>
|
||||
expect(screen.getByRole('button', { name: 'Zone' })).toBeEnabled(),
|
||||
)
|
||||
await user.click(screen.getByRole('button', { name: 'Zone' }))
|
||||
triangle.forEach(([x, y]) => clickAt(x, y))
|
||||
expect(
|
||||
screen.getByRole('button', { name: 'Fermer le tracé' }),
|
||||
).toBeEnabled()
|
||||
fireEvent.keyDown(screen.getByTestId('annotation-canvas'), { key: 'Enter' })
|
||||
const panel = await fillCreation(user)
|
||||
expect(within(panel).getByLabelText('x du sommet 3')).toHaveValue('50')
|
||||
await user.click(within(panel).getByRole('button', { name: 'Créer' }))
|
||||
expect(annotations()[0]).toMatchObject({
|
||||
kind: 'zone',
|
||||
geometry: { type: 'polygon', points: triangle },
|
||||
})
|
||||
})
|
||||
|
||||
it('drops the tracing on Escape with nothing created', async () => {
|
||||
await mount({ text: drawnText() })
|
||||
await waitFor(() =>
|
||||
expect(screen.getByRole('button', { name: 'Zone' })).toBeEnabled(),
|
||||
)
|
||||
await userEvent.setup().click(screen.getByRole('button', { name: 'Zone' }))
|
||||
clickAt(20, 20)
|
||||
expect(screen.getByText('1 sommet(s) posé(s).')).toBeInTheDocument()
|
||||
fireEvent.keyDown(screen.getByTestId('annotation-canvas'), {
|
||||
key: 'Escape',
|
||||
})
|
||||
expect(screen.queryByText('1 sommet(s) posé(s).')).toBeNull()
|
||||
expect(annotations()).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('closes on a click at the first vertex once three are placed', async () => {
|
||||
await mount({ text: drawnText() })
|
||||
await waitFor(() =>
|
||||
expect(screen.getByRole('button', { name: 'Zone' })).toBeEnabled(),
|
||||
)
|
||||
await userEvent.setup().click(screen.getByRole('button', { name: 'Zone' }))
|
||||
triangle.forEach(([x, y]) => clickAt(x, y))
|
||||
clickAt(20.5, 20)
|
||||
expect(await screen.findByTestId('annotation-creation')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('links only to a detail view directly under this one', 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: 10, width: 80, height: 80 },
|
||||
parent_view_id: 'view-main',
|
||||
},
|
||||
})
|
||||
await mount({ text: withView })
|
||||
const user = userEvent.setup()
|
||||
await waitFor(() =>
|
||||
expect(
|
||||
screen.getByRole('button', { name: 'Accès à une vue détaillée' }),
|
||||
).toBeEnabled(),
|
||||
)
|
||||
await user.click(screen.getByRole('button', { name: 'Ajouter un accès' }))
|
||||
const panel = await fillCreation(user)
|
||||
const opens = within(panel).getByLabelText('Vue ouverte')
|
||||
expect(
|
||||
within(opens)
|
||||
.getAllByRole('option')
|
||||
.map((o) => o.textContent),
|
||||
).toEqual(['Choisir…', 'Tête'])
|
||||
await user.selectOptions(opens, 'view-det')
|
||||
await user.click(within(panel).getByRole('button', { name: 'Créer' }))
|
||||
expect(annotations()[0]).toMatchObject({
|
||||
kind: 'detail_trigger',
|
||||
opens_view_id: 'view-det',
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('polygons and paths', () => {
|
||||
const zoneText = (geometry: object) =>
|
||||
insert(drawnText(), { kind: 'annotation', value: zoneAnnotation(geometry) })
|
||||
|
||||
it('adds and removes a vertex as part of the whole group', async () => {
|
||||
await mount({ text: zoneText({ type: 'polygon', points: triangle }) })
|
||||
const panel = await selectFromList()
|
||||
const user = userEvent.setup()
|
||||
await user.click(within(panel).getByLabelText('x du sommet 1'))
|
||||
await user.click(
|
||||
within(panel).getByRole('button', { name: 'Ajouter un sommet' }),
|
||||
)
|
||||
expect(geometryOf('zone-a')).toMatchObject({
|
||||
points: [
|
||||
[20, 20],
|
||||
[50, 20],
|
||||
[80, 20],
|
||||
[50, 80],
|
||||
],
|
||||
})
|
||||
await user.click(within(panel).getByRole('button', { name: 'Retirer' }))
|
||||
expect(geometryOf('zone-a')).toMatchObject({
|
||||
points: triangle.slice(0, 3).map((p, i) => (i === 0 ? [50, 20] : p)),
|
||||
})
|
||||
})
|
||||
|
||||
it('never offers to remove a vertex below three', async () => {
|
||||
await mount({ text: zoneText({ type: 'polygon', points: triangle }) })
|
||||
const panel = await selectFromList()
|
||||
expect(
|
||||
within(panel).getByRole('button', { name: 'Retirer' }),
|
||||
).toBeDisabled()
|
||||
})
|
||||
|
||||
it('moves a selected polygon as a block', async () => {
|
||||
await mount({ text: zoneText({ type: 'polygon', points: triangle }) })
|
||||
await selectFromList()
|
||||
const hit = screen
|
||||
.getByTestId('annotation-canvas-drawing')
|
||||
.querySelector('[data-anno-id="zone-a"]')!
|
||||
fireEvent.pointerDown(hit, {
|
||||
clientX: 50,
|
||||
clientY: 40,
|
||||
button: 0,
|
||||
pointerId: 1,
|
||||
})
|
||||
fireEvent.pointerMove(hit, { clientX: 55, clientY: 40, pointerId: 1 })
|
||||
fireEvent.pointerUp(hit, { clientX: 60, clientY: 45, pointerId: 1 })
|
||||
expect(geometryOf('zone-a')).toMatchObject({
|
||||
points: [
|
||||
[30, 25],
|
||||
[90, 25],
|
||||
[60, 85],
|
||||
],
|
||||
})
|
||||
})
|
||||
|
||||
it('replaces a path by a traced polygon and never shows its data', async () => {
|
||||
await mount({
|
||||
text: zoneText({ type: 'path', d: 'M 20 20 L 80 20 L 50 80 Z' }),
|
||||
})
|
||||
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',
|
||||
}),
|
||||
)
|
||||
const canvas = screen.getByTestId('annotation-canvas')
|
||||
triangle.forEach(([x, y]) =>
|
||||
fireEvent.click(canvas, { clientX: x, clientY: y }),
|
||||
)
|
||||
fireEvent.keyDown(canvas, { key: 'Enter' })
|
||||
await waitFor(() =>
|
||||
expect(geometryOf('zone-a')).toEqual({
|
||||
type: 'polygon',
|
||||
points: triangle,
|
||||
}),
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
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,
|
||||
})
|
||||
expect(geometryOf('anno-a')).toMatchObject({ cx: 60 })
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,278 @@
|
||||
/** Lot H: the proposal arithmetic (pure) and the core's verdict on each proposal (C26). */
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import {
|
||||
attemptPoint,
|
||||
correctionFor,
|
||||
confirmCorrection,
|
||||
} from '@/editor/annotations/correction'
|
||||
import {
|
||||
centreCircle,
|
||||
centreTriangle,
|
||||
correctedCircle,
|
||||
defaultRadius,
|
||||
parseFrame,
|
||||
proposePoint,
|
||||
type Frame,
|
||||
} from '@/editor/annotations/frame'
|
||||
import {
|
||||
bed,
|
||||
circleInserter,
|
||||
currentErrors,
|
||||
drawnText,
|
||||
frameOf,
|
||||
withCrop,
|
||||
} from '../helpers/annotationsCore'
|
||||
|
||||
const f = (x: number, y: number, w: number, h: number): Frame => ({
|
||||
x,
|
||||
y,
|
||||
w,
|
||||
h,
|
||||
})
|
||||
|
||||
describe('parseFrame', () => {
|
||||
it('reads the attribute text with Number, origin offsets kept', () => {
|
||||
expect(parseFrame('-50 20 40 30')).toEqual(f(-50, 20, 40, 30))
|
||||
expect(parseFrame('0,0,10.5,10')).toEqual(f(0, 0, 10.5, 10))
|
||||
expect(parseFrame('743.0451049804688 0 10 10')?.x).toBe(743.0451049804688)
|
||||
})
|
||||
|
||||
it('gives no frame for anything but four finite numbers over a positive span', () => {
|
||||
for (const text of [
|
||||
null,
|
||||
'',
|
||||
'0 0 100',
|
||||
'0 0 a 5',
|
||||
'0 0 0 5',
|
||||
'0 0 5 -1',
|
||||
'0 0 Infinity 5',
|
||||
]) {
|
||||
expect(parseFrame(text)).toBeNull()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('defaultRadius', () => {
|
||||
it('is min(12 + 0.03 max, min / 2) floored to 0.01', () => {
|
||||
expect(defaultRadius(f(0, 0, 40, 30))).toBe(13.2)
|
||||
expect(defaultRadius(f(0, 0, 500, 380))).toBe(27)
|
||||
expect(defaultRadius(f(0, 0, 20, 20))).toBe(10)
|
||||
expect(defaultRadius(f(0, 0, 1000, 30))).toBe(15)
|
||||
expect(defaultRadius(f(0, 0, 1, 1))).toBe(0.5)
|
||||
expect(defaultRadius(f(0, 0, 0.0299, 5))).toBe(0.01)
|
||||
})
|
||||
})
|
||||
|
||||
describe('proposePoint', () => {
|
||||
const frame = f(0, 0, 40, 30)
|
||||
|
||||
it('keeps the click with r0 when it is r0 or more from every edge (A)', () => {
|
||||
expect(proposePoint(frame, { x: 20.004, y: 15.006 })).toEqual({
|
||||
kind: 'circle',
|
||||
circle: { cx: 20, cy: 15.01, r: 13.2 },
|
||||
shrunk: false,
|
||||
})
|
||||
expect(proposePoint(frame, { x: 13.2, y: 15 })).toMatchObject({
|
||||
shrunk: false,
|
||||
})
|
||||
})
|
||||
|
||||
it('keeps the click with a smaller radius when it is nearer an edge (B)', () => {
|
||||
expect(proposePoint(frame, { x: 13.19, y: 15 })).toEqual({
|
||||
kind: 'circle',
|
||||
circle: { cx: 13.19, cy: 15, r: 13.19 },
|
||||
shrunk: true,
|
||||
})
|
||||
expect(proposePoint(frame, { x: 5.555, y: 15 })).toMatchObject({
|
||||
circle: { cx: 5.56, r: 5.56 },
|
||||
})
|
||||
})
|
||||
|
||||
it('goes straight to C when B would be under 0.01, and nothing outside F', () => {
|
||||
expect(proposePoint(frame, { x: 0, y: 0 }).kind).toBe('corrected')
|
||||
expect(proposePoint(frame, { x: 40, y: 15 }).kind).toBe('corrected')
|
||||
expect(proposePoint(frame, { x: 0.004, y: 15 }).kind).toBe('corrected')
|
||||
expect(proposePoint(frame, { x: -0.1, y: 15 })).toEqual({ kind: 'outside' })
|
||||
expect(proposePoint(frame, { x: 20, y: 30.01 })).toEqual({
|
||||
kind: 'outside',
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('correctedCircle (C)', () => {
|
||||
it('clamps per axis with r0, inward on the hundredth grid', () => {
|
||||
expect(correctedCircle(f(0, 0, 40, 30), { x: 0, y: 0 })).toEqual({
|
||||
cx: 13.2,
|
||||
cy: 13.2,
|
||||
r: 13.2,
|
||||
})
|
||||
expect(correctedCircle(f(0, 0, 40, 30), { x: 40, y: 30 })).toEqual({
|
||||
cx: 26.8,
|
||||
cy: 16.8,
|
||||
r: 13.2,
|
||||
})
|
||||
expect(correctedCircle(f(0.003, 0, 40, 30), { x: 0.003, y: 15 }).cx).toBe(
|
||||
13.21,
|
||||
)
|
||||
})
|
||||
|
||||
it('uses the exact clamped value when the grid leaves an empty interval', () => {
|
||||
const frame = f(0.003, 0.004, 20, 20)
|
||||
expect(correctedCircle(frame, { x: 0.003, y: 0.004 })).toEqual({
|
||||
cx: 10.003,
|
||||
cy: 10.004,
|
||||
r: 10,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('keyboard proposals', () => {
|
||||
it('holds a point at the centre of F with r0 and a triangle of 10 % of the smaller side', () => {
|
||||
expect(centreCircle(f(-50, 20, 40, 30))).toEqual({
|
||||
cx: -30,
|
||||
cy: 35,
|
||||
r: 13.2,
|
||||
})
|
||||
expect(centreTriangle(f(0, 0, 100, 50))).toEqual([
|
||||
[50, 22.5],
|
||||
[52.5, 27.5],
|
||||
[47.5, 27.5],
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe('correctionFor', () => {
|
||||
const frame = f(0, 0, 40, 30)
|
||||
|
||||
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']),
|
||||
).not.toBeNull()
|
||||
const edge = { cx: 5, cy: 15, r: 13.2 }
|
||||
expect(
|
||||
correctionFor(frame, edge, ['annotation.geometry.out_of_frame'])?.circle
|
||||
.cx,
|
||||
).toBe(13.2)
|
||||
expect(
|
||||
correctionFor(frame, { cx: 20, cy: 15, r: 13.2 }, [
|
||||
'annotation.geometry.too_small',
|
||||
]),
|
||||
).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']),
|
||||
).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
const FRAMES = [
|
||||
'0 0 40 30',
|
||||
'0 0 20 20',
|
||||
'0 0 100 300',
|
||||
'0 0 500 380',
|
||||
'0 0 640 480',
|
||||
'0 0 200 150',
|
||||
'0 0 4000 200',
|
||||
'-50 20 40 30',
|
||||
'0.003 0.004 20 20',
|
||||
'1040000 1040000 8000 6000',
|
||||
]
|
||||
|
||||
function clicksOf(frame: Frame) {
|
||||
const r0 = defaultRadius(frame)
|
||||
const mid = { x: frame.x + frame.w / 2, y: frame.y + frame.h / 2 }
|
||||
const right = frame.x + frame.w
|
||||
const bottom = frame.y + frame.h
|
||||
return [
|
||||
{ x: frame.x, y: frame.y },
|
||||
{ x: right, y: frame.y },
|
||||
{ x: frame.x, y: bottom },
|
||||
{ x: right, y: bottom },
|
||||
{ x: mid.x, y: frame.y },
|
||||
{ x: mid.x, y: bottom },
|
||||
{ x: frame.x, y: mid.y },
|
||||
{ x: right, y: mid.y },
|
||||
mid,
|
||||
{ x: frame.x + r0, y: mid.y },
|
||||
{ x: frame.x + r0 - 0.005, y: mid.y },
|
||||
]
|
||||
}
|
||||
|
||||
describe.each(FRAMES)('a point placed in the frame %s', (viewBox) => {
|
||||
it('is created or corrected for every click, and the core never keeps an invalid one', () => {
|
||||
const frame = frameOf(bed(viewBox))!
|
||||
for (const click of clicksOf(frame)) {
|
||||
const b = bed(viewBox)
|
||||
const proposal = proposePoint(frame, click)
|
||||
expect(proposal.kind).not.toBe('outside')
|
||||
if (proposal.kind === 'outside') continue
|
||||
const insert = circleInserter(b)
|
||||
const attempt = attemptPoint(frame, proposal.circle, insert)
|
||||
expect(attempt).toMatchObject({ status: 'created' })
|
||||
expect(currentErrors(b).size).toBe(0)
|
||||
}
|
||||
})
|
||||
|
||||
it('a corrected proposal is inserted only through the confirmation', () => {
|
||||
const frame = frameOf(bed(viewBox))!
|
||||
const proposal = proposePoint(frame, { x: frame.x, y: frame.y })
|
||||
if (proposal.kind !== 'corrected')
|
||||
throw new Error('corner must be corrected')
|
||||
const b = bed(viewBox)
|
||||
const correction = {
|
||||
click: { x: frame.x, y: frame.y },
|
||||
circle: proposal.circle,
|
||||
}
|
||||
expect(b.preview.state()).toMatchObject({ ok: true })
|
||||
expect(b.state.document.annotations).toHaveLength(0)
|
||||
expect(confirmCorrection(correction, circleInserter(b))).toMatchObject({
|
||||
status: 'created',
|
||||
})
|
||||
const kept = b.preview.state()
|
||||
if (!kept.ok) throw new Error('state')
|
||||
expect(kept.value.document.annotations[0].geometry).toEqual({
|
||||
type: 'circle',
|
||||
...proposal.circle,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('a very elongated frame', () => {
|
||||
it('refuses 1000 × 30 on too_small with the core message and creates nothing', () => {
|
||||
const b = bed('0 0 1000 30')
|
||||
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))
|
||||
expect(attempt.status).toBe('refused')
|
||||
expect(b.preview.document).toBe(b.text)
|
||||
})
|
||||
})
|
||||
|
||||
describe('a detail crop', () => {
|
||||
const crop = { min_x: 100, min_y: 200, width: 100, height: 300 }
|
||||
const cropBed = () => bed('0 0 1000 1000', withCrop(drawnText(), crop))
|
||||
|
||||
it('takes F from the composed root of the crop, origin offsets kept', () => {
|
||||
expect(frameOf(cropBed(), 'view-det')).toEqual(f(100, 200, 100, 300))
|
||||
})
|
||||
|
||||
it('places a point inside the crop and corrects one on its edge, never outside it', () => {
|
||||
const b = cropBed()
|
||||
const frame = frameOf(b, 'view-det')!
|
||||
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({
|
||||
status: 'created',
|
||||
})
|
||||
expect(proposePoint(frame, { x: 99, y: 350 })).toEqual({ kind: 'outside' })
|
||||
const edge = proposePoint(frame, { x: 100, y: 200 })
|
||||
expect(edge.kind).toBe('corrected')
|
||||
expect(currentErrors(b).size).toBe(0)
|
||||
})
|
||||
})
|
||||
@@ -235,7 +235,9 @@ describe('cap and quota', () => {
|
||||
await screen.findByText(fr['editor.draft.status.copy_cap']),
|
||||
).toBeInTheDocument()
|
||||
expect(storedCopy()).toBeNull()
|
||||
const items = screen.getAllByRole('listitem')
|
||||
const items = screen
|
||||
.getAllByRole('listitem')
|
||||
.filter((item) => /Autre fiche/.test(item.textContent ?? ''))
|
||||
expect(items).toHaveLength(2)
|
||||
expect(
|
||||
within(items[0]).getByText(/Autre fiche · révision 5 · \d\d:\d\d/),
|
||||
|
||||
@@ -167,7 +167,8 @@ describe('tabs', () => {
|
||||
'Vérification',
|
||||
])
|
||||
await user.click(screen.getByRole('button', { name: 'Plus' }))
|
||||
const rest = within(screen.getByRole('list')).getAllByRole('button')
|
||||
const more = document.getElementById('editor-tabs-more') as HTMLElement
|
||||
const rest = within(more).getAllByRole('button')
|
||||
expect(rest.map((b) => b.textContent)).toEqual([
|
||||
'Général',
|
||||
'Vues et illustrations',
|
||||
|
||||
Reference in new issue
Block a user