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:
bchanot committed 2026-10-14 00:41:34 +02:00
1 parent 794dfd9d0b
commit 74fcb37564
65 files changed
+2227 -1678

No files matched your search

+75 -36
View File
@@ -1,17 +1,23 @@
/**
* Replaces anatomy.horse / char.svgCatalogLink: the bundled reference sheet
* loads, validates, and indexes as the canonical document says.
* loads, validates, and indexes as the canonical document says. Since R2 the
* provider hands the raw payload over and the core parses and indexes it;
* the facts the TypeScript index no longer exposes (annotations per view,
* region by code, spine annotation of a region) are read from the document
* the core parsed.
*/
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { beforeAll, describe, expect, it } from 'vitest'
import type {
Annotation,
FindingCategory,
FindingNode,
} from '@faunosteo/core-wasm/types'
import { bundledSheetProvider } from '@/sheets/provider'
import {
buildSheetIndex,
isObservable,
pickLabel,
type SheetIndex,
} from '@/services/sheetIndex'
import { isObservable, type SheetIndex } from '@/services/sheetIndex'
import { setSheetProvider } from '@/sheets/useSheet'
import { pickLabel } from '@/i18n/pickLocaleLabel'
import { loadTestSheet } from '../helpers/sheet'
/** The seed manifest the bundle reads its asset hashes from. */
@@ -25,19 +31,49 @@ beforeAll(async () => {
sheet = await loadTestSheet()
})
/** Annotations drawn on a view, in document order. */
function annotationsOfView(viewId: string): Annotation[] {
return sheet.sheet.document.annotations.filter((a) => a.view_id === viewId)
}
/** The first spine annotation of a region on a view (what the core's rules use). */
function spineAnnotation(code: string, viewId: string) {
return sheet.sheet.document.annotations
.filter(isObservable)
.find((a) => a.view_id === viewId && a.vertebral_region_code === code)
}
/** The name of the category or node at a path, read from the zone's tree. */
function nodeName(zoneId: string, path: string[]): string | undefined {
let nodes: readonly (FindingCategory | FindingNode)[] =
sheet.findingZone(zoneId)?.categories ?? []
let node: FindingCategory | FindingNode | undefined
for (const segment of path) {
node = nodes.find((n) => n.id === segment)
if (!node) return undefined
nodes = node.children
}
return node?.name.fr
}
describe('bundled sheet provider', () => {
it('returns the equine draft with its reference, assets and migration map', async () => {
const loaded = await bundledSheetProvider.getReferenceSheet('equine')
expect(loaded).not.toBeNull()
expect(loaded!.status).toBe('draft')
expect(loaded!.speciesId).toBe('equine')
expect(loaded!.ref.documentSha256).toMatch(/^[0-9a-f]{64}$/)
expect(loaded!.ref.versionId).toBeNull()
expect(loaded!.migrationMap.sheet).toEqual(loaded!.ref)
expect(loaded!.ref.species_id).toBe('equine')
expect(loaded!.ref.document_sha256).toMatch(/^[0-9a-f]{64}$/)
expect(loaded!.ref.version_id).toBeNull()
expect((loaded!.migrationMap as { sheet: unknown }).sheet).toEqual({
sheetId: loaded!.ref.sheet_id,
versionId: loaded!.ref.version_id,
documentSha256: loaded!.ref.document_sha256,
})
expect(Object.keys(loaded!.assets)).toHaveLength(1)
expect(loaded!.assets[loaded!.document.views[0]!.svg_asset_id!]).toContain(
'<svg',
)
const document = loaded!.document as {
views: { svg_asset_id: string | null }[]
}
expect(loaded!.assets[document.views[0]!.svg_asset_id!]).toContain('<svg')
expect(await bundledSheetProvider.getReferenceSheet('canine')).toBeNull()
})
@@ -48,7 +84,8 @@ describe('bundled sheet provider', () => {
svg_asset_id: string
}[]
const horse = manifest[0]!
expect(loaded!.schemaVersion).toBe(2)
expect(loaded!.ref.schema_version).toBe(2)
expect(sheet.sheet.schemaVersion).toBe(2)
expect(Object.keys(loaded!.assetHashes)).toEqual([horse.svg_asset_id])
expect(loaded!.assetHashes[horse.svg_asset_id]).toBe(horse.sha256)
expect(horse.sha256).toMatch(/^[0-9a-f]{64}$/)
@@ -58,7 +95,7 @@ describe('bundled sheet provider', () => {
describe('sheet index', () => {
it('exposes the views, 28 annotations (27 observable), 27 finding zones and 5 regions', () => {
const { document } = sheet.sheet
expect(sheet.mainView.id).toBe('main')
expect(sheet.mainViewId).toBe('main')
expect(sheet.view('head')).toMatchObject({
kind: 'detail',
parent_view_id: 'main',
@@ -66,8 +103,8 @@ describe('sheet index', () => {
})
expect(document.annotations).toHaveLength(28)
expect(document.annotations.filter(isObservable)).toHaveLength(27)
expect(sheet.annotationsOfView('head')).toHaveLength(9)
expect(sheet.annotationsOfView('main')).toHaveLength(19)
expect(annotationsOfView('head')).toHaveLength(9)
expect(annotationsOfView('main')).toHaveLength(19)
expect(document.finding_zones).toHaveLength(27)
expect(sheet.regions().map((r) => r.code)).toEqual([
'C',
@@ -76,7 +113,7 @@ describe('sheet index', () => {
'S',
'Cd',
])
expect(sheet.region('Cd')).toMatchObject({
expect(sheet.regions().find((r) => r.code === 'Cd')).toMatchObject({
count: 18,
count_range: { min: 15, max: 21 },
})
@@ -91,8 +128,8 @@ describe('sheet index', () => {
expect(a.finding_zone_id, a.id).not.toBeNull()
expect(sheet.findingZone(a.finding_zone_id!), a.id).toBeDefined()
}
expect(sheet.spineAnnotation('C', 'main')?.id).toBe('cervical')
expect(sheet.spineAnnotation('C', 'head')).toBeUndefined()
expect(spineAnnotation('C', 'main')?.id).toBe('cervical')
expect(spineAnnotation('C', 'head')).toBeUndefined()
expect(sheet.isSpine('cervical')).toBe(true)
expect(sheet.isSpine('hip')).toBe(false)
expect(sheet.isSpine('head-trigger')).toBe(false)
@@ -101,7 +138,7 @@ describe('sheet index', () => {
it('maps spine annotations to their region codes on the main view', () => {
const codes = ['C', 'T', 'L', 'S', 'Cd']
for (const code of codes) {
const a = sheet.spineAnnotation(code, 'main')
const a = spineAnnotation(code, 'main')
expect(a, code).toBeDefined()
expect(a!.vertebral_region_code).toBe(code)
expect(sheet.isSpine(a!.id)).toBe(true)
@@ -123,27 +160,24 @@ describe('sheet index', () => {
})
it('resolves finding paths and reports absent ones', () => {
expect(
sheet.nodeAt('hip', [
'hip-dysfunctions',
'articular',
'restriction-flexion',
])?.name.fr,
).toBe('Restriction en flexion')
expect(sheet.nodeAt('hip', ['hip-dysfunctions'])?.name.fr).toBe(
const leaf = ['hip-dysfunctions', 'articular', 'restriction-flexion']
expect(sheet.hasNode('hip', leaf)).toBe(true)
expect(nodeName('hip', leaf)).toBe('Restriction en flexion')
expect(sheet.hasNode('hip', ['hip-dysfunctions'])).toBe(true)
expect(nodeName('hip', ['hip-dysfunctions'])).toBe(
'Dysfonctions ostéopathiques',
)
expect(
sheet.nodeAt('cervical', [
sheet.hasNode('cervical', [
'cervical-observations',
'stumbling-proprioceptive',
]),
).toBeUndefined()
expect(sheet.nodeAt('nowhere', ['x'])).toBeUndefined()
expect(sheet.nodeAt('hip', [])).toBeUndefined()
).toBe(false)
expect(sheet.hasNode('nowhere', ['x'])).toBe(false)
expect(sheet.hasNode('hip', [])).toBe(false)
})
it('names annotations and triggers in both locales', () => {
it('names annotations and triggers in both locales', async () => {
expect(pickLabel(sheet.name(sheet.annotation('hip')!), 'fr')).toBe(
'Hanche (coxo-fémorale)',
)
@@ -153,6 +187,11 @@ describe('sheet index', () => {
expect(pickLabel(sheet.name(sheet.annotation('head-trigger')!), 'en')).toBe(
'Head (left lateral view)',
)
expect(buildSheetIndex(sheet.sheet).mainView).toEqual(sheet.mainView)
// A second load of the same payload indexes it the same way.
setSheetProvider(bundledSheetProvider)
const again = await loadTestSheet()
expect(again).not.toBe(sheet)
expect(again.mainViewId).toBe(sheet.mainViewId)
expect(again.sheet.document).toEqual(sheet.sheet.document)
})
})