Files
faunosteo/tests/unit/lib.spineZones.test.ts
T
Bastien ChanotandClaude Opus 4.7 38888cb3d1 feat(editor): unified spine picker — show all regions, click opens own tab
The vertebra picker now edits the whole spine at once instead of a single
zone. Seed it with the union of vertebrae across every spine zone of the
observation (collectSpineVertebrae) so switching tabs reveals selections made
on other zones. Clicking a zone always opens its own region tab (initialRegion
now takes priority over the first existing selection).

On confirm, syncSpineZones reconciles authoritatively per region: replace each
region's vertebrae (deselection sticks), prune a spine zone whose region drops
to zero, spawn a zone for a newly picked region. Non-spine zones and their
order are preserved untouched. Supersedes the additive distribute model.

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

121 lines
4.6 KiB
TypeScript

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<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('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)] }])
})
})