0279953 chose the stopped message from CoreRuntime.stopped alone: it
promised the report was kept even when the tab's storage refused the
draft (export, the banner's advice, needs the core), four surfaces still
offered a retry that could not work, and a failed recovery below the cap
was a dead end since nothing called retry().
The runtime restarts a failed recovery on the next call (compiled module
reused, cap still counts traps) and notifies status listeners. One
CoreStoppedNotice on the editor, Home, New report and the full-page
notice: reload when saved, else core.stopped_unsaved with a core-free
copy download (also on the storage-full banner) and a beforeunload
guard. Export, import and new report say the stopped message; a core
failure reading an unread draft is FAILED, not held. ESLint census of
the failure keys; core.stopped says "plusieurs erreurs".
327 lines
13 KiB
TypeScript
327 lines
13 KiB
TypeScript
/**
|
|
* R2 maintenance B (owner rule 2026-10-14): no session text is removed or
|
|
* overwritten implicitly when a save or a quarantine fails. A text whose
|
|
* safety copy fails stays where it is (`held`), nothing is opened over it,
|
|
* and every later action that would replace it (new report, import, reset,
|
|
* an unrelated store change) leaves it alone until the practitioner decides.
|
|
* Covers the quota errors, the refused writes, an occupied quarantine slot,
|
|
* corrupted texts and the later overwriting actions.
|
|
*/
|
|
import {
|
|
afterEach,
|
|
beforeAll,
|
|
beforeEach,
|
|
describe,
|
|
expect,
|
|
it,
|
|
vi,
|
|
} from 'vitest'
|
|
import { useReportStore } from '@/state/reportStore'
|
|
import { ARCHIVE_KEY, DRAFT_KEY, QUARANTINE_KEYS } from '@/state/persistence'
|
|
import type { SheetIndex } from '@/services/sheetIndex'
|
|
import { archiveEnvelope, loadTestSheet, minimalReport } from '../helpers/sheet'
|
|
|
|
let sheet: SheetIndex
|
|
beforeAll(async () => {
|
|
sheet = await loadTestSheet()
|
|
})
|
|
beforeEach(() => {
|
|
useReportStore.setState({
|
|
report: null,
|
|
archive: null,
|
|
activeObservationIndex: 0,
|
|
saveFailed: false,
|
|
})
|
|
sessionStorage.clear()
|
|
})
|
|
afterEach(() => vi.restoreAllMocks())
|
|
|
|
const store = () => useReportStore.getState()
|
|
const UNREADABLE = JSON.stringify({ version: 7 })
|
|
|
|
const newDraft = () => ({
|
|
date: '2026-10-14',
|
|
practitioner: { name: 'Dr. Dupont' },
|
|
animal: { speciesId: 'equine', name: 'Mistral' },
|
|
owner: { name: 'Jean Martin' },
|
|
observations: [],
|
|
conclusion: '',
|
|
recommendations: '',
|
|
})
|
|
|
|
/** The storage refuses writes to the keys `refused` matches, as a full quota does. */
|
|
function refuseWrites(refused: (key: string) => boolean) {
|
|
const setItem = sessionStorage.setItem.bind(sessionStorage)
|
|
vi.spyOn(sessionStorage, 'setItem').mockImplementation((key, value) => {
|
|
if (refused(key)) throw new DOMException('full', 'QuotaExceededError')
|
|
setItem(key, value)
|
|
})
|
|
}
|
|
const quarantineFull = () =>
|
|
refuseWrites((key) => key.startsWith('osteo:quarantine:'))
|
|
|
|
/** Every session key and its value: what must not move. */
|
|
function snapshotStorage(): Record<string, string | null> {
|
|
const keys = [DRAFT_KEY, ARCHIVE_KEY, ...Object.values(QUARANTINE_KEYS)]
|
|
return Object.fromEntries(keys.map((k) => [k, sessionStorage.getItem(k)]))
|
|
}
|
|
|
|
/** The editor's sequence for a draft it cannot set aside, then the later actions. */
|
|
function expectEveryLaterActionRefused(before: Record<string, string | null>) {
|
|
expect(store().initReport(newDraft(), sheet.sheet.ref, sheet)).toEqual({
|
|
ok: false,
|
|
reason: 'held',
|
|
})
|
|
const report = minimalReport(sheet)
|
|
expect(
|
|
store().hydrateImport({ report, archive: archiveEnvelope(sheet, report) }, sheet),
|
|
).toEqual({ ok: false, reason: 'held' })
|
|
store().reset()
|
|
store().setActiveObservation(3)
|
|
store().setConclusion('x')
|
|
expect(store().report).toBeNull()
|
|
expect(snapshotStorage()).toEqual(before)
|
|
}
|
|
|
|
describe('an unreadable draft that cannot be set aside', () => {
|
|
it('is held verbatim when the quota refuses the quarantine write', () => {
|
|
sessionStorage.setItem(DRAFT_KEY, UNREADABLE)
|
|
quarantineFull()
|
|
const before = snapshotStorage()
|
|
expect(store().restoreDraft(sheet)).toEqual({
|
|
kind: 'held',
|
|
held: [{ text: 'draft', reason: 'unreadable', bytes: UNREADABLE.length }],
|
|
})
|
|
expectEveryLaterActionRefused(before)
|
|
})
|
|
|
|
it('never replaces an earlier text already in its quarantine slot', () => {
|
|
sessionStorage.setItem(QUARANTINE_KEYS.draft, '{"earlier":true}')
|
|
sessionStorage.setItem(DRAFT_KEY, '{not json')
|
|
const before = snapshotStorage()
|
|
expect(store().restoreDraft(sheet)).toMatchObject({
|
|
kind: 'held',
|
|
held: [{ text: 'draft', reason: 'unreadable' }],
|
|
})
|
|
expectEveryLaterActionRefused(before)
|
|
})
|
|
|
|
it('is set aside when the slot already holds the same text (idempotent)', () => {
|
|
sessionStorage.setItem(QUARANTINE_KEYS.draft, UNREADABLE)
|
|
sessionStorage.setItem(DRAFT_KEY, UNREADABLE)
|
|
expect(store().restoreDraft(sheet)).toEqual({ kind: 'quarantined' })
|
|
expect(sessionStorage.getItem(DRAFT_KEY)).toBeNull()
|
|
})
|
|
|
|
it('keeps a lone surrogate (L1) text byte for byte', () => {
|
|
const raw = JSON.stringify(minimalReport(sheet, { conclusion: 'a\ud800b' }))
|
|
sessionStorage.setItem(DRAFT_KEY, raw)
|
|
quarantineFull()
|
|
expect(store().restoreDraft(sheet)).toMatchObject({ kind: 'held' })
|
|
expect(sessionStorage.getItem(DRAFT_KEY)).toBe(raw)
|
|
})
|
|
})
|
|
|
|
describe('an unreadable draft and the archive beside it', () => {
|
|
it('go to quarantine together, so no later session removes the archive', () => {
|
|
sessionStorage.setItem(DRAFT_KEY, UNREADABLE)
|
|
sessionStorage.setItem(ARCHIVE_KEY, '{"archive":1}')
|
|
expect(store().restoreDraft(sheet)).toEqual({ kind: 'quarantined' })
|
|
expect(sessionStorage.getItem(QUARANTINE_KEYS.draft)).toBe(UNREADABLE)
|
|
expect(sessionStorage.getItem(QUARANTINE_KEYS.archive)).toBe('{"archive":1}')
|
|
expect(sessionStorage.getItem(DRAFT_KEY)).toBeNull()
|
|
expect(sessionStorage.getItem(ARCHIVE_KEY)).toBeNull()
|
|
})
|
|
|
|
it('both stay in place, the draft copy undone, when the archive copy fails', () => {
|
|
sessionStorage.setItem(DRAFT_KEY, UNREADABLE)
|
|
sessionStorage.setItem(ARCHIVE_KEY, '{"archive":1}')
|
|
refuseWrites((key) => key === QUARANTINE_KEYS.archive)
|
|
const before = snapshotStorage()
|
|
expect(store().restoreDraft(sheet)).toMatchObject({
|
|
kind: 'held',
|
|
held: [
|
|
{ text: 'draft', reason: 'unreadable' },
|
|
{ text: 'archive', reason: 'companion' },
|
|
],
|
|
})
|
|
expect(snapshotStorage()).toEqual(before)
|
|
expectEveryLaterActionRefused(before)
|
|
})
|
|
})
|
|
|
|
describe('an unreadable archive beside a readable draft', () => {
|
|
it('is held, and the draft is not opened over it', () => {
|
|
sessionStorage.setItem(DRAFT_KEY, JSON.stringify(minimalReport(sheet)))
|
|
sessionStorage.setItem(ARCHIVE_KEY, '{not json')
|
|
quarantineFull()
|
|
const before = snapshotStorage()
|
|
expect(store().restoreDraft(sheet)).toMatchObject({
|
|
kind: 'held',
|
|
held: [{ text: 'archive', reason: 'unreadable' }],
|
|
})
|
|
expectEveryLaterActionRefused(before)
|
|
})
|
|
|
|
it('once deleted on request, lets the draft open', () => {
|
|
sessionStorage.setItem(DRAFT_KEY, JSON.stringify(minimalReport(sheet)))
|
|
sessionStorage.setItem(ARCHIVE_KEY, '{not json')
|
|
quarantineFull()
|
|
expect(store().restoreDraft(sheet)).toMatchObject({ kind: 'held' })
|
|
store().discardSessionText('archive')
|
|
expect(sessionStorage.getItem(DRAFT_KEY)).not.toBeNull()
|
|
expect(store().restoreDraft(sheet)).toMatchObject({ kind: 'restored' })
|
|
})
|
|
})
|
|
|
|
describe('an archive with no draft beside it', () => {
|
|
it('is set aside, or held when that fails', () => {
|
|
sessionStorage.setItem(ARCHIVE_KEY, '{"orphan":1}')
|
|
quarantineFull()
|
|
expect(store().restoreDraft(sheet)).toMatchObject({
|
|
kind: 'held',
|
|
held: [{ text: 'archive', reason: 'companion' }],
|
|
})
|
|
vi.restoreAllMocks()
|
|
expect(store().restoreDraft(sheet)).toBeNull()
|
|
expect(sessionStorage.getItem(QUARANTINE_KEYS.archive)).toBe('{"orphan":1}')
|
|
})
|
|
})
|
|
|
|
describe('a draft rebased with unresolved items whose original cannot be kept', () => {
|
|
const stale = () =>
|
|
JSON.stringify(
|
|
minimalReport(sheet, {
|
|
sheet: { ...sheet.sheet.ref, documentSha256: 'c'.repeat(64) },
|
|
observations: [
|
|
{
|
|
id: 'o',
|
|
zones: [{ annotationId: 'point-gone', viewId: 'main' }],
|
|
findings: [],
|
|
freeTextFindings: [],
|
|
comment: '',
|
|
},
|
|
],
|
|
}),
|
|
)
|
|
|
|
it('is held unrebased: the mirror never overwrites the original', () => {
|
|
const raw = stale()
|
|
sessionStorage.setItem(DRAFT_KEY, raw)
|
|
quarantineFull()
|
|
expect(store().restoreDraft(sheet)).toMatchObject({
|
|
kind: 'held',
|
|
held: [{ text: 'draft', reason: 'pre_rebase' }],
|
|
})
|
|
expect(sessionStorage.getItem(DRAFT_KEY)).toBe(raw)
|
|
expect(store().report).toBeNull()
|
|
})
|
|
|
|
it('opens the adapted version only on the confirmed replacement', () => {
|
|
const raw = stale()
|
|
sessionStorage.setItem(DRAFT_KEY, raw)
|
|
quarantineFull()
|
|
expect(store().restoreDraft(sheet, { replaceOriginal: true })).toMatchObject({
|
|
kind: 'restored',
|
|
rebase: { rebased: true, unresolvedZones: 1 },
|
|
})
|
|
expect(store().report).not.toBeNull()
|
|
expect(sessionStorage.getItem(DRAFT_KEY)).not.toBe(raw)
|
|
})
|
|
})
|
|
|
|
describe('a new session over texts this page never read (after a reload)', () => {
|
|
it('sets an unreadable draft and its archive aside before starting', () => {
|
|
sessionStorage.setItem(DRAFT_KEY, UNREADABLE)
|
|
sessionStorage.setItem(ARCHIVE_KEY, '{"archive":1}')
|
|
expect(store().initReport(newDraft(), sheet.sheet.ref, sheet)).toEqual({ ok: true })
|
|
expect(sessionStorage.getItem(QUARANTINE_KEYS.draft)).toBe(UNREADABLE)
|
|
expect(sessionStorage.getItem(QUARANTINE_KEYS.archive)).toBe('{"archive":1}')
|
|
expect(JSON.parse(sessionStorage.getItem(DRAFT_KEY)!).version).toBe(3)
|
|
})
|
|
|
|
it('is refused, nothing touched, when that set-aside fails', () => {
|
|
sessionStorage.setItem(DRAFT_KEY, UNREADABLE)
|
|
quarantineFull()
|
|
const before = snapshotStorage()
|
|
expectEveryLaterActionRefused(before)
|
|
})
|
|
|
|
it('sets an unreadable archive beside a readable draft aside before an import', () => {
|
|
sessionStorage.setItem(DRAFT_KEY, JSON.stringify(minimalReport(sheet)))
|
|
sessionStorage.setItem(ARCHIVE_KEY, '{not json')
|
|
const report = minimalReport(sheet, { conclusion: 'imported' })
|
|
expect(store().hydrateImport({ report, archive: null }, sheet)).toEqual({ ok: true })
|
|
expect(sessionStorage.getItem(QUARANTINE_KEYS.archive)).toBe('{not json')
|
|
})
|
|
|
|
it('replaces a readable draft as before (explicit new report)', () => {
|
|
sessionStorage.setItem(
|
|
DRAFT_KEY,
|
|
JSON.stringify(minimalReport(sheet, { conclusion: 'previous' })),
|
|
)
|
|
expect(store().initReport(newDraft(), sheet.sheet.ref, sheet)).toEqual({ ok: true })
|
|
expect(JSON.parse(sessionStorage.getItem(DRAFT_KEY)!).conclusion).toBe('')
|
|
expect(sessionStorage.getItem(QUARANTINE_KEYS.draft)).toBeNull()
|
|
})
|
|
|
|
it('fails (not held) when the core cannot read the draft at all, touching nothing', () => {
|
|
const draft = JSON.stringify(minimalReport(sheet))
|
|
sessionStorage.setItem(DRAFT_KEY, draft)
|
|
const broken = {
|
|
...sheet,
|
|
ops: { restoreDraft: () => ({ ok: false, kind: 'core_unavailable' }) },
|
|
} as unknown as SheetIndex
|
|
const before = snapshotStorage()
|
|
expect(store().initReport(newDraft(), sheet.sheet.ref, broken)).toEqual({ ok: false })
|
|
expect(
|
|
store().hydrateImport({ report: minimalReport(sheet), archive: null }, broken),
|
|
).toEqual({ ok: false })
|
|
expect(sessionStorage.getItem(DRAFT_KEY)).toBe(draft)
|
|
expect(snapshotStorage()).toEqual(before)
|
|
expect(store().report).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('the draft mirror', () => {
|
|
it('reports a refused write and keeps the previous text in place', () => {
|
|
store().initReport(newDraft(), sheet.sheet.ref, sheet)
|
|
const saved = sessionStorage.getItem(DRAFT_KEY)
|
|
refuseWrites((key) => key === DRAFT_KEY)
|
|
store().setConclusion('not saved')
|
|
expect(store().saveFailed).toBe(true)
|
|
expect(sessionStorage.getItem(DRAFT_KEY)).toBe(saved)
|
|
vi.restoreAllMocks()
|
|
store().setConclusion('saved again')
|
|
expect(store().saveFailed).toBe(false)
|
|
expect(JSON.parse(sessionStorage.getItem(DRAFT_KEY)!).conclusion).toBe('saved again')
|
|
})
|
|
|
|
it('is never written or removed by a change that leaves the report alone', () => {
|
|
sessionStorage.setItem(DRAFT_KEY, '{"unread":true}')
|
|
store().setActiveObservation(2)
|
|
useReportStore.setState({ archive: null })
|
|
expect(sessionStorage.getItem(DRAFT_KEY)).toBe('{"unread":true}')
|
|
})
|
|
|
|
it('never leaves the replaced archive beside a new draft when the archive write is refused', () => {
|
|
const first = minimalReport(sheet, { conclusion: 'first' })
|
|
store().hydrateImport({ report: first, archive: archiveEnvelope(sheet, first) }, sheet)
|
|
refuseWrites((key) => key === ARCHIVE_KEY)
|
|
const second = minimalReport(sheet, { conclusion: 'second' })
|
|
store().hydrateImport({ report: second, archive: archiveEnvelope(sheet, second) }, sheet)
|
|
expect(sessionStorage.getItem(ARCHIVE_KEY)).toBeNull()
|
|
expect(JSON.parse(sessionStorage.getItem(DRAFT_KEY)!).conclusion).toBe('second')
|
|
// The tab's copy lacks the imported archive: the banner says so until a write succeeds.
|
|
expect(store().saveFailed).toBe(true)
|
|
store().setConclusion('edited')
|
|
expect(store().saveFailed).toBe(true)
|
|
})
|
|
|
|
it('a deletion request is ignored while a report is open', () => {
|
|
store().initReport(newDraft(), sheet.sheet.ref, sheet)
|
|
store().discardSessionText('draft')
|
|
expect(sessionStorage.getItem(DRAFT_KEY)).not.toBeNull()
|
|
})
|
|
})
|