|
| 1 | +import { FileFilterManager } from "../utils/FileFilterManager"; |
| 2 | +import { FilterMode } from "../common/setting-definition"; |
| 3 | +import { FileFilterSettings } from "../common/setting-definition"; |
| 4 | + |
| 5 | +// Mock TFile for testing |
| 6 | +class MockTFile { |
| 7 | + constructor(public path: string, public extension: string) {} |
| 8 | +} |
| 9 | + |
| 10 | +describe("FileFilterManager", () => { |
| 11 | + describe("Basic Filtering", () => { |
| 12 | + it("should allow all files when disabled", () => { |
| 13 | + const config: FileFilterSettings = { |
| 14 | + enabled: false, |
| 15 | + mode: FilterMode.BLACKLIST, |
| 16 | + rules: [{ type: "folder", path: ".obsidian", enabled: true }], |
| 17 | + }; |
| 18 | + |
| 19 | + const manager = new FileFilterManager(config); |
| 20 | + const file = new MockTFile(".obsidian/config.json", "json") as any; |
| 21 | + |
| 22 | + expect(manager.shouldIncludeFile(file)).toBe(true); |
| 23 | + }); |
| 24 | + |
| 25 | + it("should filter files in blacklist mode", () => { |
| 26 | + const config: FileFilterSettings = { |
| 27 | + enabled: true, |
| 28 | + mode: FilterMode.BLACKLIST, |
| 29 | + rules: [ |
| 30 | + { type: "folder", path: ".obsidian", enabled: true }, |
| 31 | + { type: "file", path: "temp.md", enabled: true }, |
| 32 | + ], |
| 33 | + }; |
| 34 | + |
| 35 | + const manager = new FileFilterManager(config); |
| 36 | + |
| 37 | + // Should exclude files in .obsidian folder |
| 38 | + const obsidianFile = new MockTFile( |
| 39 | + ".obsidian/config.json", |
| 40 | + "json" |
| 41 | + ) as any; |
| 42 | + expect(manager.shouldIncludeFile(obsidianFile)).toBe(false); |
| 43 | + |
| 44 | + // Should exclude specific file |
| 45 | + const tempFile = new MockTFile("temp.md", "md") as any; |
| 46 | + expect(manager.shouldIncludeFile(tempFile)).toBe(false); |
| 47 | + |
| 48 | + // Should include other files |
| 49 | + const normalFile = new MockTFile("notes/my-note.md", "md") as any; |
| 50 | + expect(manager.shouldIncludeFile(normalFile)).toBe(true); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should filter files in whitelist mode", () => { |
| 54 | + const config: FileFilterSettings = { |
| 55 | + enabled: true, |
| 56 | + mode: FilterMode.WHITELIST, |
| 57 | + rules: [ |
| 58 | + { type: "folder", path: "notes", enabled: true }, |
| 59 | + { type: "file", path: "important.md", enabled: true }, |
| 60 | + ], |
| 61 | + }; |
| 62 | + |
| 63 | + const manager = new FileFilterManager(config); |
| 64 | + |
| 65 | + // Should include files in notes folder |
| 66 | + const notesFile = new MockTFile("notes/my-note.md", "md") as any; |
| 67 | + expect(manager.shouldIncludeFile(notesFile)).toBe(true); |
| 68 | + |
| 69 | + // Should include specific file |
| 70 | + const importantFile = new MockTFile("important.md", "md") as any; |
| 71 | + expect(manager.shouldIncludeFile(importantFile)).toBe(true); |
| 72 | + |
| 73 | + // Should exclude other files |
| 74 | + const otherFile = new MockTFile("other/file.md", "md") as any; |
| 75 | + expect(manager.shouldIncludeFile(otherFile)).toBe(false); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe("Pattern Matching", () => { |
| 80 | + it("should match wildcard patterns", () => { |
| 81 | + const config: FileFilterSettings = { |
| 82 | + enabled: true, |
| 83 | + mode: FilterMode.BLACKLIST, |
| 84 | + rules: [ |
| 85 | + { type: "pattern", path: "*.tmp", enabled: true }, |
| 86 | + { type: "pattern", path: "temp/*", enabled: true }, |
| 87 | + ], |
| 88 | + }; |
| 89 | + |
| 90 | + const manager = new FileFilterManager(config); |
| 91 | + |
| 92 | + // Should exclude .tmp files |
| 93 | + const tmpFile = new MockTFile("cache.tmp", "tmp") as any; |
| 94 | + expect(manager.shouldIncludeFile(tmpFile)).toBe(false); |
| 95 | + |
| 96 | + // Should exclude files in temp folder |
| 97 | + const tempFile = new MockTFile("temp/data.json", "json") as any; |
| 98 | + expect(manager.shouldIncludeFile(tempFile)).toBe(false); |
| 99 | + |
| 100 | + // Should include normal files |
| 101 | + const normalFile = new MockTFile("notes/note.md", "md") as any; |
| 102 | + expect(manager.shouldIncludeFile(normalFile)).toBe(true); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + describe("Folder Hierarchy", () => { |
| 107 | + it("should match nested folders", () => { |
| 108 | + const config: FileFilterSettings = { |
| 109 | + enabled: true, |
| 110 | + mode: FilterMode.BLACKLIST, |
| 111 | + rules: [{ type: "folder", path: "archive", enabled: true }], |
| 112 | + }; |
| 113 | + |
| 114 | + const manager = new FileFilterManager(config); |
| 115 | + |
| 116 | + // Should exclude files in archive folder |
| 117 | + const archiveFile = new MockTFile("archive/old.md", "md") as any; |
| 118 | + expect(manager.shouldIncludeFile(archiveFile)).toBe(false); |
| 119 | + |
| 120 | + // Should exclude files in nested archive folders |
| 121 | + const nestedArchiveFile = new MockTFile( |
| 122 | + "archive/2023/old.md", |
| 123 | + "md" |
| 124 | + ) as any; |
| 125 | + expect(manager.shouldIncludeFile(nestedArchiveFile)).toBe(false); |
| 126 | + |
| 127 | + // Should include files in other folders |
| 128 | + const normalFile = new MockTFile("notes/current.md", "md") as any; |
| 129 | + expect(manager.shouldIncludeFile(normalFile)).toBe(true); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + describe("Rule Management", () => { |
| 134 | + it("should respect disabled rules", () => { |
| 135 | + const config: FileFilterSettings = { |
| 136 | + enabled: true, |
| 137 | + mode: FilterMode.BLACKLIST, |
| 138 | + rules: [ |
| 139 | + { type: "folder", path: ".obsidian", enabled: false }, |
| 140 | + { type: "folder", path: ".trash", enabled: true }, |
| 141 | + ], |
| 142 | + }; |
| 143 | + |
| 144 | + const manager = new FileFilterManager(config); |
| 145 | + |
| 146 | + // Should include files from disabled rule |
| 147 | + const obsidianFile = new MockTFile( |
| 148 | + ".obsidian/config.json", |
| 149 | + "json" |
| 150 | + ) as any; |
| 151 | + expect(manager.shouldIncludeFile(obsidianFile)).toBe(true); |
| 152 | + |
| 153 | + // Should exclude files from enabled rule |
| 154 | + const trashFile = new MockTFile(".trash/deleted.md", "md") as any; |
| 155 | + expect(manager.shouldIncludeFile(trashFile)).toBe(false); |
| 156 | + }); |
| 157 | + |
| 158 | + it("should update configuration dynamically", () => { |
| 159 | + const initialConfig: FileFilterSettings = { |
| 160 | + enabled: true, |
| 161 | + mode: FilterMode.BLACKLIST, |
| 162 | + rules: [{ type: "folder", path: ".obsidian", enabled: true }], |
| 163 | + }; |
| 164 | + |
| 165 | + const manager = new FileFilterManager(initialConfig); |
| 166 | + const file = new MockTFile(".obsidian/config.json", "json") as any; |
| 167 | + |
| 168 | + // Initially should exclude |
| 169 | + expect(manager.shouldIncludeFile(file)).toBe(false); |
| 170 | + |
| 171 | + // Update configuration to disable filtering |
| 172 | + const newConfig: FileFilterSettings = { |
| 173 | + enabled: false, |
| 174 | + mode: FilterMode.BLACKLIST, |
| 175 | + rules: [], |
| 176 | + }; |
| 177 | + |
| 178 | + manager.updateConfig(newConfig); |
| 179 | + |
| 180 | + // Should now include |
| 181 | + expect(manager.shouldIncludeFile(file)).toBe(true); |
| 182 | + }); |
| 183 | + }); |
| 184 | + |
| 185 | + describe("Performance and Caching", () => { |
| 186 | + it("should cache filter results", () => { |
| 187 | + const config: FileFilterSettings = { |
| 188 | + enabled: true, |
| 189 | + mode: FilterMode.BLACKLIST, |
| 190 | + rules: [{ type: "folder", path: ".obsidian", enabled: true }], |
| 191 | + }; |
| 192 | + |
| 193 | + const manager = new FileFilterManager(config); |
| 194 | + const file = new MockTFile(".obsidian/config.json", "json") as any; |
| 195 | + |
| 196 | + // First call |
| 197 | + const result1 = manager.shouldIncludeFile(file); |
| 198 | + |
| 199 | + // Second call should use cache |
| 200 | + const result2 = manager.shouldIncludeFile(file); |
| 201 | + |
| 202 | + expect(result1).toBe(result2); |
| 203 | + expect(result1).toBe(false); |
| 204 | + |
| 205 | + // Verify cache is working |
| 206 | + const stats = manager.getStats(); |
| 207 | + expect(stats.cacheSize).toBeGreaterThan(0); |
| 208 | + }); |
| 209 | + |
| 210 | + it("should clear cache when configuration changes", () => { |
| 211 | + const config: FileFilterSettings = { |
| 212 | + enabled: true, |
| 213 | + mode: FilterMode.BLACKLIST, |
| 214 | + rules: [{ type: "folder", path: ".obsidian", enabled: true }], |
| 215 | + }; |
| 216 | + |
| 217 | + const manager = new FileFilterManager(config); |
| 218 | + const file = new MockTFile(".obsidian/config.json", "json") as any; |
| 219 | + |
| 220 | + // Populate cache |
| 221 | + manager.shouldIncludeFile(file); |
| 222 | + expect(manager.getStats().cacheSize).toBeGreaterThan(0); |
| 223 | + |
| 224 | + // Update configuration |
| 225 | + const newConfig: FileFilterSettings = { |
| 226 | + enabled: false, |
| 227 | + mode: FilterMode.BLACKLIST, |
| 228 | + rules: [], |
| 229 | + }; |
| 230 | + |
| 231 | + manager.updateConfig(newConfig); |
| 232 | + |
| 233 | + // Cache should be cleared |
| 234 | + expect(manager.getStats().cacheSize).toBe(0); |
| 235 | + }); |
| 236 | + }); |
| 237 | + |
| 238 | + describe("Statistics", () => { |
| 239 | + it("should provide accurate statistics", () => { |
| 240 | + const config: FileFilterSettings = { |
| 241 | + enabled: true, |
| 242 | + mode: FilterMode.BLACKLIST, |
| 243 | + rules: [ |
| 244 | + { type: "folder", path: ".obsidian", enabled: true }, |
| 245 | + { type: "file", path: "temp.md", enabled: false }, |
| 246 | + { type: "pattern", path: "*.tmp", enabled: true }, |
| 247 | + ], |
| 248 | + }; |
| 249 | + |
| 250 | + const manager = new FileFilterManager(config); |
| 251 | + const stats = manager.getStats(); |
| 252 | + |
| 253 | + expect(stats.enabled).toBe(true); |
| 254 | + expect(stats.rulesCount).toBe(2); // Only enabled rules |
| 255 | + expect(stats.cacheSize).toBe(0); // No cache yet |
| 256 | + }); |
| 257 | + }); |
| 258 | +}); |
0 commit comments