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

155 lines
4.7 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import {
normalizeLabel,
levenshtein,
scorePair,
matchZoneFindings,
} from '@/lib/zoneMatch'
import type { ZoneFindings } from '@/domain/schema/findings'
const ZONES: ZoneFindings[] = [
{
id: 'sphenoid',
name_fr: 'Sphénoïde',
name_en: 'Sphenoid',
categories: [],
},
{
id: 'ssb',
name_fr: 'Synchondrose Sphéno-Basilaire (SSB)',
name_en: 'Spheno-Basilar Synchondrosis (SBS)',
categories: [],
},
{
id: 'atm',
name_fr: 'Articulation Temporo-Mandibulaire (ATM)',
name_en: 'Temporomandibular Joint (TMJ)',
categories: [],
},
]
describe('normalizeLabel', () => {
it('lowercases, strips accents and non-alphanumerics', () => {
expect(normalizeLabel('Sphénoïde')).toBe('sphenoide')
})
it('strips parens and whitespace', () => {
expect(normalizeLabel('Articulation Temporo-Mandibulaire (ATM)')).toBe(
'articulationtemporomandibulaireatm',
)
})
it('keeps short bare labels intact', () => {
expect(normalizeLabel('(ATM)')).toBe('atm')
})
it('returns empty string for nullish inputs', () => {
expect(normalizeLabel(undefined)).toBe('')
expect(normalizeLabel(null)).toBe('')
expect(normalizeLabel('')).toBe('')
})
})
describe('levenshtein', () => {
it('returns 0 for identical strings', () => {
expect(levenshtein('abc', 'abc')).toBe(0)
})
it('handles classic textbook case', () => {
expect(levenshtein('kitten', 'sitting')).toBe(3)
})
it('returns length when one string is empty', () => {
expect(levenshtein('', 'abc')).toBe(3)
expect(levenshtein('abc', '')).toBe(3)
})
it('costs 1 per single-char typo', () => {
expect(levenshtein('sphenoide', 'sphenoid')).toBe(1)
expect(levenshtein('shenoide', 'sphenoide')).toBe(1)
})
})
describe('scorePair', () => {
it('scores exact normalized matches at 0', () => {
expect(scorePair('Sphenoid', 'Sphénoïde')).toBeGreaterThan(-1)
expect(scorePair('Sphenoid', 'sphenoid')).toBe(0)
})
it('scores substring matches at 1', () => {
expect(scorePair('(ATM)', 'Articulation Temporo-Mandibulaire (ATM)')).toBe(1)
})
it('routes a substring hit ("sphenoide" contains "sphenoid") to score 1', () => {
expect(scorePair('sphenoide', 'sphenoid')).toBe(1)
})
it('routes a pure-edit typo to Levenshtein scoring (2+d)', () => {
expect(scorePair('shenoide', 'sphenoid')).toBeGreaterThanOrEqual(2)
expect(scorePair('shenoide', 'sphenoid')).toBeLessThanOrEqual(5)
})
it('returns infinity for unrelated strings', () => {
expect(scorePair('Sphenoid', 'Synchondrose Sphéno-Basilaire (SSB)')).toBe(
Number.POSITIVE_INFINITY,
)
})
it('rejects empty inputs', () => {
expect(scorePair('', 'Sphenoid')).toBe(Number.POSITIVE_INFINITY)
expect(scorePair('Sphenoid', '')).toBe(Number.POSITIVE_INFINITY)
})
})
describe('matchZoneFindings', () => {
it('matches the SVG Sphenoid bone to the sphenoid zone via EN exact', () => {
const z = matchZoneFindings({ fr: 'Sphénoïde', en: 'Sphenoid' }, ZONES)
expect(z?.id).toBe('sphenoid')
})
it('matches the SVG SSB bone to the ssb zone via EN exact', () => {
const z = matchZoneFindings(
{ fr: 'Synchondrose Sphéno-Basilaire (SSB)', en: 'Spheno-Basilar Synchondrosis (SBS)' },
ZONES,
)
expect(z?.id).toBe('ssb')
})
it('matches the SVG ATM bone to the atm zone', () => {
const z = matchZoneFindings(
{ fr: 'Articulation Temporo-Mandibulaire (ATM)', en: 'Temporomandibular Joint (TMJ)' },
ZONES,
)
expect(z?.id).toBe('atm')
})
it('matches via FR-only labels when EN is empty', () => {
const z = matchZoneFindings({ fr: 'Sphénoïde', en: '' }, ZONES)
expect(z?.id).toBe('sphenoid')
})
it('matches abbreviation typed alone via substring', () => {
const z = matchZoneFindings({ fr: '(ATM)', en: '' }, ZONES)
expect(z?.id).toBe('atm')
})
it('tolerates small typos via Levenshtein', () => {
expect(matchZoneFindings({ fr: '', en: 'sphenoide' }, ZONES)?.id).toBe('sphenoid')
expect(matchZoneFindings({ fr: '', en: 'shenoide' }, ZONES)?.id).toBe('sphenoid')
expect(matchZoneFindings({ fr: '', en: 'sphenoi' }, ZONES)?.id).toBe('sphenoid')
})
it('returns undefined for clearly unrelated zones', () => {
expect(matchZoneFindings({ fr: 'Hanche (coxo-fémorale)', en: 'Hip joint' }, ZONES)).toBeUndefined()
expect(matchZoneFindings({ fr: 'hyoïde', en: 'Hyoid' }, ZONES)).toBeUndefined()
})
it('does not collapse Sphenoid into the SSB zone', () => {
expect(matchZoneFindings({ fr: 'Sphénoïde', en: 'Sphenoid' }, ZONES)?.id).toBe('sphenoid')
})
it('returns undefined when the catalog is empty', () => {
expect(matchZoneFindings({ fr: 'Sphénoïde', en: 'Sphenoid' }, [])).toBeUndefined()
})
})