Consumers now fed by the core (crates/core through @faunosteo/core-wasm):
- the sheet: the bundled payload is parsed and indexed by the core
(SheetHandle, SheetIndexData); SheetIndex reads precomputed tables only;
- the store: new report, add / remove / move observation, draft restore
(v2 migration + rebase) through the core, actions answering { ok };
- the editor click chain (zone.click), laterality and vertebra pickers
(zone.add_sides, vertebra.initial / sync / select), the findings picker
(finding.toggle by clicked leaf position), zone removal;
- the import: engine byte scan (TypeScript until R4), then the core's
layered reading and rebase in one call; the export: the core builds the
sheet snapshot, the frozen TypeScript engine writes the PDF (R4).
Lazy core: Home and the new-report form read the sheet availability from
the bundle and load the module on import / submit only; the auth shell
never references it. Degraded states (core_unavailable, invalid_sheet)
show the validated notice with a retry and never redirect or touch the
draft; an unreadable draft or archive is set aside under
osteo:quarantine:* (only content kinds; a failed write keeps it in place
and the editor stays), a rebase that flags items keeps the raw pre-rebase
draft. Four strings, wording validated by the owner.
Deleted: services/zoneInteraction.ts and its suite (13 cases ported to
cargo), the TypeScript Finding builder and vertebra selection rules of the
pickers, store rules, persistence parsing, hydrateReport. ESLint forbids
@faunosteo/domain in apps/app/src; the report:compare CLI keeps the frozen
domain until R4 (scripts/compare/sheet.ts). App suite 313/313 on the real
core (24 new tests: quarantine, degraded editor, core failures, N-1
reader); step6 goldens and the frozen envelopes unchanged.
152 lines
5.4 KiB
TypeScript
152 lines
5.4 KiB
TypeScript
/**
|
|
* R2 plan P12 (rev 2): a draft or an archive the running build cannot read is
|
|
* set aside under a quarantine key, never dropped; only the content failures
|
|
* of the core (`invalid`, `unsupported_version`) quarantine, any other failure
|
|
* leaves both keys where they were; the source key is removed only once the
|
|
* quarantine write succeeded; a draft rebased with unresolved items keeps its
|
|
* raw pre-rebase text.
|
|
*/
|
|
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 { loadTestSheet, minimalReport } from '../helpers/sheet'
|
|
|
|
let sheet: SheetIndex
|
|
beforeAll(async () => {
|
|
sheet = await loadTestSheet()
|
|
})
|
|
beforeEach(() => {
|
|
sessionStorage.clear()
|
|
useReportStore.setState({
|
|
report: null,
|
|
archive: null,
|
|
activeObservationIndex: 0,
|
|
})
|
|
})
|
|
afterEach(() => vi.restoreAllMocks())
|
|
|
|
const restore = () => useReportStore.getState().restoreDraft(sheet)
|
|
|
|
/** A sheet whose core answers `kind` to every draft restore. */
|
|
function sheetFailingWith(kind: string): SheetIndex {
|
|
return {
|
|
...sheet,
|
|
ops: { restoreDraft: () => ({ ok: false, kind }) },
|
|
} as unknown as SheetIndex
|
|
}
|
|
|
|
describe('an unreadable draft', () => {
|
|
it.each([
|
|
['a known version that does not parse', JSON.stringify({ version: 3 })],
|
|
['an unknown version', JSON.stringify({ version: 7 })],
|
|
['text that is not JSON', '{not json'],
|
|
])('%s is moved to its quarantine key', (_label, raw) => {
|
|
sessionStorage.setItem(DRAFT_KEY, raw)
|
|
expect(restore()).toEqual({ kind: 'quarantined' })
|
|
expect(sessionStorage.getItem(QUARANTINE_KEYS.draft)).toBe(raw)
|
|
expect(sessionStorage.getItem(DRAFT_KEY)).toBeNull()
|
|
expect(useReportStore.getState().report).toBeNull()
|
|
})
|
|
|
|
it('stays where it is when the quarantine write fails (storage full)', () => {
|
|
const raw = JSON.stringify({ version: 7 })
|
|
sessionStorage.setItem(DRAFT_KEY, raw)
|
|
const setItem = sessionStorage.setItem.bind(sessionStorage)
|
|
vi.spyOn(sessionStorage, 'setItem').mockImplementation((key, value) => {
|
|
if (key.startsWith('osteo:quarantine:')) {
|
|
throw new DOMException('full', 'QuotaExceededError')
|
|
}
|
|
setItem(key, value)
|
|
})
|
|
// Not announced as set aside: the editor stays put instead of leaving for Home.
|
|
expect(restore()).toEqual({ kind: 'unreadable_kept' })
|
|
expect(sessionStorage.getItem(DRAFT_KEY)).toBe(raw)
|
|
expect(sessionStorage.getItem(QUARANTINE_KEYS.draft)).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('a core failure while reading the draft', () => {
|
|
it.each(['core_unavailable', 'internal', 'rebase_failed'])(
|
|
'%s leaves the draft and the archive untouched',
|
|
(kind) => {
|
|
const draft = JSON.stringify(minimalReport(sheet))
|
|
sessionStorage.setItem(DRAFT_KEY, draft)
|
|
sessionStorage.setItem(ARCHIVE_KEY, '{"kept":true}')
|
|
const result = useReportStore
|
|
.getState()
|
|
.restoreDraft(sheetFailingWith(kind))
|
|
expect(result).toEqual({ kind: 'core_unavailable' })
|
|
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()
|
|
},
|
|
)
|
|
})
|
|
|
|
describe('an unreadable archive beside a readable draft', () => {
|
|
it('goes to its quarantine key while the draft restores', () => {
|
|
sessionStorage.setItem(
|
|
DRAFT_KEY,
|
|
JSON.stringify(minimalReport(sheet, { conclusion: 'kept' })),
|
|
)
|
|
sessionStorage.setItem(ARCHIVE_KEY, '{not json')
|
|
expect(restore()).toMatchObject({ kind: 'restored' })
|
|
expect(useReportStore.getState().report?.conclusion).toBe('kept')
|
|
expect(useReportStore.getState().archive).toBeNull()
|
|
expect(sessionStorage.getItem(QUARANTINE_KEYS.archive)).toBe('{not json')
|
|
expect(sessionStorage.getItem(ARCHIVE_KEY)).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('a draft rebased onto the loaded sheet', () => {
|
|
const OTHER_SHA = 'c'.repeat(64)
|
|
|
|
it('keeps its raw text when the rebase flagged items', () => {
|
|
const stale = minimalReport(sheet, {
|
|
sheet: { ...sheet.sheet.ref, documentSha256: OTHER_SHA },
|
|
observations: [
|
|
{
|
|
id: 'o',
|
|
zones: [{ annotationId: 'point-gone', viewId: 'main' }],
|
|
findings: [],
|
|
freeTextFindings: [],
|
|
comment: '',
|
|
},
|
|
],
|
|
})
|
|
const raw = JSON.stringify(stale)
|
|
sessionStorage.setItem(DRAFT_KEY, raw)
|
|
expect(restore()).toMatchObject({
|
|
kind: 'restored',
|
|
rebase: { rebased: true, unresolvedZones: 1 },
|
|
})
|
|
expect(sessionStorage.getItem(QUARANTINE_KEYS.draftPreRebase)).toBe(raw)
|
|
// The mirror now holds the rebased draft.
|
|
expect(
|
|
JSON.parse(sessionStorage.getItem(DRAFT_KEY)!).sheet.documentSha256,
|
|
).toBe(sheet.sheet.ref.documentSha256)
|
|
})
|
|
|
|
it('keeps nothing aside when the rebase resolved everything', () => {
|
|
const moved = minimalReport(sheet, {
|
|
sheet: { ...sheet.sheet.ref, documentSha256: OTHER_SHA },
|
|
})
|
|
sessionStorage.setItem(DRAFT_KEY, JSON.stringify(moved))
|
|
expect(restore()).toMatchObject({
|
|
kind: 'restored',
|
|
rebase: { rebased: true, unresolvedZones: 0, unresolvedFindings: 0 },
|
|
})
|
|
expect(sessionStorage.getItem(QUARANTINE_KEYS.draftPreRebase)).toBeNull()
|
|
})
|
|
})
|