import { describe, expect, it } from 'vitest' import { buildSheetIndex, isObservable, pickLabel, type SheetIndexSource, } from '../src' import { HORSE_MAIN_ASSET_ID, horseSlice } from './fixtures/horse-slice' const SHA = 'a'.repeat(64) function source() { return { ref: { documentSha256: SHA }, document: horseSlice(), assets: { [HORSE_MAIN_ASSET_ID]: '' }, extra: 'kept by the index', } satisfies SheetIndexSource & { extra: string } } describe('buildSheetIndex', () => { it('keeps the source as given, typed as the caller passed it', () => { const index = buildSheetIndex(source()) expect(index.sheet.extra).toBe('kept by the index') expect(index.sheet.ref.documentSha256).toBe(SHA) expect(index.mainView.id).toBe('view-main') expect(index.view('view-head')?.kind).toBe('detail') expect(index.view('nowhere')).toBeUndefined() }) it('looks up annotations, zones, regions and spine markers by id', () => { const index = buildSheetIndex(source()) expect(index.annotation('hip')?.kind).toBe('point') expect(index.annotationsOfView('view-head').map((a) => a.id)).toContain( 'atm', ) expect(index.findingZone('cervical')?.name.en).toBe('Cervical Vertebrae') expect(index.regions().map((r) => r.code)).toEqual([ 'C', 'T', 'L', 'S', 'Cd', ]) expect(index.region('Cd')?.count_range).toEqual({ min: 15, max: 21 }) expect(index.spineAnnotation('C', 'view-main')?.id).toBe( 'cervical-vertebrae', ) expect(index.spineAnnotation('C', 'view-head')).toBeUndefined() expect(index.isSpine('cervical-vertebrae')).toBe(true) expect(index.isSpine('hip')).toBe(false) expect(index.isSpine('head-trigger')).toBe(false) }) it('resolves finding paths and reports absent ones', () => { const index = buildSheetIndex(source()) expect( index.nodeAt('hip', [ 'hip-dysfunctions', 'articular', 'restriction-flexion', ])?.name.fr, ).toBe('Restriction en flexion') expect(index.nodeAt('hip', ['hip-dysfunctions'])?.name.fr).toBe( 'Dysfonctions ostéopathiques', ) expect(index.nodeAt('hip', ['hip-dysfunctions', 'ghost'])).toBeUndefined() expect(index.nodeAt('nowhere', ['x'])).toBeUndefined() expect(index.nodeAt('hip', [])).toBeUndefined() }) it('names a nameless trigger after the view it opens', () => { const index = buildSheetIndex(source()) expect(index.name(index.annotation('head-trigger')!)).toEqual({ fr: 'Tête', en: 'Head', }) expect(index.name(index.annotation('hip')!).en).toBe('Hip joint') }) it('refuses a document without a main view', () => { const src = source() src.document.views = src.document.views.filter((v) => v.kind !== 'main') expect(() => buildSheetIndex(src)).toThrow('no main view') }) }) describe('pickLabel / isObservable', () => { it('falls back to the other locale and to the empty string', () => { expect(pickLabel({ fr: 'Hanche', en: 'Hip' }, 'fr')).toBe('Hanche') expect(pickLabel({ fr: 'Hanche', en: 'Hip' }, 'en')).toBe('Hip') expect(pickLabel({ fr: 'Hanche', en: '' }, 'en')).toBe('Hanche') expect(pickLabel({ fr: '', en: 'Hip' }, 'fr')).toBe('Hip') expect(pickLabel(null, 'fr')).toBe('') expect(pickLabel(undefined, 'en')).toBe('') }) it('tells observable annotations from triggers', () => { const index = buildSheetIndex(source()) expect(isObservable(index.annotation('hip'))).toBe(true) expect(isObservable(index.annotation('head-trigger'))).toBe(false) expect(isObservable(undefined)).toBe(false) }) })