feat(anatomy): R5.3 curated draft in Rust (changeset applier, migration map, stats, seed SQL) and pnpm r5:import-equine

crates/core/src/curation/draft behind the curation feature: strict changeset and references parsing as JSON.parse + Zod, the six ops, rename cascades, the B/C review rule, the R2 shape and integrity gate, migration map, document stats, seed SQL. import-equine writes both folders; --check reproduces legacy-import/ and the R5.3 draft files byte for byte (12 files, 0 differences), --out claims an absent or empty, non-symlink destination and never deletes. Parity: 825 records, 46 changes, stats, 612 leaves 442/141/29, 58 synthetic TypeScript cases (oracle r5/curation-synthetic.json).
This commit is contained in:
bchanot committed 2026-10-23 00:10:55 +02:00
1 parent 5e114da2ce
commit e0f149b28a
20 files changed
+4252 -58

No files matched your search

@@ -0,0 +1,901 @@
/**
* R5.3 freeze (owner brief 2026-10-22 §7, differential tests): the TypeScript changeset applier,
* migration map, stats and seed SQL on synthetic changesets over the mini sheet, so the Rust
* port is held to the generic contract and not only to the equine changeset. Each case is a
* raw `changes.json` text; the oracle records the formatted document, the records, the stats,
* the migration map (replayed on a fixed legacy id map) and the seed SQL, or the refusal.
* `MAKE_R5_ORACLES=1` writes crates/core/tests/oracles/r5/curation-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,
ReferencesFileSchema,
renderDraftSeedSql,
} from '../src'
import { formatJson } 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>
interface Case {
name: string
changes_file: Json
}
const REFERENCES_FILE = { references: MINI_REFERENCES }
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 SEED_ASSETS = [
{
file: 'mini.svg',
sha256: 'ab'.repeat(32),
byte_size: 42,
svg_asset_id: '3f6c1b52-8a4e-4d17-9b0c-5e2a7d91c4f8',
},
]
const MOBILITY = 'finding:hip/hip-dysfunctions/mobility'
function change(ops: unknown[], extra: Json = {}): Json {
return {
id: 'CHG-001',
level: 'A',
kind: 'technical',
title: { fr: 'Essai', en: 'Test' },
rationale: 'Essai',
reference_ids: [],
...extra,
ops,
}
}
function one(ops: unknown[], extra: Json = {}): Json {
return { changes: [change(ops, extra)] }
}
function leaf(id: string, extra: Json = {}): Json {
return { id, name: { fr: `${id} fr`, en: `${id} en` }, ...extra }
}
const ACCEPTED: Case[] = [
{
name: 'set-fields',
changes_file: one([
{
op: 'set',
target: 'region:C',
field: 'count_range',
value: { min: 6, max: 8 },
},
{ op: 'set', target: 'document', field: 'name.en', value: 'Mini sheet' },
{
op: 'set',
target: 'document',
field: 'scope_disclaimer.fr',
value: 'Périmètre',
},
{
op: 'set',
target: 'annotation:hip',
field: 'geometry',
value: { type: 'circle', cx: 1.5, cy: 2, r: 0.25 },
},
{
op: 'set',
target: 'annotation:neck',
field: 'color',
value: '#aabbcc',
},
{
op: 'set',
target: `${MOBILITY}/flexion`,
field: 'description.en',
value: 'Flexion described',
},
{
op: 'set',
target: 'finding:hip/anamnesis',
field: 'name.fr',
value: 'Anamnèse complète',
},
{
op: 'set',
target: 'zone:neck',
field: 'name',
value: { fr: 'Encolure', en: 'Neck' },
},
{ op: 'set', target: 'view:head', field: 'name.en', value: 'Skull' },
{ op: 'set', target: 'category:bones', field: 'color', value: '#112233' },
{ op: 'set', target: 'reference:paper', field: 'doi', value: '10.1/y' },
{
op: 'set',
target: `${MOBILITY}/flexion`,
field: 'review_status',
value: 'reviewed',
},
]),
},
{
name: 'subtree-sets',
changes_file: one([
{
op: 'set',
target: 'finding:hip/hip-dysfunctions',
field: 'review_status',
value: 'reviewed',
scope: 'subtree',
},
{
op: 'set',
target: 'finding:hip/hip-dysfunctions',
field: 'kind',
value: 'custom',
scope: 'subtree',
},
{
op: 'set',
target: MOBILITY,
field: 'description',
value: { fr: 'Même texte' },
scope: 'subtree',
},
{
op: 'set',
target: `${MOBILITY}/flexion`,
field: 'description',
value: { fr: 'Même texte' },
},
{
op: 'set',
target: MOBILITY,
field: 'description',
value: { fr: 'Même texte' },
scope: 'subtree',
},
]),
},
{
name: 'renames-cascade',
changes_file: one([
{ op: 'cite', target: MOBILITY, reference_ids: ['atlas', 'paper'] },
{
op: 'cite',
target: 'finding:neck/neck-dysfunctions/vet',
reference_ids: ['atlas'],
},
{ op: 'rename', target: 'zone:hip', id: 'hip-joint' },
{ op: 'rename', target: 'region:C', id: 'CE' },
{ op: 'rename', target: 'view:main', id: 'principal' },
{ op: 'rename', target: 'view:head', id: 'skull' },
{ op: 'rename', target: 'category:joints', id: 'articulations' },
{ op: 'rename', target: 'reference:atlas', id: 'atlas-2' },
{
op: 'rename',
target: 'finding:hip-joint/hip-dysfunctions/mobility',
id: 'motion',
},
{ op: 'rename', target: 'annotation:hip', id: 'coxofemoral' },
]),
},
{
name: 'add-remove-move',
changes_file: one([
{ op: 'add', target: `${MOBILITY}/flexion`, value: leaf('deep-flexion') },
{ op: 'add', target: MOBILITY, value: leaf('rotation'), position: 0 },
{
op: 'add',
target: MOBILITY,
value: leaf('abduction'),
position: { after: 'flexion' },
},
{
op: 'add',
target: MOBILITY,
value: leaf('adduction'),
position: { before: 'extension' },
},
{
op: 'add',
target: 'finding:hip/anamnesis',
value: leaf('surgery'),
position: 1,
},
{
op: 'add',
target: 'zone:neck',
value: {
id: 'neck-anamnesis',
kind: 'anamnesis',
name: { fr: 'Anamnèse', en: 'Anamnesis' },
children: [leaf('fall')],
},
},
{
op: 'add',
target: 'document',
field: 'references',
value: {
id: 'web',
type: 'web',
citation: 'A page',
url: 'https://example.org/a',
doi: null,
},
},
{
op: 'add',
target: 'document',
field: 'finding_zones',
value: {
id: 'carpus',
name: { fr: 'Carpe', en: 'Carpus' },
categories: [
{
id: 'carpus-dysfunctions',
kind: 'dysfunction',
name: { fr: 'Dysfonctions', en: 'Dysfunctions' },
children: [leaf('effusion')],
},
],
},
},
{
op: 'add',
target: 'document',
field: 'annotations',
value: {
id: 'carpus',
kind: 'point',
view_id: 'main',
category_id: 'joints',
laterality: 'bilateral',
geometry: { type: 'circle', cx: 3, cy: 4, r: 1 },
name: { fr: 'Carpe', en: 'Carpus' },
finding_zone_id: 'carpus',
vertebral_region_code: null,
},
},
{
op: 'add',
target: 'document',
field: 'regions',
value: { code: 'L', name: { fr: 'Lombaires', en: 'Lumbar' }, count: 6 },
position: { after: 'T' },
},
{
op: 'add',
target: 'document',
field: 'categories',
value: {
id: 'muscles',
name: { fr: 'Muscles', en: 'Muscles' },
color: '#ff0000',
},
position: 0,
},
{ op: 'remove', target: `${MOBILITY}/extension` },
{
op: 'move',
target: `${MOBILITY}/rotation`,
to: `${MOBILITY}/flexion`,
position: 0,
},
{
op: 'move',
target: 'finding:hip/hip-dysfunctions/pain',
to: 'finding:neck/neck-dysfunctions',
position: { before: 'vet' },
},
{
op: 'move',
target: 'finding:hip/anamnesis/trauma',
to: 'finding:hip/anamnesis/surgery',
},
{
op: 'move',
target: `${MOBILITY}/adduction`,
to: MOBILITY,
position: { after: 'abduction' },
},
]),
},
{
name: 'cite',
changes_file: one([
{
op: 'cite',
target: MOBILITY,
reference_ids: ['paper', 'atlas', 'paper'],
},
{ op: 'cite', target: MOBILITY, reference_ids: ['atlas'] },
{ op: 'cite', target: 'zone:hip', reference_ids: ['atlas'] },
{
op: 'cite',
target: 'zone:neck',
reference_ids: ['paper'],
scope: 'subtree',
},
{
op: 'cite',
target: 'finding:hip/anamnesis',
reference_ids: ['paper'],
scope: 'subtree',
},
]),
},
{
name: 'review-rule-b',
changes_file: one(
[
{
op: 'set',
target: MOBILITY,
field: 'name',
value: { fr: 'Mobilité', en: 'Mobility' },
},
{
op: 'add',
target: 'finding:hip/anamnesis',
value: {
...leaf('new'),
children: [
leaf('child'),
{ ...leaf('confirmed'), review_status: 'to_confirm' },
],
},
},
{
op: 'cite',
target: 'zone:neck',
reference_ids: ['atlas'],
scope: 'subtree',
},
{
op: 'rename',
target: 'finding:neck/neck-dysfunctions/stiffness',
id: 'rigidity',
},
{
op: 'set',
target: 'annotation:neck',
field: 'name.en',
value: 'Cervical',
},
{ op: 'set', target: 'view:head', field: 'name.fr', value: 'Crâne' },
{ op: 'set', target: 'zone:hip', field: 'name.en', value: 'Hip joint' },
{
op: 'move',
target: `${MOBILITY}/flexion`,
to: 'finding:hip/anamnesis',
},
{
op: 'set',
target: 'finding:hip/anamnesis/flexion',
field: 'description.fr',
value: 'Déplacée',
},
],
{
level: 'B',
kind: 'enrichment',
reference_ids: ['atlas'],
priority: 'high',
},
),
},
{
name: 'review-rule-c',
changes_file: one(
[
{
op: 'set',
target: 'annotation:hip',
field: 'laterality',
value: 'left',
},
{
op: 'set',
target: 'finding:neck/neck-dysfunctions',
field: 'description.fr',
value: 'À confirmer',
},
{
op: 'set',
target: 'finding:neck/neck-dysfunctions/vet',
field: 'review_status',
value: 'inherited',
},
],
{
level: 'C',
kind: 'correction',
question: 'Latéralité ?',
priority: 'low',
},
),
},
{
name: 'rehome',
changes_file: one([
{
op: 'add',
target: 'zone:hip',
value: {
id: 'hip-new',
kind: 'dysfunction',
name: { fr: 'N', en: 'N' },
children: [
{
id: 'mobility',
name: { fr: 'M', en: 'M' },
children: [{ id: 'flexion', name: { fr: 'F', en: 'F' } }],
},
],
},
},
{ op: 'remove', target: 'finding:hip/hip-dysfunctions' },
{ op: 'rename', target: 'annotation:hip', id: 'coxofemoral' },
]),
},
{
// TypeScript: `z.unknown()` accepts the missing `value`, the set writes `undefined` and
// JSON.stringify drops the key. The Rust port refuses this op (declared difference).
name: 'declared-set-without-value-drops-key',
changes_file: one([
{
op: 'set',
target: `${MOBILITY}/flexion`,
field: 'description.en',
value: 'x',
},
{ op: 'set', target: `${MOBILITY}/flexion`, field: 'description.en' },
]),
},
{
name: 'two-changes',
changes_file: {
changes: [
change([{ op: 'rename', target: 'zone:neck', id: 'cervical' }], {
id: 'CHG-010',
kind: 'terminology',
}),
change(
[
{
op: 'set',
target: 'finding:cervical/neck-dysfunctions/stiffness',
field: 'name.fr',
value: 'Raideur',
},
{ op: 'cite', target: 'zone:cervical', reference_ids: ['paper'] },
],
{
id: 'CHG-0011',
level: 'C',
kind: 'translation',
question: 'Terme exact ?',
reference_ids: ['paper'],
},
),
],
},
},
]
const REFUSED: Case[] = [
{
name: 'schema-unknown-change-key',
changes_file: one([{ op: 'remove', target: `${MOBILITY}/flexion` }], {
extra: 1,
}),
},
{
name: 'schema-unknown-op-key',
changes_file: one([
{ op: 'remove', target: `${MOBILITY}/flexion`, note: 'x' },
]),
},
{
name: 'schema-bad-id',
changes_file: one([{ op: 'remove', target: `${MOBILITY}/flexion` }], {
id: 'CHG-12',
}),
},
{
name: 'schema-level-c-without-question',
changes_file: one([{ op: 'remove', target: `${MOBILITY}/flexion` }], {
level: 'C',
}),
},
{
name: 'schema-duplicate-change-ids',
changes_file: {
changes: [
change([{ op: 'remove', target: `${MOBILITY}/flexion` }]),
change([{ op: 'remove', target: `${MOBILITY}/extension` }]),
],
},
},
{ name: 'schema-empty-ops', changes_file: one([]) },
{
name: 'schema-empty-cite',
changes_file: one([{ op: 'cite', target: MOBILITY, reference_ids: [] }]),
},
{
name: 'schema-unknown-op',
changes_file: one([{ op: 'swap', target: MOBILITY }]),
},
{
name: 'schema-negative-position',
changes_file: one([
{ op: 'add', target: MOBILITY, value: leaf('x'), position: -1 },
]),
},
{
name: 'schema-fraction-position',
changes_file: one([
{ op: 'add', target: MOBILITY, value: leaf('x'), position: 1.5 },
]),
},
{
name: 'schema-two-anchors',
changes_file: one([
{
op: 'add',
target: MOBILITY,
value: leaf('x'),
position: { after: 'flexion', before: 'extension' },
},
]),
},
{
name: 'schema-title-missing-en',
changes_file: one([{ op: 'remove', target: `${MOBILITY}/flexion` }], {
title: { fr: 'Essai' },
}),
},
{
name: 'schema-empty-rationale',
changes_file: one([{ op: 'remove', target: `${MOBILITY}/flexion` }], {
rationale: '',
}),
},
{
name: 'schema-bad-priority',
changes_file: one([{ op: 'remove', target: `${MOBILITY}/flexion` }], {
priority: 'urgent',
}),
},
{
name: 'schema-empty-field',
changes_file: one([{ op: 'set', target: MOBILITY, field: '', value: 1 }]),
},
{
name: 'schema-set-without-value',
changes_file: one([{ op: 'set', target: MOBILITY, field: 'name.fr' }]),
},
{
name: 'schema-null-scope',
changes_file: one([
{ op: 'cite', target: MOBILITY, reference_ids: ['atlas'], scope: null },
]),
},
{
name: 'schema-null-position',
changes_file: one([
{ op: 'add', target: MOBILITY, value: leaf('x'), position: null },
]),
},
{
name: 'schema-null-field',
changes_file: one([
{ op: 'add', target: MOBILITY, field: null, value: leaf('x') },
]),
},
{
name: 'apply-unknown-target',
changes_file: one([
{ op: 'set', target: 'zone:nope', field: 'name', value: {} },
]),
},
{
name: 'apply-malformed-target',
changes_file: one([
{ op: 'set', target: 'finding:hip', field: 'name', value: {} },
]),
},
{
name: 'apply-unknown-kind',
changes_file: one([
{ op: 'set', target: 'galaxy:x', field: 'name', value: {} },
]),
},
{
name: 'apply-unknown-field',
changes_file: one([
{ op: 'set', target: 'zone:hip', field: 'colour', value: 1 },
]),
},
{
name: 'apply-set-id',
changes_file: one([
{ op: 'set', target: 'zone:hip', field: 'id', value: 'x' },
]),
},
{
name: 'apply-malformed-field',
changes_file: one([
{ op: 'set', target: 'zone:hip', field: 'name..fr', value: 'x' },
]),
},
{
name: 'apply-add-existing',
changes_file: one([
{ op: 'add', target: MOBILITY, value: leaf('flexion') },
]),
},
{
name: 'apply-add-unknown-sibling',
changes_file: one([
{
op: 'add',
target: MOBILITY,
value: leaf('x'),
position: { after: 'nope' },
},
]),
},
{
name: 'apply-add-out-of-range',
changes_file: one([
{ op: 'add', target: MOBILITY, value: leaf('x'), position: 3 },
]),
},
{
name: 'apply-add-non-object',
changes_file: one([{ op: 'add', target: MOBILITY, value: 'x' }]),
},
{
name: 'apply-add-document-without-field',
changes_file: one([{ op: 'add', target: 'document', value: leaf('x') }]),
},
{
name: 'apply-add-field-off-document',
changes_file: one([
{ op: 'add', target: 'zone:hip', field: 'categories', value: leaf('x') },
]),
},
{
name: 'apply-add-without-id',
changes_file: one([
{ op: 'add', target: MOBILITY, value: { name: { fr: 'a', en: 'a' } } },
]),
},
{
name: 'apply-add-to-annotation',
changes_file: one([
{ op: 'add', target: 'annotation:hip', value: leaf('x') },
]),
},
{
name: 'apply-rename-taken',
changes_file: one([{ op: 'rename', target: 'zone:hip', id: 'neck' }]),
},
{
name: 'apply-rename-document',
changes_file: one([{ op: 'rename', target: 'document', id: 'x' }]),
},
{
name: 'apply-cite-undeclared',
changes_file: one([
{ op: 'cite', target: MOBILITY, reference_ids: ['ghost'] },
]),
},
{
name: 'apply-cite-annotation',
changes_file: one([
{ op: 'cite', target: 'annotation:hip', reference_ids: ['atlas'] },
]),
},
{
name: 'apply-change-undeclared',
changes_file: one([{ op: 'remove', target: `${MOBILITY}/flexion` }], {
reference_ids: ['ghost'],
}),
},
{
name: 'apply-move-into-itself',
changes_file: one([
{ op: 'move', target: MOBILITY, to: `${MOBILITY}/flexion` },
]),
},
{
name: 'apply-move-non-finding',
changes_file: one([{ op: 'move', target: 'zone:hip', to: 'zone:neck' }]),
},
{
name: 'apply-move-node-to-zone',
changes_file: one([
{ op: 'move', target: `${MOBILITY}/flexion`, to: 'zone:neck' },
]),
},
{
name: 'apply-remove-document',
changes_file: one([{ op: 'remove', target: 'document' }]),
},
{
name: 'apply-remove-zone-integrity',
changes_file: one([{ op: 'remove', target: 'zone:hip' }]),
},
{
name: 'apply-shape-error',
changes_file: one([
{ op: 'set', target: 'region:C', field: 'count', value: 'seven' },
]),
},
{
name: 'apply-subtree-on-zone',
changes_file: one([
{
op: 'set',
target: 'zone:hip',
field: 'name',
value: {},
scope: 'subtree',
},
]),
},
{
name: 'apply-subtree-no-field',
changes_file: one([
{
op: 'set',
target: MOBILITY,
field: 'kind',
value: 'x',
scope: 'subtree',
},
]),
},
{
name: 'apply-b-sets-reviewed',
changes_file: one(
[
{
op: 'set',
target: MOBILITY,
field: 'review_status',
value: 'reviewed',
},
],
{ level: 'B' },
),
},
{
name: 'apply-second-change-fails',
changes_file: {
changes: [
change([{ op: 'rename', target: 'zone:neck', id: 'cervical' }]),
change(
[{ op: 'remove', target: 'finding:neck/neck-dysfunctions/vet' }],
{ id: 'CHG-002' },
),
],
},
},
]
const REFERENCE_FILES: Array<{ name: string; file: unknown }> = [
{
name: 'references-duplicate-id',
file: { references: [...MINI_REFERENCES, MINI_REFERENCES[0]] },
},
{
name: 'references-unknown-key',
file: { references: [{ ...MINI_REFERENCES[0], note: 'x' }] },
},
{
name: 'references-bad-type',
file: { references: [{ ...MINI_REFERENCES[0], type: 'blog' }] },
},
{
name: 'references-extra-top-key',
file: { references: MINI_REFERENCES, extra: true },
},
{
name: 'references-bad-url',
file: { references: [{ ...MINI_REFERENCES[0], url: 'ftp://x' }] },
},
]
async function run(c: Case, ok: boolean) {
try {
const changeset = ChangesetSchema.parse(c.changes_file)
const baseline = miniSheet()
const result = applyChangeset(baseline, MINI_REFERENCES, changeset)
const document = await formatJson(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',
})
if (!ok) throw new Error(`${c.name}: expected a refusal`)
return {
...c,
ok: true,
document,
records: result.records,
stats: documentStats(result.document),
migration_map: await formatJson(migration.map),
migration_stats: migration.stats,
seed_sql: renderDraftSeedSql({
sheetId: SHEET.sheetId,
assets: SEED_ASSETS,
documentJson: document,
changeNote: "Essai 'quoted'",
}),
}
} catch (error) {
if (ok) throw error
return {
...c,
ok: false,
error: error instanceof Error ? error.message : String(error),
}
}
}
function referenceCases() {
return REFERENCE_FILES.map(({ name, file }) => {
const parsed = ReferencesFileSchema.safeParse(file)
expect(parsed.success, name).toBe(false)
return { name, file, ok: false }
})
}
describe('R5.3 synthetic curation oracle', () => {
it('curation-synthetic.json', async () => {
const rows = []
for (const c of ACCEPTED) rows.push(await run(c, true))
for (const c of REFUSED) rows.push(await run(c, false))
const oracle = {
baseline: miniSheet(),
references_file: REFERENCES_FILE,
cases: rows,
reference_cases: referenceCases(),
}
const text = `${JSON.stringify(oracle)}\n`
const file = resolve(OUT, 'curation-synthetic.json')
if (MAKE) {
mkdirSync(OUT, { recursive: true })
writeFileSync(file, text)
}
expect(text).toBe(readFileSync(file, 'utf8'))
})
})