|
| 1 | +import { describe, it, before, beforeEach } from 'node:test' |
| 2 | +import assert from 'node:assert/strict' |
| 3 | +import LangModule from '../lib/LangModule.js' |
| 4 | + |
| 5 | +describe('LangModule', () => { |
| 6 | + let instance |
| 7 | + |
| 8 | + before(() => { |
| 9 | + // Create a minimal mock for AbstractModule dependencies |
| 10 | + instance = new LangModule() |
| 11 | + // Mock the required app structure |
| 12 | + instance.app = { |
| 13 | + name: 'test-app', |
| 14 | + dependencies: {}, |
| 15 | + errors: { |
| 16 | + UNKNOWN_LANG: { setData: (data) => ({ data }) } |
| 17 | + } |
| 18 | + } |
| 19 | + // Mock log method |
| 20 | + instance.log = () => {} |
| 21 | + // Mock getConfig method |
| 22 | + instance.getConfig = (key) => { |
| 23 | + if (key === 'defaultLang') return 'en' |
| 24 | + return undefined |
| 25 | + } |
| 26 | + }) |
| 27 | + |
| 28 | + describe('#supportedLanguages', () => { |
| 29 | + beforeEach(() => { |
| 30 | + instance.phrases = {} |
| 31 | + }) |
| 32 | + |
| 33 | + it('should return empty array when no phrases loaded', () => { |
| 34 | + assert.deepEqual(instance.supportedLanguages, []) |
| 35 | + }) |
| 36 | + |
| 37 | + it('should return array of language keys', () => { |
| 38 | + instance.phrases = { |
| 39 | + en: { 'app.test': 'Test' }, |
| 40 | + fr: { 'app.test': 'Test FR' } |
| 41 | + } |
| 42 | + assert.deepEqual(instance.supportedLanguages, ['en', 'fr']) |
| 43 | + }) |
| 44 | + |
| 45 | + it('should return single language', () => { |
| 46 | + instance.phrases = { |
| 47 | + de: { 'app.test': 'Test DE' } |
| 48 | + } |
| 49 | + assert.deepEqual(instance.supportedLanguages, ['de']) |
| 50 | + }) |
| 51 | + }) |
| 52 | + |
| 53 | + describe('#storeStrings()', () => { |
| 54 | + beforeEach(() => { |
| 55 | + instance.phrases = {} |
| 56 | + }) |
| 57 | + |
| 58 | + it('should store string with language prefix', () => { |
| 59 | + instance.storeStrings('en.app.test', 'Test Value') |
| 60 | + assert.equal(instance.phrases.en['app.test'], 'Test Value') |
| 61 | + }) |
| 62 | + |
| 63 | + it('should create language key if not exists', () => { |
| 64 | + instance.storeStrings('fr.app.hello', 'Bonjour') |
| 65 | + assert.ok(instance.phrases.fr) |
| 66 | + assert.equal(instance.phrases.fr['app.hello'], 'Bonjour') |
| 67 | + }) |
| 68 | + |
| 69 | + it('should store multiple strings for same language', () => { |
| 70 | + instance.storeStrings('en.app.test1', 'Value 1') |
| 71 | + instance.storeStrings('en.app.test2', 'Value 2') |
| 72 | + assert.equal(instance.phrases.en['app.test1'], 'Value 1') |
| 73 | + assert.equal(instance.phrases.en['app.test2'], 'Value 2') |
| 74 | + }) |
| 75 | + |
| 76 | + it('should handle nested keys with dots', () => { |
| 77 | + instance.storeStrings('en.app.nested.key', 'Nested Value') |
| 78 | + assert.equal(instance.phrases.en['app.nested.key'], 'Nested Value') |
| 79 | + }) |
| 80 | + }) |
| 81 | + |
| 82 | + describe('#translate()', () => { |
| 83 | + beforeEach(() => { |
| 84 | + instance.phrases = { |
| 85 | + en: { |
| 86 | + 'app.simple': 'Simple text', |
| 87 | + 'app.withdata': 'Hello $' + '{name}', |
| 88 | + 'app.multiple': 'User $' + '{user} has $' + '{count} items', |
| 89 | + 'app.array': 'Items: $' + '{items}', |
| 90 | + 'app.arraymap': 'Names: $map{users:name:, }', |
| 91 | + 'error.TEST_ERROR': 'Test error message' |
| 92 | + }, |
| 93 | + fr: { |
| 94 | + 'app.simple': 'Texte simple', |
| 95 | + 'app.withdata': 'Bonjour $' + '{name}' |
| 96 | + } |
| 97 | + } |
| 98 | + }) |
| 99 | + |
| 100 | + it('should return simple translated string', () => { |
| 101 | + const result = instance.translate('en', 'app.simple') |
| 102 | + assert.equal(result, 'Simple text') |
| 103 | + }) |
| 104 | + |
| 105 | + it('should return string in specified language', () => { |
| 106 | + const result = instance.translate('fr', 'app.simple') |
| 107 | + assert.equal(result, 'Texte simple') |
| 108 | + }) |
| 109 | + |
| 110 | + it('should return key if translation not found', () => { |
| 111 | + const result = instance.translate('en', 'app.missing') |
| 112 | + assert.equal(result, 'app.missing') |
| 113 | + }) |
| 114 | + |
| 115 | + it('should use default language if lang not provided as string', () => { |
| 116 | + const result = instance.translate(null, 'app.simple') |
| 117 | + assert.equal(result, 'Simple text') |
| 118 | + }) |
| 119 | + |
| 120 | + it('should replace single placeholder with data', () => { |
| 121 | + const result = instance.translate('en', 'app.withdata', { name: 'John' }) |
| 122 | + assert.equal(result, 'Hello John') |
| 123 | + }) |
| 124 | + |
| 125 | + it('should replace multiple placeholders with data', () => { |
| 126 | + const result = instance.translate('en', 'app.multiple', { user: 'Alice', count: 5 }) |
| 127 | + assert.equal(result, 'User Alice has 5 items') |
| 128 | + }) |
| 129 | + |
| 130 | + it('should handle missing data gracefully', () => { |
| 131 | + const result = instance.translate('en', 'app.withdata', {}) |
| 132 | + assert.equal(result, 'Hello $' + '{name}') |
| 133 | + }) |
| 134 | + |
| 135 | + it('should replace array placeholders', () => { |
| 136 | + const result = instance.translate('en', 'app.array', { items: ['a', 'b', 'c'] }) |
| 137 | + assert.ok(result.includes('a')) |
| 138 | + assert.ok(result.includes('b')) |
| 139 | + assert.ok(result.includes('c')) |
| 140 | + }) |
| 141 | + |
| 142 | + it('should handle $map syntax with arrays of objects', () => { |
| 143 | + const users = [ |
| 144 | + { name: 'Alice', age: 30 }, |
| 145 | + { name: 'Bob', age: 25 } |
| 146 | + ] |
| 147 | + const result = instance.translate('en', 'app.arraymap', { users }) |
| 148 | + assert.equal(result, 'Names: Alice, Bob') |
| 149 | + }) |
| 150 | + |
| 151 | + it('should translate error objects by calling translateError', () => { |
| 152 | + const mockError = { |
| 153 | + constructor: { name: 'AdaptError' }, |
| 154 | + code: 'TEST_ERROR', |
| 155 | + data: {} |
| 156 | + } |
| 157 | + const result = instance.translate('en', mockError) |
| 158 | + assert.equal(result, 'Test error message') |
| 159 | + }) |
| 160 | + }) |
| 161 | + |
| 162 | + describe('#translateError()', () => { |
| 163 | + beforeEach(() => { |
| 164 | + instance.phrases = { |
| 165 | + en: { |
| 166 | + 'error.TEST_CODE': 'Error: $' + '{message}', |
| 167 | + 'error.SIMPLE': 'Simple error' |
| 168 | + } |
| 169 | + } |
| 170 | + }) |
| 171 | + |
| 172 | + it('should translate error with code', () => { |
| 173 | + const error = { |
| 174 | + constructor: { name: 'AdaptError' }, |
| 175 | + code: 'SIMPLE', |
| 176 | + data: {} |
| 177 | + } |
| 178 | + const result = instance.translateError('en', error) |
| 179 | + assert.equal(result, 'Simple error') |
| 180 | + }) |
| 181 | + |
| 182 | + it('should translate error with data', () => { |
| 183 | + const error = { |
| 184 | + constructor: { name: 'TestError' }, |
| 185 | + code: 'TEST_CODE', |
| 186 | + data: { message: 'Something went wrong' } |
| 187 | + } |
| 188 | + const result = instance.translateError('en', error) |
| 189 | + assert.equal(result, 'Error: Something went wrong') |
| 190 | + }) |
| 191 | + |
| 192 | + it('should return non-error values unchanged', () => { |
| 193 | + const result = instance.translateError('en', 'just a string') |
| 194 | + assert.equal(result, 'just a string') |
| 195 | + }) |
| 196 | + |
| 197 | + it('should return null unchanged', () => { |
| 198 | + const result = instance.translateError('en', null) |
| 199 | + assert.equal(result, null) |
| 200 | + }) |
| 201 | + |
| 202 | + it('should return undefined unchanged', () => { |
| 203 | + const result = instance.translateError('en', undefined) |
| 204 | + assert.equal(result, undefined) |
| 205 | + }) |
| 206 | + }) |
| 207 | + |
| 208 | + describe('#getPhrasesForLang()', () => { |
| 209 | + it('should return undefined when phrases structure does not match expected format', () => { |
| 210 | + // The current structure is { lang: { key: value } } |
| 211 | + // but getPhrasesForLang expects keys like 'lang.key' |
| 212 | + instance.phrases = { |
| 213 | + en: { |
| 214 | + 'app.test1': 'Test 1', |
| 215 | + 'app.test2': 'Test 2' |
| 216 | + } |
| 217 | + } |
| 218 | + const result = instance.getPhrasesForLang('en') |
| 219 | + // Returns undefined because the structure doesn't match what the method expects |
| 220 | + assert.equal(result, undefined) |
| 221 | + }) |
| 222 | + |
| 223 | + it('should work with flat key structure if provided', () => { |
| 224 | + // If phrases were structured as expected by this method |
| 225 | + instance.phrases = { |
| 226 | + 'en.app.test1': 'Test 1', |
| 227 | + 'en.app.test2': 'Test 2', |
| 228 | + 'en.error.ERR1': 'Error 1', |
| 229 | + 'fr.app.test': 'Test FR' |
| 230 | + } |
| 231 | + const result = instance.getPhrasesForLang('en') |
| 232 | + assert.ok(result) |
| 233 | + assert.equal(Object.keys(result).length, 3) |
| 234 | + assert.equal(result['app.test1'], 'Test 1') |
| 235 | + assert.equal(result['app.test2'], 'Test 2') |
| 236 | + assert.equal(result['error.ERR1'], 'Error 1') |
| 237 | + }) |
| 238 | + |
| 239 | + it('should return undefined for language with only one phrase', () => { |
| 240 | + instance.phrases = { |
| 241 | + 'en.app.test': 'Test' |
| 242 | + } |
| 243 | + const result = instance.getPhrasesForLang('en') |
| 244 | + assert.equal(result, undefined) |
| 245 | + }) |
| 246 | + |
| 247 | + it('should return undefined for non-existent language', () => { |
| 248 | + instance.phrases = { |
| 249 | + 'en.app.test1': 'Test 1', |
| 250 | + 'en.app.test2': 'Test 2' |
| 251 | + } |
| 252 | + const result = instance.getPhrasesForLang('de') |
| 253 | + assert.equal(result, undefined) |
| 254 | + }) |
| 255 | + }) |
| 256 | +}) |
0 commit comments