|
| 1 | +import { FileSuggestHelper, FileFilterConfig } from '../../../src/suggest/FileSuggestHelper'; |
| 2 | +import { TFile } from 'obsidian'; |
| 3 | +import type TaskNotesPlugin from '../../../src/main'; |
| 4 | + |
| 5 | +// Mock parseFrontMatterAliases |
| 6 | +jest.mock('obsidian', () => ({ |
| 7 | + ...jest.requireActual('obsidian'), |
| 8 | + parseFrontMatterAliases: jest.fn((frontmatter: any) => { |
| 9 | + if (!frontmatter || !frontmatter.aliases) return []; |
| 10 | + if (Array.isArray(frontmatter.aliases)) return frontmatter.aliases; |
| 11 | + return [frontmatter.aliases]; |
| 12 | + }), |
| 13 | +})); |
| 14 | + |
| 15 | +describe('FileSuggestHelper', () => { |
| 16 | + let mockPlugin: any; |
| 17 | + let mockFiles: TFile[]; |
| 18 | + let projectFilterConfig: FileFilterConfig; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + // Create mock files |
| 22 | + mockFiles = [ |
| 23 | + { |
| 24 | + basename: 'Project A', |
| 25 | + path: 'projects/Project A.md', |
| 26 | + extension: 'md', |
| 27 | + parent: { path: 'projects' } |
| 28 | + } as TFile, |
| 29 | + { |
| 30 | + basename: 'Project B', |
| 31 | + path: 'projects/Project B.md', |
| 32 | + extension: 'md', |
| 33 | + parent: { path: 'projects' } |
| 34 | + } as TFile, |
| 35 | + { |
| 36 | + basename: 'Note 1', |
| 37 | + path: 'notes/Note 1.md', |
| 38 | + extension: 'md', |
| 39 | + parent: { path: 'notes' } |
| 40 | + } as TFile, |
| 41 | + { |
| 42 | + basename: 'Note 2', |
| 43 | + path: 'notes/Note 2.md', |
| 44 | + extension: 'md', |
| 45 | + parent: { path: 'notes' } |
| 46 | + } as TFile, |
| 47 | + ]; |
| 48 | + |
| 49 | + // Create project filter configuration |
| 50 | + projectFilterConfig = { |
| 51 | + requiredTags: ['project'], |
| 52 | + includeFolders: [], |
| 53 | + propertyKey: '', |
| 54 | + propertyValue: '' |
| 55 | + }; |
| 56 | + |
| 57 | + // Create mock plugin with settings |
| 58 | + mockPlugin = { |
| 59 | + app: { |
| 60 | + vault: { |
| 61 | + getMarkdownFiles: jest.fn(() => mockFiles), |
| 62 | + }, |
| 63 | + metadataCache: { |
| 64 | + getFileCache: jest.fn((file: TFile) => { |
| 65 | + // Project files have #project tag |
| 66 | + if (file.path.startsWith('projects/')) { |
| 67 | + return { |
| 68 | + frontmatter: { |
| 69 | + tags: ['project'], |
| 70 | + type: 'project' |
| 71 | + }, |
| 72 | + tags: [{ tag: '#project', position: { start: { line: 0, col: 0, offset: 0 }, end: { line: 0, col: 8, offset: 8 } } }] |
| 73 | + }; |
| 74 | + } |
| 75 | + // Note files don't have #project tag |
| 76 | + return { |
| 77 | + frontmatter: {}, |
| 78 | + tags: [] |
| 79 | + }; |
| 80 | + }), |
| 81 | + }, |
| 82 | + }, |
| 83 | + settings: { |
| 84 | + suggestionDebounceMs: 0 |
| 85 | + }, |
| 86 | + fieldMapper: { |
| 87 | + mapFromFrontmatter: jest.fn((fm: any) => ({ |
| 88 | + title: fm.title || '' |
| 89 | + })) |
| 90 | + } |
| 91 | + } as unknown as TaskNotesPlugin; |
| 92 | + }); |
| 93 | + |
| 94 | + describe('Filter Configuration', () => { |
| 95 | + it('should return ALL files when no filterConfig is provided', async () => { |
| 96 | + const results = await FileSuggestHelper.suggest(mockPlugin, ''); |
| 97 | + |
| 98 | + // Should return ALL files (4 total) - no filtering |
| 99 | + expect(results.length).toBe(4); |
| 100 | + const basenames = results.map(r => r.insertText); |
| 101 | + expect(basenames).toContain('Project A'); |
| 102 | + expect(basenames).toContain('Project B'); |
| 103 | + expect(basenames).toContain('Note 1'); |
| 104 | + expect(basenames).toContain('Note 2'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should apply filters when filterConfig is provided', async () => { |
| 108 | + const results = await FileSuggestHelper.suggest( |
| 109 | + mockPlugin, |
| 110 | + 'Project', |
| 111 | + 20, |
| 112 | + projectFilterConfig |
| 113 | + ); |
| 114 | + |
| 115 | + // Should only return files with #project tag |
| 116 | + expect(results.length).toBe(2); |
| 117 | + expect(results.every(r => r.insertText.startsWith('Project'))).toBe(true); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should return ALL files when filterConfig is undefined', async () => { |
| 121 | + const results = await FileSuggestHelper.suggest( |
| 122 | + mockPlugin, |
| 123 | + '', |
| 124 | + 20, |
| 125 | + undefined |
| 126 | + ); |
| 127 | + |
| 128 | + // Should return ALL files (4 total) |
| 129 | + expect(results.length).toBe(4); |
| 130 | + const basenames = results.map(r => r.insertText); |
| 131 | + expect(basenames).toContain('Project A'); |
| 132 | + expect(basenames).toContain('Project B'); |
| 133 | + expect(basenames).toContain('Note 1'); |
| 134 | + expect(basenames).toContain('Note 2'); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe('Tag Filtering', () => { |
| 139 | + it('should filter by required tags when configured', async () => { |
| 140 | + const filterConfig: FileFilterConfig = { |
| 141 | + requiredTags: ['project'] |
| 142 | + }; |
| 143 | + |
| 144 | + const results = await FileSuggestHelper.suggest( |
| 145 | + mockPlugin, |
| 146 | + '', |
| 147 | + 20, |
| 148 | + filterConfig |
| 149 | + ); |
| 150 | + |
| 151 | + // Only files with #project tag |
| 152 | + expect(results.length).toBe(2); |
| 153 | + expect(results.every(r => r.insertText.startsWith('Project'))).toBe(true); |
| 154 | + }); |
| 155 | + |
| 156 | + it('should NOT filter by tags when no filterConfig provided', async () => { |
| 157 | + const results = await FileSuggestHelper.suggest( |
| 158 | + mockPlugin, |
| 159 | + '', |
| 160 | + 20 |
| 161 | + ); |
| 162 | + |
| 163 | + // All files should be returned |
| 164 | + expect(results.length).toBe(4); |
| 165 | + }); |
| 166 | + }); |
| 167 | + |
| 168 | + describe('Folder Filtering', () => { |
| 169 | + it('should filter by included folders when configured', async () => { |
| 170 | + const filterConfig: FileFilterConfig = { |
| 171 | + includeFolders: ['projects'] |
| 172 | + }; |
| 173 | + |
| 174 | + const results = await FileSuggestHelper.suggest( |
| 175 | + mockPlugin, |
| 176 | + '', |
| 177 | + 20, |
| 178 | + filterConfig |
| 179 | + ); |
| 180 | + |
| 181 | + // Only files in projects/ folder |
| 182 | + expect(results.length).toBe(2); |
| 183 | + expect(results.every(r => r.insertText.startsWith('Project'))).toBe(true); |
| 184 | + }); |
| 185 | + |
| 186 | + it('should NOT filter by folders when no filterConfig provided', async () => { |
| 187 | + const results = await FileSuggestHelper.suggest( |
| 188 | + mockPlugin, |
| 189 | + '', |
| 190 | + 20 |
| 191 | + ); |
| 192 | + |
| 193 | + // All files should be returned |
| 194 | + expect(results.length).toBe(4); |
| 195 | + }); |
| 196 | + }); |
| 197 | + |
| 198 | + describe('Property Filtering', () => { |
| 199 | + it('should filter by property when configured', async () => { |
| 200 | + const filterConfig: FileFilterConfig = { |
| 201 | + propertyKey: 'type', |
| 202 | + propertyValue: 'project' |
| 203 | + }; |
| 204 | + |
| 205 | + const results = await FileSuggestHelper.suggest( |
| 206 | + mockPlugin, |
| 207 | + '', |
| 208 | + 20, |
| 209 | + filterConfig |
| 210 | + ); |
| 211 | + |
| 212 | + // Only files with type: project |
| 213 | + expect(results.length).toBe(2); |
| 214 | + expect(results.every(r => r.insertText.startsWith('Project'))).toBe(true); |
| 215 | + }); |
| 216 | + |
| 217 | + it('should NOT filter by property when no filterConfig provided', async () => { |
| 218 | + const results = await FileSuggestHelper.suggest( |
| 219 | + mockPlugin, |
| 220 | + '', |
| 221 | + 20 |
| 222 | + ); |
| 223 | + |
| 224 | + // All files should be returned |
| 225 | + expect(results.length).toBe(4); |
| 226 | + }); |
| 227 | + }); |
| 228 | + |
| 229 | + describe('Multiple Filters Combined', () => { |
| 230 | + it('should apply all filters when configured', async () => { |
| 231 | + const filterConfig: FileFilterConfig = { |
| 232 | + requiredTags: ['project'], |
| 233 | + includeFolders: ['projects'], |
| 234 | + propertyKey: 'type', |
| 235 | + propertyValue: 'project' |
| 236 | + }; |
| 237 | + |
| 238 | + const results = await FileSuggestHelper.suggest( |
| 239 | + mockPlugin, |
| 240 | + '', |
| 241 | + 20, |
| 242 | + filterConfig |
| 243 | + ); |
| 244 | + |
| 245 | + // Only files matching ALL criteria |
| 246 | + expect(results.length).toBe(2); |
| 247 | + expect(results.every(r => r.insertText.startsWith('Project'))).toBe(true); |
| 248 | + }); |
| 249 | + |
| 250 | + it('should ignore all filters when no filterConfig provided', async () => { |
| 251 | + const results = await FileSuggestHelper.suggest( |
| 252 | + mockPlugin, |
| 253 | + '', |
| 254 | + 20 |
| 255 | + ); |
| 256 | + |
| 257 | + // All files should be returned regardless of filters |
| 258 | + expect(results.length).toBe(4); |
| 259 | + }); |
| 260 | + }); |
| 261 | + |
| 262 | + describe('Query Matching', () => { |
| 263 | + it('should match query regardless of filter settings', async () => { |
| 264 | + const resultsWithoutFilters = await FileSuggestHelper.suggest( |
| 265 | + mockPlugin, |
| 266 | + 'Note 1', |
| 267 | + 20 |
| 268 | + ); |
| 269 | + |
| 270 | + // Should match "Note 1" specifically |
| 271 | + expect(resultsWithoutFilters.length).toBeGreaterThanOrEqual(1); |
| 272 | + expect(resultsWithoutFilters.some(r => r.insertText === 'Note 1')).toBe(true); |
| 273 | + }); |
| 274 | + }); |
| 275 | +}); |
| 276 | + |
0 commit comments