feat(anatomy): parse SVG convention into Bone map with validation
This commit is contained in:
1 parent
3dc7153361
commit
ac50ac5c2c
2 files changed
+156
-29
No files matched your search
@@ -0,0 +1,85 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import {
|
||||
parseSvgBones,
|
||||
getBoneIdFromEvent,
|
||||
isValidCategory,
|
||||
} from '@/anatomy/svgConvention'
|
||||
import { SvgConventionError } from '@/anatomy/types'
|
||||
|
||||
function svgFromString(s: string): SVGSVGElement {
|
||||
const doc = new DOMParser().parseFromString(s, 'image/svg+xml')
|
||||
return doc.documentElement as unknown as SVGSVGElement
|
||||
}
|
||||
|
||||
const FIXTURE = `
|
||||
<svg xmlns="http://www.w3.org/2000/svg">
|
||||
<circle data-bone-id="epaule" data-bone-fr="Épaule" data-bone-en="Shoulder"
|
||||
data-category="articulation" class="bone osteo articulation hitbox bone-epaule" />
|
||||
<circle data-bone-id="epaule" data-bone-fr="Épaule" data-bone-en="Shoulder"
|
||||
data-category="articulation" class="bone osteo articulation visible bone-epaule" />
|
||||
<polygon data-bone-id="cervical-vertebrae-spine-zone" data-bone-fr="Vertèbres cervicales"
|
||||
data-bone-en="Cervical Vertebrae" data-category="spine-zone"
|
||||
class="bone osteo spine-zone bone-cervical-vertebrae" />
|
||||
<polygon data-bone-id="zone-head" data-bone-fr="Crane" data-bone-en="Head"
|
||||
data-category="head" data-opens-detail="head" />
|
||||
</svg>`
|
||||
|
||||
describe('parseSvgBones', () => {
|
||||
it('groups hitbox + visible by data-bone-id', () => {
|
||||
const map = parseSvgBones(svgFromString(FIXTURE))
|
||||
const epaule = map.get('epaule')
|
||||
expect(epaule).toBeDefined()
|
||||
expect(epaule?.fr).toBe('Épaule')
|
||||
expect(epaule?.category).toBe('articulation')
|
||||
expect(epaule?.hitboxEls.length).toBe(1)
|
||||
expect(epaule?.visibleEls.length).toBe(1)
|
||||
})
|
||||
|
||||
it('captures opensDetail when present', () => {
|
||||
const map = parseSvgBones(svgFromString(FIXTURE))
|
||||
expect(map.get('zone-head')?.opensDetail).toBe('head')
|
||||
})
|
||||
|
||||
it('records spine-zone single-element bones', () => {
|
||||
const map = parseSvgBones(svgFromString(FIXTURE))
|
||||
const bone = map.get('cervical-vertebrae-spine-zone')
|
||||
expect(bone?.category).toBe('spine-zone')
|
||||
})
|
||||
})
|
||||
|
||||
describe('parseSvgBones validation', () => {
|
||||
it('throws on missing data-bone-fr', () => {
|
||||
const bad = `<svg xmlns="http://www.w3.org/2000/svg">
|
||||
<circle data-bone-id="x" data-bone-en="X" data-category="os" />
|
||||
</svg>`
|
||||
expect(() => parseSvgBones(svgFromString(bad))).toThrow(SvgConventionError)
|
||||
})
|
||||
|
||||
it('throws on unknown category', () => {
|
||||
const bad = `<svg xmlns="http://www.w3.org/2000/svg">
|
||||
<circle data-bone-id="x" data-bone-fr="X" data-bone-en="X" data-category="bogus" />
|
||||
</svg>`
|
||||
expect(() => parseSvgBones(svgFromString(bad))).toThrow(SvgConventionError)
|
||||
})
|
||||
})
|
||||
|
||||
describe('isValidCategory', () => {
|
||||
it('accepts documented categories', () => {
|
||||
expect(isValidCategory('articulation')).toBe(true)
|
||||
expect(isValidCategory('spine-zone')).toBe(true)
|
||||
expect(isValidCategory('cadran-crane')).toBe(true)
|
||||
})
|
||||
|
||||
it('rejects unknown values', () => {
|
||||
expect(isValidCategory('nope')).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('getBoneIdFromEvent', () => {
|
||||
it('returns null when no bone in ancestry', () => {
|
||||
const div = document.createElement('div')
|
||||
const evt = new Event('click')
|
||||
Object.defineProperty(evt, 'target', { value: div })
|
||||
expect(getBoneIdFromEvent(evt)).toBeNull()
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user