Files
faunosteo/apps/app/tests/unit/char.svgCatalogLink.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

223 lines
9.2 KiB
TypeScript

/**
* Characterisation (Step 1): the real SVG assets, their attributes, the
* SVG ↔ catalog link computed by zoneMatch, the opens-detail ↔ manifest
* contract and the spine helpers on the real Horse.svg.
* Oracle: docs/faunosteo-audit/06-svg-findings-link.md §4 (full table).
*/
import { describe, it, expect } from 'vitest'
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { dirname, resolve } from 'node:path'
import { parseSvgBones } from '@/anatomy/svgConvention'
import { matchZoneFindings, scorePair, levenshtein, normalizeLabel } from '@/lib/zoneMatch'
import {
regionFromLabel,
findSpineZoneId,
collectSpineVertebrae,
syncSpineZones,
} from '@/lib/spineZones'
import { getSpecies } from '@/domain/species/registry'
import FINDINGS from '@/domain/species/equine/pathologies'
import type { Bone } from '@/anatomy/types'
const HERE = dirname(fileURLToPath(import.meta.url))
const HORSE_TEXT = readFileSync(resolve(HERE, '../../public/species/equine/Horse.svg'), 'utf8')
const HEAD_TEXT = readFileSync(resolve(HERE, '../../public/species/equine/Horse_head.svg'), 'utf8')
function parse(text: string): Map<string, Bone> {
const doc = new DOMParser().parseFromString(text, 'image/svg+xml')
return parseSvgBones(doc.documentElement as unknown as SVGSVGElement)
}
const horse = parse(HORSE_TEXT)
const head = parse(HEAD_TEXT)
const zones = Object.values(FINDINGS.zones)
/** Expected mapping, audit 06 §4. Trigger zone-mpby55kd is intentionally absent. */
const EXPECTED: Record<string, string> = {
'point-mpbwrkp0': 'hind-fetlock',
'point-mpbwsi9q': 'hock',
'point-mpbwt4fa': 'stifle',
'point-mpbwtst0': 'hip',
'point-mpbwugty': 'sacroiliac',
'point-mpbwv4xy': 'shoulder',
'point-mpbwvpvt': 'elbow',
'point-mpbwwaru': 'fore-fetlock',
'point-mpbwwxvn': 'fore-sesamoids',
'point-mpbwxnb4': 'l6-s1',
'point-mpbwyd9i': 't18-l1',
'point-mpbwyyc9': 'c7-t1',
'zone-mpbx00jq': 'caudal',
'zone-mpbx0xnp': 'sacral',
'zone-mpbx1rm9': 'lumbar',
'zone-mpbx2rpe': 'thoracic',
'zone-mpbx5hx2': 'cervical',
'point-mpbykomv': 'parietal',
'point-mpbylh35': 'frontal',
'point-mpbymcln': 'sphenoid',
'point-mpbyn2k3': 'temporal',
'point-mpbynls5': 'occipital',
'point-mpbyo7zq': 'atm',
'point-mpbyphay': 'ssb',
'point-mpbyq841': 'hyoid',
'point-mpbyqxbg': 'c0-c1',
}
describe('SVG attribute census (raw text, audit 03 §10.H)', () => {
const count = (attr: string) =>
(HORSE_TEXT.match(new RegExp(`${attr}="`, 'g')) ?? []).length +
(HEAD_TEXT.match(new RegExp(`${attr}="`, 'g')) ?? []).length
it('counts data-anno-* attributes across both files', () => {
expect(count('data-anno-id')).toBe(48)
expect(count('data-anno-category')).toBe(48)
expect(count('data-anno-category-name')).toBe(18)
expect(count('data-anno-name-fr')).toBe(47)
expect(count('data-anno-name-en')).toBe(47)
expect(count('data-anno-lateral')).toBe(18)
expect(count('data-anno-special')).toBe(5)
expect(count('data-anno-opens-detail')).toBe(1)
})
it('uses only five category slugs, two of them with degraded accents', () => {
const cats = new Set([...horse.values(), ...head.values()].map((b) => b.category))
expect([...cats].sort()).toEqual([
'articulations', 'cadrans-du-cr-ne', 'charni-res-vert-brales', 'os', 'zone-colonne',
])
})
it('lateral is always "bilateral", special always "vertebral-precision"', () => {
const all = [...horse.values(), ...head.values()]
expect(all.filter((b) => b.lateral).every((b) => b.lateral === 'bilateral')).toBe(true)
expect(all.filter((b) => b.lateral)).toHaveLength(9)
expect(all.filter((b) => b.special).every((b) => b.special === 'vertebral-precision')).toBe(true)
expect(all.filter((b) => b.special)).toHaveLength(5)
})
it('main and detail id spaces do not overlap (27 distinct ids)', () => {
const ids = new Set([...horse.keys(), ...head.keys()])
expect(ids.size).toBe(horse.size + head.size)
expect(ids.size).toBe(27)
})
it('Horse_head.svg is the same drawing as Horse.svg with a different viewBox', () => {
expect(HORSE_TEXT).toMatch(/viewBox="0 0 2073\.4 1629\.4"/)
expect(HEAD_TEXT).toMatch(/viewBox="60 100 500 380"/)
const body = (t: string) => t.split('\n').slice(2, 703).join('\n')
expect(body(HEAD_TEXT)).toBe(body(HORSE_TEXT))
})
})
describe('SVG ↔ catalog link (audit 06 §4)', () => {
it('maps the 26 named annotations to their catalog zone, exact EN and FR match', () => {
const all = new Map([...horse, ...head])
for (const [id, zoneId] of Object.entries(EXPECTED)) {
const bone = all.get(id)
expect(bone, id).toBeDefined()
const tree = matchZoneFindings({ fr: bone!.fr, en: bone!.en }, zones)
expect(tree?.id, id).toBe(zoneId)
expect(scorePair(bone!.en, tree!.name_en), `${id} EN`).toBe(0)
expect(scorePair(bone!.fr, tree!.name_fr), `${id} FR`).toBe(0)
}
})
it('leaves the detail trigger unmatched (no FR/EN name)', () => {
const trigger = horse.get('zone-mpby55kd')!
expect(trigger.fr).toBe('')
expect(trigger.en).toBe('')
expect(trigger.opensDetail).toBe('head')
expect(trigger.category).toBe('articulations')
expect(matchZoneFindings({ fr: trigger.fr, en: trigger.en }, zones)).toBeUndefined()
})
it('leaves no catalog zone orphaned', () => {
const matched = new Set(Object.values(EXPECTED))
expect(matched.size).toBe(26)
expect(zones.map((z) => z.id).filter((id) => !matched.has(id))).toEqual([])
})
it('no normalized EN or FR zone name is a substring of another (substring rule unreachable today)', () => {
for (const lang of ['name_en', 'name_fr'] as const) {
const norms = zones.map((z) => normalizeLabel(z[lang]))
for (let i = 0; i < norms.length; i += 1)
for (let j = 0; j < norms.length; j += 1)
if (i !== j) expect(norms[i].includes(norms[j]), `${lang} ${zones[i].id} ⊃ ${zones[j].id}`).toBe(false)
}
})
it('KNOWN: two FR pairs are within the fuzzy tolerance and would collide if EN ever diverged', () => {
const d = (a: string, b: string) => levenshtein(normalizeLabel(a), normalizeLabel(b))
const fetlock = d('Boulet ant. (métacarpo-phalangien)', 'Boulet post. (métatarso-phalangien)')
const spine = d('Vertèbres sacrales', 'Vertèbres caudales')
expect(fetlock).toBe(5)
expect(spine).toBe(3)
expect(scorePair('Boulet ant. (métacarpo-phalangien)', 'Boulet post. (métatarso-phalangien)')).toBe(2 + 5)
expect(scorePair('Vertèbres sacrales', 'Vertèbres caudales')).toBe(2 + 3)
})
})
describe('opens-detail ↔ species manifest', () => {
const species = getSpecies('equine')!
it('every opens-detail value is a key of species.details, and every key has one trigger', () => {
const triggers = [...horse.values()].filter((b) => b.opensDetail).map((b) => b.opensDetail!)
expect(triggers).toEqual(['head'])
for (const key of triggers) expect(species.details[key]).toBeDefined()
expect(Object.keys(species.details)).toEqual(['head'])
})
it('the detail SVG carries no trigger of its own (one level of detail)', () => {
expect([...head.values()].some((b) => b.opensDetail)).toBe(false)
})
it('manifest paths point at the two committed files', () => {
expect(species.mainSvg).toBe('/species/equine/Horse.svg')
expect(species.details.head).toBe('/species/equine/Horse_head.svg')
})
})
describe('spine helpers on the real Horse.svg', () => {
it('resolves each region to its zone-colonne polygon by label', () => {
expect(findSpineZoneId('C', horse)).toBe('zone-mpbx5hx2')
expect(findSpineZoneId('T', horse)).toBe('zone-mpbx2rpe')
expect(findSpineZoneId('L', horse)).toBe('zone-mpbx1rm9')
expect(findSpineZoneId('S', horse)).toBe('zone-mpbx0xnp')
expect(findSpineZoneId('Cd', horse)).toBe('zone-mpbx00jq')
})
it('KNOWN: hinge labels also resolve to a region, but hinges have no special so it is never used', () => {
expect(regionFromLabel(horse.get('point-mpbwyyc9')!.fr)).toBe('C')
expect(horse.get('point-mpbwyyc9')!.special).toBeUndefined()
})
it('spawns a lumbar zone from scratch and keeps a joint zone untouched', () => {
const next = syncSpineZones([{ boneId: 'point-mpbwtst0', side: 'left' }], [{ region: 'L', index: 2 }], horse)
expect(next).toEqual([
{ boneId: 'point-mpbwtst0', side: 'left' },
{ boneId: 'zone-mpbx1rm9', vertebrae: [{ region: 'L', index: 2 }] },
])
})
it('collects vertebrae across spine zones with the clicked zone first', () => {
const zonesSel = [
{ boneId: 'zone-mpbx5hx2', vertebrae: [{ region: 'C' as const, index: 3 }] },
{ boneId: 'zone-mpbx1rm9', vertebrae: [{ region: 'L' as const, index: 1 }] },
]
expect(collectSpineVertebrae(zonesSel, horse, 'zone-mpbx1rm9')).toEqual([
{ region: 'L', index: 1 },
{ region: 'C', index: 3 },
])
})
it('detail-sourced zones never count as spine zones', () => {
const next = syncSpineZones(
[{ boneId: 'zone-mpbx5hx2', detailSourceId: 'head' }],
[{ region: 'C', index: 1 }],
horse,
)
// passes through untouched, then a fresh cervical zone is spawned
expect(next[0]).toEqual({ boneId: 'zone-mpbx5hx2', detailSourceId: 'head' })
expect(next[1]).toEqual({ boneId: 'zone-mpbx5hx2', vertebrae: [{ region: 'C', index: 1 }] })
})
})