/** * 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(' { 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) }) })