Files
faunosteo/apps/app/tests/unit/services.zoneInteraction.test.ts
T

215 lines
6.9 KiB
TypeScript

/**
* Replaces char.clickChain (Step 1) one-to-one: every branch of the legacy
* zone-click chain, now decided by the pure service on the bundled sheet's
* annotations, with outcomes in Report v3 shape.
*/
import { beforeAll, describe, expect, it } from 'vitest'
import type { Annotation } from '@faunosteo/domain'
import type { SheetIndex } from '@/services/sheetIndex'
import {
addSides,
collectVertebrae,
decideZoneClick,
ensureZone,
refOf,
removeAllSides,
removeZoneAt,
sidesPresent,
syncVertebrae,
toggleZone,
} from '@/services/zoneInteraction'
import { emptyObservation, loadTestSheet } from '../helpers/sheet'
let sheet: SheetIndex
const a = (id: string): Annotation => {
const annotation = sheet.annotation(id)
if (!annotation) throw new Error(`no annotation ${id}`)
return annotation
}
beforeAll(async () => {
sheet = await loadTestSheet()
})
describe('decideZoneClick', () => {
it('opens the detail view for the trigger, even without an active observation', () => {
expect(decideZoneClick(a('head-trigger'), undefined)).toEqual({
kind: 'open_view',
viewId: 'head',
})
expect(decideZoneClick(a('head-trigger'), emptyObservation())).toEqual({
kind: 'open_view',
viewId: 'head',
})
})
it('only activates a fallback observation when none is active', () => {
expect(decideZoneClick(a('c7-t1'), undefined)).toEqual({
kind: 'activate_fallback',
})
expect(decideZoneClick(undefined, emptyObservation())).toEqual({
kind: 'ignore',
})
})
it('always opens the vertebra picker on a spine zone', () => {
const obs = toggleZone(emptyObservation(), a('cervical'))
expect(decideZoneClick(a('cervical'), emptyObservation())).toEqual({
kind: 'open_vertebra_picker',
})
expect(decideZoneClick(a('cervical'), obs)).toEqual({
kind: 'open_vertebra_picker',
})
})
it('routes bilateral zones through the side picker, or clears both sides', () => {
const hip = a('hip')
expect(decideZoneClick(hip, emptyObservation())).toEqual({
kind: 'open_laterality_picker',
})
const left = addSides(emptyObservation(), hip, ['left'])
expect(decideZoneClick(hip, left)).toEqual({
kind: 'open_laterality_picker',
})
const both = addSides(left, hip, ['right'])
expect(decideZoneClick(hip, both)).toEqual({ kind: 'remove_both_sides' })
})
it('toggles plain zones; the ten bilateral annotations and the 18 others split as the sheet says', () => {
expect(decideZoneClick(a('c7-t1'), emptyObservation())).toEqual({
kind: 'toggle',
})
const observable = sheet.sheet.document.annotations.filter(
(x) => x.kind !== 'detail_trigger',
)
const decisions = observable.map(
(x) => decideZoneClick(x, emptyObservation()).kind,
)
expect(
decisions.filter((k) => k === 'open_laterality_picker'),
).toHaveLength(10)
expect(decisions.filter((k) => k === 'open_vertebra_picker')).toHaveLength(
5,
)
expect(decisions.filter((k) => k === 'toggle')).toHaveLength(12)
})
})
describe('reducers', () => {
it('toggleZone adds a zone with snapshots then removes it', () => {
const added = toggleZone(emptyObservation(), a('c7-t1'))
expect(added.zones).toEqual([
{
annotationId: 'c7-t1',
viewId: 'main',
labelFrSnapshot: 'Charnière C7-T1 (cervico-thoracique)',
labelEnSnapshot: 'C7-T1 junction (cervicothoracic)',
},
])
expect(toggleZone(added, a('c7-t1')).zones).toEqual([])
})
it('ensureZone keeps an existing zone and reports its index', () => {
const base = toggleZone(
toggleZone(emptyObservation(), a('c7-t1')),
a('cervical'),
)
expect(ensureZone(base, a('cervical')).index).toBe(1)
const { observation, index } = ensureZone(emptyObservation(), a('cervical'))
expect(index).toBe(0)
expect(observation.zones[0]!.annotationId).toBe('cervical')
})
it('addSides appends only the missing sides and never removes one', () => {
const hip = a('hip')
const left = addSides(emptyObservation(), hip, ['left'])
expect(sidesPresent(left, refOf(hip))).toEqual(['left'])
const same = addSides(left, hip, ['left'])
expect(same).toBe(left)
const both = addSides(left, hip, ['left', 'right'])
expect(both.zones.map((z) => z.side)).toEqual(['left', 'right'])
expect(removeAllSides(both, refOf(hip)).zones).toEqual([])
})
it('removeZoneAt removes one zone by index and keeps findings (BDR-019 wart kept, D5)', () => {
const obs = {
...toggleZone(emptyObservation(), a('c7-t1')),
findings: [
{
findingZoneId: 'c7-t1',
path: ['c7-t1-dysfunctions', 'segmental', 'fixation'],
labelFrSnapshot: 'F',
labelEnSnapshot: 'F',
},
],
}
const out = removeZoneAt(obs, 0)
expect(out.zones).toEqual([])
expect(out.findings).toHaveLength(1)
})
it('detail-view zones are distinct from main-view zones with the same id', () => {
const atm = a('atm')
const obs = toggleZone(emptyObservation(), atm)
expect(obs.zones[0]).toMatchObject({ annotationId: 'atm', viewId: 'head' })
expect(decideZoneClick(atm, obs)).toEqual({ kind: 'toggle' })
})
})
describe('vertebrae (BDR-019 sync, region codes from the sheet)', () => {
it('collects vertebrae across spine zones, the priority zone first', () => {
const obs = {
...emptyObservation(),
zones: [
{
annotationId: 'thoracic',
viewId: 'main',
vertebrae: [{ region: 'T', index: 2 }],
},
{
annotationId: 'cervical',
viewId: 'main',
vertebrae: [
{ region: 'C', index: 3 },
{ region: 'C', index: 4 },
],
},
{ annotationId: 'c7-t1', viewId: 'main' },
],
}
expect(collectVertebrae(obs, sheet, 1)).toEqual([
{ region: 'C', index: 3 },
{ region: 'C', index: 4 },
{ region: 'T', index: 2 },
])
})
it('replaces per region, prunes emptied zones and spawns zones for new regions', () => {
const base = ensureZone(emptyObservation(), a('cervical')).observation
const out = syncVertebrae(
base,
[
{ region: 'L', index: 3 },
{ region: 'L', index: 4 },
{ region: 'Cd', index: 20 },
],
sheet,
'main',
)
expect(out.zones.map((z) => z.annotationId)).toEqual(['lumbar', 'caudal'])
expect(out.zones[0]!.vertebrae).toEqual([
{ region: 'L', index: 3 },
{ region: 'L', index: 4 },
])
expect(out.zones[0]!.labelFrSnapshot).toBe('Vertèbres lombaires')
const cleared = syncVertebrae(out, [], sheet, 'main')
expect(cleared.zones).toEqual([])
})
it('keeps non-spine zones untouched and ignores unknown regions', () => {
const base = toggleZone(emptyObservation(), a('c7-t1'))
const out = syncVertebrae(base, [{ region: 'XX', index: 1 }], sheet, 'main')
expect(out.zones).toEqual(base.zones)
})
})