231 lines
7.9 KiB
TypeScript
231 lines
7.9 KiB
TypeScript
import { readFile } from 'node:fs/promises'
|
|
import { join } from 'node:path'
|
|
import { DOMParser, XMLSerializer, type Element } from '@xmldom/xmldom'
|
|
import { beforeAll, describe, expect, it } from 'vitest'
|
|
import {
|
|
composeViewSvg,
|
|
EQUINE_DRAFT_DIR,
|
|
HALO_FILTER_ID,
|
|
highlightLayer,
|
|
renderAnnotationLayer,
|
|
renderAppReviewDrawing,
|
|
type DraftResult,
|
|
type LayerElement,
|
|
} from '../src'
|
|
import { REPO_ROOT, runDraft } from './helpers'
|
|
|
|
const draftDir = join(REPO_ROOT, EQUINE_DRAFT_DIR)
|
|
const HALO_FILTER_MARKUP =
|
|
`<defs><filter id="${HALO_FILTER_ID}" x="-100%" y="-100%" width="300%" ` +
|
|
'height="300%" filterUnits="userSpaceOnUse">' +
|
|
'<feGaussianBlur in="SourceGraphic" stdDeviation="18" result="haloWide"/>' +
|
|
'<feGaussianBlur in="SourceGraphic" stdDeviation="8" result="haloTight"/>' +
|
|
'<feMerge><feMergeNode in="haloWide"/><feMergeNode in="haloWide"/>' +
|
|
'<feMergeNode in="haloWide"/><feMergeNode in="haloTight"/>' +
|
|
'<feMergeNode in="haloTight"/><feMergeNode in="SourceGraphic"/>' +
|
|
'</feMerge></filter></defs>'
|
|
|
|
let draft: DraftResult
|
|
let horseSvg: string
|
|
|
|
/** The committed cleaned drawing, parsed fresh for every test. */
|
|
function parseHorse(): Element {
|
|
const doc = new DOMParser().parseFromString(horseSvg, 'image/svg+xml')
|
|
return doc.documentElement!
|
|
}
|
|
|
|
function serialize(root: Element): string {
|
|
return new XMLSerializer().serializeToString(root.ownerDocument!)
|
|
}
|
|
|
|
function elements(list: { length: number; item(i: number): Element | null }) {
|
|
return Array.from({ length: list.length }, (_, i) => list.item(i)!)
|
|
}
|
|
|
|
/** Element children of the layer `<g>`, the last child of the root. */
|
|
function layerChildren(root: Element): Element[] {
|
|
const layer = root.lastChild as Element
|
|
expect(layer.nodeName).toBe('g')
|
|
return elements(layer.childNodes as never).filter((n) => n.nodeType === 1)
|
|
}
|
|
|
|
function dataAttributeCount(root: Element): number {
|
|
return (serialize(root).match(/ data-[a-z-]+="/g) ?? []).length
|
|
}
|
|
|
|
beforeAll(async () => {
|
|
;({ draft } = await runDraft())
|
|
const [asset] = draft.assets
|
|
horseSvg = await readFile(join(draftDir, 'assets', asset!.file), 'utf8')
|
|
})
|
|
|
|
describe('composeViewSvg with a highlight', () => {
|
|
it('keeps both elements of a selected point and the selected spine zone, haloed on the main view', () => {
|
|
const root = composeViewSvg({
|
|
root: parseHorse(),
|
|
document: draft.document,
|
|
viewId: 'main',
|
|
highlight: { annotationIds: new Set(['hip', 'cervical']), halo: true },
|
|
})
|
|
const kept = layerChildren(root)
|
|
expect(kept.map((e) => e.nodeName)).toEqual(['circle', 'circle', 'polygon'])
|
|
for (const element of kept) {
|
|
expect(element.getAttribute('fill')).toBe('#b85a3f')
|
|
expect(element.getAttribute('fill-opacity')).toBe('0.85')
|
|
expect(element.getAttribute('filter')).toBe(`url(#${HALO_FILTER_ID})`)
|
|
}
|
|
expect(kept[0]!.getAttribute('r')).toBe('34')
|
|
expect(kept[1]!.getAttribute('r')).toBe('22')
|
|
expect(root.getAttribute('viewBox')).toBe('0 0 2073.4 1629.4')
|
|
expect(root.hasAttribute('width')).toBe(false)
|
|
expect(root.hasAttribute('height')).toBe(false)
|
|
})
|
|
|
|
it('creates <defs> as the first child of an asset without one and fills it with the halo filter', () => {
|
|
const root = composeViewSvg({
|
|
root: parseHorse(),
|
|
document: draft.document,
|
|
viewId: 'main',
|
|
highlight: { annotationIds: new Set(['hip']), halo: true },
|
|
})
|
|
expect(root.firstChild!.nodeName).toBe('defs')
|
|
expect(root.getElementsByTagName('defs')).toHaveLength(1)
|
|
expect(serialize(root)).toContain(HALO_FILTER_MARKUP)
|
|
})
|
|
|
|
it('never duplicates the filter on a root that already holds it', () => {
|
|
const root = parseHorse()
|
|
const input = {
|
|
root,
|
|
document: draft.document,
|
|
viewId: 'main',
|
|
highlight: { annotationIds: new Set(['hip']), halo: true },
|
|
}
|
|
composeViewSvg(input)
|
|
composeViewSvg(input)
|
|
const filters = elements(root.getElementsByTagName('filter')).filter(
|
|
(f) => f.getAttribute('id') === HALO_FILTER_ID,
|
|
)
|
|
expect(filters).toHaveLength(1)
|
|
expect(root.getElementsByTagName('defs')).toHaveLength(1)
|
|
})
|
|
|
|
it('frames the head view on its view box without any halo', () => {
|
|
const root = composeViewSvg({
|
|
root: parseHorse(),
|
|
document: draft.document,
|
|
viewId: 'head',
|
|
highlight: { annotationIds: new Set(['parietal']), halo: false },
|
|
})
|
|
expect(root.getAttribute('viewBox')).toBe('60 100 500 380')
|
|
expect(root.getElementsByTagName('defs')).toHaveLength(0)
|
|
expect(serialize(root)).not.toContain(HALO_FILTER_ID)
|
|
const kept = layerChildren(root)
|
|
expect(kept.map((e) => e.nodeName)).toEqual(['circle', 'circle'])
|
|
for (const element of kept) {
|
|
expect(element.getAttribute('fill')).toBe('#b85a3f')
|
|
expect(element.hasAttribute('filter')).toBe(false)
|
|
}
|
|
})
|
|
|
|
it('leaves an empty layer when nothing is selected', () => {
|
|
const root = composeViewSvg({
|
|
root: parseHorse(),
|
|
document: draft.document,
|
|
viewId: 'main',
|
|
highlight: { annotationIds: new Set(), halo: true },
|
|
})
|
|
expect(layerChildren(root)).toHaveLength(0)
|
|
})
|
|
|
|
it('keeps the <title> of every kept marker and emits no data attribute', () => {
|
|
const root = composeViewSvg({
|
|
root: parseHorse(),
|
|
document: draft.document,
|
|
viewId: 'main',
|
|
highlight: { annotationIds: new Set(['hip', 'cervical']), halo: true },
|
|
})
|
|
const titles = elements(root.lastChild!.childNodes as never)
|
|
.filter((n) => n.nodeType === 1)
|
|
.map((e) => e.getElementsByTagName('title').item(0)!.textContent)
|
|
expect(titles).toEqual([
|
|
'Hanche (coxo-fémorale) / Hip joint',
|
|
'Hanche (coxo-fémorale) / Hip joint',
|
|
'Vertèbres cervicales / Cervical vertebrae',
|
|
])
|
|
expect(dataAttributeCount(root)).toBe(0)
|
|
})
|
|
|
|
it('refuses an unknown view', () => {
|
|
expect(() =>
|
|
composeViewSvg({
|
|
root: parseHorse(),
|
|
document: draft.document,
|
|
viewId: 'nope',
|
|
highlight: { annotationIds: new Set(), halo: false },
|
|
}),
|
|
).toThrow('unknown view')
|
|
})
|
|
})
|
|
|
|
describe('highlightLayer on the layer tree', () => {
|
|
function flatten(node: LayerElement): LayerElement[] {
|
|
return [node, ...node.children.flatMap(flatten)]
|
|
}
|
|
|
|
it('filters, restyles and strips data attributes without touching the input', () => {
|
|
const layer = renderAnnotationLayer({
|
|
document: draft.document,
|
|
viewId: 'main',
|
|
})
|
|
const before = JSON.stringify(layer)
|
|
const highlighted = highlightLayer(layer, {
|
|
annotationIds: new Set(['cervical']),
|
|
halo: false,
|
|
})
|
|
expect(JSON.stringify(layer)).toBe(before)
|
|
expect(highlighted.children).toHaveLength(1)
|
|
expect(highlighted.children[0]!.attrs).toMatchObject({
|
|
fill: '#b85a3f',
|
|
'fill-opacity': '0.85',
|
|
'stroke-width': '2',
|
|
})
|
|
expect(highlighted.children[0]!.attrs.filter).toBeUndefined()
|
|
const dataKeys = flatten(highlighted).flatMap((n) =>
|
|
Object.keys(n.attrs).filter((k) => k.startsWith('data-')),
|
|
)
|
|
expect(dataKeys).toEqual([])
|
|
})
|
|
})
|
|
|
|
describe('composeViewSvg without a highlight (review drawings)', () => {
|
|
it.each(['main', 'head'])(
|
|
'renderAppReviewDrawing(%s) equals the committed review drawing',
|
|
async (viewId) => {
|
|
const committed = await readFile(
|
|
join(draftDir, 'review', `app-${viewId}.svg`),
|
|
'utf8',
|
|
)
|
|
const rendered = renderAppReviewDrawing({
|
|
cleanedSvg: horseSvg,
|
|
document: draft.document,
|
|
viewId,
|
|
})
|
|
expect(rendered).toBe(committed)
|
|
},
|
|
)
|
|
|
|
it('keeps the full interactive layer with its data attributes', () => {
|
|
const root = composeViewSvg({
|
|
root: parseHorse(),
|
|
document: draft.document,
|
|
viewId: 'main',
|
|
})
|
|
expect(root.getElementsByTagName('defs')).toHaveLength(0)
|
|
expect((root.lastChild as Element).getAttribute('data-layer')).toBe(
|
|
'annotations',
|
|
)
|
|
expect(dataAttributeCount(root)).toBeGreaterThan(0)
|
|
})
|
|
})
|