import { describe, it, expect } from 'vitest' import { findSpineZoneId, collectSpineVertebrae, syncSpineZones, } from '@/lib/spineZones' import type { Bone } from '@/anatomy/types' import type { ObservationZone, Vertebra } from '@/domain/schema/observation' function spine(id: string, fr: string, en: string): Bone { return { id, fr, en, category: 'zone-colonne', special: 'vertebral-precision', els: [] } } const BONES = new Map([ ['z-c', spine('z-c', 'Vertèbres cervicales', 'Cervical Vertebrae')], ['z-t', spine('z-t', 'Vertèbres thoraciques', 'Thoracic Vertebrae')], ['z-l', spine('z-l', 'Vertèbres lombaires', 'Lumbar Vertebrae')], ['z-s', spine('z-s', 'Vertèbres sacrales', 'Sacral Vertebrae')], ['z-cd', spine('z-cd', 'Vertèbres caudales', 'Coccygeal Vertebrae')], ['z-shoulder', { id: 'z-shoulder', fr: 'Épaule', en: 'Shoulder', category: 'zone', els: [] }], ]) const v = (region: Vertebra['region'], index: number): Vertebra => ({ region, index }) describe('findSpineZoneId', () => { it('maps each region to its spine zone', () => { expect(findSpineZoneId('C', BONES)).toBe('z-c') expect(findSpineZoneId('T', BONES)).toBe('z-t') expect(findSpineZoneId('L', BONES)).toBe('z-l') expect(findSpineZoneId('S', BONES)).toBe('z-s') expect(findSpineZoneId('Cd', BONES)).toBe('z-cd') }) it('returns undefined when no spine zone exists for a region', () => { const only = new Map([['z-l', BONES.get('z-l')!]]) expect(findSpineZoneId('C', only)).toBeUndefined() }) }) describe('collectSpineVertebrae', () => { it('unions vertebrae across all spine zones', () => { const zones: ObservationZone[] = [ { boneId: 'z-l', vertebrae: [v('L', 1), v('L', 2)] }, { boneId: 'z-c', vertebrae: [v('C', 3)] }, ] expect(collectSpineVertebrae(zones, BONES)).toEqual([v('L', 1), v('L', 2), v('C', 3)]) }) it('lists the priority zone vertebrae first', () => { const zones: ObservationZone[] = [ { boneId: 'z-c', vertebrae: [v('C', 3)] }, { boneId: 'z-l', vertebrae: [v('L', 1)] }, ] expect(collectSpineVertebrae(zones, BONES, 'z-l')).toEqual([v('L', 1), v('C', 3)]) }) it('ignores non-spine zones and dedups', () => { const zones: ObservationZone[] = [ { boneId: 'z-shoulder' }, { boneId: 'z-l', vertebrae: [v('L', 1), v('L', 1)] }, ] expect(collectSpineVertebrae(zones, BONES)).toEqual([v('L', 1)]) }) }) describe('syncSpineZones', () => { it('keeps same-region vertebrae on the existing zone', () => { const zones: ObservationZone[] = [{ boneId: 'z-l' }] const next = syncSpineZones(zones, [v('L', 1), v('L', 2)], BONES) expect(next).toEqual([{ boneId: 'z-l', vertebrae: [v('L', 1), v('L', 2)] }]) }) it('spawns a new zone for a newly picked region', () => { const zones: ObservationZone[] = [{ boneId: 'z-l' }] const next = syncSpineZones(zones, [v('L', 1), v('C', 3)], BONES) expect(next).toHaveLength(2) expect(next[0]).toEqual({ boneId: 'z-l', vertebrae: [v('L', 1)] }) expect(next[1]).toEqual({ boneId: 'z-c', vertebrae: [v('C', 3)] }) }) it('replaces a region authoritatively (deselection sticks)', () => { const zones: ObservationZone[] = [ { boneId: 'z-l', vertebrae: [v('L', 1), v('L', 2)] }, { boneId: 'z-c', vertebrae: [v('C', 2), v('C', 3)] }, ] const next = syncSpineZones(zones, [v('L', 1), v('C', 3)], BONES) expect(next).toHaveLength(2) expect(next[0].vertebrae).toEqual([v('L', 1)]) expect(next[1].vertebrae).toEqual([v('C', 3)]) }) it('prunes a spine zone whose region dropped to zero', () => { const zones: ObservationZone[] = [ { boneId: 'z-l', vertebrae: [v('L', 1)] }, { boneId: 'z-c', vertebrae: [v('C', 1)] }, ] const next = syncSpineZones(zones, [v('C', 1)], BONES) expect(next).toEqual([{ boneId: 'z-c', vertebrae: [v('C', 1)] }]) }) it('preserves non-spine zones untouched', () => { const zones: ObservationZone[] = [ { boneId: 'z-shoulder' }, { boneId: 'z-l', vertebrae: [v('L', 1)] }, ] const next = syncSpineZones(zones, [v('L', 2)], BONES) expect(next).toEqual([ { boneId: 'z-shoulder' }, { boneId: 'z-l', vertebrae: [v('L', 2)] }, ]) }) it('drops vertebrae of a region with no spine zone in the SVG', () => { const noCervical = new Map(BONES) noCervical.delete('z-c') const zones: ObservationZone[] = [{ boneId: 'z-l', vertebrae: [v('L', 1)] }] const next = syncSpineZones(zones, [v('L', 1), v('C', 2)], noCervical) expect(next).toEqual([{ boneId: 'z-l', vertebrae: [v('L', 1)] }]) }) })