Files
faunosteo/packages/anatomy/tests/r5.review-synthetic-oracles.test.ts
T

308 lines
8.3 KiB
TypeScript

/**
* R5.4 freeze (owner brief 2026-10-23 §4, "tester les cas nécessaires"): the TypeScript review
* files on synthetic changesets over the mini sheet and a small drawing, so the Rust port of
* the CHANGELOG, the review report, the review overlays and the application's drawings is held
* to the generic contract and not only to the equine data. Each case records the raw and the
* formatted Markdown and the four drawings. `MAKE_R5_ORACLES=1` writes
* crates/core/tests/oracles/r5/review-synthetic.json.
*/
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
import {
applyChangeset,
buildReportMigrationMap,
ChangesetSchema,
documentStats,
renderAppReviewDrawing,
renderChangelog,
renderReviewOverlay,
renderReviewReport,
} from '../src'
import { changeSummary } from '../src/curation/schema'
import { formatMarkdown } from '../src/format'
import { MINI_REFERENCES, miniSheet } from './fixtures/mini-sheet'
const HERE = dirname(fileURLToPath(import.meta.url))
const OUT = resolve(HERE, '../../../crates/core/tests/oracles/r5')
const MAKE = process.env.MAKE_R5_ORACLES === '1'
type Json = Record<string, unknown>
const DRAWING = [
'<?xml version="1.0" encoding="UTF-8"?>',
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 80" width="100" height="80">',
' <path d="M0 0 L10 10" fill="#ccbbaa"/>',
' <g id="body"><circle cx="5" cy="5" r="2" fill="#998877"/></g>',
'</svg>',
'',
].join('\n')
const LEGACY_ID_MAP = {
views: { head: 'head' },
annotations: { 'legacy-hip': 'hip', 'legacy-neck': 'neck' },
categories: {},
finding_nodes: {},
assets: {},
}
const SHEET = {
sheetId: '11111111-1111-5111-8111-111111111111',
versionId: null,
documentSha256: '0'.repeat(64),
}
const ANOMALIES = [
{ code: 'a.one', level: 'a_valider', subject: 's', message: 'm' },
{ code: 'a.two', level: 'informatif', subject: 's', message: 'm' },
{ code: 'a.three', level: 'a_valider', subject: 's', message: 'm' },
]
const MOBILITY = 'finding:hip/hip-dysfunctions/mobility'
function change(id: string, ops: unknown[], extra: Json = {}): Json {
return {
id,
level: 'A',
kind: 'technical',
title: { fr: 'Essai', en: 'Test' },
rationale: 'Essai',
reference_ids: [],
...extra,
ops,
}
}
interface Case {
name: string
changes_file: Json
}
const LONG = 'mot '.repeat(40)
const CASES: Case[] = [
{
name: 'every-section',
changes_file: {
changes: [
change(
'CHG-001',
[
{
op: 'set',
target: MOBILITY,
field: 'name',
value: { fr: 'Mobilité | pipe', en: 'Mobility\nline' },
},
{
op: 'set',
target: 'finding:neck/neck-dysfunctions/stiffness',
field: 'requires_vet_referral',
value: true,
},
{
op: 'set',
target: 'annotation:hip',
field: 'geometry',
value: { type: 'circle', cx: 30, cy: 20.5, r: 4 },
},
{
op: 'set',
target: 'annotation:neck',
field: 'geometry',
value: {
type: 'polygon',
points: [
[10, 10],
[20, 10],
[15, 25],
],
},
},
],
{ kind: 'correction', reference_ids: ['atlas'] },
),
change(
'CHG-002',
[
{
op: 'set',
target: `${MOBILITY}/flexion`,
field: 'description',
value: { fr: LONG, en: `${LONG}é` },
},
{
op: 'cite',
target: 'zone:hip',
reference_ids: ['paper'],
scope: 'subtree',
},
],
{ level: 'B', kind: 'translation', priority: 'high' },
),
change(
'CHG-010',
[
{
op: 'add',
target: MOBILITY,
value: {
id: 'rotation',
name: { fr: 'Rotation « axiale »', en: 'Axial rotation' },
},
},
],
{
level: 'C',
kind: 'enrichment',
question: 'Faut-il la garder ?',
priority: 'low',
reference_ids: ['paper'],
},
),
change(
'CHG-0011',
[{ op: 'remove', target: 'finding:hip/anamnesis/trauma' }],
{
level: 'C',
kind: 'restructure',
question: 'Retrait confirmé ?',
priority: 'high',
},
),
change('CHG-003', [{ op: 'remove', target: `${MOBILITY}/extension` }], {
kind: 'removal',
}),
],
},
},
{
name: 'detail-view-and-path',
changes_file: {
changes: [
change('CHG-001', [
{
op: 'add',
target: 'document',
field: 'annotations',
value: {
id: 'skull',
kind: 'point',
view_id: 'head',
category_id: 'bones',
laterality: 'unpaired',
geometry: { type: 'path', d: 'M1 1 L5 5' },
name: { fr: 'Crâne', en: 'Skull' },
finding_zone_id: 'neck',
vertebral_region_code: null,
},
},
{
op: 'set',
target: 'view:head',
field: 'view_box',
value: { min_x: 2.5, min_y: 1, width: 40, height: 30 },
},
]),
],
},
},
{
name: 'nothing-but-a-level-a',
changes_file: {
changes: [
change('CHG-001', [
{ op: 'set', target: 'document', field: 'name.en', value: 'Small' },
]),
],
},
},
{
// Prettier measures an emoji with its RGI table; the port refuses (declared, R5.2).
name: 'declared-emoji-in-a-cell',
changes_file: {
changes: [
change('CHG-001', [
{
op: 'set',
target: MOBILITY,
field: 'name.en',
value: 'Mobility 🐴',
},
]),
],
},
},
]
async function run(c: Case) {
const changeset = ChangesetSchema.parse(c.changes_file)
const baseline = miniSheet()
const result = applyChangeset(baseline, MINI_REFERENCES, changeset)
const before = documentStats(baseline)
const after = documentStats(result.document)
const migration = buildReportMigrationMap({
baseline: miniSheet() as never,
final: result.document as never,
legacyIdMap: LEGACY_ID_MAP,
records: result.records,
sheet: SHEET,
mainViewId: 'main',
})
const changelogRaw = renderChangelog(result, before, after, migration.stats)
const reviewRaw = renderReviewReport({
changes: changeset.changes.map(changeSummary),
records: result.records,
document: result.document,
statsBefore: before,
legacyAnomalies: ANOMALIES as never,
})
const drawings: Record<string, string> = {}
for (const view of result.document.views) {
drawings[`annotations-${view.id}`] = renderReviewOverlay({
cleanedSvg: DRAWING,
before: miniSheet(),
after: result.document,
viewId: view.id,
})
}
for (const view of result.document.views) {
drawings[`app-${view.id}`] = renderAppReviewDrawing({
cleanedSvg: DRAWING,
document: result.document,
viewId: view.id,
})
}
return {
...c,
changelog_raw: changelogRaw,
changelog: await formatMarkdown(changelogRaw),
review_raw: reviewRaw,
review: await formatMarkdown(reviewRaw),
drawings,
}
}
describe('R5.4 synthetic review oracle', () => {
it('review-synthetic.json', async () => {
const rows = []
for (const c of CASES) rows.push(await run(c))
const oracle = {
baseline: miniSheet(),
references: MINI_REFERENCES,
drawing: DRAWING,
anomalies: ANOMALIES,
legacy_id_map: LEGACY_ID_MAP,
cases: rows,
}
const text = `${JSON.stringify(oracle)}\n`
const file = resolve(OUT, 'review-synthetic.json')
if (MAKE) {
mkdirSync(OUT, { recursive: true })
writeFileSync(file, text)
}
expect(text).toBe(readFileSync(file, 'utf8'))
})
})