feat(app): export and import through @faunosteo/pdf — snapshot, archived branding and labels, browser rasterizer, imported archive kept in session, explicit rebase with messages; legacy PDF modules removed

This commit is contained in:
bchanot committed 2026-10-11 13:32:14 +02:00
1 parent 20dafc0b00
commit 9bd5173a9c
43 files changed
+1492 -1989

No files matched your search

+159 -14
View File
@@ -1,17 +1,31 @@
/**
* Replaces state.reportStore / state.reportStore.restore / .setters /
* state.draftMirror: the v3 store, its actions, the sessionStorage mirror
* and the migration of a v2 draft on restore.
* state.draftMirror: the v3 store, its actions, the sessionStorage mirror,
* the migration of a v2 draft on restore, and (Step 6) the archive kept
* beside the report and the rebase of a stale draft.
*/
import { beforeAll, beforeEach, describe, expect, it } from 'vitest'
import {
afterEach,
beforeAll,
beforeEach,
describe,
expect,
it,
vi,
} from 'vitest'
import type { Annotation } from '@faunosteo/domain'
import {
createReportStore,
useReportStore,
emptyObservation,
} from '@/state/reportStore'
import { DRAFT_KEY, sessionStoragePersistence } from '@/state/persistence'
import {
ARCHIVE_KEY,
DRAFT_KEY,
sessionStoragePersistence,
} from '@/state/persistence'
import type { SheetIndex } from '@/services/sheetIndex'
import { loadTestSheet, minimalReport } from '../helpers/sheet'
import { archiveEnvelope, loadTestSheet, minimalReport } from '../helpers/sheet'
import fullV2 from '../fixtures/report-v2.full.json'
let sheet: SheetIndex
@@ -20,8 +34,13 @@ beforeAll(async () => {
})
beforeEach(() => {
sessionStorage.clear()
useReportStore.setState({ report: null, activeObservationIndex: 0 })
useReportStore.setState({
report: null,
archive: null,
activeObservationIndex: 0,
})
})
afterEach(() => vi.restoreAllMocks())
const draft = () => ({
date: '2026-10-10',
@@ -117,12 +136,12 @@ describe('sessionStorage mirror and restore', () => {
it('restores a v3 draft, and migrates a v2 draft left by the previous version', () => {
const v3 = minimalReport(sheet, { conclusion: 'kept' })
sessionStorage.setItem(DRAFT_KEY, JSON.stringify(v3))
useReportStore.getState().restoreDraft(sheet.sheet.migrationMap)
useReportStore.getState().restoreDraft(sheet)
expect(useReportStore.getState().report?.conclusion).toBe('kept')
useReportStore.setState({ report: null })
sessionStorage.setItem(DRAFT_KEY, JSON.stringify(fullV2))
useReportStore.getState().restoreDraft(sheet.sheet.migrationMap)
useReportStore.getState().restoreDraft(sheet)
const restored = useReportStore.getState().report!
expect(restored.version).toBe(3)
expect(
@@ -139,16 +158,16 @@ describe('sessionStorage mirror and restore', () => {
useReportStore.getState().initReport(draft(), sheet.sheet.ref)
const before = useReportStore.getState().report
sessionStorage.setItem(DRAFT_KEY, JSON.stringify(minimalReport(sheet)))
useReportStore.getState().restoreDraft(sheet.sheet.migrationMap)
useReportStore.getState().restoreDraft(sheet)
expect(useReportStore.getState().report).toBe(before)
})
it('createReportStore accepts another persistence adapter', () => {
const saved: unknown[] = []
const store = createReportStore({
load: () => null,
save: (r) => saved.push(r),
})
const store = createReportStore(
{ load: () => null, save: (r) => saved.push(r) },
{ load: () => null, save: () => undefined },
)
store.getState().initReport(draft(), sheet.sheet.ref)
expect(saved.at(-1)).toMatchObject({ version: 3 })
})
@@ -173,7 +192,133 @@ describe('store edge cases', () => {
})
it('restoreDraft with no draft leaves the store empty', () => {
useReportStore.getState().restoreDraft(sheet.sheet.migrationMap)
useReportStore.getState().restoreDraft(sheet)
expect(useReportStore.getState().report).toBeNull()
})
})
describe('archive of an imported Step 6 PDF', () => {
const OTHER_SHA = 'b'.repeat(64)
it('hydrateImport keeps the archive and mirrors it under osteo:archive', () => {
const report = minimalReport(sheet)
const archive = archiveEnvelope(sheet, report)
useReportStore.getState().hydrateImport({ report, archive })
expect(useReportStore.getState().archive).toEqual(archive)
expect(JSON.parse(sessionStorage.getItem(ARCHIVE_KEY)!).report.id).toBe(
report.id,
)
expect(JSON.parse(sessionStorage.getItem(DRAFT_KEY)!).id).toBe(report.id)
})
it('the archive is written at the session edges only, never on an edit', () => {
const archiveSaves: unknown[] = []
const store = createReportStore(
{ load: () => null, save: () => undefined },
{ load: () => null, save: (a) => archiveSaves.push(a) },
)
const report = minimalReport(sheet)
const archive = archiveEnvelope(sheet, report)
store.getState().hydrateImport({ report, archive })
expect(archiveSaves).toEqual([archive])
store.getState().setConclusion('edited')
store.getState().addObservation()
store.getState().setRecommendations('again')
expect(archiveSaves).toHaveLength(1)
expect(store.getState().report?.conclusion).toBe('edited')
store.getState().reset()
expect(archiveSaves).toEqual([archive, null])
})
it('restoreDraft brings the archive back with the draft', () => {
const report = minimalReport(sheet, { conclusion: 'kept' })
const archive = archiveEnvelope(sheet, report)
useReportStore.getState().hydrateImport({ report, archive })
// Emptying the state clears the draft mirror, not the archive mirror.
useReportStore.setState({ report: null, archive: null })
expect(sessionStorage.getItem(DRAFT_KEY)).toBeNull()
expect(sessionStorage.getItem(ARCHIVE_KEY)).not.toBeNull()
sessionStorage.setItem(DRAFT_KEY, JSON.stringify(report))
const outcome = useReportStore.getState().restoreDraft(sheet)
expect(outcome).toMatchObject({ rebased: false })
expect(useReportStore.getState().report?.conclusion).toBe('kept')
expect(useReportStore.getState().archive).toEqual(archive)
})
it.each([
[
'initReport',
() => useReportStore.getState().initReport(draft(), sheet.sheet.ref),
],
[
'hydrateReport',
() => useReportStore.getState().hydrateReport(minimalReport(sheet)),
],
['reset', () => useReportStore.getState().reset()],
])('%s clears the archive and removes its mirror', (_name, act) => {
const report = minimalReport(sheet)
useReportStore
.getState()
.hydrateImport({ report, archive: archiveEnvelope(sheet, report) })
act()
expect(useReportStore.getState().archive).toBeNull()
expect(sessionStorage.getItem(ARCHIVE_KEY)).toBeNull()
})
it('restoreDraft rebases a draft written on another revision of the sheet, labels from the archived snapshot', () => {
const stale = minimalReport(sheet, {
sheet: { ...sheet.sheet.ref, documentSha256: OTHER_SHA },
observations: [
{
id: 'o-stale',
zones: [
{ annotationId: 'point-gone', viewId: 'main' },
{ annotationId: 'hip', viewId: 'main', side: 'left' },
],
findings: [],
freeTextFindings: [],
comment: '',
},
],
})
const archive = archiveEnvelope(sheet, stale)
archive.sheetSnapshot.ref.documentSha256 = OTHER_SHA
archive.sheetSnapshot.document.annotations.push({
...sheet.annotation('hip')!,
id: 'point-gone',
name: { fr: 'Point disparu', en: 'Gone point' },
} as Annotation)
sessionStorage.setItem(DRAFT_KEY, JSON.stringify(stale))
sessionStorage.setItem(ARCHIVE_KEY, JSON.stringify(archive))
const outcome = useReportStore.getState().restoreDraft(sheet)
expect(outcome).toMatchObject({
rebased: true,
unresolvedZones: 1,
unresolvedFindings: 0,
})
const restored = useReportStore.getState().report!
expect(restored.sheet).toEqual(sheet.sheet.ref)
expect(restored.observations[0]!.zones[0]).toMatchObject({
unresolved: true,
labelFrSnapshot: 'Point disparu',
})
expect(restored.observations[0]!.zones[1]!.unresolved).toBeUndefined()
expect(useReportStore.getState().archive).toEqual(archive)
// The mirror now holds the rebased draft.
expect(
JSON.parse(sessionStorage.getItem(DRAFT_KEY)!).sheet.documentSha256,
).toBe(sheet.sheet.ref.documentSha256)
})
it('ignores a corrupt or foreign osteo:archive', () => {
sessionStorage.setItem(DRAFT_KEY, JSON.stringify(minimalReport(sheet)))
sessionStorage.setItem(ARCHIVE_KEY, '{not json')
useReportStore.getState().restoreDraft(sheet)
expect(useReportStore.getState().report).not.toBeNull()
expect(useReportStore.getState().archive).toBeNull()
useReportStore.setState({ report: null })
sessionStorage.setItem(ARCHIVE_KEY, JSON.stringify({ hello: 'world' }))
useReportStore.getState().restoreDraft(sheet)
expect(useReportStore.getState().archive).toBeNull()
})
})