Files
faunosteo/apps/app/tests/unit/char.catalog.test.ts
T
bchanot 8673475293 chore(workspace): move the app to apps/app under a pnpm workspace (step 2)
144 files moved with git mv (pure renames). Root package.json delegates
dev/typecheck/test:run/lint/build to the packages; Node 24 LTS and
pnpm 10.13.1 pinned (.nvmrc, .node-version, engines, packageManager,
root devDependency, engine-strict). Dockerfile builds @faunosteo/app on
node:24-alpine and copies apps/app/dist; nginx and runtime config
unchanged. CI workflow for Gitea Actions. packages/ reserved (README only).
Docs synced: WORKSPACE.md, CLAUDE.md, README, DEPLOY, CHANGELOG, plan.
271/271 tests, typecheck, lint, build green from the root.
2026-10-08 21:51:13 +02:00

170 lines
7.0 KiB
TypeScript

/**
* Characterisation (Step 1): the equine findings catalog as it exists today.
* Pins the facts of docs/faunosteo-audit/05-findings-editor.md §2.4 so that
* the future legacy importer (plan Step 4) can be checked against them, and
* so that any deliberate business correction shows up as a test change.
*
* Reads the files directly (no Zod) to count what is really on disk.
*/
import { describe, it, expect } from 'vitest'
import { readFileSync } from 'node:fs'
import { createHash } from 'node:crypto'
import { fileURLToPath } from 'node:url'
import { dirname, resolve } from 'node:path'
import { SpeciesFindingsSchema } from '@/domain/schema/findings'
const HERE = dirname(fileURLToPath(import.meta.url))
const SRC_PATH = resolve(HERE, '../../src/domain/species/equine/pathologies.json')
const PUBLIC_PATH = resolve(HERE, '../../public/species/equine/pathologies.json')
interface RawNode {
id: string
name_fr?: string
name_en?: string
description_fr?: string
description_en?: string
requires_vet_referral?: boolean
sources?: unknown[]
children?: RawNode[]
}
interface RawZone { id: string; name_fr: string; name_en: string; categories: RawNode[] }
interface RawCatalog {
schema_version: string
scope_disclaimer: string
species: { id: string; name_fr: string; name_en: string }
zones: Record<string, RawZone>
}
const srcText = readFileSync(SRC_PATH, 'utf8')
const catalog = JSON.parse(srcText) as RawCatalog
const zoneKeys = Object.keys(catalog.zones)
function walk(nodes: RawNode[], depth: number, visit: (n: RawNode, depth: number) => void) {
for (const n of nodes) {
visit(n, depth)
if (n.children) walk(n.children, depth + 1, visit)
}
}
function allNodes(): Array<{ node: RawNode; depth: number; zone: string }> {
const out: Array<{ node: RawNode; depth: number; zone: string }> = []
for (const key of zoneKeys) {
walk(catalog.zones[key].categories, 1, (node, depth) => out.push({ node, depth, zone: key }))
}
return out
}
const isLeaf = (n: RawNode) => !n.children || n.children.length === 0
describe('catalog copies (src vs public)', () => {
it('the two pathologies.json copies are byte-identical (manual sync, no script)', () => {
const publicText = readFileSync(PUBLIC_PATH, 'utf8')
const md5 = (s: string) => createHash('md5').update(s).digest('hex')
expect(md5(publicText)).toBe(md5(srcText))
})
it('the bundled copy passes the Zod schema (boot-time contract)', () => {
expect(() => SpeciesFindingsSchema.parse(catalog)).not.toThrow()
})
})
describe('catalog shape (audit 05 §2.4)', () => {
it('declares schema_version "2.0" and species equine / Cheval / Horse', () => {
expect(catalog.schema_version).toBe('2.0')
expect(catalog.species).toEqual({ id: 'equine', name_fr: 'Cheval', name_en: 'Horse' })
expect(catalog.scope_disclaimer.length).toBeGreaterThan(50)
})
it('has exactly 26 zones in cranio-caudal insertion order', () => {
expect(zoneKeys).toEqual([
'parietal', 'frontal', 'sphenoid', 'temporal', 'occipital', 'ssb', 'atm', 'hyoid',
'c0-c1', 'cervical', 'c7-t1', 'shoulder', 'elbow', 'fore-fetlock', 'fore-sesamoids',
'thoracic', 't18-l1', 'lumbar', 'l6-s1', 'sacral', 'sacroiliac', 'hip', 'stifle',
'hock', 'hind-fetlock', 'caudal',
])
})
it('every zone.id equals its record key (convention, not enforced by Zod)', () => {
for (const key of zoneKeys) expect(catalog.zones[key].id).toBe(key)
})
it('every zone has exactly 3 root categories, the third always "anamnesis"', () => {
for (const key of zoneKeys) {
const cats = catalog.zones[key].categories
expect(cats, key).toHaveLength(3)
expect(cats[2].id, key).toBe('anamnesis')
}
})
it('root category ids follow <zone>-dysfunctions / <zone>-observations, except the known sphenoid anomaly', () => {
const anomalies: string[] = []
for (const key of zoneKeys) {
const [dys, obs] = catalog.zones[key].categories
if (dys.id !== `${key}-dysfunctions`) anomalies.push(`${key}:${dys.id}`)
if (obs.id !== `${key}-observations`) anomalies.push(`${key}:${obs.id}`)
}
// KNOWN: sphenoid reuses the ssb category id (audit 05 §2.4). The business
// correction is deferred to the editor before the first official version
// (OPEN_QUESTIONS Q9); the importer must NOT silently fix it.
expect(anomalies).toEqual(['sphenoid:ssb-dysfunctions'])
})
it('counts 792 nodes, 612 leaves, max depth 3, 228 leaves at depth 2 and 384 at depth 3', () => {
const nodes = allNodes()
expect(nodes).toHaveLength(792)
const leaves = nodes.filter((n) => isLeaf(n.node))
expect(leaves).toHaveLength(612)
expect(Math.max(...nodes.map((n) => n.depth))).toBe(3)
expect(leaves.filter((n) => n.depth === 2)).toHaveLength(228)
expect(leaves.filter((n) => n.depth === 3)).toHaveLength(384)
})
it('flags 41 vet-referral nodes, all of them leaves', () => {
const flagged = allNodes().filter((n) => n.node.requires_vet_referral === true)
expect(flagged).toHaveLength(41)
expect(flagged.every((n) => isLeaf(n.node))).toBe(true)
})
it('carries 62 description_fr, 0 description_en, 0 sources (CLAUDE.md "≥1 source" rule not met)', () => {
const nodes = allNodes()
expect(nodes.filter((n) => typeof n.node.description_fr === 'string')).toHaveLength(62)
expect(nodes.filter((n) => typeof n.node.description_en === 'string')).toHaveLength(0)
expect(nodes.filter((n) => Array.isArray(n.node.sources))).toHaveLength(0)
})
it('every node has non-empty name_fr and name_en', () => {
for (const { node, zone } of allNodes()) {
expect(node.name_fr, `${zone}/${node.id}`).toBeTruthy()
expect(node.name_en, `${zone}/${node.id}`).toBeTruthy()
}
})
it('node ids are unique among siblings only (same id reused across zones)', () => {
const idCount = new Map<string, number>()
for (const { node } of allNodes()) idCount.set(node.id, (idCount.get(node.id) ?? 0) + 1)
expect(idCount.get('anamnesis')).toBe(26)
expect(idCount.get('trauma-direct')).toBe(26)
expect(idCount.get('no-identified-cause')).toBe(26)
expect(idCount.get('amplitude-asymmetry')).toBe(11)
// sibling uniqueness holds everywhere
const checkSiblings = (nodes: RawNode[], where: string) => {
const ids = nodes.map((n) => n.id)
expect(new Set(ids).size, where).toBe(ids.length)
for (const n of nodes) if (n.children) checkSiblings(n.children, `${where}/${n.id}`)
}
for (const key of zoneKeys) checkSiblings(catalog.zones[key].categories, key)
})
it('anamnesis exists in exactly three content variants (vertebral / appendicular / head)', () => {
const variants = new Map<string, string[]>()
for (const key of zoneKeys) {
const anam = catalog.zones[key].categories[2]
const sig = (anam.children ?? []).map((c) => c.id).join(',')
variants.set(sig, [...(variants.get(sig) ?? []), key])
}
expect(variants.size).toBe(3)
const groups = [...variants.values()].map((g) => g.length).sort((a, b) => a - b)
expect(groups).toEqual([8, 9, 9])
})
})