-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathSettingsSearch.test.ts
More file actions
274 lines (218 loc) · 8.84 KB
/
SettingsSearch.test.ts
File metadata and controls
274 lines (218 loc) · 8.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Mock Obsidian's prepareFuzzySearch
const mockPrepareFuzzySearch = jest.fn((query: string) => {
return (target: string) => {
// Simple mock implementation for testing
return target.toLowerCase().includes(query.toLowerCase());
};
});
// Mock translations
const mockT = jest.fn((key: string) => key);
jest.mock("obsidian", () => ({
prepareFuzzySearch: mockPrepareFuzzySearch
}));
jest.mock("../translations/helper", () => ({
t: mockT
}));
import { SettingsIndexer } from "../components/settings/SettingsIndexer";
import { SETTINGS_METADATA } from "../data/settings-metadata";
describe("Settings Search Tests", () => {
let indexer: SettingsIndexer;
beforeEach(() => {
indexer = new SettingsIndexer();
jest.clearAllMocks();
});
describe("Settings Metadata", () => {
test("should have valid metadata structure", () => {
expect(SETTINGS_METADATA).toBeDefined();
expect(Array.isArray(SETTINGS_METADATA)).toBe(true);
expect(SETTINGS_METADATA.length).toBeGreaterThan(0);
// Check first few items have required fields
const firstItem = SETTINGS_METADATA[0];
expect(firstItem).toHaveProperty("id");
expect(firstItem).toHaveProperty("tabId");
expect(firstItem).toHaveProperty("name");
expect(firstItem).toHaveProperty("translationKey");
expect(firstItem).toHaveProperty("keywords");
expect(Array.isArray(firstItem.keywords)).toBe(true);
});
test("should have progress bar settings", () => {
const progressBarMain = SETTINGS_METADATA.find(item => item.id === "progress-bar-main");
expect(progressBarMain).toBeDefined();
expect(progressBarMain?.name).toBe("Progress bar");
expect(progressBarMain?.tabId).toBe("progress-bar");
expect(progressBarMain?.keywords).toContain("progress");
});
test("should have enable task filter setting", () => {
const taskFilter = SETTINGS_METADATA.find(item => item.id === "enable-task-filter");
expect(taskFilter).toBeDefined();
expect(taskFilter?.name).toBe("Enable Task Filter");
expect(taskFilter?.tabId).toBe("task-filter");
});
});
describe("SettingsIndexer", () => {
test("should initialize correctly", () => {
expect(indexer).toBeDefined();
// Should not be initialized yet
const stats = indexer.getStats();
expect(stats.itemCount).toBeGreaterThan(0);
});
test("should build index on first search", () => {
const results = indexer.search("progress");
expect(mockT).toHaveBeenCalled();
});
test("should search by name", () => {
const results = indexer.search("progress");
console.log("Search results for 'progress':", results.length);
results.forEach(result => {
console.log(`- ${result.item.name} (${result.matchType}, score: ${result.score})`);
});
expect(results.length).toBeGreaterThan(0);
// Should find progress bar settings
const progressResult = results.find(result =>
result.item.id === "progress-bar-main" ||
result.item.name.toLowerCase().includes("progress")
);
expect(progressResult).toBeDefined();
});
test("should search by description", () => {
const results = indexer.search("toggle");
console.log("Search results for 'toggle':", results.length);
results.forEach(result => {
console.log(`- ${result.item.name} (${result.matchType}, score: ${result.score})`);
if (result.item.description) {
console.log(` Description: ${result.item.description.substring(0, 100)}...`);
}
});
expect(results.length).toBeGreaterThan(0);
});
test("should search by keywords", () => {
const results = indexer.search("checkbox");
console.log("Search results for 'checkbox':", results.length);
results.forEach(result => {
console.log(`- ${result.item.name} (${result.matchType}, score: ${result.score})`);
console.log(` Keywords: ${result.item.keywords.join(", ")}`);
});
expect(results.length).toBeGreaterThan(0);
});
test("should prioritize name matches over description matches", () => {
const results = indexer.search("filter");
console.log("Search results for 'filter' with scores:");
results.forEach(result => {
console.log(`- ${result.item.name} (${result.matchType}, score: ${result.score})`);
});
expect(results.length).toBeGreaterThan(0);
// Find name matches and description matches
const nameMatches = results.filter(r => r.matchType === "name");
const descriptionMatches = results.filter(r => r.matchType === "description");
if (nameMatches.length > 0 && descriptionMatches.length > 0) {
// Name matches should have higher scores
const highestNameScore = Math.max(...nameMatches.map(r => r.score));
const highestDescScore = Math.max(...descriptionMatches.map(r => r.score));
expect(highestNameScore).toBeGreaterThan(highestDescScore);
}
});
test("should return empty results for empty query", () => {
const results = indexer.search("");
expect(results.length).toBe(0);
});
test("should return empty results for very short query", () => {
const results = indexer.search("a");
expect(results.length).toBe(0);
});
test("should return results for 2+ character query", () => {
const results = indexer.search("pr");
expect(results.length).toBeGreaterThan(0);
});
test("should limit results", () => {
const results = indexer.search("task");
console.log(`Found ${results.length} results for 'task'`);
expect(results.length).toBeLessThanOrEqual(10); // Default limit
});
test("should handle case insensitive search", () => {
const lowerResults = indexer.search("progress");
const upperResults = indexer.search("PROGRESS");
const mixedResults = indexer.search("Progress");
expect(lowerResults.length).toBe(upperResults.length);
expect(lowerResults.length).toBe(mixedResults.length);
});
});
describe("Translation Integration", () => {
test("should call translation function for setting names", () => {
indexer.search("progress");
// Should have called t() for translating setting names
expect(mockT).toHaveBeenCalled();
// Check if it was called with expected translation keys
const calls = mockT.mock.calls.map((call: any) => call[0]);
console.log("Translation calls:", calls.slice(0, 10)); // Show first 10 calls
expect(calls).toContain("Progress bar");
});
});
describe("Edge Cases", () => {
test("should handle special characters in search", () => {
const results = indexer.search("task-filter");
expect(results.length).toBeGreaterThanOrEqual(0);
});
test("should handle unicode characters", () => {
const results = indexer.search("设置");
expect(results.length).toBeGreaterThanOrEqual(0);
});
test("should handle numbers in search", () => {
const results = indexer.search("100");
expect(results.length).toBeGreaterThanOrEqual(0);
});
});
describe("Performance", () => {
test("should build index quickly", () => {
const start = performance.now();
indexer.initialize();
const end = performance.now();
const buildTime = end - start;
console.log(`Index build time: ${buildTime.toFixed(2)}ms`);
expect(buildTime).toBeLessThan(50); // Should be under 50ms
});
test("should search quickly", () => {
indexer.initialize(); // Pre-initialize
const start = performance.now();
const results = indexer.search("progress");
const end = performance.now();
const searchTime = end - start;
console.log(`Search time: ${searchTime.toFixed(2)}ms for ${results.length} results`);
expect(searchTime).toBeLessThan(10); // Should be under 10ms
});
});
describe("Specific Setting Tests", () => {
test("should find 'Enable Task Filter' setting", () => {
const results = indexer.search("enable task filter");
console.log("Searching for 'enable task filter':");
results.forEach(result => {
console.log(`- ${result.item.name} (ID: ${result.item.id})`);
});
const exactMatch = results.find(r => r.item.id === "enable-task-filter");
expect(exactMatch).toBeDefined();
});
test("should find progress bar settings", () => {
const results = indexer.search("progress bar");
console.log("Searching for 'progress bar':");
results.forEach(result => {
console.log(`- ${result.item.name} (ID: ${result.item.id})`);
});
const progressBarMain = results.find(r => r.item.id === "progress-bar-main");
expect(progressBarMain).toBeDefined();
expect(progressBarMain?.matchType).toBe("name");
});
test("should find settings by partial name", () => {
const results = indexer.search("workflow");
console.log("Searching for 'workflow':");
results.forEach(result => {
console.log(`- ${result.item.name} (ID: ${result.item.id})`);
});
expect(results.length).toBeGreaterThan(0);
// Should find workflow-related settings
const workflowSettings = results.filter(r =>
r.item.name.toLowerCase().includes("workflow") ||
r.item.tabId === "workflow"
);
expect(workflowSettings.length).toBeGreaterThan(0);
});
});
});