Constats opens each element in one modal dialog (view, edit with a local draft, creation) driven by the editor view layer of the browser history. The citations panel becomes a modal side sheet stacked over it; its geometry, exit machinery and page inertness are removed. Back and Forward restore the layers, a dirty form asks first.
256 lines
9.3 KiB
TypeScript
256 lines
9.3 KiB
TypeScript
/** R2F-1: the history entry, its ownership, the write plan and the write budget. */
|
|
import { describe, expect, it } from 'vitest'
|
|
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',
|
|
userId: 'U',
|
|
...patch,
|
|
})
|
|
const stateOf = (value: unknown) => ({ [ENTRY_KEY]: value })
|
|
|
|
const node = { kind: 'finding', zone_id: 'z', path: ['f', 'n'] } as const
|
|
|
|
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('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 account that wrote it', () => {
|
|
const mine = { sheetId: 'S', userId: 'U' }
|
|
expect(ownedEntry(stateOf(entry()), mine)).not.toBeNull()
|
|
expect(ownedEntry(stateOf(entry()), { ...mine, sheetId: 'T' })).toBeNull()
|
|
expect(ownedEntry(stateOf(entry()), { ...mine, userId: 'V' })).toBeNull()
|
|
})
|
|
|
|
it('refuses an owner without a sheet or an account', () => {
|
|
const blank = entry({ sheetId: '', userId: '' })
|
|
expect(ownedEntry(stateOf(blank), { sheetId: '', userId: '' })).toBeNull()
|
|
expect(
|
|
ownedEntry(stateOf(entry()), { sheetId: 'S', userId: '' }),
|
|
).toBeNull()
|
|
})
|
|
|
|
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('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'])
|
|
})
|
|
})
|