import { describe, expect, it } from 'vitest' import type { AnatomicalSheetDocument } from '@faunosteo/core-wasm/types' import { citersOf } from '@/editor/references/citations' import { edit, insert } from '../helpers/editorFixtures' import { baseText } from '../helpers/editorHarness' const name = (n: string) => ({ fr: n, en: n }) const reference = (id: string) => ({ kind: 'reference', value: { id, type: 'book', citation: id, url: null, doi: null }, }) as const function citing(): AnatomicalSheetDocument { let text = insert(baseText(), reference('ref-a')) text = insert(text, reference('ref-b')) text = insert(text, reference('ref-none')) text = insert(text, { kind: 'zone', value: { id: 'zone-a', name: name('Zone'), categories: [] }, }) text = edit(text, { op: 'insert', parent: { kind: 'zone', id: 'zone-a' }, element: { kind: 'finding_category', value: { id: 'fcat-a', kind: 'dysfunction', name: name('Cat'), description: {}, reference_ids: ['ref-a', 'ref-b'], review_status: 'to_confirm', children: [], }, }, }) text = edit(text, { op: 'insert', parent: { kind: 'finding', zone_id: 'zone-a', path: ['fcat-a'] }, element: { kind: 'finding_node', value: { id: 'fnode-a', name: name('Node'), description: {}, requires_vet_referral: false, reference_ids: ['ref-a'], review_status: 'to_confirm', children: [], }, }, }) return JSON.parse(text) } describe('citersOf', () => { it('lists the citers of a category and of nested nodes, in document order', () => { const citers = citersOf(citing()) const names = (id: string) => (citers.get(id) ?? []).map((c) => c.name.fr) expect(names('ref-a')).toEqual(['Cat', 'Node']) expect(names('ref-b')).toEqual(['Cat']) expect(citers.get('ref-none')).toBeUndefined() }) it('marks families and gives the trail from the zone', () => { const [family, node] = citersOf(citing()).get('ref-a')! expect(family.isFamily).toBe(true) expect(family.trail.map((t) => t.fr)).toEqual(['Zone']) expect(node.isFamily).toBe(false) expect(node.trail.map((t) => t.fr)).toEqual(['Zone', 'Cat']) expect(node.key).toEqual({ kind: 'finding', zone_id: 'zone-a', path: ['fcat-a', 'fnode-a'], }) }) it('is memoised on the parsed document', () => { const doc = citing() expect(citersOf(doc)).toBe(citersOf(doc)) }) it('is empty without findings', () => { expect(citersOf(JSON.parse(baseText())).size).toBe(0) }) })