Files
faunosteo/tests/unit/lib.spineZones.test.ts
T
Bastien ChanotandClaude Opus 4.7 9544924165 feat(editor): cross-region vertebra picks spawn their spine zone
Picking vertebrae in a region tab other than the clicked zone now creates
(or additively merges) the matching spine zone, so the SVG highlights it,
it appears in the observation's selected zones, and its findings picker shows.

Extract distributeVertebraeToZones + findSpineZoneId + regionFromLabel into
src/lib/spineZones.ts (regionFromLabel re-exported from VertebraPicker for
back-compat). Target region replaces (deselection sticks); other regions union
(side-tab picks never wipe a pre-existing zone). Unmapped regions fall back to
the target zone.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-27 18:14:01 +02:00

88 lines
3.7 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import {
findSpineZoneId,
distributeVertebraeToZones,
} 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<string, Bone>([
['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('distributeVertebraeToZones', () => {
it('keeps same-region vertebrae on the target zone', () => {
const zones: ObservationZone[] = [{ boneId: 'z-l' }]
const next = distributeVertebraeToZones(zones, 0, [v('L', 1), v('L', 2)], BONES)
expect(next).toHaveLength(1)
expect(next[0].vertebrae).toEqual([v('L', 1), v('L', 2)])
})
it('spawns a new zone for cross-region vertebrae', () => {
const zones: ObservationZone[] = [{ boneId: 'z-l' }]
const next = distributeVertebraeToZones(zones, 0, [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('merges (does not wipe) a pre-existing other-region zone', () => {
const zones: ObservationZone[] = [
{ boneId: 'z-l', vertebrae: [v('L', 1)] },
{ boneId: 'z-c', vertebrae: [v('C', 2)] },
]
const next = distributeVertebraeToZones(zones, 0, [v('L', 1), v('C', 4)], BONES)
expect(next).toHaveLength(2)
expect(next[0].vertebrae).toEqual([v('L', 1)])
expect(next[1].vertebrae).toEqual([v('C', 2), v('C', 4)])
})
it('replaces the target region so deselection within it sticks', () => {
const zones: ObservationZone[] = [{ boneId: 'z-l', vertebrae: [v('L', 1), v('L', 2)] }]
const next = distributeVertebraeToZones(zones, 0, [v('L', 1)], BONES)
expect(next[0].vertebrae).toEqual([v('L', 1)])
})
it('falls back to dumping all vertebrae on a non-spine target zone', () => {
const zones: ObservationZone[] = [{ boneId: 'z-shoulder' }]
const next = distributeVertebraeToZones(zones, 0, [v('L', 1), v('C', 2)], BONES)
expect(next).toHaveLength(1)
expect(next[0].vertebrae).toEqual([v('L', 1), v('C', 2)])
})
it('routes vertebrae of an unmapped region back onto the target zone', () => {
const noCervical = new Map(BONES)
noCervical.delete('z-c')
const zones: ObservationZone[] = [{ boneId: 'z-l' }]
const next = distributeVertebraeToZones(zones, 0, [v('L', 1), v('C', 2)], noCervical)
expect(next).toHaveLength(1)
expect(next[0].vertebrae).toEqual([v('L', 1), v('C', 2)])
})
})