import { ReportMigrationMapSchema } from '@faunosteo/domain' import { beforeAll, describe, expect, it } from 'vitest' import { applyChangeset, buildReportMigrationMap, type DraftResult, type ImportResult, } from '../src' import { changesetOf, MINI_REFERENCES, miniSheet } from './fixtures/mini-sheet' import { runDraft } from './helpers' const SHEET = { sheetId: 'sheet', versionId: null, documentSha256: 'f'.repeat(64), } describe('report migration map on the real draft', () => { let result: ImportResult let draft: DraftResult let map: ReturnType['map'] let stats: ReturnType['stats'] beforeAll(async () => { ;({ result, draft } = await runDraft()) ;({ map, stats } = buildReportMigrationMap({ baseline: draft.baseline as Record, final: draft.document as Record, legacyIdMap: draft.legacyIdMap, records: draft.records, sheet: SHEET, mainViewId: 'main', })) }) it('is a valid map covering every legacy node, with the expected fates', () => { expect(ReportMigrationMapSchema.parse(map)).toBeTruthy() expect(stats).toEqual({ nodes: 792, identical: 571, remapped: 180, rehomed: 7, removed: 34, }) expect(Object.keys(map.finding_paths)).toHaveLength(792) }) it('maps every legacy annotation with its legacy labels and the views', () => { expect(Object.keys(map.annotations)).toHaveLength(27) expect(map.annotations['point-mpbwtst0']).toEqual({ id: 'hip', name: { fr: 'Hanche (coxo-fémorale)', en: 'Hip joint' }, }) expect(map.annotations['zone-mpby55kd']).toEqual({ id: 'head-trigger', name: { fr: '', en: '' }, }) expect(map.views).toEqual({ head: 'head' }) expect(map.main_view_id).toBe('main') expect(result.legacyIdMap.annotations['point-mpbwtst0']).toBe('hip') }) it('every target path exists in the draft; identical, renamed, re-homed and removed cases', () => { const final = new Set() for (const zone of draft.document.finding_zones) { const walk = ( nodes: readonly { id: string; children?: readonly unknown[] }[], prefix: string, ) => { for (const n of nodes) { final.add(`${prefix}/${n.id}`) walk((n.children ?? []) as never, `${prefix}/${n.id}`) } } walk(zone.categories as never, zone.id) } for (const target of Object.values(map.finding_paths)) { if (target !== null) expect(final.has(target), target).toBe(true) } expect( map.finding_paths['hip/hip-dysfunctions/articular/restriction-flexion'], ).toBe('hip/hip-dysfunctions/articular/restriction-flexion') expect( map.finding_paths[ 'cervical/cervical-observations/stumbling-proprioceptive' ], ).toBe('cervical/cervical-consultation-reasons/stumbling-proprioceptive') expect( map.finding_paths[ 'sacroiliac/sacroiliac-dysfunctions/muscular/sacrotuberous-tension' ], ).toBe('sacroiliac/sacroiliac-dysfunctions/muscular/sacrotuberous-ligament') expect( map.finding_paths['sphenoid/ssb-dysfunctions/sutures/sphenopetrosal'], ).toBe('sphenoid/sphenoid-dysfunctions/sutures/sphenopetrosal') expect( map.finding_paths['ssb/ssb-dysfunctions/sutures/sphenozygomatic'], ).toBeNull() expect( map.finding_paths['frontal/frontal-dysfunctions/sutures/frontozygomatic'], ).toBeNull() }) }) describe('report migration map replay on a synthetic changeset', () => { function mapOf(ops: Parameters[0]) { const baseline = miniSheet() const result = applyChangeset(baseline, MINI_REFERENCES, changesetOf(ops)) return buildReportMigrationMap({ baseline: miniSheet() as never, final: result.document as never, legacyIdMap: { views: { head: 'head' }, annotations: { 'legacy-hip': 'hip' }, categories: {}, finding_nodes: {}, assets: {}, }, records: result.records, sheet: SHEET, mainViewId: 'main', }) } it('follows a rename, a move and a removal, and a renamed annotation', () => { const { map, stats } = mapOf([ { op: 'rename', target: 'finding:hip/hip-dysfunctions/mobility', id: 'motion', }, { op: 'move', target: 'finding:hip/hip-dysfunctions/motion/flexion', to: 'finding:hip/anamnesis', }, { op: 'remove', target: 'finding:neck/neck-dysfunctions/vet' }, { op: 'rename', target: 'annotation:hip', id: 'coxofemoral' }, ]) expect(map.finding_paths['hip/hip-dysfunctions/mobility/extension']).toBe( 'hip/hip-dysfunctions/motion/extension', ) expect(map.finding_paths['hip/hip-dysfunctions/mobility/flexion']).toBe( 'hip/anamnesis/flexion', ) expect(map.finding_paths['neck/neck-dysfunctions/vet']).toBeNull() expect(map.finding_paths['neck/neck-dysfunctions/stiffness']).toBe( 'neck/neck-dysfunctions/stiffness', ) expect(map.annotations['legacy-hip']!.id).toBe('coxofemoral') expect(stats.removed).toBe(1) }) it('re-homes a node whose category was replaced by another one holding the same path', () => { const { map, stats } = mapOf([ { op: 'add', target: 'zone:hip', value: { id: 'hip-new', kind: 'dysfunction', name: { fr: 'N', en: 'N' }, children: [ { id: 'mobility', name: { fr: 'M', en: 'M' }, children: [{ id: 'flexion', name: { fr: 'F', en: 'F' } }], }, ], }, }, { op: 'remove', target: 'finding:hip/hip-dysfunctions' }, ]) expect(map.finding_paths['hip/hip-dysfunctions/mobility/flexion']).toBe( 'hip/hip-new/mobility/flexion', ) expect( map.finding_paths['hip/hip-dysfunctions/mobility/extension'], ).toBeNull() expect(map.finding_paths['hip/hip-dysfunctions']).toBeNull() expect(stats.rehomed).toBe(2) }) })