|
1 | 1 | import { describe, expect, it } from 'vitest'; |
2 | 2 | import { ref } from 'vue'; |
3 | | -import { |
4 | | - createNestedFolders, |
5 | | - createSpecialCharFolders, |
6 | | - createWorkBookmarks, |
7 | | -} from '../../test-utils/bookmarkFactory'; |
| 3 | +import { createWorkBookmarks } from '../../test-utils/bookmarkFactory'; |
8 | 4 | import { useFolderSearch } from '../useFolderSearch'; |
| 5 | +import type { BookmarkFolder } from '../useFolderTree'; |
9 | 6 |
|
10 | 7 | describe('useFolderSearch', () => { |
11 | | - describe('searchFolders', () => { |
| 8 | + describe('exact mode', () => { |
12 | 9 | it('returns empty results when query is empty', () => { |
13 | | - const allFolders = ref(createWorkBookmarks()); |
14 | | - const { searchQuery, searchResults, searchFolders } = |
15 | | - useFolderSearch(allFolders); |
| 10 | + const { searchQuery, searchResults, searchFolders, isFuzzyEnabled } = |
| 11 | + useFolderSearch(ref(createWorkBookmarks())); |
16 | 12 |
|
| 13 | + isFuzzyEnabled.value = false; |
17 | 14 | searchQuery.value = ''; |
18 | 15 | searchFolders(); |
19 | 16 |
|
20 | 17 | expect(searchResults.value).toEqual([]); |
21 | 18 | }); |
22 | 19 |
|
23 | | - it('finds folders by name (case-insensitive)', () => { |
24 | | - const allFolders = ref(createWorkBookmarks()); |
25 | | - const { searchQuery, searchResults, searchFolders } = |
26 | | - useFolderSearch(allFolders); |
| 20 | + it('finds folders case-insensitively with null indexes', () => { |
| 21 | + const { searchQuery, searchResults, searchFolders, isFuzzyEnabled } = |
| 22 | + useFolderSearch(ref(createWorkBookmarks())); |
27 | 23 |
|
| 24 | + isFuzzyEnabled.value = false; |
28 | 25 | searchQuery.value = 'WORK'; |
29 | 26 | searchFolders(); |
30 | 27 |
|
31 | 28 | expect(searchResults.value).toHaveLength(2); |
32 | | - expect(searchResults.value[0].title).toBe('Work Projects'); |
33 | | - expect(searchResults.value[1].title).toBe('work notes'); |
| 29 | + expect(searchResults.value[0].folder.title).toBe('Work Projects'); |
| 30 | + expect(searchResults.value[0].indexes).toBeNull(); |
34 | 31 | }); |
| 32 | + }); |
35 | 33 |
|
36 | | - it('finds folders with special characters', () => { |
37 | | - const allFolders = ref(createSpecialCharFolders()); |
38 | | - const { searchQuery, searchResults, searchFolders } = |
39 | | - useFolderSearch(allFolders); |
40 | | - |
41 | | - searchQuery.value = '$100'; |
| 34 | + describe('fuzzy mode', () => { |
| 35 | + it('finds folders with fuzzy matching and returns indexes', () => { |
| 36 | + const folders: BookmarkFolder[] = [ |
| 37 | + { id: '1', title: 'kotlin-lang-lambda', path: '' }, |
| 38 | + { id: '2', title: 'javascript-tutorial', path: '' }, |
| 39 | + ]; |
| 40 | + const { searchQuery, searchResults, searchFolders, isFuzzyEnabled } = |
| 41 | + useFolderSearch(ref(folders)); |
| 42 | + |
| 43 | + isFuzzyEnabled.value = true; |
| 44 | + searchQuery.value = 'ktln'; |
42 | 45 | searchFolders(); |
43 | 46 |
|
44 | 47 | expect(searchResults.value).toHaveLength(1); |
45 | | - expect(searchResults.value[0].title).toBe('Price: $100 (USD)'); |
| 48 | + expect(searchResults.value[0].folder.title).toBe('kotlin-lang-lambda'); |
| 49 | + expect(searchResults.value[0].indexes!.length).toBeGreaterThan(0); |
46 | 50 | }); |
47 | 51 |
|
48 | | - it('searches within nested folder structures', () => { |
49 | | - const allFolders = ref(createNestedFolders()); |
50 | | - const { searchQuery, searchResults, searchFolders } = |
51 | | - useFolderSearch(allFolders); |
| 52 | + it('filters out poor matches based on threshold', () => { |
| 53 | + const folders: BookmarkFolder[] = [{ id: '1', title: 'xyz', path: '' }]; |
| 54 | + const { searchQuery, searchResults, searchFolders, isFuzzyEnabled } = |
| 55 | + useFolderSearch(ref(folders)); |
52 | 56 |
|
53 | | - searchQuery.value = 'Fiction'; |
| 57 | + isFuzzyEnabled.value = true; |
| 58 | + searchQuery.value = 'abc'; |
54 | 59 | searchFolders(); |
55 | 60 |
|
56 | | - expect(searchResults.value.length).toBeGreaterThan(0); |
57 | | - expect(searchResults.value.some((r) => r.title === 'Fiction')).toBe(true); |
| 61 | + expect(searchResults.value).toHaveLength(0); |
58 | 62 | }); |
59 | 63 | }); |
60 | 64 |
|
61 | 65 | describe('highlightText', () => { |
62 | | - it('returns unhighlighted text when query is empty', () => { |
63 | | - const allFolders = ref([]); |
64 | | - const { highlightText } = useFolderSearch(allFolders); |
65 | | - |
66 | | - const result = highlightText('Sample Text', ''); |
67 | | - |
68 | | - expect(result).toEqual([{ text: 'Sample Text', highlighted: false }]); |
| 66 | + it('highlights by indexes when provided (fuzzy)', () => { |
| 67 | + const { highlightText } = useFolderSearch(ref([])); |
| 68 | + |
| 69 | + const result = highlightText('Hello', '', [0, 2, 4]); |
| 70 | + |
| 71 | + expect(result).toEqual([ |
| 72 | + { text: 'H', highlighted: true }, |
| 73 | + { text: 'e', highlighted: false }, |
| 74 | + { text: 'l', highlighted: true }, |
| 75 | + { text: 'l', highlighted: false }, |
| 76 | + { text: 'o', highlighted: true }, |
| 77 | + ]); |
69 | 78 | }); |
70 | 79 |
|
71 | | - it('highlights matching text correctly', () => { |
72 | | - const allFolders = ref([]); |
73 | | - const { highlightText } = useFolderSearch(allFolders); |
| 80 | + it('highlights by substring when indexes is null (exact)', () => { |
| 81 | + const { highlightText } = useFolderSearch(ref([])); |
74 | 82 |
|
75 | | - const result = highlightText('Hello World', 'World'); |
| 83 | + const result = highlightText('Hello World', 'World', null); |
76 | 84 |
|
77 | | - expect(result).toHaveLength(2); |
78 | | - expect(result[0]).toEqual({ text: 'Hello ', highlighted: false }); |
79 | | - expect(result[1]).toEqual({ text: 'World', highlighted: true }); |
| 85 | + expect(result).toEqual([ |
| 86 | + { text: 'Hello ', highlighted: false }, |
| 87 | + { text: 'World', highlighted: true }, |
| 88 | + ]); |
80 | 89 | }); |
81 | 90 |
|
82 | | - it('escapes special regex characters', () => { |
83 | | - const allFolders = ref([]); |
84 | | - const { highlightText } = useFolderSearch(allFolders); |
| 91 | + it('groups consecutive highlighted characters', () => { |
| 92 | + const { highlightText } = useFolderSearch(ref([])); |
85 | 93 |
|
86 | | - const result = highlightText('Cost: $100 (USD)', '$100'); |
| 94 | + const result = highlightText('Hello', '', [0, 1, 2]); |
87 | 95 |
|
88 | | - expect(result).toContainEqual({ text: '$100', highlighted: true }); |
| 96 | + expect(result).toEqual([ |
| 97 | + { text: 'Hel', highlighted: true }, |
| 98 | + { text: 'lo', highlighted: false }, |
| 99 | + ]); |
89 | 100 | }); |
90 | 101 |
|
91 | | - it('highlights text case-insensitively', () => { |
92 | | - const allFolders = ref([]); |
93 | | - const { highlightText } = useFolderSearch(allFolders); |
| 102 | + it('preserves original case in substring mode', () => { |
| 103 | + const { highlightText } = useFolderSearch(ref([])); |
94 | 104 |
|
95 | | - const result = highlightText('JavaScript Tutorial', 'script'); |
| 105 | + const result = highlightText('JavaScript', 'script', null); |
96 | 106 |
|
97 | | - expect( |
98 | | - result.some((part) => part.highlighted && part.text === 'Script'), |
99 | | - ).toBe(true); |
| 107 | + expect(result).toContainEqual({ text: 'Script', highlighted: true }); |
100 | 108 | }); |
101 | 109 | }); |
102 | 110 | }); |
0 commit comments