feat(app): R2 switch — the application runs its domain rules in the Rust core
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.
This commit is contained in:
1 parent
794dfd9d0b
commit
74fcb37564
65 files changed
+2227
-1678
No files matched your search
@@ -0,0 +1,178 @@
|
||||
/**
|
||||
* R2 plan rev 2 (robustness 7): a store action whose core call fails changes
|
||||
* nothing and answers `{ ok: false }`, and the callers stop: the new-report
|
||||
* page does not open the editor, the home page shows the import error instead
|
||||
* of opening a report.
|
||||
*/
|
||||
import {
|
||||
afterEach,
|
||||
beforeAll,
|
||||
beforeEach,
|
||||
describe,
|
||||
expect,
|
||||
it,
|
||||
vi,
|
||||
} from 'vitest'
|
||||
import {
|
||||
cleanup,
|
||||
fireEvent,
|
||||
render,
|
||||
screen,
|
||||
waitFor,
|
||||
} from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { MemoryRouter, Route, Routes } from 'react-router-dom'
|
||||
import HomeRoute from '@/app/routes/HomeRoute'
|
||||
import NewReportRoute from '@/app/routes/NewReportRoute'
|
||||
import { setCoreRuntime } from '@/core/runtime'
|
||||
import { readAttachment } from '@/pdf/import'
|
||||
import { bundledSheetProvider } from '@/sheets/provider'
|
||||
import { setSheetProvider } from '@/sheets/useSheet'
|
||||
import { useReportStore } from '@/state/reportStore'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
import { fr } from '@/i18n/fr'
|
||||
import type { SheetIndex } from '@/services/sheetIndex'
|
||||
import { testCoreRuntime, unavailableCoreRuntime } from '../helpers/core'
|
||||
import {
|
||||
emptyObservation,
|
||||
loadTestSheet,
|
||||
minimalReport,
|
||||
} from '../helpers/sheet'
|
||||
|
||||
vi.mock('@/pdf/import', async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import('@/pdf/import')>()),
|
||||
readAttachment: vi.fn(),
|
||||
}))
|
||||
const readMock = vi.mocked(readAttachment)
|
||||
|
||||
let sheet: SheetIndex
|
||||
beforeAll(async () => {
|
||||
sheet = await loadTestSheet()
|
||||
})
|
||||
beforeEach(() => {
|
||||
useLocaleStore.setState({ locale: 'fr' })
|
||||
sessionStorage.clear()
|
||||
localStorage.clear()
|
||||
vi.stubGlobal('alert', vi.fn())
|
||||
useReportStore.setState({
|
||||
report: null,
|
||||
archive: null,
|
||||
activeObservationIndex: 0,
|
||||
})
|
||||
})
|
||||
afterEach(async () => {
|
||||
cleanup()
|
||||
vi.unstubAllGlobals()
|
||||
setCoreRuntime(await testCoreRuntime())
|
||||
setSheetProvider(bundledSheetProvider)
|
||||
})
|
||||
|
||||
const draft = () => ({
|
||||
date: '2026-10-10',
|
||||
practitioner: { name: 'Dr. Dupont' },
|
||||
animal: { speciesId: 'equine', name: 'Mistral' },
|
||||
owner: { name: 'Jean Martin' },
|
||||
observations: [],
|
||||
conclusion: '',
|
||||
recommendations: '',
|
||||
})
|
||||
|
||||
describe('store actions on a failing core', () => {
|
||||
it('change nothing and answer { ok: false }', () => {
|
||||
const report = minimalReport(sheet, {
|
||||
observations: [emptyObservation('a'), emptyObservation('b')],
|
||||
})
|
||||
useReportStore.setState({ report, activeObservationIndex: 1 })
|
||||
setCoreRuntime(unavailableCoreRuntime())
|
||||
const s = useReportStore.getState()
|
||||
expect(s.addObservation()).toEqual({ ok: false })
|
||||
expect(s.moveObservationUp(1)).toEqual({ ok: false })
|
||||
expect(s.removeObservation(0)).toEqual({ ok: false })
|
||||
expect(s.initReport(draft(), sheet.sheet.ref)).toEqual({ ok: false })
|
||||
expect(useReportStore.getState().report).toBe(report)
|
||||
expect(useReportStore.getState().activeObservationIndex).toBe(1)
|
||||
})
|
||||
|
||||
it('answer { ok: true } on the live core', () => {
|
||||
const s = useReportStore.getState()
|
||||
expect(s.initReport(draft(), sheet.sheet.ref)).toEqual({ ok: true })
|
||||
expect(s.addObservation()).toEqual({ ok: true })
|
||||
expect(useReportStore.getState().report!.observations).toHaveLength(2)
|
||||
})
|
||||
})
|
||||
|
||||
describe('callers stop on a failed core call', () => {
|
||||
async function fillNewReport() {
|
||||
const user = userEvent.setup()
|
||||
render(
|
||||
<MemoryRouter initialEntries={['/new']}>
|
||||
<Routes>
|
||||
<Route path="/new" element={<NewReportRoute />} />
|
||||
<Route path="/editor" element={<p data-testid="editor-route" />} />
|
||||
</Routes>
|
||||
</MemoryRouter>,
|
||||
)
|
||||
await user.type(
|
||||
screen.getByLabelText(fr['form.practitioner.name']!),
|
||||
'Dr. Dupont',
|
||||
)
|
||||
await user.type(screen.getByLabelText(fr['form.animal.name']!), 'Mistral')
|
||||
await user.type(
|
||||
screen.getByLabelText(fr['form.owner.name']!),
|
||||
'Jean Martin',
|
||||
)
|
||||
const submit = screen.getByRole('button', { name: fr['new_report.submit'] })
|
||||
await waitFor(() => expect(submit).toBeEnabled())
|
||||
await user.click(submit)
|
||||
}
|
||||
|
||||
it('the new-report page stays put when the core cannot load', async () => {
|
||||
setCoreRuntime(unavailableCoreRuntime())
|
||||
await fillNewReport()
|
||||
await waitFor(() =>
|
||||
expect(window.alert).toHaveBeenCalledWith(fr['sheet.core_unavailable']),
|
||||
)
|
||||
expect(screen.queryByTestId('editor-route')).toBeNull()
|
||||
expect(useReportStore.getState().report).toBeNull()
|
||||
})
|
||||
|
||||
it('the new-report page stays put when the creation fails', async () => {
|
||||
useReportStore.setState({ initReport: () => ({ ok: false }) })
|
||||
try {
|
||||
await fillNewReport()
|
||||
await waitFor(() =>
|
||||
expect(window.alert).toHaveBeenCalledWith(fr['core.action_failed']),
|
||||
)
|
||||
expect(screen.queryByTestId('editor-route')).toBeNull()
|
||||
} finally {
|
||||
useReportStore.setState({
|
||||
initReport: useReportStore.getInitialState().initReport,
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
it('the home page shows the import error when the core cannot load', async () => {
|
||||
readMock.mockResolvedValue({ version: 3 })
|
||||
setCoreRuntime(unavailableCoreRuntime())
|
||||
setSheetProvider(bundledSheetProvider)
|
||||
render(
|
||||
<MemoryRouter initialEntries={['/']}>
|
||||
<Routes>
|
||||
<Route path="/" element={<HomeRoute />} />
|
||||
<Route path="/editor" element={<p data-testid="editor-route" />} />
|
||||
</Routes>
|
||||
</MemoryRouter>,
|
||||
)
|
||||
const button = screen.getByRole('button', { name: fr['home.import.cta'] })
|
||||
await waitFor(() => expect(button).toBeEnabled())
|
||||
const file = new File([new Uint8Array([37, 80, 68, 70])], 'bilan.pdf', {
|
||||
type: 'application/pdf',
|
||||
})
|
||||
fireEvent.change(document.querySelector('input[type="file"]')!, {
|
||||
target: { files: [file] },
|
||||
})
|
||||
await screen.findByText(fr['home.import.error_core_unavailable']!)
|
||||
expect(screen.queryByTestId('editor-route')).toBeNull()
|
||||
expect(useReportStore.getState().report).toBeNull()
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user