430 lines
13 KiB
TypeScript
430 lines
13 KiB
TypeScript
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 */
|
|
|
|
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 hipDysfunctions(result: CurationResult): any {
|
|
return zone(result, 'hip').categories[0]
|
|
}
|
|
|
|
function childIds(node: any): string[] {
|
|
return (node.children ?? []).map((child: any) => child.id)
|
|
}
|
|
|
|
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(hipDysfunctions(result).children[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(hipDysfunctions(result).children[0].children[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 = result.document.annotations.find((a) => a.id === 'hip') as any
|
|
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')
|
|
})
|
|
|
|
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
|
|
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
|
|
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(
|
|
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(hipDysfunctions(result).children[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(hipDysfunctions(result).children[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(hipDysfunctions(result).children[0].children[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(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(hipDysfunctions(result).children[0])).toEqual([
|
|
'pain',
|
|
'flexion',
|
|
'extension',
|
|
])
|
|
expect(childIds(zone(result, 'hip').categories[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(hipDysfunctions(result).children[0].reference_ids).toEqual([
|
|
'atlas',
|
|
'paper',
|
|
])
|
|
expect(
|
|
hipDysfunctions(result).children[0].children[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)
|
|
})
|
|
})
|