feat(store): restore sessionStorage draft on store init

B-3: the osteo:draft mirror was write-only — a page refresh at
/editor bounced to home and lost the session despite the advertised
auto-save. Store now initializes report from readPersistedDraft()
(per-tab sessionStorage: refresh keeps the session, closing the tab
drops it; reset still clears). TDD: 4 new tests incl. refresh
simulation via module re-import — 178/178 green.
This commit is contained in:
Bastien Chanot committed 2026-07-12 16:25:21 +02:00
1 parent b047d352c1
commit f58a49f7fc
2 files changed
+75 -15

No files matched your search

+17 -15
View File
@@ -36,8 +36,24 @@ function emptyObservation(): Observation {
}
}
const DRAFT_KEY = 'osteo:draft'
/** Reads the auto-saved draft back from sessionStorage (null if absent/invalid). */
export function readPersistedDraft(): Report | null {
if (typeof sessionStorage === 'undefined') return null
const raw = sessionStorage.getItem(DRAFT_KEY)
if (!raw) return null
try {
const parsed = ReportSchema.safeParse(JSON.parse(raw))
return parsed.success ? parsed.data : null
} catch {
return null
}
}
export const useReportStore = create<ReportStore>((set) => ({
report: null,
// Restore the per-tab draft so a page refresh never loses the session.
report: readPersistedDraft(),
activeObservationIndex: 0,
initReport: (draft) =>
@@ -140,8 +156,6 @@ export const useReportStore = create<ReportStore>((set) => ({
reset: () => set({ report: null, activeObservationIndex: 0 }),
}))
const DRAFT_KEY = 'osteo:draft'
useReportStore.subscribe((state) => {
if (typeof sessionStorage === 'undefined') return
if (state.report) {
@@ -150,15 +164,3 @@ useReportStore.subscribe((state) => {
sessionStorage.removeItem(DRAFT_KEY)
}
})
export function readPersistedDraft(): Report | null {
if (typeof sessionStorage === 'undefined') return null
const raw = sessionStorage.getItem(DRAFT_KEY)
if (!raw) return null
try {
const parsed = ReportSchema.safeParse(JSON.parse(raw))
return parsed.success ? parsed.data : null
} catch {
return null
}
}
@@ -0,0 +1,58 @@
import { describe, it, expect, beforeEach, vi } from 'vitest'
const draft = {
date: '2026-05-18',
practitioner: { name: 'Dr. Dupont' },
animal: { speciesId: 'equine', name: 'Mistral' },
owner: { name: 'Jean Martin' },
observations: [],
conclusion: '',
recommendations: '',
}
/** Fresh module instance = simulated page load. */
async function freshStore() {
const mod = await import('@/state/reportStore')
return mod.useReportStore
}
beforeEach(() => {
vi.resetModules()
sessionStorage.clear()
})
describe('reportStore draft restore', () => {
it('restores the sessionStorage draft on store init (page refresh)', async () => {
const store1 = await freshStore()
store1.getState().initReport(draft)
const saved = store1.getState().report!
vi.resetModules() // simulate refresh: new module, sessionStorage intact
const store2 = await freshStore()
expect(store2.getState().report).not.toBeNull()
expect(store2.getState().report!.id).toBe(saved.id)
expect(store2.getState().report!.animal.name).toBe('Mistral')
})
it('starts empty when the stored draft is invalid', async () => {
sessionStorage.setItem('osteo:draft', '{"version":999,"garbage":true}')
const store = await freshStore()
expect(store.getState().report).toBeNull()
})
it('starts empty when no draft exists', async () => {
const store = await freshStore()
expect(store.getState().report).toBeNull()
})
it('clears the draft after reset (no resurrection on next load)', async () => {
const store1 = await freshStore()
store1.getState().initReport(draft)
store1.getState().reset()
vi.resetModules()
const store2 = await freshStore()
expect(store2.getState().report).toBeNull()
})
})