Important 1: a touch press fires pointer leave between up and click; only a mouse leave (or a pointer cancel) forgets the refused field, so Annuler no longer undoes an unrelated change. Important 2: the LAN spec reads the revision before the attach, through shownRevision, which throws instead of NaN (spec stays not run). Minor 1: refocusIfHidden refocuses a still shown element the commit blurred (references search on a 1 024 px crossing). Minor 2: discardChanges keeps lastSave (reload path still drops it). Minor 3: the overview filter term lives in UiState / the read-only twin, not the card. Minor 4: categories section drops its inner h3 and the unused editor.general.categories keys. AC8: badge test. Assertions rewritten or added (R-24): - editor-tabs-general « Général no longer holds the categories editor »: queryByText of the deleted heading (now vacuous) -> queryByLabelText of the category name fields. - editor-focus-request « Annotations: selects the annotation »: findAllByText /anno-b|Sélection/ -> the properties panel holds Centre x = 20 (anno-b) and owns the focused heading. - editor-shell-layout panel walk: added after.at(-1) === 'side'. - editor-recovery title « Annuler les modifications » -> « Abandonner ». - new: touch/cancel/mouse press, references search crossing, discard keeps lastSave, filter kept, one categories heading, badge 1/2/0, shownRevision.
335 lines
15 KiB
TypeScript
335 lines
15 KiB
TypeScript
/** Lot D: the local recovery copy (offer, restore, ignore, cap, quota) on the Node core. */
|
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
import { SheetFailure } from '@faunosteo/supabase'
|
|
import { leaveGuardArmed } from '@/editor/state/editorStore'
|
|
import {
|
|
SHEET, USER, VERSION, baseText, commitMove, copyKeyOf, openReady, resetStore, sb, serverHas,
|
|
storedCopy, store,
|
|
} from '../helpers/editorHarness'
|
|
import { ASSET, svgBytes } from '../helpers/editorFixtures'
|
|
|
|
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(),
|
|
}))
|
|
|
|
afterEach(resetStore)
|
|
|
|
/** The text of the server document with anno-a moved: what a copy would hold. */
|
|
function movedText(x: number): string {
|
|
return JSON.stringify(
|
|
JSON.parse(baseText(), (key, value) => (key === 'cx' ? x : value)),
|
|
)
|
|
}
|
|
|
|
function seed(text: string, revision: number, over: Record<string, unknown> = {}, version = VERSION, user = USER) {
|
|
sessionStorage.setItem(
|
|
copyKeyOf(version, user),
|
|
JSON.stringify({
|
|
v: 1, user_id: user, version_id: version, sheet_id: SHEET, sheet_name: 'Fiche',
|
|
revision, written_at: '2026-11-03T10:00:00Z', document: JSON.parse(text), drawings: [], ...over,
|
|
}),
|
|
)
|
|
}
|
|
|
|
describe('writing the copy', () => {
|
|
it('writes synchronously on a committed change while unsaved, with the base revision', async () => {
|
|
await openReady({ revision: 3 })
|
|
expect(storedCopy()).toBeNull()
|
|
commitMove(40)
|
|
expect(storedCopy()).toMatchObject({
|
|
v: 1, user_id: USER, version_id: VERSION, sheet_id: SHEET, sheet_name: 'Fiche', revision: 3,
|
|
document: JSON.parse(store.getState().documentText),
|
|
drawings: [{ asset_id: ASSET }],
|
|
})
|
|
expect(store.getState()).toMatchObject({ copyWritten: true, copyIssue: null })
|
|
expect(leaveGuardArmed(store.getState())).toBe(true)
|
|
})
|
|
|
|
it('leaves the copy untouched in a mode that cannot save', async () => {
|
|
seed(movedText(10), 3)
|
|
const raw = sessionStorage.getItem(copyKeyOf())
|
|
await openReady({ canEdit: false })
|
|
expect(store.getState()).toMatchObject({ recovery: 'none', offer: null, unusableCopy: true })
|
|
expect(sessionStorage.getItem(copyKeyOf())).toBe(raw)
|
|
})
|
|
})
|
|
|
|
describe('the blocking offer', () => {
|
|
it('refuses every change and never writes or removes the copy while pending', async () => {
|
|
seed(movedText(10), 3)
|
|
const raw = sessionStorage.getItem(copyKeyOf())
|
|
await openReady({ revision: 3 })
|
|
const state = store.getState()
|
|
expect(state).toMatchObject({ recovery: 'pending', offer: { revision: 3, serverRevision: 3 } })
|
|
expect(leaveGuardArmed(state)).toBe(true)
|
|
const blocked = { ok: false, reason: 'blocked', kind: 'recovery.pending' }
|
|
expect(commitMove(40)).toMatchObject(blocked)
|
|
expect(state.undoStep()).toMatchObject(blocked)
|
|
expect(state.redoStep()).toMatchObject(blocked)
|
|
expect(await state.save()).toMatchObject(blocked)
|
|
expect(await state.submit()).toMatchObject(blocked)
|
|
expect(await state.discardChanges({ confirmed: true })).toMatchObject(blocked)
|
|
expect(await state.reloadServerDraft({ confirmed: true })).toMatchObject(blocked)
|
|
expect(sb.saveSheetDraft).not.toHaveBeenCalled()
|
|
expect(sessionStorage.getItem(copyKeyOf())).toBe(raw)
|
|
expect(state.documentText).toBe(state.savedText)
|
|
})
|
|
|
|
it('offers nothing for a copy equal to the server document, whatever the key order', async () => {
|
|
seed(baseText(), 3)
|
|
await openReady()
|
|
expect(store.getState().recovery).toBe('none')
|
|
})
|
|
|
|
it('needs a confirmation to ignore, then removes the copy and resumes from the server', async () => {
|
|
seed(movedText(10), 3)
|
|
await openReady()
|
|
expect(store.getState().ignoreCopy({ confirmed: false })).toMatchObject({ kind: 'confirmation_required' })
|
|
expect(storedCopy()).not.toBeNull()
|
|
expect(store.getState().ignoreCopy({ confirmed: true })).toEqual({ ok: true })
|
|
expect(storedCopy()).toBeNull()
|
|
expect(store.getState()).toMatchObject({ recovery: 'none', offer: null })
|
|
expect(commitMove(40)).toEqual({ ok: true })
|
|
})
|
|
})
|
|
|
|
describe('restore', () => {
|
|
it('applies the copy in one store update and rewrites it on its base revision', async () => {
|
|
seed(movedText(10), 3)
|
|
await openReady({ revision: 3 })
|
|
const seen: Array<{ recovery: string; text: string }> = []
|
|
const stop = store.subscribe((s) => seen.push({ recovery: s.recovery, text: s.documentText }))
|
|
expect(await store.getState().restoreCopy()).toEqual({ ok: true })
|
|
stop()
|
|
const state = store.getState()
|
|
expect(state.documentText).toBe(movedText(10))
|
|
expect(state).toMatchObject({ savedRevision: 3, staleBase: false, unsaved: true, recovery: 'none' })
|
|
expect(state.savedText).toBe(JSON.stringify(JSON.parse(baseText())))
|
|
expect(seen.filter((s) => s.recovery === 'none').every((s) => s.text === movedText(10))).toBe(true)
|
|
expect(storedCopy()).toMatchObject({ revision: 3, document: JSON.parse(movedText(10)) })
|
|
expect(state.previewState?.document.annotations[0]).toMatchObject({ geometry: { cx: 10 } })
|
|
})
|
|
|
|
it('downloads and adds the copy drawings the page lacks', async () => {
|
|
serverHas(baseText(), 3, false)
|
|
sb.downloadSheetDrawing.mockResolvedValue(svgBytes().bytes)
|
|
seed(movedText(10), 3, { drawings: [{ asset_id: ASSET, sha256: svgBytes().sha256 }] })
|
|
store.getState().open(VERSION, USER)
|
|
await vi.waitFor(() => expect(store.getState().status).toBe('ready'))
|
|
expect(store.getState().previewState?.drawings[0]).toMatchObject({ state: 'missing' })
|
|
await store.getState().restoreCopy()
|
|
expect(store.getState().previewState?.drawings[0]).toMatchObject({ state: 'ok' })
|
|
expect(store.getState().drawings).toEqual([expect.objectContaining({ asset_id: ASSET })])
|
|
})
|
|
|
|
it('keeps the base revision R of an older copy, sets staleBase and meets the conflict', async () => {
|
|
seed(movedText(10), 2)
|
|
await openReady({ revision: 5 })
|
|
expect(store.getState().offer).toMatchObject({ revision: 2, serverRevision: 5 })
|
|
await store.getState().restoreCopy()
|
|
expect(store.getState()).toMatchObject({ savedRevision: 2, staleBase: true, unsaved: true })
|
|
commitMove(30)
|
|
expect(storedCopy()).toMatchObject({ revision: 2 })
|
|
expect(await store.getState().submit()).toMatchObject({ ok: false, kind: 'stale_base' })
|
|
sb.saveSheetDraft.mockRejectedValue(new SheetFailure('draft_conflict'))
|
|
sb.getOfficialSheetAdmin.mockResolvedValue({
|
|
draft_version: { version_id: VERSION, version_number: 1, revision: 5 },
|
|
draft_last_save: null,
|
|
} as never)
|
|
await store.getState().save()
|
|
expect(sb.saveSheetDraft).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({ expected_revision: 2 }))
|
|
expect(store.getState().conflict).toMatchObject({ status: 'known', revision: 5 })
|
|
})
|
|
|
|
it('stays unsaved under staleBase when edited back to the server text (no dead end)', async () => {
|
|
seed(movedText(10), 2)
|
|
await openReady({ revision: 5 })
|
|
await store.getState().restoreCopy()
|
|
expect(commitMove(50)).toMatchObject({ ok: true })
|
|
expect(store.getState().documentText).toBe(store.getState().savedText)
|
|
expect(store.getState()).toMatchObject({ staleBase: true, unsaved: true })
|
|
})
|
|
|
|
it('a restored copy holding bare nodes takes the core defaults-filled document', async () => {
|
|
const bare = JSON.parse(baseText())
|
|
bare.finding_zones = [
|
|
{
|
|
id: 'z', name: { fr: 'Z', en: 'Z' },
|
|
categories: [{ id: 'c', kind: 'dysfunction', name: { fr: 'C', en: 'C' }, children: [{ id: 'n', name: { fr: 'N', en: 'N' } }] }],
|
|
},
|
|
]
|
|
seed(JSON.stringify(bare), 3)
|
|
await openReady({ revision: 3 })
|
|
await store.getState().restoreCopy()
|
|
const node = JSON.parse(store.getState().documentText).finding_zones[0].categories[0].children[0]
|
|
expect(node).toMatchObject({ reference_ids: [], children: [], review_status: 'inherited' })
|
|
})
|
|
|
|
it('takes the reload path for « Abandonner les modifications » under staleBase', async () => {
|
|
seed(movedText(10), 2)
|
|
await openReady({ revision: 5 })
|
|
await store.getState().restoreCopy()
|
|
serverHas(baseText(), 6)
|
|
expect(await store.getState().discardChanges({ confirmed: true })).toEqual({ ok: true })
|
|
expect(store.getState()).toMatchObject({ savedRevision: 6, staleBase: false, unsaved: false })
|
|
expect(store.getState().documentText).toBe(store.getState().savedText)
|
|
expect(storedCopy()).toBeNull()
|
|
})
|
|
|
|
it('keeps `lastSave` on a plain discard, drops it on the reload path', async () => {
|
|
await openReady({ revision: 3 })
|
|
const saved = { revision: 3, savedAt: '2026-11-04T13:32:00Z' }
|
|
store.setState({ lastSave: saved })
|
|
commitMove(40)
|
|
expect(await store.getState().discardChanges({ confirmed: true })).toEqual({ ok: true })
|
|
expect(store.getState().lastSave).toBe(saved)
|
|
store.setState({ staleBase: true })
|
|
serverHas(baseText(), 6)
|
|
await store.getState().discardChanges({ confirmed: true })
|
|
expect(store.getState().lastSave).toBeNull()
|
|
})
|
|
|
|
it('reports a copy the core refuses, keeps it, blocks editing until ignored', async () => {
|
|
sessionStorage.setItem(
|
|
copyKeyOf(),
|
|
JSON.stringify({
|
|
v: 1, user_id: USER, version_id: VERSION, sheet_id: SHEET, sheet_name: 'X',
|
|
revision: 3, written_at: 'T', document: { not: 'a document' }, drawings: [],
|
|
}),
|
|
)
|
|
const raw = sessionStorage.getItem(copyKeyOf())
|
|
await openReady()
|
|
const outcome = await store.getState().restoreCopy()
|
|
expect(outcome).toMatchObject({ ok: false, reason: 'core' })
|
|
const state = store.getState()
|
|
expect(state.recovery).toBe('refused')
|
|
expect(state.refusedKind).toEqual(expect.any(String))
|
|
expect(state.documentText).toBe(state.savedText)
|
|
expect(commitMove(40)).toMatchObject({ kind: 'recovery.pending' })
|
|
expect(await state.restoreCopy()).toMatchObject({ ok: false })
|
|
expect(sessionStorage.getItem(copyKeyOf())).toBe(raw)
|
|
expect(store.getState().ignoreCopy({ confirmed: true })).toEqual({ ok: true })
|
|
expect(commitMove(40)).toEqual({ ok: true })
|
|
})
|
|
})
|
|
|
|
describe('an unreadable copy', () => {
|
|
it.each([
|
|
['malformed JSON', () => 'not json{'],
|
|
['an unknown version', () => JSON.stringify({ v: 2 })],
|
|
['another user in the key', () =>
|
|
JSON.stringify({ v: 1, user_id: 'x', version_id: VERSION, sheet_id: SHEET, sheet_name: '', revision: 1, written_at: 'T', document: {}, drawings: [] })],
|
|
])('is refused and kept byte for byte (%s)', async (_label, raw) => {
|
|
sessionStorage.setItem(copyKeyOf(), raw())
|
|
await openReady()
|
|
expect(store.getState()).toMatchObject({ recovery: 'refused', refusedKind: 'unreadable' })
|
|
expect(commitMove(40)).toMatchObject({ kind: 'recovery.pending' })
|
|
expect(sessionStorage.getItem(copyKeyOf())).toBe(raw())
|
|
expect(leaveGuardArmed(store.getState())).toBe(true)
|
|
expect(store.getState().ignoreCopy({ confirmed: false })).toMatchObject({ kind: 'confirmation_required' })
|
|
expect(store.getState().ignoreCopy({ confirmed: true })).toEqual({ ok: true })
|
|
expect(storedCopy()).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('cap, quota and other accounts', () => {
|
|
const other = (n: number) => `ver-other-${n}`
|
|
|
|
it('refuses a third version, keeps both others, retries after an explicit abandonment', async () => {
|
|
seed(baseText(), 1, {}, other(1))
|
|
seed(baseText(), 2, { sheet_name: 'Autre' }, other(2))
|
|
await openReady()
|
|
commitMove(40)
|
|
expect(store.getState()).toMatchObject({ copyWritten: false, copyIssue: 'cap' })
|
|
expect(storedCopy()).toBeNull()
|
|
expect(store.getState().otherCopies.map((c) => [c.versionId, c.revision])).toEqual([
|
|
[other(1), 1], [other(2), 2],
|
|
])
|
|
expect(store.getState().abandonOtherCopy(other(1), { confirmed: false })).toMatchObject({ kind: 'confirmation_required' })
|
|
expect(storedCopy(other(1))).not.toBeNull()
|
|
expect(store.getState().abandonOtherCopy(other(1), { confirmed: true })).toEqual({ ok: true })
|
|
expect(storedCopy(other(1))).toBeNull()
|
|
expect(storedCopy(other(2))).not.toBeNull()
|
|
expect(storedCopy()).not.toBeNull()
|
|
expect(store.getState()).toMatchObject({ copyWritten: true, copyIssue: null })
|
|
})
|
|
|
|
it('reports a full storage and keeps the work in the document', async () => {
|
|
await openReady()
|
|
const spy = vi.spyOn(sessionStorage, 'setItem').mockImplementation(() => {
|
|
throw new DOMException('full', 'QuotaExceededError')
|
|
})
|
|
commitMove(40)
|
|
expect(store.getState()).toMatchObject({ copyWritten: false, copyIssue: 'quota', unsaved: true })
|
|
spy.mockRestore()
|
|
store.getState().retryCopyWrite()
|
|
expect(store.getState()).toMatchObject({ copyWritten: true, copyIssue: null })
|
|
})
|
|
|
|
it('never lists, counts, offers or removes the copies of another account', async () => {
|
|
seed(movedText(10), 3, {}, VERSION, 'user-2')
|
|
seed(baseText(), 1, {}, other(1), 'user-2')
|
|
seed(baseText(), 1, {}, other(2), 'user-2')
|
|
seed(baseText(), 1, {}, other(3), 'user-2')
|
|
await openReady()
|
|
expect(store.getState()).toMatchObject({ recovery: 'none', otherCopies: [] })
|
|
commitMove(40)
|
|
expect(store.getState()).toMatchObject({ copyWritten: true, copyIssue: null })
|
|
expect(store.getState().abandonOtherCopy(other(1), { confirmed: true })).toEqual({ ok: true })
|
|
for (const version of [VERSION, other(1), other(2), other(3)]) {
|
|
expect(storedCopy(version, 'user-2')).not.toBeNull()
|
|
}
|
|
})
|
|
|
|
it('removes the copy only on the explicit outcomes', async () => {
|
|
await openReady()
|
|
commitMove(40)
|
|
store.getState().undoStep()
|
|
store.getState().redoStep()
|
|
sb.saveSheetDraft.mockRejectedValue(new SheetFailure('draft.frozen'))
|
|
await store.getState().save()
|
|
expect(storedCopy()).not.toBeNull()
|
|
sb.saveSheetDraft.mockResolvedValue({ revision: 4 } as never)
|
|
await store.getState().save()
|
|
expect(storedCopy()).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('becoming editable with a stored copy', () => {
|
|
it('offers a different copy and keeps its bytes, commit refused', async () => {
|
|
seed(movedText(10), 3)
|
|
const raw = sessionStorage.getItem(copyKeyOf())
|
|
await openReady({ canEdit: false })
|
|
store.getState().setCanEdit(true)
|
|
expect(store.getState()).toMatchObject({ recovery: 'pending', unusableCopy: false })
|
|
expect(commitMove(40)).toMatchObject({ kind: 'recovery.pending' })
|
|
expect(sessionStorage.getItem(copyKeyOf())).toBe(raw)
|
|
})
|
|
|
|
it('refuses an unreadable copy and never overwrites it', async () => {
|
|
sessionStorage.setItem(copyKeyOf(), 'not json{')
|
|
await openReady({ canEdit: false })
|
|
store.getState().setCanEdit(true)
|
|
expect(store.getState()).toMatchObject({ recovery: 'refused', refusedKind: 'unreadable' })
|
|
expect(commitMove(40)).toMatchObject({ kind: 'recovery.pending' })
|
|
expect(sessionStorage.getItem(copyKeyOf())).toBe('not json{')
|
|
})
|
|
|
|
it('does not write over a copy flagged unusable', async () => {
|
|
seed(movedText(10), 3)
|
|
const raw = sessionStorage.getItem(copyKeyOf())
|
|
await openReady()
|
|
store.getState().ignoreCopy({ confirmed: true })
|
|
seed(movedText(10), 3)
|
|
store.setState({ unusableCopy: true })
|
|
commitMove(40)
|
|
expect(sessionStorage.getItem(copyKeyOf())).toBe(raw)
|
|
})
|
|
})
|