import { describe, expect, it } from 'vitest' import type { AnatomicalSheetDocument } from '@faunosteo/core-wasm/types' import { citationCounts } 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('citationCounts', () => { it('counts the citers of a category and of nested nodes, zero when uncited', () => { const counts = citationCounts(citing()) expect(counts.get('ref-a')).toBe(2) expect(counts.get('ref-b')).toBe(1) expect(counts.get('ref-none') ?? 0).toBe(0) }) it('is memoised on the parsed document', () => { const doc = citing() expect(citationCounts(doc)).toBe(citationCounts(doc)) }) it('is empty without findings', () => { expect(citationCounts(JSON.parse(baseText())).size).toBe(0) }) })