diff --git a/src/domain/schema/findings.ts b/src/domain/schema/findings.ts new file mode 100644 index 00000000..0f17997b --- /dev/null +++ b/src/domain/schema/findings.ts @@ -0,0 +1,67 @@ +import { z } from 'zod' + +/** + * Schema for the species findings tree. + * + * Source of truth for what a practitioner can attach to an observation: + * a hierarchical tree of dysfunctions/observations per anatomical zone, + * authored by the domain expert in JSON and parsed at boot. + * + * Field names match the JSON authoring contract (snake_case). + * Recursive children are typed via z.lazy + an explicit ZodType annotation + * so TS can resolve the self-reference. + */ + +export const SourceRefSchema = z.object({ + type: z.enum(['manual', 'journal', 'database', 'web']), + citation: z.string().min(1), + url: z.string().url().optional(), +}) + +export type SourceRef = z.infer + +export interface FindingNode { + id: string + name_fr: string + name_en: string + description_fr?: string + description_en?: string + requires_vet_referral?: boolean + sources?: SourceRef[] + children?: FindingNode[] +} + +export const FindingNodeSchema: z.ZodType = z.lazy(() => + z.object({ + id: z.string().min(1), + name_fr: z.string().min(1), + name_en: z.string().min(1), + description_fr: z.string().optional(), + description_en: z.string().optional(), + requires_vet_referral: z.boolean().optional(), + sources: z.array(SourceRefSchema).optional(), + children: z.array(FindingNodeSchema).optional(), + }), +) + +export const ZoneFindingsSchema = z.object({ + id: z.string().min(1), + name_fr: z.string().min(1), + name_en: z.string().min(1), + categories: z.array(FindingNodeSchema), +}) + +export type ZoneFindings = z.infer + +export const SpeciesFindingsSchema = z.object({ + schema_version: z.string().min(1), + scope_disclaimer: z.string().min(1), + species: z.object({ + id: z.string().min(1), + name_fr: z.string().min(1), + name_en: z.string().min(1), + }), + zones: z.record(z.string(), ZoneFindingsSchema), +}) + +export type SpeciesFindings = z.infer diff --git a/src/domain/species/equine/index.ts b/src/domain/species/equine/index.ts index 06f1d7d4..1ac88ea4 100644 --- a/src/domain/species/equine/index.ts +++ b/src/domain/species/equine/index.ts @@ -9,5 +9,5 @@ export const equine: SpeciesManifest = { head: '/species/equine/Horse_head.svg', }, vertebralFormula: EQUINE_VERTEBRAL_FORMULA, - pathologies: () => import('./pathologies'), + findings: () => import('./pathologies'), } diff --git a/src/domain/species/equine/pathologies.json b/src/domain/species/equine/pathologies.json index 9aed26d5..5d76ffff 100644 --- a/src/domain/species/equine/pathologies.json +++ b/src/domain/species/equine/pathologies.json @@ -2,7 +2,7 @@ "schema_version": "1.1", "scope_disclaimer": "Les éléments listés correspondent au périmètre légal de l'ostéopathie animale : dysfonctions ostéopathiques palpées, observations cliniques et motifs de consultation. Aucun diagnostic médical n'est posé. Toute pathologie organique relève du vétérinaire.", "species": { - "id": "equin", + "id": "equine", "name_fr": "Cheval", "name_en": "Horse" }, diff --git a/src/domain/species/equine/pathologies.ts b/src/domain/species/equine/pathologies.ts index fabc22a1..d6fd1ece 100644 --- a/src/domain/species/equine/pathologies.ts +++ b/src/domain/species/equine/pathologies.ts @@ -1,10 +1,13 @@ -import type { Pathology } from '@/domain/schema/pathology' +import { + SpeciesFindingsSchema, + type SpeciesFindings, +} from '@/domain/schema/findings' +import data from './pathologies.json' /** - * Equine pathology database. - * To be authored post-scaffold (40–60 entries, source-cited). - * Each entry must pass PathologySchema.parse() and have ≥1 source. + * Equine findings tree. Authored as JSON, parsed via Zod at boot so any + * malformed data crashes loudly instead of corrupting an observation later. */ -const EQUINE_PATHOLOGIES: Pathology[] = [] +const EQUINE_FINDINGS: SpeciesFindings = SpeciesFindingsSchema.parse(data) -export default EQUINE_PATHOLOGIES +export default EQUINE_FINDINGS diff --git a/src/domain/species/types.ts b/src/domain/species/types.ts index b8f94ab9..bd606e38 100644 --- a/src/domain/species/types.ts +++ b/src/domain/species/types.ts @@ -1,4 +1,4 @@ -import type { Pathology } from '@/domain/schema/pathology' +import type { SpeciesFindings } from '@/domain/schema/findings' /** Vertebral formula defines segment counts for a species. */ export interface VertebralFormula { @@ -9,8 +9,8 @@ export interface VertebralFormula { Cd: number } -/** Lazy import returning a module with a default export of Pathology[]. */ -export type PathologiesLoader = () => Promise<{ default: Pathology[] }> +/** Lazy import returning a module with a default export of SpeciesFindings. */ +export type FindingsLoader = () => Promise<{ default: SpeciesFindings }> /** Species manifest — one per registered species. */ export interface SpeciesManifest { @@ -25,6 +25,6 @@ export interface SpeciesManifest { */ details: Record vertebralFormula: VertebralFormula - /** Lazy-loaded pathology database for this species */ - pathologies: PathologiesLoader + /** Lazy-loaded findings tree (zones + categories + leaves) for this species */ + findings: FindingsLoader } diff --git a/tests/unit/species.equineFindings.test.ts b/tests/unit/species.equineFindings.test.ts new file mode 100644 index 00000000..d2e34cea --- /dev/null +++ b/tests/unit/species.equineFindings.test.ts @@ -0,0 +1,52 @@ +import { describe, it, expect } from 'vitest' +import EQUINE_FINDINGS from '@/domain/species/equine/pathologies' +import { + SpeciesFindingsSchema, + type FindingNode, +} from '@/domain/schema/findings' + +function walkLeaves(node: FindingNode, fn: (leaf: FindingNode) => void): void { + if (!node.children || node.children.length === 0) { + fn(node) + return + } + for (const child of node.children) walkLeaves(child, fn) +} + +describe('equine findings tree', () => { + it('parses against SpeciesFindingsSchema', () => { + expect(() => SpeciesFindingsSchema.parse(EQUINE_FINDINGS)).not.toThrow() + }) + + it('species id matches the registry id', () => { + expect(EQUINE_FINDINGS.species.id).toBe('equine') + }) + + it('scope_disclaimer is non-empty', () => { + expect(EQUINE_FINDINGS.scope_disclaimer.length).toBeGreaterThan(0) + }) + + it('has at least one zone with at least one category with at least one leaf', () => { + const zoneIds = Object.keys(EQUINE_FINDINGS.zones) + expect(zoneIds.length).toBeGreaterThanOrEqual(1) + const someZone = EQUINE_FINDINGS.zones[zoneIds[0]] + expect(someZone.categories.length).toBeGreaterThanOrEqual(1) + let leafCount = 0 + walkLeaves(someZone.categories[0], () => { + leafCount += 1 + }) + expect(leafCount).toBeGreaterThanOrEqual(1) + }) + + it('every leaf carries both FR and EN labels', () => { + const missing: string[] = [] + for (const zone of Object.values(EQUINE_FINDINGS.zones)) { + for (const category of zone.categories) { + walkLeaves(category, (leaf) => { + if (!leaf.name_fr || !leaf.name_en) missing.push(leaf.id) + }) + } + } + expect(missing).toEqual([]) + }) +}) diff --git a/tests/unit/species.equinePathologies.test.ts b/tests/unit/species.equinePathologies.test.ts deleted file mode 100644 index 6952bb74..00000000 --- a/tests/unit/species.equinePathologies.test.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { describe, it, expect } from 'vitest' -import { PathologySchema } from '@/domain/schema/pathology' -import EQUINE_PATHOLOGIES from '@/domain/species/equine/pathologies' - -describe('equine pathologies database', () => { - it('has between 0 and 100 entries (empty allowed for v1)', () => { - expect(EQUINE_PATHOLOGIES.length).toBeGreaterThanOrEqual(0) - expect(EQUINE_PATHOLOGIES.length).toBeLessThanOrEqual(100) - }) - - it('every entry parses against the schema', () => { - for (const p of EQUINE_PATHOLOGIES) { - const r = PathologySchema.safeParse(p) - if (!r.success) throw new Error(`${p.id}: ${r.error.message}`) - } - }) - - it('every entry has at least one source', () => { - for (const p of EQUINE_PATHOLOGIES) { - expect(p.sources.length).toBeGreaterThanOrEqual(1) - } - }) - - it('every entry is tagged for equine when species-scoped', () => { - for (const p of EQUINE_PATHOLOGIES) { - if (p.speciesIds.length > 0) { - expect(p.speciesIds).toContain('equine') - } - } - }) - - it('every entry has a unique id', () => { - const ids = new Set() - for (const p of EQUINE_PATHOLOGIES) { - expect(ids.has(p.id)).toBe(false) - ids.add(p.id) - } - }) -})