Files
faunosteo/packages/anatomy/tests/curation.apply.test.ts
T

530 lines
15 KiB
TypeScript

import { applyChangeset, type CurationResult, type Op } from '../src'
import { changesetOf, MINI_REFERENCES, miniSheet } from './fixtures/mini-sheet'
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): Plain {
return plain(result.document.finding_zones.find((z) => z.id === id))
}
function hipDysfunctions(result: CurationResult): Plain {
return (zone(result, 'hip').categories as Plain[])[0]!
}
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'
describe('applyChangeset: set', () => {
it('sets a field and records before / after', () => {
const result = run([
{
op: 'set',
target: 'region:C',
field: 'count_range',
value: { min: 6, max: 8 },
},
])
expect(result.document.vertebral_config.regions[0]!.count_range).toEqual({
min: 6,
max: 8,
})
expect(result.records).toHaveLength(1)
expect(result.records[0]).toMatchObject({
target: 'region:C',
field: 'count_range',
before: null,
after: { min: 6, max: 8 },
})
})
it('creates a missing nested object for a dot path', () => {
const result = run([
{
op: 'set',
target: MOBILITY,
field: 'description.en',
value: 'Mobility',
},
])
expect(nodes(hipDysfunctions(result))[0]!.description).toEqual({
en: 'Mobility',
})
})
it('applies a subtree set to the target and every descendant', () => {
const result = run([
{
op: 'set',
target: 'finding:hip/hip-dysfunctions',
field: 'review_status',
value: 'to_confirm',
scope: 'subtree',
},
])
expect(result.records.map((r) => r.target)).toEqual([
'finding:hip/hip-dysfunctions',
MOBILITY,
`${MOBILITY}/flexion`,
`${MOBILITY}/extension`,
'finding:hip/hip-dysfunctions/pain',
])
expect(nodes(nodes(hipDysfunctions(result))[0]!)[1]!.review_status).toBe(
'to_confirm',
)
})
it('skips items without the field and items already at the value', () => {
const result = run([
{
op: 'set',
target: 'finding:neck/neck-dysfunctions',
field: 'requires_vet_referral',
value: true,
scope: 'subtree',
},
])
expect(result.records.map((r) => r.target)).toEqual([
'finding:neck/neck-dysfunctions/stiffness',
])
})
})
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 = 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(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 = 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 = 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(plain(result.document.views[1]).parent_view_id).toBe('overview')
expect(
result.document.annotations.every((a) => a.view_id === 'overview'),
).toBe(true)
})
it('renames an annotation category: annotations follow', () => {
const result = run([
{ op: 'rename', target: 'category:joints', id: 'articulations' },
])
const hip = result.document.annotations.find((a) => a.id === 'hip')!
expect(hip.category_id).toBe('articulations')
})
it('renames a reference: every citation follows', () => {
const result = run([
{ op: 'cite', target: MOBILITY, reference_ids: ['atlas'] },
{ op: 'rename', target: 'reference:atlas', id: 'atlas-2' },
])
expect(nodes(hipDysfunctions(result))[0]!.reference_ids).toEqual([
'atlas-2',
])
expect(result.records.at(-1)).toMatchObject({
target: MOBILITY,
field: 'reference_ids',
before: ['atlas'],
after: ['atlas-2'],
})
})
})
describe('applyChangeset: add, remove, move', () => {
const node = (id: string) => ({ id, name: { fr: id, en: id } })
it.each([
[{ after: 'flexion' }, ['flexion', 'new', 'extension']],
[{ before: 'flexion' }, ['new', 'flexion', 'extension']],
[1, ['flexion', 'new', 'extension']],
[undefined, ['flexion', 'extension', 'new']],
] as const)('adds a node at position %j', (position, expected) => {
const op: Op = { op: 'add', target: MOBILITY, value: node('new') }
if (position !== undefined) op.position = position
const result = run([op])
expect(childIds(nodes(hipDysfunctions(result))[0]!)).toEqual(expected)
expect(result.records[0]).toMatchObject({
target: `${MOBILITY}/new`,
before: null,
})
})
it('adds children under a lean leaf', () => {
const result = run([
{ op: 'add', target: `${MOBILITY}/flexion`, value: node('left') },
])
expect(childIds(nodes(nodes(hipDysfunctions(result))[0]!)[0]!)).toEqual([
'left',
])
})
it('adds a zone, an annotation and a reference at document level', () => {
const result = run([
{
op: 'add',
target: 'document',
field: 'finding_zones',
value: { id: 'back', name: { fr: 'Dos', en: 'Back' }, categories: [] },
},
{
op: 'add',
target: 'document',
field: 'annotations',
position: { after: 'neck' },
value: {
id: 'back',
kind: 'zone',
view_id: 'main',
category_id: 'bones',
laterality: 'unpaired',
geometry: null,
name: { fr: 'Dos', en: 'Back' },
finding_zone_id: 'back',
vertebral_region_code: 'T',
},
},
{
op: 'add',
target: 'document',
field: 'references',
value: {
id: 'book',
type: 'book',
citation: 'Book',
url: null,
doi: null,
},
},
{ op: 'cite', target: 'finding:hip/anamnesis', reference_ids: ['book'] },
])
expect(result.document.annotations.map((a) => a.id)).toEqual([
'hip',
'neck',
'back',
'head-trigger',
])
expect(result.records.map((r) => r.target)).toEqual([
'zone:back',
'annotation:back',
'reference:book',
'finding:hip/anamnesis',
])
})
it('removes a node and records the whole item', () => {
const result = run([{ op: 'remove', target: MOBILITY }])
expect(childIds(hipDysfunctions(result))).toEqual(['pain'])
expect(result.records[0]).toMatchObject({ target: MOBILITY, after: null })
expect(childIds(plain(result.records[0]!.before))).toEqual([
'flexion',
'extension',
])
})
it('moves a node into a sibling, then across zones', () => {
const result = run([
{
op: 'move',
target: 'finding:hip/hip-dysfunctions/pain',
to: MOBILITY,
position: { before: 'flexion' },
},
{
op: 'move',
target: 'finding:neck/neck-dysfunctions/stiffness',
to: 'finding:hip/anamnesis',
},
])
expect(childIds(nodes(hipDysfunctions(result))[0]!)).toEqual([
'pain',
'flexion',
'extension',
])
expect(childIds((zone(result, 'hip').categories as Plain[])[1]!)).toEqual([
'trauma',
'stiffness',
])
expect(result.records.map((r) => r.after)).toEqual([
`${MOBILITY}/pain`,
'finding:hip/anamnesis/stiffness',
])
})
})
describe('applyChangeset: cite', () => {
it('cites one item without duplicates', () => {
const result = run([
{ op: 'cite', target: MOBILITY, reference_ids: ['atlas'] },
{ op: 'cite', target: MOBILITY, reference_ids: ['atlas', 'paper'] },
])
expect(nodes(hipDysfunctions(result))[0]!.reference_ids).toEqual([
'atlas',
'paper',
])
expect(
nodes(nodes(hipDysfunctions(result))[0]!)[0]!.reference_ids,
).toBeUndefined()
expect(result.records).toHaveLength(2)
})
it('cites a subtree, and a zone stands for its categories', () => {
const subtree = run([
{
op: 'cite',
target: MOBILITY,
reference_ids: ['atlas'],
scope: 'subtree',
},
])
expect(subtree.records.map((r) => r.target)).toEqual([
MOBILITY,
`${MOBILITY}/flexion`,
`${MOBILITY}/extension`,
])
const own = run([
{ op: 'cite', target: 'zone:hip', reference_ids: ['paper'] },
])
expect(own.records.map((r) => r.target)).toEqual([
'finding:hip/hip-dysfunctions',
'finding:hip/anamnesis',
])
})
})
describe('applyChangeset: errors fail loud', () => {
it.each([
[
[{ op: 'set', target: 'zone:nope', field: 'name', value: {} }],
'unknown target',
],
[
[{ op: 'set', target: 'finding:hip', field: 'name', value: {} }],
'malformed target',
],
[
[{ op: 'set', target: 'galaxy:x', field: 'name', value: {} }],
'unknown target',
],
[
[{ op: 'set', target: 'zone:hip', field: 'colour', value: 1 }],
'unknown field',
],
[
[{ op: 'set', target: 'zone:hip', field: 'id', value: 'x' }],
'unknown field',
],
[
[
{
op: 'add',
target: MOBILITY,
value: { id: 'flexion', name: { fr: 'a', en: 'a' } },
},
],
'already exists',
],
[
[
{
op: 'add',
target: MOBILITY,
value: { id: 'x', name: { fr: 'a', en: 'a' } },
position: { after: 'nope' },
},
],
'unknown sibling',
],
[[{ op: 'rename', target: 'zone:hip', id: 'neck' }], 'already exists'],
[
[{ op: 'cite', target: MOBILITY, reference_ids: ['ghost'] }],
'undeclared reference',
],
[
[{ op: 'move', target: MOBILITY, to: `${MOBILITY}/flexion` }],
'into itself',
],
] as Array<[Op[], string]>)('%j → %s', (ops, message) => {
expect(() => run(ops)).toThrow(message)
expect(() => run(ops)).toThrow('CHG-001 op 0')
})
it('rejects a change citing an undeclared reference', () => {
const changeset = changesetOf(
[{ op: 'remove', target: `${MOBILITY}/flexion` }],
{
reference_ids: ['ghost'],
},
)
expect(() =>
applyChangeset(miniSheet(), MINI_REFERENCES, changeset),
).toThrow('CHG-001: undeclared reference(s): ghost')
})
it('rejects an integrity error left after the ops', () => {
const remove: Op[] = [{ op: 'remove', target: 'zone:hip' }]
expect(() => run(remove)).toThrow('integrity errors')
expect(() => run(remove)).toThrow('Finding zone "hip" does not exist')
})
it('rejects a shape error left after the ops', () => {
expect(() =>
run([{ op: 'set', target: 'region:C', field: 'count', value: 'seven' }]),
).toThrow('not shape-valid')
})
})
describe('applyChangeset: purity', () => {
const ops: Op[] = [
{ op: 'rename', target: 'zone:hip', id: 'hip-joint' },
{
op: 'cite',
target: 'zone:hip-joint',
reference_ids: ['atlas'],
scope: 'subtree',
},
]
it('is deterministic', () => {
expect(run(ops)).toEqual(run(ops))
})
it('never mutates its inputs', () => {
const baseline = miniSheet()
const references = structuredClone(MINI_REFERENCES)
const changeset = changesetOf(ops)
const copies = structuredClone({ baseline, references, changeset })
const result = applyChangeset(baseline, references, changeset)
expect({ baseline, references, changeset }).toEqual(copies)
expect(result.document.references).toEqual(references)
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"/,
)
})
})