import { describe, expect, it } from 'vitest'; import { addKeywordPath, collectKeywordPaths, keywordTree, normalizeKeywordPath, pathAncestors, pathLeaf, pathsUnder, removeKeywordPath, } from './keywords'; const empty = { keywords: [] as string[], keywordPaths: [] as string[] }; describe('keyword paths', () => { it('normalises every separator to the Lightroom form', () => { expect(normalizeKeywordPath('Nature>Birds>Robin')).toBe('Nature > Birds > Robin'); expect(normalizeKeywordPath('Nature/Birds / Robin ')).toBe('Nature > Birds > Robin'); expect(normalizeKeywordPath('Nature | Birds')).toBe('Nature > Birds'); expect(normalizeKeywordPath(' ')).toBe(''); }); it('reads the leaf and the ancestor chain', () => { expect(pathLeaf('Nature > Birds > Robin')).toBe('Robin'); expect(pathAncestors('Nature > Birds > Robin')).toEqual([ 'Nature', 'Nature > Birds', 'Nature > Birds > Robin', ]); }); it('records ancestors and puts the leaf in the flat list', () => { const next = addKeywordPath(empty, 'Nature > Birds > Robin'); expect(next.keywordPaths).toEqual([ 'Nature', 'Nature > Birds', 'Nature > Birds > Robin', ]); expect(next.keywords).toEqual(['Robin']); }); it('keeps a plain word flat', () => { const next = addKeywordPath(empty, 'Robin', false); expect(next.keywords).toEqual(['Robin']); expect(next.keywordPaths).toEqual([]); }); it('does not duplicate on case alone', () => { const first = addKeywordPath(empty, 'Nature > Birds'); const second = addKeywordPath(first, 'nature > birds'); expect(second.keywordPaths).toEqual(['Nature', 'Nature > Birds']); expect(second.keywords).toEqual(['Birds']); }); it('removes a subtree and the leaves it stranded', () => { const full = addKeywordPath(addKeywordPath(empty, 'Nature > Birds > Robin'), 'Nature > Birds > Wren'); const next = removeKeywordPath(full, 'Nature > Birds'); expect(next.keywordPaths).toEqual(['Nature']); // Robin and Wren came only from the removed branch, Birds was the node. expect(next.keywords).toEqual([]); }); it('keeps a leaf that another path still uses', () => { const both = addKeywordPath(addKeywordPath(empty, 'Nature > Birds > Robin'), 'Garden > Robin'); const next = removeKeywordPath(both, 'Nature > Birds'); expect(next.keywords).toEqual(['Robin']); expect(next.keywordPaths).toEqual(['Nature', 'Garden', 'Garden > Robin']); }); it('collects suggestions once, sorted', () => { const rows = [ { keywordPaths: ['Nature > Birds', 'Nature'] }, { keywordPaths: ['nature', 'Places > Coast'] }, {}, ]; expect(collectKeywordPaths(rows)).toEqual(['Nature', 'Nature > Birds', 'Places > Coast']); }); it('builds a tree with parents before children', () => { const tree = keywordTree(['Nature > Birds > Robin', 'Nature > Plants', 'Places']); expect(tree.map((node) => node.name)).toEqual(['Nature', 'Places']); const nature = tree[0]; expect(nature.path).toBe('Nature'); expect(nature.children.map((node) => node.name)).toEqual(['Birds', 'Plants']); expect(nature.children[0].children.map((node) => node.name)).toEqual(['Robin']); expect(nature.children[0].children[0].path).toBe('Nature > Birds > Robin'); }); it('expands a node to everything under it', () => { const paths = ['Nature', 'Nature > Birds', 'Nature > Birds > Robin', 'Places > Coast']; expect(pathsUnder(paths, 'Nature > Birds')).toEqual([ 'Nature > Birds', 'Nature > Birds > Robin', ]); expect(pathsUnder(paths, 'Places')).toEqual(['Places > Coast']); // A prefix that is not a path boundary must not match. expect(pathsUnder(paths, 'Nat')).toEqual([]); }); });