feat(anatomy): report migration map and sheet reference generated by the pipeline, pure annotation-layer renderer (legacy marker look) shared with the app, app review drawings, marker colours as document data

This commit is contained in:
bchanot committed 2026-10-10 11:17:34 +02:00
1 parent 70bf992017
commit 0a17e390cf
25 files changed
+3533 -28

No files matched your search

@@ -0,0 +1,183 @@
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<typeof buildReportMigrationMap>['map']
let stats: ReturnType<typeof buildReportMigrationMap>['stats']
beforeAll(async () => {
;({ result, draft } = await runDraft())
;({ map, stats } = buildReportMigrationMap({
baseline: draft.baseline as Record<string, unknown>,
final: draft.document as Record<string, unknown>,
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<string>()
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<typeof changesetOf>[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)
})
})
+158
View File
@@ -0,0 +1,158 @@
import { DOMParser } from '@xmldom/xmldom'
import { beforeAll, describe, expect, it } from 'vitest'
import {
createLayerElement,
renderAnnotationLayer,
serializeLayer,
styleForView,
type DraftResult,
type LayerElement,
} from '../src'
import { miniSheet } from './fixtures/mini-sheet'
import { runDraft } from './helpers'
function flatten(node: LayerElement): LayerElement[] {
return [node, ...node.children.flatMap(flatten)]
}
function byId(layer: LayerElement, id: string): LayerElement[] {
return flatten(layer).filter((n) => n.attrs['data-anno-id'] === id)
}
describe('annotation layer on the real draft', () => {
let draft: DraftResult
beforeAll(async () => {
;({ draft } = await runDraft())
})
it('draws every annotation of the main view as the legacy pair, zone or trigger', () => {
const layer = renderAnnotationLayer({
document: draft.document,
viewId: 'main',
})
expect(layer.attrs).toEqual({
'data-layer': 'annotations',
'data-view-id': 'main',
})
const main = draft.document.annotations.filter((a) => a.view_id === 'main')
for (const a of main) {
const els = byId(layer, a.id)
if (a.kind === 'point')
expect(els.map((e) => e.tag)).toEqual(['circle', 'circle'])
else expect(els).toHaveLength(1)
}
const [hitbox, visible] = byId(layer, 'hip')
expect(hitbox!.attrs).toMatchObject({
r: '34',
fill: 'transparent',
'pointer-events': 'all',
'data-anno-lateral': 'bilateral',
'data-anno-zone': 'hip',
})
expect(visible!.attrs).toMatchObject({
r: '22',
fill: '#ff9500',
'fill-opacity': '0.65',
stroke: '#000',
'stroke-width': '1.5',
'pointer-events': 'none',
})
expect(hitbox!.children[0]).toMatchObject({
tag: 'title',
text: 'Hanche (coxo-fémorale) / Hip joint',
})
const [spine] = byId(layer, 'cervical')
expect(spine!.tag).toBe('polygon')
expect(spine!.attrs).toMatchObject({
fill: '#e100ff',
'fill-opacity': '0.3',
'stroke-width': '2',
'data-anno-region': 'C',
})
const [trigger] = byId(layer, 'head-trigger')
expect(trigger!.attrs).toMatchObject({
'data-anno-kind': 'detail_trigger',
'data-anno-opens-view': 'head',
'stroke-dasharray': '8,4',
'fill-opacity': '0.06',
class: 'detail-zone',
})
expect(trigger!.attrs['data-anno-name-fr']).toBe(
'Tête (vue latérale gauche)',
)
})
it('uses the detail style, the annotation colour override and the view box on the head view', () => {
const layer = renderAnnotationLayer({
document: draft.document,
viewId: 'head',
})
const [c0c1Hit, c0c1Vis] = byId(layer, 'c0-c1')
expect(c0c1Hit!.attrs.r).toBe('27')
expect(c0c1Vis!.attrs).toMatchObject({
r: '15',
fill: '#ff0000',
'fill-opacity': '0.5',
})
expect(byId(layer, 'atm')[1]!.attrs.fill).toBe('#ff8800')
expect(byId(layer, 'hyoid')[1]!.attrs.fill).toBe('#d507c4')
expect(byId(layer, 'parietal')[1]!.attrs.fill).toBe('#1baa08')
expect(styleForView(draft.document.views[1]!).visibleFillOpacity).toBe(0.5)
expect(flatten(layer).some((n) => n.attrs['data-anno-id'] === 'hip')).toBe(
false,
)
})
it('applies states as data-state on both elements of a point', () => {
const states = new Map([['hip', 'current' as const]])
const layer = renderAnnotationLayer({
document: draft.document,
viewId: 'main',
states,
})
expect(
byId(layer, 'hip').every((e) => e.attrs['data-state'] === 'current'),
).toBe(true)
expect(
byId(layer, 'stifle').every((e) => e.attrs['data-state'] === undefined),
).toBe(true)
})
it('materialises through a DOM and serialises to the same markup', () => {
const layer = renderAnnotationLayer({
document: draft.document,
viewId: 'head',
})
const doc = new DOMParser().parseFromString(
'<svg xmlns="http://www.w3.org/2000/svg"/>',
'image/svg+xml',
)
const element = createLayerElement(
doc as never,
layer,
) as unknown as Element
expect(element.getAttribute('data-layer')).toBe('annotations')
expect(element.getElementsByTagName('circle')).toHaveLength(18)
const text = serializeLayer(layer)
expect(text).toContain('<g data-layer="annotations" data-view-id="head">')
expect(text).toContain('<title>Appareil hyoïdien / Hyoid apparatus</title>')
expect(text).not.toContain('<script')
})
})
describe('annotation layer edge cases', () => {
it('skips annotations without geometry and falls back to grey without any colour', () => {
const sheet = miniSheet()
const layer = renderAnnotationLayer({ document: sheet, viewId: 'main' })
const ids = new Set(
flatten(layer)
.map((n) => n.attrs['data-anno-id'])
.filter(Boolean),
)
expect(ids.has('hip')).toBe(true)
expect(byId(layer, 'hip')[1]!.attrs.fill).toBe('#888888')
expect(() =>
renderAnnotationLayer({ document: sheet, viewId: 'nope' }),
).toThrow('unknown view')
})
})