/** * Replaces anatomy.horse / char.svgCatalogLink: the bundled reference sheet * loads, validates, and indexes as the canonical document says. */ import { readFileSync } from 'node:fs' import { resolve } from 'node:path' import { beforeAll, describe, expect, it } from 'vitest' import { bundledSheetProvider } from '@/sheets/provider' import { buildSheetIndex, isObservable, pickLabel, type SheetIndex, } from '@/services/sheetIndex' 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() }) 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(Object.keys(loaded!.assets)).toHaveLength(1) expect(loaded!.assets[loaded!.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!.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.mainView.id).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(sheet.annotationsOfView('head')).toHaveLength(9) expect(sheet.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.region('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(sheet.spineAnnotation('C', 'main')?.id).toBe('cervical') expect(sheet.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 = sheet.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', () => { expect( sheet.nodeAt('hip', [ 'hip-dysfunctions', 'articular', 'restriction-flexion', ])?.name.fr, ).toBe('Restriction en flexion') expect(sheet.nodeAt('hip', ['hip-dysfunctions'])?.name.fr).toBe( 'Dysfonctions ostéopathiques', ) expect( sheet.nodeAt('cervical', [ 'cervical-observations', 'stumbling-proprioceptive', ]), ).toBeUndefined() expect(sheet.nodeAt('nowhere', ['x'])).toBeUndefined() expect(sheet.nodeAt('hip', [])).toBeUndefined() }) it('names annotations and triggers in both locales', () => { 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)', ) expect(buildSheetIndex(sheet.sheet).mainView).toEqual(sheet.mainView) }) })