fix(store): drop the replaced archive when the new one cannot be written

This commit is contained in:
bchanot committed 2026-10-14 23:15:54 +02:00
1 parent e5e2211476
commit a37c50b9ee
4 files changed
+23 -5

No files matched your search

+3 -1
View File
@@ -266,7 +266,9 @@ export function createReportStore(
) {
/** The archive mirror is explicit: these actions are the session's edges. */
const withArchive = (patch: Patch, archive: ReportArchiveEnvelope | null) => {
archives.save(archive)
// A refused write must not leave the replaced session's archive beside the new draft:
// the next restore would pair them. That archive belongs to the session being replaced.
if (!archives.save(archive)) archives.save(null)
return { ...patch, archive }
}
@@ -301,6 +301,16 @@ describe('the draft mirror', () => {
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')
})
it('a deletion request is ignored while a report is open', () => {
store().initReport(newDraft(), sheet.sheet.ref, sheet)
store().discardSessionText('draft')