474 lines
15 KiB
TypeScript
474 lines
15 KiB
TypeScript
import {
|
|
AnatomicalSheetDocumentSchema,
|
|
SheetValidationError,
|
|
assertPublishable,
|
|
createEmptySheetDocument,
|
|
isPublishable,
|
|
validateSheetDocument,
|
|
type AnatomicalSheetDocument,
|
|
type Annotation,
|
|
type SheetView,
|
|
} from '../src'
|
|
import { HORSE_MAIN_ASSET_ID, horseSlice } from './fixtures/horse-slice'
|
|
|
|
/** [code, path] pairs: what each rule test pins down. */
|
|
function issuesOf(doc: AnatomicalSheetDocument): Array<[string, string]> {
|
|
return validateSheetDocument(doc).map((issue) => [issue.code, issue.path])
|
|
}
|
|
|
|
function mutated(
|
|
mutate: (doc: AnatomicalSheetDocument) => void,
|
|
): AnatomicalSheetDocument {
|
|
const doc = horseSlice()
|
|
mutate(doc)
|
|
return doc
|
|
}
|
|
|
|
function point(id: string, fields: Partial<Annotation> = {}): Annotation {
|
|
return {
|
|
id,
|
|
kind: 'point',
|
|
view_id: 'view-main',
|
|
category_id: 'articulations',
|
|
laterality: 'bilateral',
|
|
geometry: null,
|
|
review_status: 'inherited',
|
|
color: null,
|
|
name: { fr: id, en: id },
|
|
finding_zone_id: 'hip',
|
|
vertebral_region_code: null,
|
|
...fields,
|
|
} as Annotation
|
|
}
|
|
|
|
function trigger(id: string, viewId: string, opensViewId: string) {
|
|
return {
|
|
id,
|
|
kind: 'detail_trigger',
|
|
view_id: viewId,
|
|
category_id: 'cadrans-du-crane',
|
|
laterality: 'unpaired',
|
|
geometry: null,
|
|
review_status: 'inherited',
|
|
color: null,
|
|
name: null,
|
|
opens_view_id: opensViewId,
|
|
} satisfies Annotation
|
|
}
|
|
|
|
function detailView(id: string, parentId: string) {
|
|
return {
|
|
id,
|
|
kind: 'detail',
|
|
name: { fr: id, en: id },
|
|
parent_view_id: parentId,
|
|
svg_asset_id: HORSE_MAIN_ASSET_ID,
|
|
view_box: null,
|
|
} satisfies SheetView
|
|
}
|
|
|
|
describe('validateSheetDocument on the horse slice', () => {
|
|
it('reports no error and no warning', () => {
|
|
expect(validateSheetDocument(horseSlice())).toEqual([])
|
|
})
|
|
})
|
|
|
|
describe('integrity rules (one mutation per code)', () => {
|
|
it('view.main.count', () => {
|
|
const doc = mutated((d) => {
|
|
d.views.push({ ...d.views[0], id: 'view-alt' })
|
|
})
|
|
expect(issuesOf(doc)).toEqual([['view.main.count', 'views']])
|
|
})
|
|
|
|
it('view.main.count with no main view', () => {
|
|
const doc = mutated((d) => {
|
|
d.views = d.views.filter((view) => view.kind !== 'main')
|
|
})
|
|
const codes = issuesOf(doc).map(([code]) => code)
|
|
expect(codes).toContain('view.main.count')
|
|
})
|
|
|
|
it('view.id.duplicate', () => {
|
|
const doc = mutated((d) => {
|
|
d.views.push(structuredClone(d.views[1]))
|
|
})
|
|
expect(issuesOf(doc)).toEqual([['view.id.duplicate', 'views[2].id']])
|
|
})
|
|
|
|
it('view.parent.missing', () => {
|
|
const doc = mutated((d) => {
|
|
d.views.push(detailView('view-orphan', 'view-ghost'))
|
|
})
|
|
expect(issuesOf(doc)).toEqual([
|
|
['view.parent.missing', 'views[2].parent_view_id'],
|
|
])
|
|
})
|
|
|
|
it('view.parent.cycle (terminates on a two-view loop)', () => {
|
|
const doc = mutated((d) => {
|
|
d.views.push(detailView('view-a', 'view-b'))
|
|
d.views.push(detailView('view-b', 'view-a'))
|
|
d.annotations.push(trigger('open-a', 'view-b', 'view-a'))
|
|
d.annotations.push(trigger('open-b', 'view-a', 'view-b'))
|
|
})
|
|
expect(issuesOf(doc)).toEqual([
|
|
['view.parent.cycle', 'views[2].parent_view_id'],
|
|
['view.parent.cycle', 'views[3].parent_view_id'],
|
|
])
|
|
})
|
|
|
|
it('view.parent.cycle on a self-parent', () => {
|
|
const doc = mutated((d) => {
|
|
d.views.push(detailView('view-self', 'view-self'))
|
|
d.annotations.push(trigger('open-self', 'view-self', 'view-self'))
|
|
})
|
|
expect(issuesOf(doc)).toEqual([
|
|
['view.parent.cycle', 'views[2].parent_view_id'],
|
|
])
|
|
})
|
|
|
|
it('view.unreachable', () => {
|
|
const doc = mutated((d) => {
|
|
d.annotations = d.annotations.filter((a) => a.id !== 'head-trigger')
|
|
})
|
|
expect(issuesOf(doc)).toEqual([['view.unreachable', 'views[1]']])
|
|
})
|
|
|
|
it('view.asset.missing on the main view', () => {
|
|
const doc = mutated((d) => {
|
|
d.views[0].svg_asset_id = null
|
|
})
|
|
expect(issuesOf(doc)).toEqual([
|
|
['view.asset.missing', 'views[0].svg_asset_id'],
|
|
])
|
|
})
|
|
|
|
it('view.asset.missing on a detail view without asset nor view_box', () => {
|
|
const doc = mutated((d) => {
|
|
d.views[1].view_box = null
|
|
})
|
|
expect(issuesOf(doc)).toEqual([['view.asset.missing', 'views[1].view_box']])
|
|
})
|
|
|
|
it('annotation.id.duplicate (across views)', () => {
|
|
const doc = mutated((d) => {
|
|
d.annotations.push({ ...structuredClone(d.annotations[4]) })
|
|
d.annotations[6].view_id = 'view-main'
|
|
})
|
|
expect(issuesOf(doc)).toEqual([
|
|
['annotation.id.duplicate', 'annotations[6].id'],
|
|
])
|
|
})
|
|
|
|
it('annotation.view.missing', () => {
|
|
const doc = mutated((d) => {
|
|
d.annotations.push(point('ghost', { view_id: 'view-ghost' }))
|
|
})
|
|
expect(issuesOf(doc)).toEqual([
|
|
['annotation.view.missing', 'annotations[6].view_id'],
|
|
])
|
|
})
|
|
|
|
it('annotation.category.missing', () => {
|
|
const doc = mutated((d) => {
|
|
d.annotations[0].category_id = 'os'
|
|
})
|
|
expect(issuesOf(doc)).toEqual([
|
|
['annotation.category.missing', 'annotations[0].category_id'],
|
|
])
|
|
})
|
|
|
|
it('annotation.opens_view.invalid when the target is not a child', () => {
|
|
const doc = mutated((d) => {
|
|
d.annotations.push(trigger('loop', 'view-head', 'view-head'))
|
|
})
|
|
expect(issuesOf(doc)).toEqual([
|
|
['annotation.opens_view.invalid', 'annotations[6].opens_view_id'],
|
|
])
|
|
})
|
|
|
|
it('annotation.opens_view.invalid when the target is main or absent', () => {
|
|
const doc = mutated((d) => {
|
|
d.annotations.push(trigger('to-main', 'view-main', 'view-main'))
|
|
d.annotations.push(trigger('to-ghost', 'view-main', 'view-ghost'))
|
|
})
|
|
expect(issuesOf(doc)).toEqual([
|
|
['annotation.opens_view.invalid', 'annotations[6].opens_view_id'],
|
|
['annotation.opens_view.invalid', 'annotations[7].opens_view_id'],
|
|
])
|
|
})
|
|
|
|
it('annotation.finding_zone.missing', () => {
|
|
const doc = mutated((d) => {
|
|
d.annotations.push(point('stifle', { finding_zone_id: 'stifle' }))
|
|
})
|
|
expect(issuesOf(doc)).toEqual([
|
|
['annotation.finding_zone.missing', 'annotations[6].finding_zone_id'],
|
|
])
|
|
})
|
|
|
|
it('annotation.region.missing', () => {
|
|
const doc = mutated((d) => {
|
|
const cervical = d.annotations[1]
|
|
if (cervical.kind === 'detail_trigger') throw new Error('fixture')
|
|
cervical.vertebral_region_code = 'Q'
|
|
})
|
|
expect(issuesOf(doc)).toEqual([
|
|
['annotation.region.missing', 'annotations[1].vertebral_region_code'],
|
|
])
|
|
})
|
|
|
|
it('annotation.no_findings is a warning', () => {
|
|
const doc = mutated((d) => {
|
|
d.annotations.push(point('mandible', { finding_zone_id: null }))
|
|
})
|
|
expect(issuesOf(doc)).toEqual([
|
|
['annotation.no_findings', 'annotations[6].finding_zone_id'],
|
|
])
|
|
expect(validateSheetDocument(doc)[0].severity).toBe('warning')
|
|
})
|
|
|
|
it('region.code.duplicate', () => {
|
|
const doc = mutated((d) => {
|
|
const regions = d.vertebral_config.regions
|
|
regions.push(structuredClone(regions[0]))
|
|
})
|
|
expect(issuesOf(doc)).toEqual([
|
|
['region.code.duplicate', 'vertebral_config.regions[5].code'],
|
|
])
|
|
})
|
|
|
|
it('category.id.duplicate', () => {
|
|
const doc = mutated((d) => {
|
|
d.categories.push(structuredClone(d.categories[0]))
|
|
})
|
|
expect(issuesOf(doc)).toEqual([
|
|
['category.id.duplicate', 'categories[4].id'],
|
|
])
|
|
})
|
|
|
|
it('finding_zone.id.duplicate', () => {
|
|
const doc = mutated((d) => {
|
|
d.finding_zones.push(structuredClone(d.finding_zones[0]))
|
|
})
|
|
expect(issuesOf(doc)).toEqual([
|
|
['finding_zone.id.duplicate', 'finding_zones[5].id'],
|
|
])
|
|
})
|
|
|
|
it('finding.id.duplicate among finding categories', () => {
|
|
const doc = mutated((d) => {
|
|
const categories = d.finding_zones[0].categories
|
|
categories.push(structuredClone(categories[0]))
|
|
})
|
|
expect(issuesOf(doc)).toEqual([
|
|
['finding.id.duplicate', 'finding_zones[0].categories[3].id'],
|
|
])
|
|
})
|
|
|
|
it('finding.id.duplicate among nested children', () => {
|
|
const doc = mutated((d) => {
|
|
const articular = d.finding_zones[0].categories[0].children[0]
|
|
articular.children.push(structuredClone(articular.children[0]))
|
|
})
|
|
expect(issuesOf(doc)).toEqual([
|
|
[
|
|
'finding.id.duplicate',
|
|
'finding_zones[0].categories[0].children[0].children[2].id',
|
|
],
|
|
])
|
|
})
|
|
|
|
it('finding ids may repeat across different sibling arrays', () => {
|
|
const doc = mutated((d) => {
|
|
const hip = d.finding_zones[0].categories
|
|
hip[1].children.push(structuredClone(hip[0].children[0]))
|
|
})
|
|
expect(issuesOf(doc)).toEqual([])
|
|
})
|
|
|
|
it('reference.id.duplicate', () => {
|
|
const doc = mutated((d) => {
|
|
d.references.push(structuredClone(d.references[0]))
|
|
})
|
|
expect(issuesOf(doc)).toEqual([
|
|
['reference.id.duplicate', 'references[1].id'],
|
|
])
|
|
})
|
|
|
|
it('finding.reference.missing on a category and on a nested node', () => {
|
|
const doc = mutated((d) => {
|
|
const hip = d.finding_zones[0].categories
|
|
hip[0].reference_ids = ['nav-2017']
|
|
hip[0].children[0].children[0].reference_ids = ['legacy-catalog', 'x']
|
|
})
|
|
expect(issuesOf(doc)).toEqual([
|
|
[
|
|
'finding.reference.missing',
|
|
'finding_zones[0].categories[0].reference_ids[0]',
|
|
],
|
|
[
|
|
'finding.reference.missing',
|
|
'finding_zones[0].categories[0].children[0].children[0].reference_ids[1]',
|
|
],
|
|
])
|
|
})
|
|
|
|
it('region.count_range.invalid when count sits outside the range', () => {
|
|
const doc = mutated((d) => {
|
|
d.vertebral_config.regions[4].count_range = { min: 19, max: 21 }
|
|
})
|
|
expect(issuesOf(doc)).toEqual([
|
|
['region.count_range.invalid', 'vertebral_config.regions[4].count_range'],
|
|
])
|
|
})
|
|
|
|
it('region.count_range accepts count on the bounds', () => {
|
|
const doc = mutated((d) => {
|
|
d.vertebral_config.regions[4].count_range = { min: 18, max: 18 }
|
|
})
|
|
expect(issuesOf(doc)).toEqual([])
|
|
})
|
|
|
|
it('finding_zone.unreferenced is a warning', () => {
|
|
const doc = mutated((d) => {
|
|
d.finding_zones.push({
|
|
id: 'stifle',
|
|
name: { fr: 'Grasset', en: 'Stifle' },
|
|
categories: [],
|
|
})
|
|
})
|
|
expect(issuesOf(doc)).toEqual([
|
|
['finding_zone.unreferenced', 'finding_zones[5]'],
|
|
])
|
|
expect(validateSheetDocument(doc)[0].severity).toBe('warning')
|
|
})
|
|
})
|
|
|
|
describe('publication gate', () => {
|
|
it('accepts the fixture', () => {
|
|
expect(isPublishable(validateSheetDocument(horseSlice()))).toBe(true)
|
|
expect(() => assertPublishable(horseSlice())).not.toThrow()
|
|
})
|
|
|
|
it('accepts a document with warnings only', () => {
|
|
const doc = mutated((d) => {
|
|
d.annotations.push(point('mandible', { finding_zone_id: null }))
|
|
})
|
|
expect(validateSheetDocument(doc)).toHaveLength(1)
|
|
expect(isPublishable(validateSheetDocument(doc))).toBe(true)
|
|
expect(() => assertPublishable(doc)).not.toThrow()
|
|
})
|
|
|
|
it('refuses a document with an error, exposing the issues', () => {
|
|
const doc = mutated((d) => {
|
|
d.views[0].svg_asset_id = null
|
|
})
|
|
expect(isPublishable(validateSheetDocument(doc))).toBe(false)
|
|
let caught: unknown
|
|
try {
|
|
assertPublishable(doc)
|
|
} catch (thrown) {
|
|
caught = thrown
|
|
}
|
|
expect(caught).toBeInstanceOf(SheetValidationError)
|
|
const codes = (caught as SheetValidationError).issues.map((i) => i.code)
|
|
expect(codes).toEqual(['view.asset.missing'])
|
|
})
|
|
})
|
|
|
|
describe('createEmptySheetDocument', () => {
|
|
const empty = () =>
|
|
createEmptySheetDocument({
|
|
species_id: 'equine',
|
|
name: { fr: 'Nouvelle fiche', en: 'New sheet' },
|
|
})
|
|
|
|
it('is valid for the shape tier', () => {
|
|
expect(AnatomicalSheetDocumentSchema.safeParse(empty()).success).toBe(true)
|
|
expect(empty().views).toEqual([
|
|
expect.objectContaining({
|
|
id: 'view-main',
|
|
kind: 'main',
|
|
svg_asset_id: null,
|
|
view_box: null,
|
|
}),
|
|
])
|
|
})
|
|
|
|
it('is not publishable: the main view has no asset', () => {
|
|
const issues = validateSheetDocument(empty())
|
|
expect(issues.map((issue) => issue.code)).toEqual(['view.asset.missing'])
|
|
expect(() => assertPublishable(empty())).toThrow(SheetValidationError)
|
|
})
|
|
})
|
|
|
|
/** True for a { fr?, en? } label object, the only thing a translator edits. */
|
|
function isLabel(value: unknown): value is Record<string, string> {
|
|
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
return false
|
|
}
|
|
const keys = Object.keys(value)
|
|
return keys.length > 0 && keys.every((key) => key === 'fr' || key === 'en')
|
|
}
|
|
|
|
/** Rewrites every label with arbitrary strings, leaving all else intact. */
|
|
function mapLabels(
|
|
value: unknown,
|
|
rewrite: (label: Record<string, string>) => unknown,
|
|
): unknown {
|
|
if (isLabel(value)) return rewrite(value)
|
|
if (Array.isArray(value)) return value.map((v) => mapLabels(v, rewrite))
|
|
if (value === null || typeof value !== 'object') return value
|
|
return Object.fromEntries(
|
|
Object.entries(value).map(([k, v]) => [k, mapLabels(v, rewrite)]),
|
|
)
|
|
}
|
|
|
|
/** Every id and reference: values of keys named id, *_id or code. */
|
|
function collectIds(value: unknown, out: string[] = []): string[] {
|
|
if (Array.isArray(value)) value.forEach((v) => collectIds(v, out))
|
|
else if (value !== null && typeof value === 'object') {
|
|
for (const [key, v] of Object.entries(value)) {
|
|
const isRef = key === 'id' || key.endsWith('_id') || key.endsWith('code')
|
|
if (isRef && typeof v === 'string') out.push(`${key}=${v}`)
|
|
else collectIds(v, out)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
describe('ID stability under translation', () => {
|
|
it('keeps ids, references and validation output when labels change', () => {
|
|
const original = horseSlice()
|
|
let counter = 0
|
|
const relabeled = AnatomicalSheetDocumentSchema.parse(
|
|
mapLabels(original, (label) =>
|
|
Object.fromEntries(
|
|
Object.keys(label).map((locale) => [locale, `x${counter++}`]),
|
|
),
|
|
),
|
|
)
|
|
expect(counter).toBeGreaterThan(50)
|
|
expect(JSON.stringify(relabeled)).not.toEqual(JSON.stringify(original))
|
|
expect(collectIds(relabeled)).toEqual(collectIds(original))
|
|
const strip = () => null
|
|
expect(mapLabels(relabeled, strip)).toEqual(mapLabels(original, strip))
|
|
expect(validateSheetDocument(relabeled)).toEqual(
|
|
validateSheetDocument(original),
|
|
)
|
|
})
|
|
|
|
it('keeps validation output identical on a broken document too', () => {
|
|
const broken = mutated((d) => {
|
|
d.annotations[0].category_id = 'os'
|
|
d.views[1].view_box = null
|
|
})
|
|
const relabeled = mapLabels(broken, () => ({ fr: 'a', en: 'b' }))
|
|
expect(validateSheetDocument(relabeled as AnatomicalSheetDocument)).toEqual(
|
|
validateSheetDocument(broken),
|
|
)
|
|
})
|
|
})
|