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.
198 lines
7.3 KiB
TypeScript
198 lines
7.3 KiB
TypeScript
/**
|
|
* Replaces anatomy.horse / char.svgCatalogLink: the bundled reference sheet
|
|
* 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 { 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. */
|
|
const ASSET_MANIFEST = resolve(
|
|
__dirname,
|
|
'../../../../supabase/seed/anatomical-sheets/equine/draft/assets.json',
|
|
)
|
|
|
|
let sheet: SheetIndex
|
|
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!.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)
|
|
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()
|
|
})
|
|
|
|
it('carries the schema version and the manifest hash of the horse asset', async () => {
|
|
const loaded = await bundledSheetProvider.getReferenceSheet('equine')
|
|
const manifest = JSON.parse(readFileSync(ASSET_MANIFEST, 'utf8')) as {
|
|
sha256: string
|
|
svg_asset_id: string
|
|
}[]
|
|
const horse = manifest[0]!
|
|
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}$/)
|
|
})
|
|
})
|
|
|
|
describe('sheet index', () => {
|
|
it('exposes the views, 28 annotations (27 observable), 27 finding zones and 5 regions', () => {
|
|
const { document } = sheet.sheet
|
|
expect(sheet.mainViewId).toBe('main')
|
|
expect(sheet.view('head')).toMatchObject({
|
|
kind: 'detail',
|
|
parent_view_id: 'main',
|
|
svg_asset_id: null,
|
|
})
|
|
expect(document.annotations).toHaveLength(28)
|
|
expect(document.annotations.filter(isObservable)).toHaveLength(27)
|
|
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',
|
|
'T',
|
|
'L',
|
|
'S',
|
|
'Cd',
|
|
])
|
|
expect(sheet.regions().find((r) => r.code === 'Cd')).toMatchObject({
|
|
count: 18,
|
|
count_range: { min: 15, max: 21 },
|
|
})
|
|
})
|
|
|
|
it('links every observable annotation to an existing finding zone, the trigger to the head view', () => {
|
|
for (const a of sheet.sheet.document.annotations) {
|
|
if (!isObservable(a)) {
|
|
expect(a.opens_view_id).toBe('head')
|
|
continue
|
|
}
|
|
expect(a.finding_zone_id, a.id).not.toBeNull()
|
|
expect(sheet.findingZone(a.finding_zone_id!), a.id).toBeDefined()
|
|
}
|
|
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)
|
|
})
|
|
|
|
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 = spineAnnotation(code, 'main')
|
|
expect(a, code).toBeDefined()
|
|
expect(a!.vertebral_region_code).toBe(code)
|
|
expect(sheet.isSpine(a!.id)).toBe(true)
|
|
}
|
|
const spineIds = sheet.sheet.document.annotations
|
|
.filter((a) => sheet.isSpine(a.id))
|
|
.map((a) => a.id)
|
|
expect(spineIds).toHaveLength(5)
|
|
const carried = sheet.sheet.document.annotations
|
|
.filter(isObservable)
|
|
.map((a) => a.vertebral_region_code)
|
|
.filter((c) => c !== null)
|
|
expect(
|
|
sheet
|
|
.regions()
|
|
.map((r) => r.code)
|
|
.sort(),
|
|
).toEqual([...carried].sort())
|
|
})
|
|
|
|
it('resolves finding paths and reports absent ones', () => {
|
|
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.hasNode('cervical', [
|
|
'cervical-observations',
|
|
'stumbling-proprioceptive',
|
|
]),
|
|
).toBe(false)
|
|
expect(sheet.hasNode('nowhere', ['x'])).toBe(false)
|
|
expect(sheet.hasNode('hip', [])).toBe(false)
|
|
})
|
|
|
|
it('names annotations and triggers in both locales', async () => {
|
|
expect(pickLabel(sheet.name(sheet.annotation('hip')!), 'fr')).toBe(
|
|
'Hanche (coxo-fémorale)',
|
|
)
|
|
expect(pickLabel(sheet.name(sheet.annotation('hip')!), 'en')).toBe(
|
|
'Hip joint',
|
|
)
|
|
expect(pickLabel(sheet.name(sheet.annotation('head-trigger')!), 'en')).toBe(
|
|
'Head (left lateral view)',
|
|
)
|
|
// 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)
|
|
})
|
|
})
|