Files
faunosteo/apps/app/tests/unit/editor-history-entry.test.ts
T
bchanot cd794f3c39 feat(editor): properties dialog over the column, transactional save
U2 Task 6 (A1, A2, A3, A9, A10, R-3, R-5, R-6, R-14, R-16).

- Dialog: `anchor` places the panel at the column's left and width,
  from its top, under a 22 % ink veil that ignores clicks; the visual
  viewport below 320 px; centred without an anchor box.
- Navigation: `annotation` layer (LAYERS, EditorView, history entry,
  leave guards, left-draft flag); Back closes, Forward reopens.
- AnnotationDialog: read body, « Modifier » form (CreationFields with
  « Couleur », missingFields, R-3 messages), one `update` on the
  current annotation by id, « Marquer comme relu », « Modifier la
  forme », « Supprimer »; read-only flip keeps the draft.
- The column always shows the overview, selected row marked; Enter on
  it, a second click or a double click, the strip's « Propriétés »
  open the dialog. « Aller à » focuses the row (R-5).
- Read-only twin: strip and read-only dialog at 1 024 px and wider,
  inline read body below. PropertiesPanel and AnnotationFields gone.
- Strip lead 256 px and a fixed status slot, so the commands fit at
  1 024 px and a status change moves nothing.
- e2e: `aside-content` exemption removed; scroll-to-top workarounds
  of editor-geometry, editor-zones and editor-journey removed.

Removed assertions mapped:
  editor-annotations-tab.test.tsx:
  - selectFromList waited for `annotation-properties` -> waits for the strip's « Propriétés » (selection without dialog, A1)
  - refused place: issues block `annotation-geometry-issues` in the panel -> the strip notice alert (present, then gone)
  - keyboard creation / C26 confirmation: `annotation-properties` after create -> strip « Propriétés » (new element selected)
  - Désélectionner from the panel -> « Désélectionner » in the strip (all 7 tests); « panel closes » -> strip has no « Propriétés »
  - undo after deselect (typed name) -> handle key series then deselect then undo
  - commits a valid typed value once without blur (name field) -> a pending handle key series committed once by the strip deselect
  - a refused typed value: the panel closes (two tests: strip deselect, tap deselect) -> removed: no field outside the dialog; the dialog's draft writes nothing before « Enregistrer » (new dialog tests)
  - not offered while a replace tracing runs: panel's Désélectionner absent -> strip offers neither « Désélectionner » nor « Propriétés » during the tracing
  - tap: isDeselected read `annotation-properties` -> strip « Propriétés » absent; link select -> strip title data-selection
  - tap: undo after tap deselect (typed name) -> handle series; typed value committed on blur -> handle series committed on blur
  - strip geometry issues: list read in the panel -> list read in the properties dialog's issues block
  - a new commit replaces the message (typed name) -> « Marquer comme relu » in the dialog
  editor-escape.test.tsx:
  - `panel()` = `annotation-properties` -> the strip's « Propriétés » or the phone twin's inline read body
  - Escape from a panel button (« Marquer comme relu ») -> from the selected row in the side column: focus on the row
  - after A then B from the panel (via « Toutes les annotations ») -> select A, select B, Escape from a button of the side column: focus on B's row
  - a text field (panel « Nom (FR) ») -> the overview « Filtrer par nom »
  - redraw via the panel's « Remplacer par un polygone » -> the strip's command
  - modal / delete dialog opened by the panel's « Supprimer » -> « Supprimer » of the properties dialog
  - read-only width: hidden `annotation-properties` -> hidden strip « Propriétés » of the kept edit body
  editor-history-buttons.test.tsx (typed text and the press, 8 tests): the annotation's committed « Nom (FR) » field of the panel -> the sheet's committed « Nom (FR) » on Général (same TextField commit-on-blur path; annotation fields are now a dialog draft)
  editor-focus-request.test.tsx: « Aller à » focuses `data-properties-heading` inside `annotation-properties` -> focuses the annotation's row, marked aria-current, no dialog (R-5)
  editor-sections.test.tsx: « Aller à » with nothing in progress focused the properties heading -> focuses the row (R-5); creation kept: no `annotation-properties` -> no strip « Propriétés »
  editor-overview.test.tsx (edit and read-only): row opens `annotation-properties`, « Toutes les annotations » returns -> row selects, stays marked aria-current, no dialog; strip « Désélectionner » returns focus to the row; filter kept across select and « Désélectionner »
  editor-citations-guards.test.tsx: LAYERS_TOP_DOWN gains 'annotation' at the bottom
  editor-keep-alive.test.tsx: `annotation-properties` visible after the round trip -> the strip's « Propriétés » visible (selection kept)
  editor-no-loss.test.tsx: ids `ro-annotation-name-fr` and `annotation-name-fr` (both panels' fields) -> the twin's inline read body (no ids), then the dialog edit form's `annotation-edit-fr`, kept through a flip to 800 px, no duplicate id; « Aller à » visible `annotation-properties` -> visible `annotation-read` (inline twin), none at 1 280
  editor-point-edit.test.tsx / editor-zone-edit.test.tsx: `select()` waited for `annotation-properties` and returned it -> waits for the strip's « Propriétés » and returns the strip (its « Désélectionner » and « Remplacer par un polygone » moved there)
  - move mode Escape: `annotation-properties` still there -> the strip's « Propriétés » back after the mode
  - « Toutes les annotations » asks first during a started move -> Enter on the selected row asks first (no dialog), and the strip offers neither « Propriétés » nor « Désélectionner » during the mode
  - Escape from the handle / slider: `annotation-properties` gone -> the strip's « Propriétés » gone
  - I-3 touch tap: `annotation-properties` gone -> the strip's « Propriétés » gone
  e2e (sandbox):
  - support/layout.ts: `aside-content` exemption removed (ShiftExemption type, the loose compare); every `swaps`/`SWAPS` argument removed (editor-annotations, editor-zones, editor-geometry)
  - support/editor.ts pickAnnotation: clicked « Toutes les annotations » first when the panel showed -> the overview is always shown: clicks the row, then scrolls back to the top (the page no longer shortens)
  - editor-annotations: `annotation-properties` visible after a creation -> strip with « Propriétés »; review toggle and « Supprimer » of the panel -> the properties dialog's; a jitter click on the selected point now opens the dialog (A1), closed by Escape before the drag; handle names exact (the marked row shares the prefix); trace close moved out of expectNoShift (creation panel until Task 7, shortcut + TODO)
  - editor-deselect: `annotation-properties` -> the strip with « Propriétés » or the twin's inline read body; Escape in a field: panel « Nom (FR) » -> overview filter; delete dialog from the panel -> from the properties dialog (Escape closes it, then the dialog); « a typed value is applied by the click that deselects » (name field, refused blank) -> a pending point-handle key series applied by the deselecting click and by « Désélectionner » (refused value: no field outside the dialog); « the selected one stays » -> a second click, double click and jitter open the dialog, Escape closes it, selection kept
  - editor-geometry: `annotation-geometry-issues` in the panel -> read in the properties dialog, then the strip notice; after the refusal the error count unchanged; scroll-to-top workaround removed
  - editor-zones: panel « Désélectionner » -> strip; scroll-to-top after pickAnnotation removed (helper does it)
  - editor-structures: panel « Supprimer » -> properties dialog « Supprimer », confirmation over it
  - editor-journey: `#annotation-name-fr` committed field + `#annotation-laterality` -> the dialog's edit form, one « Enregistrer »; scroll-to-top removed
  - editor-shell / editor-shell-journeys: « Aller à » focus on `data-properties-heading` -> the annotation's row (aria-current), no dialog (R-5)
  - editor-shell-layout: focus kind `annotation-properties` -> removed (the overview is the side)
2026-11-17 22:01:57 +01:00

452 lines
18 KiB
TypeScript

/** R2F-1: the history entry, its ownership, the write plan and the write budget. */
import { describe, expect, it } from 'vitest'
import type { ElementKey } from '@faunosteo/core-wasm/types'
import {
createWriteBudget,
ENTRY_KEY,
layerCount,
ownedEntry,
planWrite,
readEntry,
sanitizeView,
type HistoryEntry,
} from '@/editor/history/entry'
import {
defaultView,
initialNavigation,
navigationReducer,
sameView,
viewOf,
type EditorView,
} from '@/editor/navigation'
const view = (tab: EditorView['tab'], patch: Partial<EditorView> = {}) => ({
...defaultView(tab),
...patch,
})
const entry = (patch: Partial<HistoryEntry> = {}): HistoryEntry => ({
id: 'e1',
view: view('general'),
below: null,
sheetId: 'S',
tag: 'T',
...patch,
})
const stateOf = (value: unknown) => ({ [ENTRY_KEY]: value })
const node: ElementKey = { kind: 'finding', zone_id: 'z', path: ['f', 'n'] }
describe('sanitizeView', () => {
it('keeps a tab and a section, and drops a malformed layer', () => {
const raw = { tab: 'annotations', section: 'views', finding: { key: {} } }
expect(sanitizeView(raw)).toEqual(view('annotations', { section: 'views' }))
})
it('keeps the reading layers of Constats: the dialog and the sheet over it', () => {
const dialog = {
tab: 'findings',
section: 'drawing',
finding: { key: node },
}
expect(sanitizeView(dialog)?.finding).toEqual({
key: node,
citations: false,
})
const sheet = { ...dialog, finding: { key: node, citations: true } }
expect(sanitizeView(sheet)?.finding).toEqual({ key: node, citations: true })
expect(layerCount(sanitizeView(sheet)!)).toBe(2)
})
it('citations need a family or a node, a layer needs the Constats tab', () => {
const family = { kind: 'finding', zone_id: 'z', path: ['f'] }
const zone = { kind: 'zone', id: 'z' }
const raw = { tab: 'findings', section: 'drawing' }
const asked = { ...raw, finding: { key: family, citations: true } }
expect(sanitizeView(asked)?.finding).toEqual({
key: family,
citations: true,
})
const ofZone = { ...raw, finding: { key: zone, citations: true } }
expect(sanitizeView(ofZone)?.finding).toEqual({
key: zone,
citations: false,
})
const elsewhere = {
tab: 'general',
finding: { key: node, citations: false },
}
expect(sanitizeView(elsewhere)?.finding).toBeNull()
})
it('a creation is a form: read back only where forms are kept', () => {
const raw = {
tab: 'findings',
section: 'drawing',
finding: { create: { kind: 'node', parent: node } },
}
expect(sanitizeView(raw)?.finding).toEqual({
create: { kind: 'node', parent: node },
})
expect(sanitizeView(raw, false)?.finding).toBeNull()
const odd = { ...raw, finding: { create: { kind: 'x', parent: null } } }
expect(sanitizeView(odd)?.finding).toBeNull()
})
it('the reference dialog is a form: over References or the sheet, never at a reload', () => {
const edit = { edit: 'ref-a' }
const tab = { ...view('references'), reference: edit }
expect(sanitizeView(tab)?.reference).toEqual(edit)
expect(sanitizeView(tab, false)?.reference).toBeNull()
const sheet = {
...view('findings'),
finding: { key: node, citations: true },
reference: edit,
}
expect(sanitizeView(sheet)?.reference).toEqual(edit)
const noSheet = { ...sheet, finding: { key: node, citations: false } }
expect(sanitizeView(noSheet)?.reference).toBeNull()
const creation = { ...view('references'), reference: { create: true } }
expect(sanitizeView(creation)?.reference).toEqual({ create: true })
const elsewhere = { ...view('general'), reference: { create: true } }
expect(sanitizeView(elsewhere)?.reference).toBeNull()
// Rev 5: a creation over the citations dialog is never read back (POP, reload).
const overCitations = { ...sheet, reference: { create: true } }
expect(sanitizeView(overCitations)?.reference).toBeNull()
expect(sanitizeView(overCitations)?.finding).toEqual(sheet.finding)
expect(
sanitizeView({ ...tab, reference: { edit: '' } })?.reference,
).toBeNull()
})
it('the finding filter names a family or a node and lives on the References tab only', () => {
const filtered = view('references', { referenceFilter: node })
expect(sanitizeView(filtered)?.referenceFilter).toEqual(node)
const family = { kind: 'finding' as const, zone_id: 'z', path: ['f'] }
const byFamily = view('references', { referenceFilter: family })
expect(sanitizeView(byFamily)?.referenceFilter).toEqual(family)
expect(sanitizeView(filtered, false)?.referenceFilter).toEqual(node)
expect(
sanitizeView({ ...view('general'), referenceFilter: node })
?.referenceFilter,
).toBeNull()
const zone = { kind: 'zone', id: 'z' }
expect(
sanitizeView({ ...view('references'), referenceFilter: zone })
?.referenceFilter,
).toBeNull()
})
it('drops what is no view: an unknown tab, a non-object', () => {
expect(sanitizeView({ tab: 'nowhere', section: 'views' })).toBeNull()
expect(sanitizeView('general')).toBeNull()
expect(sanitizeView(null)).toBeNull()
})
it('falls back to the drawing on an unknown section', () => {
expect(sanitizeView({ tab: 'annotations', section: 'x' })?.section).toBe(
'drawing',
)
})
})
describe('readEntry and ownership', () => {
it('reads a well-formed entry and nothing else', () => {
expect(readEntry(stateOf(entry()))).toEqual(entry())
expect(readEntry(null)).toBeNull()
expect(readEntry({})).toBeNull()
expect(readEntry(stateOf({ ...entry(), id: 3 }))).toBeNull()
expect(readEntry(stateOf({ ...entry(), view: 'x' }))).toBeNull()
})
it('restores only for the sheet and the session tag that wrote it', () => {
const mine = { sheetId: 'S', tag: 'T' }
expect(ownedEntry(stateOf(entry()), mine)).not.toBeNull()
expect(ownedEntry(stateOf(entry()), { ...mine, sheetId: 'X' })).toBeNull()
expect(ownedEntry(stateOf(entry()), { ...mine, tag: 'V' })).toBeNull()
})
it('refuses an owner without a sheet or a tag: no tag yet restores nothing', () => {
const blank = entry({ sheetId: '', tag: '' })
expect(ownedEntry(stateOf(blank), { sheetId: '', tag: '' })).toBeNull()
expect(ownedEntry(stateOf(entry()), { sheetId: 'S', tag: '' })).toBeNull()
})
it('reads an entry of the older shape (an account id, no tag) as foreign', () => {
const { id, view, below, sheetId } = entry()
const older = { id, view, below, sheetId, userId: 'T' }
expect(readEntry(stateOf(older))).toBeNull()
expect(ownedEntry(stateOf(older), { sheetId: 'S', tag: 'T' })).toBeNull()
})
it('never writes an account id in the entry', () => {
expect(Object.keys(readEntry(stateOf(entry()))!).sort()).toEqual([
'below',
'id',
'sheetId',
'tag',
'view',
])
})
it('keeps only navigation: nothing typed survives the read', () => {
const raw = { ...entry(), draft: 'typed text', term: 'search' }
expect(JSON.stringify(readEntry(stateOf(raw)))).not.toContain('typed')
})
})
describe('planWrite', () => {
const top = entry({ view: view('general'), below: null })
it('does nothing for the same view', () => {
expect(planWrite(top, view('general'), false)).toBe('none')
// a section only counts on Annotations
const other = view('general', { section: 'views' })
expect(planWrite(top, other, false)).toBe('none')
})
it('pushes an added tab or section, replaces on a replace hint', () => {
expect(planWrite(top, view('findings'), false)).toBe('push')
expect(planWrite(top, view('findings'), true)).toBe('replace')
const onAnnotations = entry({ view: view('annotations') })
const views = view('annotations', { section: 'views' })
expect(planWrite(onAnnotations, views, false)).toBe('push')
})
it('goes back for a section left for its drawing, else replaces', () => {
const drawing = view('annotations')
const inSection = entry({
view: view('annotations', { section: 'views' }),
below: drawing,
})
expect(planWrite(inSection, drawing, false)).toBe('back')
const reloaded = entry({ view: inSection.view, below: null })
expect(planWrite(reloaded, drawing, false)).toBe('replace')
// another section, or another tab, is a step forward
const regions = view('annotations', { section: 'regions' })
expect(planWrite(inSection, regions, false)).toBe('push')
expect(planWrite(inSection, view('general'), false)).toBe('push')
})
it('a finding dialog and its sheet go back one layer at a time', () => {
const dialog = view('findings', {
finding: { key: node, citations: false },
})
const sheet = view('findings', { finding: { key: node, citations: true } })
expect(planWrite(entry({ view: view('findings') }), dialog, false)).toBe(
'push',
)
expect(planWrite(entry({ view: dialog }), sheet, false)).toBe('push')
const onSheet = entry({ view: sheet, below: dialog })
expect(planWrite(onSheet, dialog, false)).toBe('back')
const opened = entry({ view: dialog, below: view('findings') })
expect(planWrite(opened, view('findings'), false)).toBe('back')
})
it('a layer left for another tab is a push, a replace hint overrides a removal', () => {
const sheet = view('findings', { finding: { key: node, citations: true } })
const top = entry({ view: sheet, below: view('findings') })
// « Tout afficher » and « Aller à »: a step forward, Back returns to the sheet
expect(planWrite(top, view('references'), false)).toBe('push')
// a deletion or a vanished element: no Back step to a dead dialog
expect(planWrite(top, view('findings'), true)).toBe('replace')
// a creation turned into its element: same layer, replaced
const create = view('findings', {
finding: { create: { kind: 'node', parent: null } },
})
const created = view('findings', {
finding: { key: node, citations: false },
})
expect(planWrite(entry({ view: create }), created, true)).toBe('replace')
})
it('goes back for a layer removed when the entry holds the view below', () => {
const layer = view('findings', { reference: { create: true } })
const opened = entry({ view: layer, below: view('findings') })
expect(layerCount(layer)).toBe(1)
expect(planWrite(opened, view('findings'), false)).toBe('back')
})
it('replaces a layer removed when the entry below is another view', () => {
const layer = view('findings', { reference: { create: true } })
const opened = entry({ view: layer, below: view('general') })
expect(planWrite(opened, view('findings'), false)).toBe('replace')
expect(planWrite(entry({ view: layer }), view('findings'), false)).toBe(
'replace',
)
})
})
describe('planWrite and the reference layers', () => {
const sheet = view('findings', { finding: { key: node, citations: true } })
const dialog = { ...sheet, reference: { edit: 'ref-a' } }
const top = (v: EditorView, below: EditorView | null) =>
entry({ view: v, below })
it('the reference dialog is a layer: added pushes, removed goes back to the view below', () => {
expect(planWrite(top(sheet, null), dialog, false)).toBe('push')
expect(planWrite(top(dialog, sheet), sheet, false)).toBe('back')
expect(planWrite(top(dialog, view('general')), sheet, false)).toBe(
'replace',
)
expect(layerCount(dialog)).toBe(3)
})
it('a creation turned into its element is a replace; the filter removal is a replace hint', () => {
const creating = view('references', { reference: { create: true } })
const editing = view('references', { reference: { edit: 'ref-n' } })
expect(planWrite(top(creating, null), editing, true)).toBe('replace')
const filtered = view('references', { referenceFilter: node })
expect(planWrite(top(filtered, sheet), view('references'), true)).toBe(
'replace',
)
})
it('« Revenir au constat » goes back when the entry below shows that finding, else pushes', () => {
const filtered = view('references', { referenceFilter: node })
const dialogOnly = view('findings', {
finding: { key: node, citations: false },
})
expect(planWrite(top(filtered, sheet), dialogOnly, false, node)).toBe(
'back',
)
expect(
planWrite(top(filtered, view('general')), dialogOnly, false, node),
).toBe('push')
expect(planWrite(top(filtered, sheet), dialogOnly, false)).toBe('push')
})
})
describe('createWriteBudget', () => {
it('lets the limit through per window, then refuses until the window slides', () => {
const take = createWriteBudget(3, 1000)
expect([0, 100, 200].map(take)).toEqual([true, true, true])
expect(take(300)).toBe(false)
expect(take(999)).toBe(false)
expect(take(1000)).toBe(true)
})
})
describe('the view the reducer holds', () => {
it('restore_view lands on a view without a move or a focus request', () => {
const state = initialNavigation('general')
const next = navigationReducer(state, {
type: 'restore_view',
view: view('annotations', { section: 'regions' }),
})
expect(viewOf(next)).toEqual(view('annotations', { section: 'regions' }))
expect(next.focus).toBeUndefined()
expect(next.visitedSections).toContain('regions')
})
it('keeps the remembered section when the view is on another tab', () => {
const base = navigationReducer(initialNavigation('annotations'), {
type: 'open_section',
section: 'views',
})
const next = navigationReducer(base, {
type: 'restore_view',
view: view('general'),
})
expect(next.section).toBe('views')
expect(
sameView(viewOf(next), view('general', { section: 'drawing' })),
).toBe(true)
})
it('opens the state on a view read at mount', () => {
const state = initialNavigation('annotations', view('findings'))
expect(state.tab).toBe('findings')
expect(state.visitedTabs).toEqual(['findings'])
})
})
describe('final review: « Aller à » is a move of its own (I-2)', () => {
it('pushes over the sheet it leaves, even with fewer layers on the same tab', () => {
const sheet = view('findings', { finding: { key: node, citations: true } })
const other: ElementKey = {
kind: 'finding',
zone_id: 'z',
path: ['f', 'm'],
}
const target = view('findings', {
finding: { key: other, citations: false },
})
const top = entry({ view: sheet, below: view('findings') })
expect(planWrite(top, target, false)).toBe('replace')
expect(planWrite(top, target, false, null, true)).toBe('push')
// An arrow-key replace still wins.
expect(planWrite(top, target, true, null, true)).toBe('replace')
})
})
describe('final review: read-back values are capped', () => {
const long = 'x'.repeat(129)
const atCap = 'x'.repeat(128)
const deep = (length: number): ElementKey => ({
kind: 'finding',
zone_id: 'z',
path: Array.from({ length }, (_, i) => `p${i}`),
})
const onFindings = (key: ElementKey) =>
view('findings', { finding: { key, citations: false } })
it('drops an entry whose id, sheet or tag is over 128 characters', () => {
expect(readEntry(stateOf(entry({ id: atCap })))).not.toBeNull()
expect(readEntry(stateOf(entry({ id: long })))).toBeNull()
expect(readEntry(stateOf(entry({ sheetId: long })))).toBeNull()
expect(readEntry(stateOf(entry({ tag: long })))).toBeNull()
})
it('drops a key with an over-long id or a path deeper than the tree allows', () => {
const zone: ElementKey = { kind: 'zone', id: long }
expect(
readEntry(stateOf(entry({ view: onFindings(zone) })))?.view.finding,
).toBeNull()
const wideZone: ElementKey = { kind: 'finding', zone_id: long, path: ['a'] }
expect(sanitizeView(onFindings(wideZone))?.finding).toBeNull()
const wideId: ElementKey = { kind: 'finding', zone_id: 'z', path: [long] }
expect(sanitizeView(onFindings(wideId))?.finding).toBeNull()
expect(sanitizeView(onFindings(deep(8)))?.finding).not.toBeNull()
expect(sanitizeView(onFindings(deep(9)))?.finding).toBeNull()
const edit = { ...onFindings(node), reference: { edit: long } }
const sheet = { ...edit, finding: { key: node, citations: true } }
expect(sanitizeView(sheet)?.reference).toBeNull()
})
it('drops a key whose path is a sparse array', () => {
const path: string[] = []
path[1] = 'n'
const sparse: ElementKey = { kind: 'finding', zone_id: 'z', path }
expect(sanitizeView(onFindings(sparse))?.finding).toBeNull()
})
})
/** U2 Task 6: the annotation dialog's layer in an entry. */
describe('the annotation layer in the history', () => {
it('is read back on the drawing of Annotations only, with an id within the cap', () => {
const raw = {
tab: 'annotations',
section: 'drawing',
annotation: { id: 'a1' },
}
expect(sanitizeView(raw)?.annotation).toEqual({ id: 'a1' })
expect(layerCount(sanitizeView(raw)!)).toBe(1)
expect(sanitizeView({ ...raw, section: 'views' })?.annotation).toBeNull()
expect(sanitizeView({ ...raw, tab: 'general' })?.annotation).toBeNull()
expect(
sanitizeView({ ...raw, annotation: { id: 7 } })?.annotation,
).toBeNull()
const long = { ...raw, annotation: { id: 'x'.repeat(200) } }
expect(sanitizeView(long)?.annotation).toBeNull()
})
it('opened pushes, closed goes back to the drawing below', () => {
const drawing = view('annotations')
const dialog = view('annotations', { annotation: { id: 'a1' } })
expect(sameView(drawing, dialog)).toBe(false)
expect(planWrite(entry({ view: drawing }), dialog, false)).toBe('push')
const top = entry({ view: dialog, below: drawing })
expect(planWrite(top, drawing, false)).toBe('back')
})
})