fix(anatomy): B/C changes leave every touched finding and annotation at to_confirm (applier rule, reviewed refused), typed test reads, docs numbers

This commit is contained in:
bchanot committed 2026-10-10 09:03:00 +02:00
1 parent 37550c29a5
commit 6034909674
10 files changed
+776 -327

No files matched your search

+124 -24
View File
@@ -1,22 +1,34 @@
import { applyChangeset, type CurationResult, type Op } from '../src'
import { changesetOf, MINI_REFERENCES, miniSheet } from './fixtures/mini-sheet'
/* eslint-disable @typescript-eslint/no-explicit-any -- tests read the plain JSON form */
type Plain = Record<string, unknown>
function plain(value: unknown): Plain {
return value as Plain
}
function nodes(item: Plain): Plain[] {
return (item.children ?? []) as Plain[]
}
function run(ops: Op[]): CurationResult {
return applyChangeset(miniSheet(), MINI_REFERENCES, changesetOf(ops))
}
function zone(result: CurationResult, id: string): any {
return result.document.finding_zones.find((z) => z.id === id)
function zone(result: CurationResult, id: string): Plain {
return plain(result.document.finding_zones.find((z) => z.id === id))
}
function hipDysfunctions(result: CurationResult): any {
return zone(result, 'hip').categories[0]
function hipDysfunctions(result: CurationResult): Plain {
return (zone(result, 'hip').categories as Plain[])[0]!
}
function childIds(node: any): string[] {
return (node.children ?? []).map((child: any) => child.id)
function annotation(result: CurationResult, id: string): Plain {
return plain(result.document.annotations.find((a) => a.id === id))
}
function childIds(node: Plain): string[] {
return nodes(node).map((child) => child.id as string)
}
const MOBILITY = 'finding:hip/hip-dysfunctions/mobility'
@@ -53,7 +65,7 @@ describe('applyChangeset: set', () => {
value: 'Mobility',
},
])
expect(hipDysfunctions(result).children[0].description).toEqual({
expect(nodes(hipDysfunctions(result))[0]!.description).toEqual({
en: 'Mobility',
})
})
@@ -75,7 +87,7 @@ describe('applyChangeset: set', () => {
`${MOBILITY}/extension`,
'finding:hip/hip-dysfunctions/pain',
])
expect(hipDysfunctions(result).children[0].children[1].review_status).toBe(
expect(nodes(nodes(hipDysfunctions(result))[0]!)[1]!.review_status).toBe(
'to_confirm',
)
})
@@ -100,34 +112,32 @@ describe('applyChangeset: rename', () => {
it('renames a zone and the annotations follow, one record each', () => {
const result = run([{ op: 'rename', target: 'zone:hip', id: 'hip-joint' }])
expect(zone(result, 'hip-joint')).toBeDefined()
const hip = result.document.annotations.find((a) => a.id === 'hip') as any
const hip = annotation(result, 'hip')
expect(hip.finding_zone_id).toBe('hip-joint')
expect(result.records.map((r) => [r.target, r.field])).toEqual([
['zone:hip', null],
['annotation:hip', 'finding_zone_id'],
])
expect((result.records[0]!.before as any).id).toBe('hip')
expect(plain(result.records[0]!.before).id).toBe('hip')
})
it('renames a region code and the vertebral annotations follow', () => {
const result = run([{ op: 'rename', target: 'region:C', id: 'Cv' }])
expect(result.document.vertebral_config.regions[0]!.code).toBe('Cv')
const neck = result.document.annotations.find((a) => a.id === 'neck') as any
const neck = annotation(result, 'neck')
expect(neck.vertebral_region_code).toBe('Cv')
})
it('renames a view: parent links and trigger targets follow', () => {
const result = run([{ op: 'rename', target: 'view:head', id: 'skull' }])
const trigger = result.document.annotations.find(
(a) => a.id === 'head-trigger',
) as any
const trigger = annotation(result, 'head-trigger')
expect(trigger.opens_view_id).toBe('skull')
expect(result.records.map((r) => r.field)).toEqual([null, 'opens_view_id'])
})
it('renames a main view: parent_view_id and view_id follow', () => {
const result = run([{ op: 'rename', target: 'view:main', id: 'overview' }])
expect((result.document.views[1] as any).parent_view_id).toBe('overview')
expect(plain(result.document.views[1]).parent_view_id).toBe('overview')
expect(
result.document.annotations.every((a) => a.view_id === 'overview'),
).toBe(true)
@@ -146,7 +156,7 @@ describe('applyChangeset: rename', () => {
{ op: 'cite', target: MOBILITY, reference_ids: ['atlas'] },
{ op: 'rename', target: 'reference:atlas', id: 'atlas-2' },
])
expect(hipDysfunctions(result).children[0].reference_ids).toEqual([
expect(nodes(hipDysfunctions(result))[0]!.reference_ids).toEqual([
'atlas-2',
])
expect(result.records.at(-1)).toMatchObject({
@@ -170,7 +180,7 @@ describe('applyChangeset: add, remove, move', () => {
const op: Op = { op: 'add', target: MOBILITY, value: node('new') }
if (position !== undefined) op.position = position
const result = run([op])
expect(childIds(hipDysfunctions(result).children[0])).toEqual(expected)
expect(childIds(nodes(hipDysfunctions(result))[0]!)).toEqual(expected)
expect(result.records[0]).toMatchObject({
target: `${MOBILITY}/new`,
before: null,
@@ -181,7 +191,7 @@ describe('applyChangeset: add, remove, move', () => {
const result = run([
{ op: 'add', target: `${MOBILITY}/flexion`, value: node('left') },
])
expect(childIds(hipDysfunctions(result).children[0].children[0])).toEqual([
expect(childIds(nodes(nodes(hipDysfunctions(result))[0]!)[0]!)).toEqual([
'left',
])
})
@@ -243,7 +253,7 @@ describe('applyChangeset: add, remove, move', () => {
const result = run([{ op: 'remove', target: MOBILITY }])
expect(childIds(hipDysfunctions(result))).toEqual(['pain'])
expect(result.records[0]).toMatchObject({ target: MOBILITY, after: null })
expect(childIds(result.records[0]!.before)).toEqual([
expect(childIds(plain(result.records[0]!.before))).toEqual([
'flexion',
'extension',
])
@@ -263,12 +273,12 @@ describe('applyChangeset: add, remove, move', () => {
to: 'finding:hip/anamnesis',
},
])
expect(childIds(hipDysfunctions(result).children[0])).toEqual([
expect(childIds(nodes(hipDysfunctions(result))[0]!)).toEqual([
'pain',
'flexion',
'extension',
])
expect(childIds(zone(result, 'hip').categories[1])).toEqual([
expect(childIds((zone(result, 'hip').categories as Plain[])[1]!)).toEqual([
'trauma',
'stiffness',
])
@@ -285,12 +295,12 @@ describe('applyChangeset: cite', () => {
{ op: 'cite', target: MOBILITY, reference_ids: ['atlas'] },
{ op: 'cite', target: MOBILITY, reference_ids: ['atlas', 'paper'] },
])
expect(hipDysfunctions(result).children[0].reference_ids).toEqual([
expect(nodes(hipDysfunctions(result))[0]!.reference_ids).toEqual([
'atlas',
'paper',
])
expect(
hipDysfunctions(result).children[0].children[0].reference_ids,
nodes(nodes(hipDysfunctions(result))[0]!)[0]!.reference_ids,
).toBeUndefined()
expect(result.records).toHaveLength(2)
})
@@ -427,3 +437,93 @@ describe('applyChangeset: purity', () => {
expect(result.document.references).not.toBe(references)
})
})
describe('applyChangeset: review rule of levels B and C', () => {
const B = { level: 'B' } as const
const withLevel = (ops: Op[], extra = B) =>
applyChangeset(miniSheet(), MINI_REFERENCES, changesetOf(ops, extra))
const status = (item: Plain) => item.review_status
it('marks a node set by a B change to_confirm, with a record', () => {
const result = withLevel([
{
op: 'set',
target: MOBILITY,
field: 'name',
value: { fr: 'a', en: 'a' },
},
])
const node = nodes(hipDysfunctions(result))[0]!
expect(status(node)).toBe('to_confirm')
const last = result.records[result.records.length - 1]!
expect(last).toMatchObject({
target: MOBILITY,
field: 'review_status',
before: 'inherited',
after: 'to_confirm',
})
expect(last.change.id).toBe('CHG-001')
})
it('marks every node of an added subtree', () => {
const value = {
id: 'new',
name: { fr: 'n', en: 'n' },
children: [{ id: 'leaf', name: { fr: 'l', en: 'l' } }],
}
const result = withLevel([
{ op: 'add', target: 'finding:hip/hip-dysfunctions', value },
])
const added = nodes(hipDysfunctions(result)).find((n) => n.id === 'new')!
expect(status(added)).toBe('to_confirm')
expect(status(nodes(added)[0]!)).toBe('to_confirm')
})
it('marks cited targets under a C change', () => {
const result = withLevel(
[{ op: 'cite', target: MOBILITY, reference_ids: ['atlas'] }],
{ level: 'C', question: 'Q?' } as never,
)
expect(status(nodes(hipDysfunctions(result))[0]!)).toBe('to_confirm')
})
it('marks an annotation set by a B change', () => {
const result = withLevel([
{
op: 'set',
target: 'annotation:hip',
field: 'name',
value: { fr: 'h', en: 'h' },
},
])
expect(status(annotation(result, 'hip'))).toBe('to_confirm')
})
it('leaves a level A change untouched', () => {
const result = run([
{
op: 'set',
target: MOBILITY,
field: 'name',
value: { fr: 'a', en: 'a' },
},
])
expect(status(nodes(hipDysfunctions(result))[0]!)).not.toBe('to_confirm')
expect(result.records).toHaveLength(1)
})
it('throws when a B change sets review_status to reviewed', () => {
expect(() =>
withLevel([
{
op: 'set',
target: MOBILITY,
field: 'review_status',
value: 'reviewed',
},
]),
).toThrow(
/CHG-001 op 0: level B cannot set "finding:hip\/hip-dysfunctions\/mobility"/,
)
})
})