fix(app): rescue copy importable, trapping draft held, replace confirmed

The JSON rescue copy could not be restored in the app, a draft that traps
the engine left the tab on a reload screen and spent a recovery on every
read, and an unsaved or in-progress report could be lost by a reload
outside the editor or by New report / Import without warning.

Home import reads the copy (content-sniffed, 16 MiB cap, strict UTF-8)
through the core's draft.restore before any session change; a trapping
(draft, archive) pair is held in the resolution panel and only re-read
on Retry; ReplaceSessionDialog guards New report and Import; the
beforeunload guard is app-wide while unsaved. Supersedes BDR-055's
"never held" for `internal` only.
This commit is contained in:
bchanot committed 2026-10-17 12:58:57 +02:00
1 parent 9464651140
commit 9bd9acaf58
22 files changed
+1090 -68

No files matched your search

+87 -1
View File
@@ -32,6 +32,7 @@ beforeEach(() => {
archive: null,
activeObservationIndex: 0,
})
useReportStore.getState().clearTrapped()
})
afterEach(() => vi.restoreAllMocks())
@@ -91,7 +92,7 @@ describe('an unreadable draft', () => {
})
describe('a core failure while reading the draft', () => {
it.each(['core_unavailable', 'internal', 'rebase_failed'])(
it.each(['core_unavailable', 'rebase_failed'])(
'%s leaves the draft and the archive untouched',
(kind) => {
const draft = JSON.stringify(minimalReport(sheet))
@@ -109,6 +110,91 @@ describe('a core failure while reading the draft', () => {
)
})
describe('a core trap while reading the draft (internal)', () => {
/** A sheet whose draft restore traps (`internal`), counting the reads. */
function trappingSheet() {
const calls = { n: 0 }
const index = {
...sheet,
ops: {
restoreDraft: () => {
calls.n += 1
return { ok: false, kind: 'internal' }
},
},
} as unknown as SheetIndex
return { index, calls }
}
const draftText = () => JSON.stringify(minimalReport(sheet))
function seed() {
const draft = draftText()
sessionStorage.setItem(DRAFT_KEY, draft)
sessionStorage.setItem(ARCHIVE_KEY, '{"kept":true}')
return draft
}
it('holds the draft (engine_failure) and its archive, both untouched', () => {
const draft = seed()
const { index } = trappingSheet()
const result = useReportStore.getState().restoreDraft(index)
expect(result).toMatchObject({
kind: 'held',
held: [
{ text: 'draft', reason: 'engine_failure' },
{ text: 'archive', reason: 'companion' },
],
})
expect(sessionStorage.getItem(DRAFT_KEY)).toBe(draft)
expect(sessionStorage.getItem(ARCHIVE_KEY)).toBe('{"kept":true}')
expect(sessionStorage.getItem(QUARANTINE_KEYS.draft)).toBeNull()
expect(useReportStore.getState().report).toBeNull()
})
it('never reads the same pair again, except on the explicit retry', () => {
seed()
const { index, calls } = trappingSheet()
const store = useReportStore.getState()
store.restoreDraft(index)
expect(store.restoreDraft(index)).toMatchObject({ kind: 'held' })
expect(calls.n).toBe(1)
store.restoreDraft(index, { retryTrapped: true })
expect(calls.n).toBe(2)
})
it('answers a new session HELD with the engine cause, without a second read', () => {
seed()
const { index, calls } = trappingSheet()
const store = useReportStore.getState()
store.restoreDraft(index)
const draft = { practitioner: { name: 'p' } } as never
expect(store.initReport(draft, sheet.sheet.ref, index)).toEqual({
ok: false,
reason: 'held',
engine: true,
})
expect(store.hydrateImport({ report: minimalReport(sheet), archive: null }, index)).toEqual({
ok: false,
reason: 'held',
engine: true,
})
expect(calls.n).toBe(1)
})
it('reads again once the stored pair changed, or after a discard', () => {
seed()
const { index, calls } = trappingSheet()
const store = useReportStore.getState()
store.restoreDraft(index)
sessionStorage.setItem(DRAFT_KEY, JSON.stringify(minimalReport(sheet, { conclusion: 'x' })))
store.restoreDraft(index)
expect(calls.n).toBe(2)
store.discardSessionText('archive')
store.restoreDraft(index)
expect(calls.n).toBe(3)
})
})
describe('an unreadable archive beside a readable draft', () => {
it('goes to its quarantine key while the draft restores', () => {
sessionStorage.setItem(