403 lines
15 KiB
TypeScript
403 lines
15 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('a sheet needs a finding node, a layer needs the Constats tab', () => {
|
|
const family = { kind: 'finding', zone_id: 'z', path: ['f'] }
|
|
const raw = { tab: 'findings', section: 'drawing' }
|
|
const asked = { ...raw, finding: { key: family, citations: true } }
|
|
expect(sanitizeView(asked)?.finding).toEqual({
|
|
key: family,
|
|
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()
|
|
expect(
|
|
sanitizeView({ ...tab, reference: { edit: '' } })?.reference,
|
|
).toBeNull()
|
|
})
|
|
|
|
it('the finding filter names a finding node and lives on the References tab only', () => {
|
|
const filtered = view('references', { referenceFilter: node })
|
|
expect(sanitizeView(filtered)?.referenceFilter).toEqual(node)
|
|
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()
|
|
})
|
|
})
|