|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest" |
| 2 | +import { Fzf } from "fzf" |
| 3 | + |
| 4 | +// Test the fallback search logic for period-prefixed queries |
| 5 | +describe("searchWorkspaceFiles period handling", () => { |
| 6 | + it("should handle period-prefixed queries with fallback search", () => { |
| 7 | + // Mock file data that would come from ripgrep |
| 8 | + const mockFiles = [ |
| 9 | + { path: ".rooignore", type: "file" as const, label: ".rooignore" }, |
| 10 | + { path: ".gitignore", type: "file" as const, label: ".gitignore" }, |
| 11 | + { path: ".env", type: "file" as const, label: ".env" }, |
| 12 | + { path: "src/app.ts", type: "file" as const, label: "app.ts" }, |
| 13 | + { path: "package.json", type: "file" as const, label: "package.json" }, |
| 14 | + ] |
| 15 | + |
| 16 | + // Test the fallback search logic directly |
| 17 | + const query = ".rooignore" |
| 18 | + |
| 19 | + // Create search items like the real function does |
| 20 | + const searchItems = mockFiles.map((item) => ({ |
| 21 | + original: item, |
| 22 | + searchStr: `${item.path} ${item.label || ""}`, |
| 23 | + })) |
| 24 | + |
| 25 | + // Test fzf search first |
| 26 | + const fzf = new Fzf(searchItems, { |
| 27 | + selector: (item) => item.searchStr, |
| 28 | + }) |
| 29 | + |
| 30 | + let fzfResults = fzf.find(query).map((result) => result.item.original) |
| 31 | + |
| 32 | + // If fzf doesn't return results for period-prefixed queries, use fallback |
| 33 | + if (fzfResults.length === 0 && query.startsWith(".")) { |
| 34 | + // Fallback: exact substring matching |
| 35 | + const exactMatches = mockFiles.filter((item) => { |
| 36 | + const searchStr = `${item.path} ${item.label || ""}` |
| 37 | + return searchStr.toLowerCase().includes(query.toLowerCase()) |
| 38 | + }) |
| 39 | + |
| 40 | + // Sort by relevance |
| 41 | + exactMatches.sort((a, b) => { |
| 42 | + const aLabel = (a.label || "").toLowerCase() |
| 43 | + const bLabel = (b.label || "").toLowerCase() |
| 44 | + const queryLower = query.toLowerCase() |
| 45 | + |
| 46 | + // Prioritize exact filename matches |
| 47 | + if (aLabel === queryLower && bLabel !== queryLower) return -1 |
| 48 | + if (bLabel === queryLower && aLabel !== queryLower) return 1 |
| 49 | + |
| 50 | + // Then prioritize filename starts with query |
| 51 | + if (aLabel.startsWith(queryLower) && !bLabel.startsWith(queryLower)) return -1 |
| 52 | + if (bLabel.startsWith(queryLower) && !aLabel.startsWith(queryLower)) return 1 |
| 53 | + |
| 54 | + // Finally sort by path length |
| 55 | + return a.path.length - b.path.length |
| 56 | + }) |
| 57 | + |
| 58 | + fzfResults = exactMatches |
| 59 | + } |
| 60 | + |
| 61 | + // Should find the .rooignore file |
| 62 | + expect(fzfResults.length).toBeGreaterThan(0) |
| 63 | + expect(fzfResults[0].path).toBe(".rooignore") |
| 64 | + expect(fzfResults[0].label).toBe(".rooignore") |
| 65 | + }) |
| 66 | + |
| 67 | + it("should prioritize exact matches over partial matches", () => { |
| 68 | + const mockFiles = [ |
| 69 | + { path: ".rooignore", type: "file" as const, label: ".rooignore" }, |
| 70 | + { path: "src/.rooignore.backup", type: "file" as const, label: ".rooignore.backup" }, |
| 71 | + { path: "docs/rooignore-guide.md", type: "file" as const, label: "rooignore-guide.md" }, |
| 72 | + ] |
| 73 | + |
| 74 | + const query = ".rooignore" |
| 75 | + |
| 76 | + // Simulate fallback search |
| 77 | + const exactMatches = mockFiles.filter((item) => { |
| 78 | + const searchStr = `${item.path} ${item.label || ""}` |
| 79 | + return searchStr.toLowerCase().includes(query.toLowerCase()) |
| 80 | + }) |
| 81 | + |
| 82 | + // Sort by relevance |
| 83 | + exactMatches.sort((a, b) => { |
| 84 | + const aLabel = (a.label || "").toLowerCase() |
| 85 | + const bLabel = (b.label || "").toLowerCase() |
| 86 | + const queryLower = query.toLowerCase() |
| 87 | + |
| 88 | + // Prioritize exact filename matches |
| 89 | + if (aLabel === queryLower && bLabel !== queryLower) return -1 |
| 90 | + if (bLabel === queryLower && aLabel !== queryLower) return 1 |
| 91 | + |
| 92 | + // Then prioritize filename starts with query |
| 93 | + if (aLabel.startsWith(queryLower) && !bLabel.startsWith(queryLower)) return -1 |
| 94 | + if (bLabel.startsWith(queryLower) && !aLabel.startsWith(queryLower)) return 1 |
| 95 | + |
| 96 | + // Finally sort by path length |
| 97 | + return a.path.length - b.path.length |
| 98 | + }) |
| 99 | + |
| 100 | + // The exact match should be first |
| 101 | + expect(exactMatches.length).toBeGreaterThan(0) |
| 102 | + expect(exactMatches[0].label).toBe(".rooignore") |
| 103 | + }) |
| 104 | + |
| 105 | + it("should handle .gitignore searches correctly", () => { |
| 106 | + const mockFiles = [ |
| 107 | + { path: ".gitignore", type: "file" as const, label: ".gitignore" }, |
| 108 | + { path: "src/.gitignore", type: "file" as const, label: ".gitignore" }, |
| 109 | + { path: "docs/gitignore-examples.md", type: "file" as const, label: "gitignore-examples.md" }, |
| 110 | + ] |
| 111 | + |
| 112 | + const query = ".gitignore" |
| 113 | + |
| 114 | + // Simulate fallback search |
| 115 | + const exactMatches = mockFiles.filter((item) => { |
| 116 | + const searchStr = `${item.path} ${item.label || ""}` |
| 117 | + return searchStr.toLowerCase().includes(query.toLowerCase()) |
| 118 | + }) |
| 119 | + |
| 120 | + // Sort by relevance |
| 121 | + exactMatches.sort((a, b) => { |
| 122 | + const aLabel = (a.label || "").toLowerCase() |
| 123 | + const bLabel = (b.label || "").toLowerCase() |
| 124 | + const queryLower = query.toLowerCase() |
| 125 | + |
| 126 | + // Prioritize exact filename matches |
| 127 | + if (aLabel === queryLower && bLabel !== queryLower) return -1 |
| 128 | + if (bLabel === queryLower && aLabel !== queryLower) return 1 |
| 129 | + |
| 130 | + // Then prioritize filename starts with query |
| 131 | + if (aLabel.startsWith(queryLower) && !bLabel.startsWith(queryLower)) return -1 |
| 132 | + if (bLabel.startsWith(queryLower) && !aLabel.startsWith(queryLower)) return 1 |
| 133 | + |
| 134 | + // Finally sort by path length |
| 135 | + return a.path.length - b.path.length |
| 136 | + }) |
| 137 | + |
| 138 | + // Should find .gitignore files |
| 139 | + expect(exactMatches.length).toBeGreaterThan(0) |
| 140 | + expect(exactMatches.some((result) => result.label === ".gitignore")).toBe(true) |
| 141 | + }) |
| 142 | +}) |
0 commit comments