170 lines
5.2 KiB
TypeScript
170 lines
5.2 KiB
TypeScript
import {
|
|
AnatomicalSheetDocumentSchema,
|
|
FindingNodeSchema,
|
|
StableIdSchema,
|
|
type AnatomicalSheetDocumentInput,
|
|
} from '../src'
|
|
import { horseSlice, horseSliceInput } from './fixtures/horse-slice'
|
|
|
|
type Tree = Record<string, unknown>
|
|
type Mutation = (doc: Tree) => void
|
|
|
|
/** Walks `path` inside a JSON tree; mutations break the typed shape on purpose. */
|
|
function at(doc: Tree, path: (string | number)[]): Tree {
|
|
return path.reduce<Tree>((node, key) => node[key] as Tree, doc)
|
|
}
|
|
|
|
function set(doc: Tree, path: (string | number)[], value: unknown): void {
|
|
at(doc, path.slice(0, -1))[String(path.at(-1))] = value
|
|
}
|
|
|
|
function unset(doc: Tree, path: (string | number)[]): void {
|
|
delete at(doc, path.slice(0, -1))[String(path.at(-1))]
|
|
}
|
|
|
|
/** Applies a mutation to the lean fixture and reports whether it parses. */
|
|
function parsesAfter(mutate: Mutation): boolean {
|
|
const doc = horseSliceInput() as unknown as Tree
|
|
mutate(doc)
|
|
return AnatomicalSheetDocumentSchema.safeParse(doc).success
|
|
}
|
|
|
|
describe('AnatomicalSheetDocumentSchema', () => {
|
|
it('accepts the horse slice fixture', () => {
|
|
const result = AnatomicalSheetDocumentSchema.safeParse(horseSliceInput())
|
|
expect(result.success).toBe(true)
|
|
})
|
|
|
|
it('accepts a parsed document again (output is valid input)', () => {
|
|
expect(AnatomicalSheetDocumentSchema.parse(horseSlice())).toEqual(
|
|
horseSlice(),
|
|
)
|
|
})
|
|
|
|
const rejections: Array<[string, Mutation]> = [
|
|
['an unknown top-level key', (d) => set(d, ['extra'], true)],
|
|
['an unknown nested key', (d) => set(d, ['views', 0, 'zoom'], 2)],
|
|
[
|
|
'a stable id with spaces',
|
|
(d) => set(d, ['annotations', 0, 'id'], 'Hip Joint'),
|
|
],
|
|
['a stable id with an accent', (d) => set(d, ['categories', 0, 'id'], 'É')],
|
|
['an empty stable id', (d) => set(d, ['finding_zones', 0, 'id'], '')],
|
|
['schema_version 1', (d) => set(d, ['schema_version'], 1)],
|
|
[
|
|
'a non-uuid svg_asset_id',
|
|
(d) => set(d, ['views', 0, 'svg_asset_id'], 'horse'),
|
|
],
|
|
[
|
|
'a polygon with 2 points',
|
|
(d) =>
|
|
set(
|
|
d,
|
|
['annotations', 1, 'geometry', 'points'],
|
|
[
|
|
[0, 0],
|
|
[1, 1],
|
|
],
|
|
),
|
|
],
|
|
[
|
|
'a circle with r 0',
|
|
(d) => set(d, ['annotations', 0, 'geometry', 'r'], 0),
|
|
],
|
|
[
|
|
'a region count of 0',
|
|
(d) => set(d, ['vertebral_config', 'regions', 0, 'count'], 0),
|
|
],
|
|
['a missing en label', (d) => unset(d, ['name', 'en'])],
|
|
[
|
|
'a detail view without parent_view_id',
|
|
(d) => unset(d, ['views', 1, 'parent_view_id']),
|
|
],
|
|
[
|
|
'a detail_trigger with finding_zone_id',
|
|
(d) => set(d, ['annotations', 3, 'finding_zone_id'], 'hip'),
|
|
],
|
|
[
|
|
'a javascript: reference url',
|
|
(d) => set(d, ['references', 0, 'url'], 'javascript:alert(1)'),
|
|
],
|
|
[
|
|
'a reference type foo',
|
|
(d) => set(d, ['references', 0, 'type'], 'foo'),
|
|
],
|
|
[
|
|
'a review_status foo',
|
|
(d) => set(d, ['annotations', 0, 'review_status'], 'foo'),
|
|
],
|
|
[
|
|
'a count_range with min 0',
|
|
(d) =>
|
|
set(d, ['vertebral_config', 'regions', 4, 'count_range', 'min'], 0),
|
|
],
|
|
[
|
|
'a finding category kind foo',
|
|
(d) => set(d, ['finding_zones', 0, 'categories', 0, 'kind'], 'foo'),
|
|
],
|
|
]
|
|
|
|
it.each(rejections)('rejects %s', (_label, mutate) => {
|
|
expect(parsesAfter(mutate)).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('StableIdSchema', () => {
|
|
it.each(['hip', 'c7-t1', 'view_main', '0'])('accepts %s', (id) => {
|
|
expect(StableIdSchema.safeParse(id).success).toBe(true)
|
|
})
|
|
|
|
it.each(['Hip Joint', 'É', '', '-hip', 'a'.repeat(65)])(
|
|
'rejects %j',
|
|
(id) => {
|
|
expect(StableIdSchema.safeParse(id).success).toBe(false)
|
|
},
|
|
)
|
|
})
|
|
|
|
describe('FindingNodeSchema defaults', () => {
|
|
it('fills the optional keys of a lean node, recursively', () => {
|
|
const node = FindingNodeSchema.parse({
|
|
id: 'articular',
|
|
name: { fr: 'Articulaire', en: 'Articular' },
|
|
children: [{ id: 'flexion', name: { fr: 'Flexion', en: 'Flexion' } }],
|
|
})
|
|
expect(node.description).toEqual({})
|
|
expect(node.requires_vet_referral).toBe(false)
|
|
expect(node.reference_ids).toEqual([])
|
|
expect(node.review_status).toBe('inherited')
|
|
expect(node.children[0]).toEqual({
|
|
id: 'flexion',
|
|
name: { fr: 'Flexion', en: 'Flexion' },
|
|
description: {},
|
|
requires_vet_referral: false,
|
|
reference_ids: [],
|
|
review_status: 'inherited',
|
|
children: [],
|
|
})
|
|
})
|
|
|
|
it('keeps explicit values of the fixture', () => {
|
|
const doc = horseSlice()
|
|
const lameness = doc.finding_zones[0].categories[1].children[0]
|
|
expect(lameness.requires_vet_referral).toBe(true)
|
|
const trauma = doc.finding_zones[0].categories[2].children[0]
|
|
expect(trauma.reference_ids).toEqual(['legacy-catalog'])
|
|
expect(doc.vertebral_config.regions[4].count_range).toEqual({
|
|
min: 15,
|
|
max: 21,
|
|
})
|
|
expect(doc.vertebral_config.regions[0].count_range).toBeNull()
|
|
expect(doc.annotations[0].review_status).toBe('inherited')
|
|
})
|
|
|
|
it('does not add default keys to the lean input', () => {
|
|
const input: AnatomicalSheetDocumentInput = horseSliceInput()
|
|
const leaf = input.finding_zones[2].categories[0].children[0]
|
|
expect(Object.keys(leaf)).toEqual(['id', 'name'])
|
|
})
|
|
})
|