Asper Header  1.0.14
The header injector extension
Loading...
Searching...
No Matches
messageProvider.test.ts
Go to the documentation of this file.
1
21import * as assert from 'assert';
22import { getMessage } from '../modules/messageProvider';
23import { messages } from '../modules/messageReference';
24
29suite('MessageProvider Test Suite', function () {
30
35 suite('Basic Message Retrieval', () => {
40 test('should retrieve simple messages without parameters', () => {
41 // Test messages that don't require parameters
42 const refreshMessage = getMessage("fileRefreshed");
43 assert.ok(typeof refreshMessage === 'string');
44 assert.ok(refreshMessage.length > 0);
45 assert.strictEqual(refreshMessage, "Refreshing file content.");
46
47 const saveFailed = getMessage("fileSaveFailed");
48 assert.ok(typeof saveFailed === 'string');
49 assert.strictEqual(saveFailed, "Failed to save the file, please try saving it again.");
50 });
51
56 test('should handle messages with single parameters', () => {
57 const testPath = '/test/path/file.json';
58 const fileLoaded = getMessage("fileLoaded", testPath);
59
60 assert.ok(typeof fileLoaded === 'string');
61 assert.ok(fileLoaded.includes(testPath));
62 assert.strictEqual(fileLoaded, `File ${testPath} loaded!`);
63 });
64
69 test('should handle messages with multiple parameters', () => {
70 const oldPath = '/old/path';
71 const newPath = '/new/path';
72 const pathUpdated = getMessage("filePathUpdated", oldPath, newPath);
73
74 assert.ok(typeof pathUpdated === 'string');
75 assert.ok(pathUpdated.includes(oldPath));
76 assert.ok(pathUpdated.includes(newPath));
77 assert.strictEqual(pathUpdated, `The path has been updated from ${oldPath} to ${newPath}.`);
78 });
79 });
80
85 suite('Parameter Substitution', () => {
90 test('should handle various data types as parameters', () => {
91 // String parameters
92 const stringParam = getMessage("fileUnloaded", "test.json");
93 assert.ok(stringParam.includes("test.json"));
94
95 // Number parameters (converted to string)
96 const numberParam = getMessage("fileLoaded", 12345);
97 assert.ok(numberParam.includes("12345"));
98
99 // Boolean parameters (converted to string)
100 const booleanParam = getMessage("fileLoaded", true);
101 assert.ok(booleanParam.includes("true"));
102 });
103
108 test('should handle undefined and null parameters gracefully', () => {
109 const undefinedParam = getMessage("fileLoaded", undefined);
110 assert.ok(typeof undefinedParam === 'string');
111 assert.ok(undefinedParam.includes("undefined"));
112
113 const nullParam = getMessage("fileLoaded", null);
114 assert.ok(typeof nullParam === 'string');
115 assert.ok(nullParam.includes("null"));
116 });
117
122 test('should handle empty string parameters', () => {
123 const emptyParam = getMessage("fileLoaded", "");
124 assert.ok(typeof emptyParam === 'string');
125 assert.strictEqual(emptyParam, "File loaded!");
126 });
127
132 test('should handle special characters in parameters', () => {
133 const specialChars = '/path/with spaces & symbols!@#$%.json';
134 const result = getMessage("fileLoaded", specialChars);
135 assert.ok(result.includes(specialChars));
136 assert.strictEqual(result, `File ${specialChars} loaded!`);
137 });
138 });
139
144 suite('Message Reference Validation', () => {
149 test('should contain all expected message keys', () => {
150 const expectedKeys = [
151 'fileLoaded',
152 'fileParseError',
153 'fileRefreshed',
154 'filePathUpdated',
155 'fileUnloaded',
156 'fileExcludedActivationDisabled',
157 'fileSaveFailed',
158 'cwdUpdated',
159 'cwdDoesNotExist'
160 ];
161
162 const englishMessages = messages.en;
163 expectedKeys.forEach(key => {
164 assert.ok(key in englishMessages, `Message key '${key}' should exist in English messages`);
165 assert.ok(typeof englishMessages[key] === 'function', `Message '${key}' should be a function`);
166 });
167 });
168
173 test('should have functions that return strings', () => {
174 // Test function-based messages
175 const englishMessages = messages.en;
176 const fileLoaded = englishMessages.fileLoaded('/test/path');
177 assert.strictEqual(typeof fileLoaded, 'string');
178
179 const parseError = englishMessages.fileParseError('/test/file', 'syntax error');
180 assert.strictEqual(typeof parseError, 'string');
181
182 const cwdUpdated = englishMessages.cwdUpdated('/old', '/new');
183 assert.strictEqual(typeof cwdUpdated, 'string');
184 });
185
190 test('should validate message function signatures', () => {
191 const englishMessages = messages.en;
192 // Single parameter functions
193 assert.ok(englishMessages.fileLoaded.length >= 1, 'fileLoaded should accept at least 1 parameter');
194 assert.ok(englishMessages.fileUnloaded.length >= 1, 'fileUnloaded should accept at least 1 parameter');
195 assert.ok(englishMessages.cwdDoesNotExist.length >= 1, 'cwdDoesNotExist should accept at least 1 parameter');
196
197 // Two parameter functions
198 assert.ok(englishMessages.fileParseError.length >= 2, 'fileParseError should accept at least 2 parameters');
199 assert.ok(englishMessages.filePathUpdated.length >= 2, 'filePathUpdated should accept at least 2 parameters');
200 assert.ok(englishMessages.cwdUpdated.length >= 2, 'cwdUpdated should accept at least 2 parameters');
201
202 // Zero parameter functions
203 assert.ok(englishMessages.fileRefreshed.length === 0, 'fileRefreshed should accept 0 parameters');
204 assert.ok(englishMessages.fileExcludedActivationDisabled.length === 0, 'fileExcludedActivationDisabled should accept 0 parameters');
205 assert.ok(englishMessages.fileSaveFailed.length === 0, 'fileSaveFailed should accept 0 parameters');
206 });
207 });
208
213 suite('Error Handling', () => {
218 test('should handle invalid message keys gracefully', () => {
219 // The implementation might throw an error or return a default message
220 // This test verifies the behavior is consistent
221 try {
222 const result = getMessage("nonExistentMessage" as any);
223 // If it returns something, it should be a string
224 assert.strictEqual(typeof result, 'string');
225 } catch (error) {
226 // If it throws, that's also acceptable behavior
227 assert.ok(error instanceof Error);
228 }
229 });
230
235 test('should handle too many parameters gracefully', () => {
236 // Providing more parameters than expected should not break
237 const result = getMessage("fileRefreshed", "extra", "parameters", "should", "be", "ignored");
238 assert.strictEqual(typeof result, 'string');
239 assert.strictEqual(result, "Refreshing file content.");
240 });
241
246 test('should handle too few parameters gracefully', () => {
247 // This might result in undefined substitution, but should not crash
248 const result = getMessage("filePathUpdated", "/only/one/param");
249 assert.strictEqual(typeof result, 'string');
250 assert.ok(result.includes("/only/one/param"));
251 });
252 });
253
258 suite('Message Content Validation', () => {
263 test('should have meaningful error messages', () => {
264 const filePath = '/test/file.json';
265 const error = 'Syntax error at line 5';
266
267 const parseError = getMessage("fileParseError", filePath, error);
268 assert.ok(parseError.includes(filePath), 'Parse error should include file path');
269 assert.ok(parseError.includes(error), 'Parse error should include error details');
270 assert.ok(parseError.toLowerCase().includes('error'), 'Parse error should mention error');
271 });
272
277 test('should have informative status messages', () => {
278 const absolutePath = '/absolute/path/to/file.json';
279 const loadedMsg = getMessage("fileLoaded", absolutePath);
280
281 assert.ok(loadedMsg.includes(absolutePath), 'Loaded message should include file path');
282 assert.ok(loadedMsg.toLowerCase().includes('loaded'), 'Loaded message should mention loaded');
283
284 const refreshMsg = getMessage("fileRefreshed");
285 assert.ok(refreshMsg.toLowerCase().includes('refresh'), 'Refresh message should mention refresh');
286 });
287
292 test('should have clear directory change messages', () => {
293 const oldCwd = '/old/directory';
294 const newCwd = '/new/directory';
295
296 const cwdUpdate = getMessage("cwdUpdated", oldCwd, newCwd);
297 assert.ok(cwdUpdate.includes(oldCwd), 'CWD update should include old directory');
298 assert.ok(cwdUpdate.includes(newCwd), 'CWD update should include new directory');
299 assert.ok(cwdUpdate.toLowerCase().includes('updated'), 'CWD update should mention update');
300
301 const nonExistentPath = '/nonexistent/path';
302 const cwdError = getMessage("cwdDoesNotExist", nonExistentPath);
303 assert.ok(cwdError.includes(nonExistentPath), 'CWD error should include path');
304 });
305 });
306
311 suite('Integration and Consistency', () => {
316 test('should maintain consistent message formatting', () => {
317 // Test that similar messages follow consistent patterns
318 const loadedMsg = getMessage("fileLoaded", "/test/file");
319 const unloadedMsg = getMessage("fileUnloaded", "/test/file");
320
321 // Check actual message format - loaded ends with '!', unloaded may not
322 assert.ok(loadedMsg.endsWith('!') || loadedMsg.endsWith('.'), 'Loaded message should end with punctuation');
323 // Adjust expectation for unloaded message based on actual implementation
324 assert.ok(typeof unloadedMsg === 'string' && unloadedMsg.length > 0, 'Unloaded message should be a valid string');
325 });
326
331 test('should handle concurrent message generation', () => {
332 // Test that multiple simultaneous getMessage calls work correctly
333 const promises = Array.from({ length: 100 }, (_, i) => {
334 return new Promise<string>((resolve) => {
335 const message = getMessage("fileLoaded", `/test/file${i}.json`);
336 resolve(message);
337 });
338 });
339
340 return Promise.all(promises).then(results => {
341 assert.strictEqual(results.length, 100);
342 results.forEach((message, index) => {
343 assert.ok(message.includes(`file${index}.json`));
344 });
345 });
346 });
347
352 test('should support message caching if implemented', () => {
353 // Test repeated calls to the same message
354 const message1 = getMessage("fileRefreshed");
355 const message2 = getMessage("fileRefreshed");
356
357 // Should return identical results
358 assert.strictEqual(message1, message2);
359
360 // Test with parameters
361 const paramMsg1 = getMessage("fileLoaded", "/same/path");
362 const paramMsg2 = getMessage("fileLoaded", "/same/path");
363 assert.strictEqual(paramMsg1, paramMsg2);
364 });
365 });
366
371 suite('Performance and Memory', () => {
376 test('should handle large parameter strings efficiently', () => {
377 const largePath = '/very/long/path/with/many/segments/'.repeat(100) + 'file.json';
378 const startTime = Date.now();
379
380 const result = getMessage("fileLoaded", largePath);
381 const elapsed = Date.now() - startTime;
382
383 assert.ok(elapsed < 100, 'Message generation should complete quickly even with large parameters');
384 assert.ok(result.includes(largePath));
385 });
386
391 test('should handle rapid successive calls efficiently', () => {
392 const startTime = Date.now();
393
394 for (let i = 0; i < 1000; i++) {
395 getMessage("fileRefreshed");
396 getMessage("fileLoaded", `/test/file${i}.json`);
397 }
398
399 const elapsed = Date.now() - startTime;
400 assert.ok(elapsed < 1000, 'Rapid message generation should complete within reasonable time');
401 });
402 });
403});
import path from path
Definition esbuild.js:10
import *as fsp from fs promises
import *as assert from assert
export const getMessage
Exported function for direct message retrieval.
export const messages
Complete message dictionary for all supported languages with type-safe function interfaces @export Ex...