/** Rev 5 Task 12: the citations draft, held as additions and removals. */ import { describe, expect, it } from 'vitest' import type { Reference } from '@faunosteo/core-wasm/types' import { addId, applyEdits, citationCommands, NO_EDITS, putCreated, removeId, sameEdits, } from '@/editor/references/citationEdits' const value = (id: string, citation: string): Reference => ({ id, type: 'book', citation, doi: null, url: null, }) describe('citation edits', () => { it('applies removals and additions to the current list, each id once', () => { const edits = addId(removeId(NO_EDITS, 'a'), 'b') expect(applyEdits(['a', 'c'], edits)).toEqual(['c', 'b']) expect(applyEdits(['a', 'b'], edits)).toEqual(['b']) expect(applyEdits([], edits)).toEqual(['b']) }) it('a removal then a citation of the same id is no change at all', () => { expect(addId(removeId(NO_EDITS, 'a'), 'a')).toEqual(NO_EDITS) expect(removeId(addId(NO_EDITS, 'n'), 'n')).toEqual(NO_EDITS) }) it('a repeat is ignored', () => { const once = addId(NO_EDITS, 'b') expect(addId(once, 'b')).toBe(once) const gone = removeId(NO_EDITS, 'a') expect(removeId(gone, 'a')).toBe(gone) }) it('a created reference joins the draft with its id; a second put replaces it', () => { const once = putCreated(NO_EDITS, value('ref-n', 'Neuf')) expect(once).toEqual({ added: ['ref-n'], removed: [], created: [value('ref-n', 'Neuf')], }) const twice = putCreated(once, value('ref-n', 'Neuf 2')) expect(twice.created).toEqual([value('ref-n', 'Neuf 2')]) expect(twice.added).toEqual(['ref-n']) expect(sameEdits(once, twice)).toBe(false) }) it('removing a created id drops it entirely', () => { const edits = putCreated(addId(NO_EDITS, 'b'), value('ref-n', 'Neuf')) expect(removeId(edits, 'ref-n')).toEqual(addId(NO_EDITS, 'b')) }) it('the save: an insert per created reference under a minted id, then the update', () => { const edits = putCreated(removeId(NO_EDITS, 'a'), value('ref-n', 'Neuf')) const minted = ['ref-m'] const commands = citationCommands( edits, ['a', 'c'], (ids) => ({ op: 'remove', key: { kind: 'reference', id: ids.join() } }), () => minted.shift()!, ) expect(commands).toEqual([ { op: 'insert', element: { kind: 'reference', value: value('ref-m', 'Neuf') }, }, { op: 'remove', key: { kind: 'reference', id: 'c,ref-m' } }, ]) }) })