Asper Header  1.0.14
The header injector extension
Loading...
Searching...
No Matches
querier.test.ts
Go to the documentation of this file.
1
28import * as assert from 'assert';
29import * as vscode from 'vscode';
30import { Query, query } from '../modules/querier';
31
40let mockInputBoxResponse: string | undefined = undefined;
41
43let mockQuickPickResponse: string | undefined = undefined;
44
47
50
52let mockLastInputOptions: vscode.InputBoxOptions | undefined = undefined;
53
55let mockLastQuickPickItems: string[] = [];
56
58let mockLastQuickPickOptions: vscode.QuickPickOptions | undefined = undefined;
59
69
72
79suite('Query Test Suite', function () {
80
85 setup(() => {
86 // Reset mock state
87 mockInputBoxResponse = undefined;
88 mockQuickPickResponse = undefined;
91 mockLastInputOptions = undefined;
93 mockLastQuickPickOptions = undefined;
94
95 // Store original methods
96 originalShowInputBox = vscode.window.showInputBox;
97 originalShowQuickPick = vscode.window.showQuickPick;
98
99 // Mock VS Code methods
100 (vscode.window as any).showInputBox = async (options?: vscode.InputBoxOptions) => {
102 mockLastInputOptions = options;
104 };
105
106 (vscode.window as any).showQuickPick = async (items: string[], options?: vscode.QuickPickOptions) => {
108 mockLastQuickPickItems = [...items];
109 mockLastQuickPickOptions = options;
111 };
112 });
113
118 teardown(() => {
119 // Restore original methods
120 (vscode.window as any).showInputBox = originalShowInputBox;
121 (vscode.window as any).showQuickPick = originalShowQuickPick;
122 });
123
128 suite('Singleton Pattern', () => {
133 test('should return the same instance when accessing Query.instance multiple times', () => {
134 const instance1 = Query.instance;
135 const instance2 = Query.instance;
136
137 assert.strictEqual(instance1, instance2, 'Query.instance should return the same instance');
138 assert.ok(instance1 instanceof Query, 'Instance should be of type Query');
139 });
140
145 test('exported query constant should be the same as Query.instance', () => {
146 const queryInstance = Query.instance;
147
148 assert.strictEqual(query, queryInstance, 'Exported query should be the same as Query.instance');
149 });
150
155 test('should create instance on first access', () => {
156 const instance = Query.instance;
157 assert.ok(instance, 'Instance should be created on first access');
158 assert.ok(typeof instance.input === 'function', 'Instance should have input method');
159 assert.ok(typeof instance.quickPick === 'function', 'Instance should have quickPick method');
160 assert.ok(typeof instance.confirm === 'function', 'Instance should have confirm method');
161 });
162 });
163
168 suite('Input Dialog Tests', () => {
173 test('should successfully return user input', async () => {
174 const expectedInput = 'Test user input';
175 const promptText = 'Enter your input:';
176
177 mockInputBoxResponse = expectedInput;
178
179 const result = await query.input(promptText);
180
181 assert.strictEqual(result, expectedInput, 'Should return the user input');
182 assert.strictEqual(mockInputBoxCallCount, 1, 'showInputBox should be called once');
183 assert.strictEqual(mockLastInputOptions?.prompt, promptText, 'Should pass prompt correctly');
184 });
185
190 test('should handle user cancellation', async () => {
191 mockInputBoxResponse = undefined;
192
193 const result = await query.input('Test prompt');
194
195 assert.strictEqual(result, undefined, 'Should return undefined when user cancels');
196 assert.strictEqual(mockInputBoxCallCount, 1, 'showInputBox should be called once');
197 });
198
203 test('should pass through InputBoxOptions correctly', async () => {
204 const promptText = 'Enter password:';
205 const options: vscode.InputBoxOptions = {
206 password: true,
207 placeHolder: 'Your password here'
208 };
209
210 mockInputBoxResponse = 'secret123';
211
212 const result = await query.input(promptText, options);
213
214 assert.strictEqual(result, 'secret123', 'Should return input value');
215 assert.strictEqual(mockLastInputOptions?.prompt, promptText, 'Should include prompt');
216 assert.strictEqual(mockLastInputOptions?.password, true, 'Should pass password option');
217 assert.strictEqual(mockLastInputOptions?.placeHolder, 'Your password here', 'Should pass placeholder');
218 });
219
224 test('should handle empty string input', async () => {
226
227 const result = await query.input('Enter something:');
228
229 assert.strictEqual(result, '', 'Should return empty string if user enters empty string');
230 });
231 });
232
237 suite('Quick Pick Dialog Tests', () => {
242 test('should successfully return selected item', async () => {
243 const items = ['Option 1', 'Option 2', 'Option 3'];
244 const placeholder = 'Select an option:';
245 const selectedItem = 'Option 2';
246
247 mockQuickPickResponse = selectedItem;
248
249 const result = await query.quickPick(items, placeholder);
250
251 assert.strictEqual(result, selectedItem, 'Should return the selected item');
252 assert.strictEqual(mockQuickPickCallCount, 1, 'showQuickPick should be called once');
253 assert.deepStrictEqual(mockLastQuickPickItems, items, 'Should pass items correctly');
254 assert.strictEqual(mockLastQuickPickOptions?.placeHolder, placeholder, 'Should pass placeholder correctly');
255 });
256
261 test('should handle user cancellation in quick pick', async () => {
262 mockQuickPickResponse = undefined;
263
264 const result = await query.quickPick(['Option 1'], 'Select:');
265
266 assert.strictEqual(result, undefined, 'Should return undefined when user cancels');
267 assert.strictEqual(mockQuickPickCallCount, 1, 'showQuickPick should be called once');
268 });
269
274 test('should handle empty items array', async () => {
275 const emptyItems: string[] = [];
276 mockQuickPickResponse = undefined;
277
278 const result = await query.quickPick(emptyItems, 'No options:');
279
280 assert.strictEqual(result, undefined, 'Should handle empty items gracefully');
281 assert.deepStrictEqual(mockLastQuickPickItems, emptyItems, 'Should pass empty array');
282 });
283
288 test('should handle single item array', async () => {
289 const singleItem = ['Only Option'];
290 mockQuickPickResponse = 'Only Option';
291
292 const result = await query.quickPick(singleItem, 'Only choice:');
293
294 assert.strictEqual(result, 'Only Option', 'Should handle single item selection');
295 });
296 });
297
302 suite('Confirmation Dialog Tests', () => {
307 test('should return true when user selects Yes', async () => {
308 // Mock getMessage to return localized strings
309 const messageModule = require('../modules/messageProvider');
310 const originalGetMessage = messageModule.getMessage;
311 messageModule.getMessage = (key: string) => {
312 if (key === 'quickPickYes') {
313 return 'Yes';
314 }
315 if (key === 'quickPickNo') {
316 return 'No';
317 }
318 return key;
319 };
320
321 try {
322 mockQuickPickResponse = 'Yes';
323
324 const result = await query.confirm('Do you want to proceed?');
325
326 assert.strictEqual(result, true, 'Should return true for Yes selection');
327 assert.strictEqual(mockQuickPickCallCount, 1, 'showQuickPick should be called once');
328 assert.deepStrictEqual(mockLastQuickPickItems, ['Yes', 'No'], 'Should present Yes/No options');
329 assert.strictEqual(mockLastQuickPickOptions?.placeHolder, 'Do you want to proceed?', 'Should use prompt as placeholder');
330 } finally {
331 messageModule.getMessage = originalGetMessage;
332 }
333 });
334
339 test('should return false when user selects No', async () => {
340 const messageModule = require('../modules/messageProvider');
341 const originalGetMessage = messageModule.getMessage;
342 messageModule.getMessage = (key: string) => {
343 if (key === 'quickPickYes') {
344 return 'Yes';
345 }
346 if (key === 'quickPickNo') {
347 return 'No';
348 }
349 return key;
350 };
351
352 try {
354
355 const result = await query.confirm('Are you sure?');
356
357 assert.strictEqual(result, false, 'Should return false for No selection');
358 } finally {
359 messageModule.getMessage = originalGetMessage;
360 }
361 });
362
367 test('should return false when user cancels confirmation', async () => {
368 const messageModule = require('../modules/messageProvider');
369 const originalGetMessage = messageModule.getMessage;
370 messageModule.getMessage = (key: string) => {
371 if (key === 'quickPickYes') {
372 return 'Yes';
373 }
374 if (key === 'quickPickNo') {
375 return 'No';
376 }
377 return key;
378 };
379
380 try {
381 mockQuickPickResponse = undefined;
382
383 const result = await query.confirm('Confirm action?');
384
385 assert.strictEqual(result, false, 'Should return false for cancellation');
386 } finally {
387 messageModule.getMessage = originalGetMessage;
388 }
389 });
390 });
391
396 suite('Integration Tests', () => {
401 test('should handle complex input options', async () => {
402 const complexOptions: vscode.InputBoxOptions = {
403 value: 'default value',
404 prompt: 'Override prompt',
405 placeHolder: 'Type here...',
406 password: false,
407 ignoreFocusOut: true
408 };
409
410 mockInputBoxResponse = 'test@example.com';
411
412 const result = await query.input('Enter email:', complexOptions);
413
414 assert.strictEqual(result, 'test@example.com', 'Should handle complex options');
415 assert.strictEqual(mockLastInputOptions?.value, 'default value', 'Should pass default value');
416 assert.strictEqual(mockLastInputOptions?.placeHolder, 'Type here...', 'Should pass placeholder');
417 assert.strictEqual(mockLastInputOptions?.ignoreFocusOut, true, 'Should pass ignoreFocusOut');
418 });
419
424 test('should merge prompt with options correctly', async () => {
425 const options: vscode.InputBoxOptions = {
426 prompt: 'Original prompt'
427 };
428
429 mockInputBoxResponse = 'result';
430
431 await query.input('Override prompt', options);
432
433 // Due to spread operator, options.prompt will override the parameter
434 assert.strictEqual(mockLastInputOptions?.prompt, 'Original prompt', 'Should use prompt from options when provided');
435 });
436
441 test('should handle special characters in items', async () => {
442 const mixedItems = ['string', '123', '', 'special@chars!'];
443 mockQuickPickResponse = '123';
444
445 const result = await query.quickPick(mixedItems, 'Pick one:');
446
447 assert.strictEqual(result, '123', 'Should handle mixed string items');
448 });
449 });
450});
Singleton user interaction manager for VS Code UI components.
Definition querier.ts:115
const instance
Singleton logger instance for application-wide use.
Definition logger.ts:870
export const Record< string,(...args:any[])=> string
import *as vscode from vscode
let originalShowQuickPick
Original showQuickPick implementation.
let mockInputBoxResponse
Mock state variables for VS Code UI testing.
let originalShowInputBox
Storage for original VS Code API methods.
let mockQuickPickCallCount
Counter for quick pick method calls.
let mockLastInputOptions
Last captured input box options for validation.
import *as assert from assert
let mockLastQuickPickItems
Last captured quick pick items for validation.
let mockInputBoxCallCount
Counter for input box method calls.
let mockLastQuickPickOptions
Last captured quick pick options for validation.
let mockQuickPickResponse
Mock response for quick pick dialogs.
export const query
Convenience singleton export for direct access to Query functionality @export Primary interface for u...
Definition querier.ts:345