feat(anatomy): curation pipeline — changeset schema, applier with before/after records, cleaned single asset, draft seed, changelog + review report generators; seed layout legacy-import/curation/draft
This commit is contained in:
1 parent
3dbbfdf1df
commit
6870d87910
58 files changed
+18167
-267
No files matched your search
@@ -0,0 +1,429 @@
|
||||
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)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,79 @@
|
||||
import { DOMParser } from '@xmldom/xmldom'
|
||||
import { createHash } from 'node:crypto'
|
||||
import { readFile } from 'node:fs/promises'
|
||||
import { join } from 'node:path'
|
||||
import {
|
||||
EQUINE_SOURCE_PATHS,
|
||||
assetIdFromSha256,
|
||||
cleanedAsset,
|
||||
parseViewBox,
|
||||
stripLegacyAnnotations,
|
||||
} from '../src'
|
||||
import { REPO_ROOT } from './helpers'
|
||||
|
||||
let source: string
|
||||
let cleaned: string
|
||||
|
||||
function count(text: string, pattern: RegExp): number {
|
||||
return [...text.matchAll(pattern)].length
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
source = await readFile(join(REPO_ROOT, EQUINE_SOURCE_PATHS.mainSvg), 'utf8')
|
||||
cleaned = stripLegacyAnnotations(source)
|
||||
})
|
||||
|
||||
describe('stripLegacyAnnotations on the real Horse.svg', () => {
|
||||
it('removes exactly the elements carrying data-anno-id', () => {
|
||||
const annotated = count(source, /<[a-z]+\b[^>]*\sdata-anno-id="/g)
|
||||
const elements = (text: string) => count(text, /<[a-zA-Z]+[\s/>]/g)
|
||||
expect(annotated).toBe(30) // 12 points × (hitbox + marker) + 6 polygons
|
||||
// each annotated element carries one <title> child, removed with it
|
||||
const titles = count(source, /data-anno-id="[^"]*"[^>]*><title>/g)
|
||||
expect(elements(source) - elements(cleaned)).toBe(annotated + titles)
|
||||
})
|
||||
|
||||
it('keeps the 270 drawing paths and every non-annotation byte', () => {
|
||||
expect(count(source, /<path\b/g)).toBe(270)
|
||||
expect(count(cleaned, /<path\b/g)).toBe(270)
|
||||
expect(cleaned).not.toContain('data-anno-')
|
||||
expect(cleaned.startsWith('<?xml version="1.0" encoding="UTF-8"?>')).toBe(
|
||||
true,
|
||||
)
|
||||
expect(cleaned).not.toContain('max-width: 100%')
|
||||
})
|
||||
|
||||
it('is deterministic and parses as XML', () => {
|
||||
expect(stripLegacyAnnotations(source)).toBe(cleaned)
|
||||
const errors: string[] = []
|
||||
const doc = new DOMParser({
|
||||
onError: (level, message) => {
|
||||
if (level !== 'warning') errors.push(message)
|
||||
},
|
||||
}).parseFromString(cleaned, 'image/svg+xml')
|
||||
expect(errors).toEqual([])
|
||||
expect(doc.documentElement?.tagName).toBe('svg')
|
||||
})
|
||||
|
||||
it('describes the cleaned bytes with their sha256', () => {
|
||||
const bytes = new TextEncoder().encode(cleaned)
|
||||
const asset = cleanedAsset(bytes)
|
||||
const sha256 = createHash('sha256').update(bytes).digest('hex')
|
||||
expect(asset).toEqual({
|
||||
file: 'horse.svg',
|
||||
sha256,
|
||||
byte_size: bytes.byteLength,
|
||||
svg_asset_id: assetIdFromSha256(sha256),
|
||||
})
|
||||
})
|
||||
|
||||
it('reads a viewBox into a document view_box', () => {
|
||||
expect(parseViewBox('60 100 500 380')).toEqual({
|
||||
min_x: 60,
|
||||
min_y: 100,
|
||||
width: 500,
|
||||
height: 380,
|
||||
})
|
||||
expect(() => parseViewBox('1 2 3')).toThrow('invalid viewBox')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,103 @@
|
||||
import {
|
||||
AnatomicalSheetDocumentSchema,
|
||||
validateSheetDocument,
|
||||
} from '@faunosteo/domain'
|
||||
import {
|
||||
buildEquineDraft,
|
||||
DRAFT_SEED_SQL_FILE,
|
||||
renderDraftArtifacts,
|
||||
type DraftResult,
|
||||
type ImportResult,
|
||||
} from '../src'
|
||||
import { changesetOf } from './fixtures/mini-sheet'
|
||||
import { runDraft } from './helpers'
|
||||
|
||||
const VIEW_BOX = { min_x: 0, min_y: 0, width: 1, height: 1 }
|
||||
|
||||
let result: ImportResult
|
||||
let draft: DraftResult
|
||||
let artifacts: Record<string, string>
|
||||
|
||||
beforeAll(async () => {
|
||||
;({ result, draft } = await runDraft())
|
||||
artifacts = await renderDraftArtifacts(draft, result.anomalies)
|
||||
})
|
||||
|
||||
const REVIEW_HEADINGS = [
|
||||
'1. Qualité des données initiales',
|
||||
'2. Principales erreurs anatomiques et ostéopathiques',
|
||||
'3. Corrections effectuées',
|
||||
'4. Ajouts',
|
||||
'5. Réorganisations du catalogue',
|
||||
'6. Éléments incertains',
|
||||
'7. Traductions',
|
||||
'8. Points de vigilance clinique',
|
||||
'9. Bibliographie',
|
||||
'10. Décisions réservées au professionnel',
|
||||
]
|
||||
|
||||
describe('equine working draft (real sources + committed curation)', () => {
|
||||
it('validates with zero integrity error', () => {
|
||||
const parsed = AnatomicalSheetDocumentSchema.parse(draft.document)
|
||||
const errors = validateSheetDocument(parsed).filter(
|
||||
(i) => i.severity === 'error',
|
||||
)
|
||||
expect(errors).toEqual([])
|
||||
expect(draft.sheetId).toBe(result.sheetId)
|
||||
})
|
||||
|
||||
it('keeps the catalog volumes and carries one drawing', () => {
|
||||
expect(draft.statsAfter.nodes).toBe(draft.statsBefore.nodes)
|
||||
expect(draft.statsAfter.leaves).toBe(draft.statsBefore.leaves)
|
||||
expect(draft.statsBefore.assets).toBe(2)
|
||||
expect(draft.statsAfter.assets).toBe(1)
|
||||
expect(draft.assets).toHaveLength(1)
|
||||
const [main, head] = draft.document.views
|
||||
expect(main!.svg_asset_id).toBe(draft.assets[0]!.svg_asset_id)
|
||||
expect(head).toMatchObject({
|
||||
svg_asset_id: null,
|
||||
view_box: { min_x: 60, min_y: 100, width: 500, height: 380 },
|
||||
})
|
||||
const cd = draft.document.vertebral_config.regions.find(
|
||||
(r) => r.code === 'Cd',
|
||||
)
|
||||
expect(cd!.count_range).toEqual({ min: 15, max: 21 })
|
||||
})
|
||||
|
||||
it('lists CHG-000 and CHG-001 in the CHANGELOG', () => {
|
||||
const changelog = artifacts['CHANGELOG.md']!
|
||||
expect(changelog).toContain('## Comparaison quantitative')
|
||||
expect(changelog).toMatch(/\| `CHG-000` .*`view:head\.view_box`/)
|
||||
expect(changelog).toMatch(/\| `CHG-001` .*`region:Cd\.count_range`/)
|
||||
})
|
||||
|
||||
it('writes the ten review sections in order', () => {
|
||||
const headings = [
|
||||
...artifacts['REVIEW-REPORT.md']!.matchAll(/^## (.+)$/gm),
|
||||
].map((m) => m[1])
|
||||
expect(headings).toEqual(REVIEW_HEADINGS)
|
||||
})
|
||||
|
||||
it('seeds a converging draft and never publishes', () => {
|
||||
const sql = artifacts[DRAFT_SEED_SQL_FILE]!
|
||||
expect(sql).toContain('private.insert_sheet_draft')
|
||||
expect(sql).toMatch(
|
||||
/update public\.anatomical_sheet_versions[\s\S]*status = 'draft'/,
|
||||
)
|
||||
expect(sql).not.toContain('publish_sheet_version')
|
||||
})
|
||||
|
||||
it('refuses a changeset that reuses the reserved CHG-000', () => {
|
||||
const [own] = changesetOf([{ op: 'remove', target: 'zone:hip' }]).changes
|
||||
const changeset = { changes: [{ ...own!, id: 'CHG-000' }] }
|
||||
const input = { importResult: result, legacyMainSvgBytes: new Uint8Array() }
|
||||
expect(() =>
|
||||
buildEquineDraft({
|
||||
...input,
|
||||
headViewBox: VIEW_BOX,
|
||||
references: [],
|
||||
changeset,
|
||||
}),
|
||||
).toThrow('CHG-000 is reserved')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,77 @@
|
||||
import { readFile } from 'node:fs/promises'
|
||||
import { join } from 'node:path'
|
||||
import {
|
||||
ChangesetSchema,
|
||||
EQUINE_CURATION_DIR,
|
||||
ReferencesFileSchema,
|
||||
needsConfirmation,
|
||||
} from '../src'
|
||||
import { REPO_ROOT } from './helpers'
|
||||
|
||||
async function committed(file: string): Promise<unknown> {
|
||||
return JSON.parse(
|
||||
await readFile(join(REPO_ROOT, EQUINE_CURATION_DIR, file), 'utf8'),
|
||||
)
|
||||
}
|
||||
|
||||
const CHANGE = {
|
||||
id: 'CHG-002',
|
||||
level: 'B',
|
||||
kind: 'terminology',
|
||||
title: { fr: 'Titre', en: 'Title' },
|
||||
rationale: 'Raison',
|
||||
reference_ids: [],
|
||||
ops: [{ op: 'remove', target: 'zone:x' }],
|
||||
}
|
||||
|
||||
describe('curation schemas', () => {
|
||||
it('parse the committed curation files', async () => {
|
||||
const changeset = ChangesetSchema.parse(await committed('changes.json'))
|
||||
expect(changeset.changes.map((c) => [c.id, c.priority])).toEqual([
|
||||
['CHG-001', 'medium'],
|
||||
])
|
||||
const refs = ReferencesFileSchema.parse(await committed('references.json'))
|
||||
expect(refs.references.map((r) => r.id)).toEqual(['legacy-catalog'])
|
||||
})
|
||||
|
||||
it.each([
|
||||
[
|
||||
'an unknown op',
|
||||
{ ...CHANGE, ops: [{ op: 'replace', target: 'zone:x' }] },
|
||||
],
|
||||
['a level C change without question', { ...CHANGE, level: 'C' }],
|
||||
['a bad change id', { ...CHANGE, id: 'CHANGE-1' }],
|
||||
['a change without ops', { ...CHANGE, ops: [] }],
|
||||
['an unknown key', { ...CHANGE, needs_confirmation: true }],
|
||||
])('rejects %s', (_label, change) => {
|
||||
expect(ChangesetSchema.safeParse({ changes: [change] }).success).toBe(false)
|
||||
})
|
||||
|
||||
it('accepts a level C change with its question', () => {
|
||||
const change = {
|
||||
...CHANGE,
|
||||
level: 'C',
|
||||
question: 'Faut-il ?',
|
||||
priority: 'high',
|
||||
}
|
||||
expect(ChangesetSchema.safeParse({ changes: [change] }).success).toBe(true)
|
||||
})
|
||||
|
||||
it('rejects duplicate change ids and duplicate reference ids', () => {
|
||||
expect(
|
||||
ChangesetSchema.safeParse({ changes: [CHANGE, CHANGE] }).success,
|
||||
).toBe(false)
|
||||
const ref = { id: 'a', type: 'web', citation: 'A', url: null, doi: null }
|
||||
expect(
|
||||
ReferencesFileSchema.safeParse({ references: [ref, ref] }).success,
|
||||
).toBe(false)
|
||||
})
|
||||
|
||||
it('derives needs_confirmation from the level', () => {
|
||||
expect(['A', 'B', 'C'].map((l) => needsConfirmation(l as 'A'))).toEqual([
|
||||
false,
|
||||
true,
|
||||
true,
|
||||
])
|
||||
})
|
||||
})
|
||||
+175
@@ -0,0 +1,175 @@
|
||||
import type {
|
||||
AnatomicalSheetDocumentInput,
|
||||
FindingNodeInput,
|
||||
Reference,
|
||||
} from '@faunosteo/domain'
|
||||
import type { ChangeInput, Changeset, Op } from '../../src'
|
||||
import { ChangesetSchema } from '../../src'
|
||||
|
||||
/**
|
||||
* Small synthetic sheet for the curation unit tests: two finding zones with
|
||||
* nested nodes, two observable annotations, one detail view opened by a
|
||||
* trigger. Lean input form, zero integrity issue as written.
|
||||
*/
|
||||
|
||||
function leaf(
|
||||
id: string,
|
||||
extra: Partial<FindingNodeInput> = {},
|
||||
): FindingNodeInput {
|
||||
return { id, name: { fr: `${id} fr`, en: `${id} en` }, ...extra }
|
||||
}
|
||||
|
||||
export const MINI_REFERENCES: Reference[] = [
|
||||
{
|
||||
id: 'atlas',
|
||||
type: 'book',
|
||||
citation: 'Atlas of anatomy',
|
||||
url: null,
|
||||
doi: null,
|
||||
},
|
||||
{
|
||||
id: 'paper',
|
||||
type: 'journal',
|
||||
citation: 'A paper',
|
||||
url: null,
|
||||
doi: '10.1/x',
|
||||
},
|
||||
]
|
||||
|
||||
export function miniSheet(): AnatomicalSheetDocumentInput {
|
||||
return {
|
||||
schema_version: 2,
|
||||
species_id: 'equine',
|
||||
name: { fr: 'Mini', en: 'Mini' },
|
||||
description: {},
|
||||
scope_disclaimer: {},
|
||||
attribution: { authors: [], license: null, notes: null },
|
||||
vertebral_config: {
|
||||
regions: [
|
||||
{ code: 'C', name: { fr: 'Cervicales', en: 'Cervical' }, count: 7 },
|
||||
{ code: 'T', name: { fr: 'Thoraciques', en: 'Thoracic' }, count: 18 },
|
||||
],
|
||||
},
|
||||
references: [],
|
||||
categories: [
|
||||
{
|
||||
id: 'joints',
|
||||
name: { fr: 'Articulations', en: 'Joints' },
|
||||
color: null,
|
||||
},
|
||||
{ id: 'bones', name: { fr: 'Os', en: 'Bones' }, color: null },
|
||||
],
|
||||
views: [
|
||||
{
|
||||
id: 'main',
|
||||
kind: 'main',
|
||||
name: { fr: 'Vue', en: 'View' },
|
||||
svg_asset_id: '3f6c1b52-8a4e-4d17-9b0c-5e2a7d91c4f8',
|
||||
view_box: null,
|
||||
},
|
||||
{
|
||||
id: 'head',
|
||||
kind: 'detail',
|
||||
parent_view_id: 'main',
|
||||
name: { fr: 'Tête', en: 'Head' },
|
||||
svg_asset_id: null,
|
||||
view_box: { min_x: 0, min_y: 0, width: 10, height: 10 },
|
||||
},
|
||||
],
|
||||
annotations: [
|
||||
{
|
||||
id: 'hip',
|
||||
kind: 'point',
|
||||
view_id: 'main',
|
||||
category_id: 'joints',
|
||||
laterality: 'bilateral',
|
||||
geometry: { type: 'circle', cx: 1, cy: 2, r: 3 },
|
||||
name: { fr: 'Hanche', en: 'Hip' },
|
||||
finding_zone_id: 'hip',
|
||||
vertebral_region_code: null,
|
||||
},
|
||||
{
|
||||
id: 'neck',
|
||||
kind: 'zone',
|
||||
view_id: 'main',
|
||||
category_id: 'bones',
|
||||
laterality: 'unpaired',
|
||||
geometry: null,
|
||||
name: { fr: 'Cou', en: 'Neck' },
|
||||
finding_zone_id: 'neck',
|
||||
vertebral_region_code: 'C',
|
||||
},
|
||||
{
|
||||
id: 'head-trigger',
|
||||
kind: 'detail_trigger',
|
||||
view_id: 'main',
|
||||
category_id: 'bones',
|
||||
laterality: 'unpaired',
|
||||
geometry: null,
|
||||
name: null,
|
||||
opens_view_id: 'head',
|
||||
},
|
||||
],
|
||||
finding_zones: [
|
||||
{
|
||||
id: 'hip',
|
||||
name: { fr: 'Hanche', en: 'Hip' },
|
||||
categories: [
|
||||
{
|
||||
id: 'hip-dysfunctions',
|
||||
kind: 'dysfunction',
|
||||
name: { fr: 'Dysfonctions', en: 'Dysfunctions' },
|
||||
children: [
|
||||
leaf('mobility', {
|
||||
children: [leaf('flexion'), leaf('extension')],
|
||||
}),
|
||||
leaf('pain'),
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 'anamnesis',
|
||||
kind: 'anamnesis',
|
||||
name: { fr: 'Anamnèse', en: 'Anamnesis' },
|
||||
children: [leaf('trauma')],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 'neck',
|
||||
name: { fr: 'Cou', en: 'Neck' },
|
||||
categories: [
|
||||
{
|
||||
id: 'neck-dysfunctions',
|
||||
kind: 'dysfunction',
|
||||
name: { fr: 'Dysfonctions', en: 'Dysfunctions' },
|
||||
children: [
|
||||
leaf('stiffness'),
|
||||
leaf('vet', { requires_vet_referral: true }),
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
/** One level A change carrying the given ops, parsed like changes.json. */
|
||||
export function changesetOf(
|
||||
ops: Op[],
|
||||
extra: Partial<ChangeInput> = {},
|
||||
): Changeset {
|
||||
return ChangesetSchema.parse({
|
||||
changes: [
|
||||
{
|
||||
id: 'CHG-001',
|
||||
level: 'A',
|
||||
kind: 'technical',
|
||||
title: { fr: 'Essai', en: 'Test' },
|
||||
rationale: 'Essai',
|
||||
reference_ids: [],
|
||||
ops,
|
||||
...extra,
|
||||
},
|
||||
],
|
||||
})
|
||||
}
|
||||
@@ -2,9 +2,14 @@ import { readFile } from 'node:fs/promises'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { join } from 'node:path'
|
||||
import {
|
||||
buildEquineDraft,
|
||||
EQUINE_SOURCE_PATHS,
|
||||
HEAD_VIEW_ID,
|
||||
importEquineLegacy,
|
||||
parseViewBox,
|
||||
readEquineCuration,
|
||||
readEquineSources,
|
||||
type DraftResult,
|
||||
type EquineLegacyInput,
|
||||
type ImportResult,
|
||||
} from '../src'
|
||||
@@ -34,3 +39,19 @@ export async function legacyAnnoIds(path: string): Promise<string[]> {
|
||||
const ids = [...text.matchAll(/data-anno-id="([^"]+)"/g)].map((m) => m[1]!)
|
||||
return [...new Set(ids)]
|
||||
}
|
||||
|
||||
/** Legacy import + committed curation files → the working draft. */
|
||||
export async function runDraft(): Promise<{
|
||||
result: ImportResult
|
||||
draft: DraftResult
|
||||
}> {
|
||||
const input = await loadInput()
|
||||
const result = importEquineLegacy(input)
|
||||
const draft = buildEquineDraft({
|
||||
importResult: result,
|
||||
legacyMainSvgBytes: input.mainSvg.bytes,
|
||||
headViewBox: parseViewBox(result.viewBoxes[HEAD_VIEW_ID] ?? null),
|
||||
...(await readEquineCuration(REPO_ROOT)),
|
||||
})
|
||||
return { result, draft }
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { renderSeedSql, DOCUMENT_DOLLAR_TAG } from '../src'
|
||||
import { DOCUMENT_DOLLAR_TAG, renderDraftSeedSql } from '../src'
|
||||
import { mapAnnotation } from '../src/legacy/annotations'
|
||||
import { assertNothingDropped } from '../src/legacy/catalog'
|
||||
import { parseAnnotatedSvg } from '../src/legacy/svg'
|
||||
@@ -39,7 +39,8 @@ describe('importer guards', () => {
|
||||
it('refuses a document that contains the dollar-quote tag', async () => {
|
||||
const result = await runImport()
|
||||
const documentJson = JSON.stringify({ note: DOCUMENT_DOLLAR_TAG })
|
||||
expect(() => renderSeedSql({ result, documentJson })).toThrow(
|
||||
const params = { sheetId: result.sheetId, assets: [], changeNote: 'x' }
|
||||
expect(() => renderDraftSeedSql({ ...params, documentJson })).toThrow(
|
||||
DOCUMENT_DOLLAR_TAG,
|
||||
)
|
||||
})
|
||||
|
||||
@@ -1,49 +1,76 @@
|
||||
import { readFile } from 'node:fs/promises'
|
||||
import { join } from 'node:path'
|
||||
import {
|
||||
EQUINE_SEED_DIR,
|
||||
DRAFT_SEED_SQL_FILE,
|
||||
EQUINE_DRAFT_DIR,
|
||||
EQUINE_LEGACY_IMPORT_DIR,
|
||||
EQUINE_SOURCE_PATHS,
|
||||
SEED_SQL_FILE,
|
||||
renderArtifacts,
|
||||
renderDraftArtifacts,
|
||||
sha256Hex,
|
||||
type AssetRecord,
|
||||
type DraftResult,
|
||||
} from '../src'
|
||||
import { REPO_ROOT, runImport } from './helpers'
|
||||
import { REPO_ROOT, runDraft } from './helpers'
|
||||
|
||||
const seedDir = join(REPO_ROOT, EQUINE_SEED_DIR)
|
||||
let artifacts: Record<string, string>
|
||||
const legacyDir = join(REPO_ROOT, EQUINE_LEGACY_IMPORT_DIR)
|
||||
const draftDir = join(REPO_ROOT, EQUINE_DRAFT_DIR)
|
||||
let legacy: Record<string, string>
|
||||
let draftFiles: Record<string, string>
|
||||
let draft: DraftResult
|
||||
|
||||
beforeAll(async () => {
|
||||
artifacts = await renderArtifacts(await runImport())
|
||||
const run = await runDraft()
|
||||
draft = run.draft
|
||||
legacy = await renderArtifacts(run.result)
|
||||
draftFiles = await renderDraftArtifacts(run.draft, run.result.anomalies)
|
||||
})
|
||||
|
||||
describe('committed equine seed (golden)', () => {
|
||||
describe('committed legacy-import folder (golden)', () => {
|
||||
it.each([
|
||||
'document.json',
|
||||
'legacy-id-map.json',
|
||||
'assets.json',
|
||||
SEED_SQL_FILE,
|
||||
'IMPORT-REPORT.md',
|
||||
])('%s equals a fresh run (rerun pnpm import:equine)', async (file) => {
|
||||
const committed = await readFile(join(seedDir, file), 'utf8')
|
||||
expect(committed).toBe(artifacts[file])
|
||||
expect(await readFile(join(legacyDir, file), 'utf8')).toBe(legacy[file])
|
||||
})
|
||||
|
||||
it('carries no SQL: the draft folder holds the only seed', () => {
|
||||
expect(Object.keys(legacy).some((file) => file.endsWith('.sql'))).toBe(
|
||||
false,
|
||||
)
|
||||
})
|
||||
|
||||
it('copies the SVG assets byte for byte with the recorded sha256', async () => {
|
||||
const assets = JSON.parse(artifacts['assets.json']!) as AssetRecord[]
|
||||
const assets = JSON.parse(legacy['assets.json']!) as AssetRecord[]
|
||||
const sources = [EQUINE_SOURCE_PATHS.mainSvg, EQUINE_SOURCE_PATHS.headSvg]
|
||||
for (const [index, asset] of assets.entries()) {
|
||||
const copy = await readFile(join(seedDir, 'assets', asset.file))
|
||||
const copy = await readFile(join(legacyDir, 'assets', asset.file))
|
||||
const original = await readFile(join(REPO_ROOT, sources[index]!))
|
||||
expect(copy.equals(original)).toBe(true)
|
||||
expect(sha256Hex(copy)).toBe(asset.sha256)
|
||||
expect(copy.byteLength).toBe(asset.byte_size)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('seeds a draft and never publishes', () => {
|
||||
const sql = artifacts[SEED_SQL_FILE]!
|
||||
expect(sql).toContain('private.insert_sheet_draft')
|
||||
expect(sql).not.toContain('publish_sheet_version')
|
||||
describe('committed draft folder (golden)', () => {
|
||||
it.each([
|
||||
'document.json',
|
||||
'assets.json',
|
||||
'CHANGELOG.md',
|
||||
'REVIEW-REPORT.md',
|
||||
DRAFT_SEED_SQL_FILE,
|
||||
])('%s equals a fresh run (rerun pnpm import:equine)', async (file) => {
|
||||
expect(await readFile(join(draftDir, file), 'utf8')).toBe(draftFiles[file])
|
||||
})
|
||||
|
||||
it('holds the cleaned drawing with the recorded sha256', async () => {
|
||||
const [asset] = draft.assets
|
||||
const bytes = await readFile(join(draftDir, 'assets', asset!.file))
|
||||
expect(bytes.toString('utf8')).toBe(draft.cleanedSvg)
|
||||
expect(sha256Hex(bytes)).toBe(asset!.sha256)
|
||||
expect(bytes.byteLength).toBe(asset!.byte_size)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user