feat(domain): canonical anatomical sheet document, integrity rules, tests

This commit is contained in:
bchanot committed 2026-10-08 23:42:42 +02:00
1 parent aea8902711
commit 36812f4625
13 files changed
+1849

No files matched your search

@@ -0,0 +1,117 @@
import {
AnatomicalSheetDocumentSchema,
FindingNodeSchema,
StableIdSchema,
type AnatomicalSheetDocumentInput,
} from '../src'
import { horseSlice, horseSliceInput } from './fixtures/horse-slice'
// Mutations deliberately break the typed shape, so the draft is untyped.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type LooseDoc = any
type Mutation = (doc: LooseDoc) => void
/** Applies a mutation to the lean fixture and reports whether it parses. */
function parsesAfter(mutate: Mutation): boolean {
const doc: LooseDoc = horseSliceInput()
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) => (d.extra = true)],
['an unknown nested key', (d) => (d.views[0].zoom = 2)],
['a stable id with spaces', (d) => (d.annotations[0].id = 'Hip Joint')],
['a stable id with an accent', (d) => (d.categories[0].id = 'É')],
['an empty stable id', (d) => (d.finding_zones[0].id = '')],
['schema_version 2', (d) => (d.schema_version = 2)],
['a non-uuid svg_asset_id', (d) => (d.views[0].svg_asset_id = 'horse')],
[
'a polygon with 2 points',
(d) =>
(d.annotations[1].geometry.points = [
[0, 0],
[1, 1],
]),
],
['a circle with r 0', (d) => (d.annotations[0].geometry.r = 0)],
['a region count of 0', (d) => (d.vertebral_config.regions[0].count = 0)],
['a missing en label', (d) => delete d.name.en],
[
'a detail view without parent_view_id',
(d) => delete d.views[1].parent_view_id,
],
[
'a detail_trigger with finding_zone_id',
(d) => (d.annotations[3].finding_zone_id = 'hip'),
],
[
'a finding category kind foo',
(d) => (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.sources).toEqual([])
expect(node.children[0]).toEqual({
id: 'flexion',
name: { fr: 'Flexion', en: 'Flexion' },
description: {},
requires_vet_referral: false,
sources: [],
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.sources).toHaveLength(1)
})
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'])
})
})