auto-translate-json-library 2.0.2 → 2.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.MD +240 -3
- package/build/package.json +30 -16
- package/build/src/bin/cli.d.ts +1 -1
- package/build/src/bin/cli.js +82 -53
- package/build/src/config.d.ts +12 -1
- package/build/src/files.js +2 -2
- package/build/src/huggingface-local.d.ts +11 -0
- package/build/src/huggingface-local.js +71 -0
- package/build/src/huggingface.d.ts +12 -0
- package/build/src/huggingface.js +76 -0
- package/build/src/index.js +17 -0
- package/build/src/lib.js +9 -1
- package/build/src/openai.js +1 -0
- package/package.json +30 -16
- package/build/tests/arb-handler.test.d.ts +0 -1
- package/build/tests/arb-handler.test.js +0 -694
- package/build/tests/cli-integration.test.d.ts +0 -1
- package/build/tests/cli-integration.test.js +0 -352
- package/build/tests/csv-handler.test.d.ts +0 -1
- package/build/tests/csv-handler.test.js +0 -507
- package/build/tests/error-handling.test.d.ts +0 -1
- package/build/tests/error-handling.test.js +0 -456
- package/build/tests/files-integration.test.d.ts +0 -1
- package/build/tests/files-integration.test.js +0 -421
- package/build/tests/json-handler.test.d.ts +0 -1
- package/build/tests/json-handler.test.js +0 -526
- package/build/tests/po-handler.test.d.ts +0 -1
- package/build/tests/po-handler.test.js +0 -768
- package/build/tests/properties-handler.test.d.ts +0 -1
- package/build/tests/properties-handler.test.js +0 -316
- package/build/tests/setup.d.ts +0 -1
- package/build/tests/setup.js +0 -5
- package/build/tests/xliff-handler.test.d.ts +0 -1
- package/build/tests/xliff-handler.test.js +0 -459
- package/build/tests/xmb-xtb-handler.test.d.ts +0 -1
- package/build/tests/xmb-xtb-handler.test.js +0 -758
- package/build/tests/xml-handler.test.d.ts +0 -1
- package/build/tests/xml-handler.test.js +0 -570
- package/build/tests/yaml-handler.test.d.ts +0 -1
- package/build/tests/yaml-handler.test.js +0 -156
|
@@ -1,526 +0,0 @@
|
|
|
1
|
-
import { JsonHandler } from '../src/format/json-handler.js';
|
|
2
|
-
import { fileURLToPath } from 'url';
|
|
3
|
-
import { dirname } from 'path';
|
|
4
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
5
|
-
const __dirname = dirname(__filename);
|
|
6
|
-
import * as fs from 'fs';
|
|
7
|
-
import * as path from 'path';
|
|
8
|
-
describe('JsonHandler', () => {
|
|
9
|
-
let handler;
|
|
10
|
-
beforeEach(() => {
|
|
11
|
-
handler = new JsonHandler();
|
|
12
|
-
});
|
|
13
|
-
describe('canHandle', () => {
|
|
14
|
-
it('should handle .json files', () => {
|
|
15
|
-
expect(handler.canHandle('test.json')).toBe(true);
|
|
16
|
-
expect(handler.canHandle('translations.json')).toBe(true);
|
|
17
|
-
});
|
|
18
|
-
it('should not handle non-json files', () => {
|
|
19
|
-
expect(handler.canHandle('test.xml')).toBe(false);
|
|
20
|
-
expect(handler.canHandle('test.txt')).toBe(false);
|
|
21
|
-
});
|
|
22
|
-
it('should validate JSON content when provided', () => {
|
|
23
|
-
expect(handler.canHandle('test.json', '{"key": "value"}')).toBe(true);
|
|
24
|
-
expect(handler.canHandle('test.json', 'invalid json')).toBe(false);
|
|
25
|
-
});
|
|
26
|
-
});
|
|
27
|
-
describe('flat JSON parsing', () => {
|
|
28
|
-
const flatJson = `{
|
|
29
|
-
"app_name": "My Application",
|
|
30
|
-
"welcome_message": "Welcome to our app!",
|
|
31
|
-
"button_text": "Click here",
|
|
32
|
-
"error_message": "Something went wrong"
|
|
33
|
-
}`;
|
|
34
|
-
it('should parse flat JSON correctly', () => {
|
|
35
|
-
const result = handler.parse(flatJson);
|
|
36
|
-
expect(result.app_name).toBe('My Application');
|
|
37
|
-
expect(result.welcome_message).toBe('Welcome to our app!');
|
|
38
|
-
expect(result.button_text).toBe('Click here');
|
|
39
|
-
expect(result.error_message).toBe('Something went wrong');
|
|
40
|
-
});
|
|
41
|
-
it('should preserve metadata for flat JSON', () => {
|
|
42
|
-
const result = handler.parse(flatJson);
|
|
43
|
-
expect(result._metadata).toBeDefined();
|
|
44
|
-
expect(result._metadata?.format).toBe('json');
|
|
45
|
-
expect(result._metadata?.originalStructure).toBeDefined();
|
|
46
|
-
expect(result._metadata?.preserveComments).toBe(false);
|
|
47
|
-
expect(result._metadata?.preserveAttributes).toBe(false);
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
describe('nested JSON parsing', () => {
|
|
51
|
-
const nestedJson = `{
|
|
52
|
-
"app": {
|
|
53
|
-
"name": "My Application",
|
|
54
|
-
"version": "1.0.0"
|
|
55
|
-
},
|
|
56
|
-
"messages": {
|
|
57
|
-
"welcome": "Welcome to our app!",
|
|
58
|
-
"errors": {
|
|
59
|
-
"network": "Network connection failed",
|
|
60
|
-
"validation": "Please check your input"
|
|
61
|
-
}
|
|
62
|
-
},
|
|
63
|
-
"buttons": {
|
|
64
|
-
"submit": "Submit",
|
|
65
|
-
"cancel": "Cancel"
|
|
66
|
-
}
|
|
67
|
-
}`;
|
|
68
|
-
it('should flatten nested JSON structure correctly', () => {
|
|
69
|
-
const result = handler.parse(nestedJson);
|
|
70
|
-
expect(result['app.name']).toBe('My Application');
|
|
71
|
-
expect(result['app.version']).toBe('1.0.0');
|
|
72
|
-
expect(result['messages.welcome']).toBe('Welcome to our app!');
|
|
73
|
-
expect(result['messages.errors.network']).toBe('Network connection failed');
|
|
74
|
-
expect(result['messages.errors.validation']).toBe('Please check your input');
|
|
75
|
-
expect(result['buttons.submit']).toBe('Submit');
|
|
76
|
-
expect(result['buttons.cancel']).toBe('Cancel');
|
|
77
|
-
});
|
|
78
|
-
it('should preserve original nested structure in metadata', () => {
|
|
79
|
-
const result = handler.parse(nestedJson);
|
|
80
|
-
expect(result._metadata?.originalStructure).toBeDefined();
|
|
81
|
-
expect(result._metadata?.originalStructure.app).toBeDefined();
|
|
82
|
-
expect(result._metadata?.originalStructure.messages).toBeDefined();
|
|
83
|
-
expect(result._metadata?.originalStructure.messages.errors).toBeDefined();
|
|
84
|
-
});
|
|
85
|
-
it('should serialize back to nested structure correctly', () => {
|
|
86
|
-
const translationData = {
|
|
87
|
-
'app.name': 'Mi Aplicación',
|
|
88
|
-
'app.version': '1.0.0',
|
|
89
|
-
'messages.welcome': '¡Bienvenido a nuestra aplicación!',
|
|
90
|
-
'messages.errors.network': 'Falló la conexión de red',
|
|
91
|
-
'messages.errors.validation': 'Por favor verifica tu entrada',
|
|
92
|
-
'buttons.submit': 'Enviar',
|
|
93
|
-
'buttons.cancel': 'Cancelar',
|
|
94
|
-
_metadata: {
|
|
95
|
-
format: 'json',
|
|
96
|
-
originalStructure: {
|
|
97
|
-
app: { name: 'My Application', version: '1.0.0' },
|
|
98
|
-
messages: {
|
|
99
|
-
welcome: 'Welcome to our app!',
|
|
100
|
-
errors: {
|
|
101
|
-
network: 'Network connection failed',
|
|
102
|
-
validation: 'Please check your input'
|
|
103
|
-
}
|
|
104
|
-
},
|
|
105
|
-
buttons: { submit: 'Submit', cancel: 'Cancel' }
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
|
-
const result = handler.serialize(translationData);
|
|
110
|
-
const parsed = JSON.parse(result);
|
|
111
|
-
expect(parsed.app.name).toBe('Mi Aplicación');
|
|
112
|
-
expect(parsed.app.version).toBe('1.0.0');
|
|
113
|
-
expect(parsed.messages.welcome).toBe('¡Bienvenido a nuestra aplicación!');
|
|
114
|
-
expect(parsed.messages.errors.network).toBe('Falló la conexión de red');
|
|
115
|
-
expect(parsed.messages.errors.validation).toBe('Por favor verifica tu entrada');
|
|
116
|
-
expect(parsed.buttons.submit).toBe('Enviar');
|
|
117
|
-
expect(parsed.buttons.cancel).toBe('Cancelar');
|
|
118
|
-
});
|
|
119
|
-
});
|
|
120
|
-
describe('array handling', () => {
|
|
121
|
-
const arrayJson = `{
|
|
122
|
-
"items": [
|
|
123
|
-
"First item",
|
|
124
|
-
"Second item",
|
|
125
|
-
"Third item"
|
|
126
|
-
],
|
|
127
|
-
"complex_items": [
|
|
128
|
-
{
|
|
129
|
-
"title": "Item 1",
|
|
130
|
-
"description": "Description 1"
|
|
131
|
-
},
|
|
132
|
-
{
|
|
133
|
-
"title": "Item 2",
|
|
134
|
-
"description": "Description 2"
|
|
135
|
-
}
|
|
136
|
-
],
|
|
137
|
-
"mixed_array": [
|
|
138
|
-
"String item",
|
|
139
|
-
42,
|
|
140
|
-
true,
|
|
141
|
-
{
|
|
142
|
-
"nested": "Nested value"
|
|
143
|
-
}
|
|
144
|
-
]
|
|
145
|
-
}`;
|
|
146
|
-
it('should handle arrays correctly', () => {
|
|
147
|
-
const result = handler.parse(arrayJson);
|
|
148
|
-
expect(result['items[0]']).toBe('First item');
|
|
149
|
-
expect(result['items[1]']).toBe('Second item');
|
|
150
|
-
expect(result['items[2]']).toBe('Third item');
|
|
151
|
-
expect(result['complex_items[0].title']).toBe('Item 1');
|
|
152
|
-
expect(result['complex_items[0].description']).toBe('Description 1');
|
|
153
|
-
expect(result['complex_items[1].title']).toBe('Item 2');
|
|
154
|
-
expect(result['complex_items[1].description']).toBe('Description 2');
|
|
155
|
-
expect(result['mixed_array[0]']).toBe('String item');
|
|
156
|
-
expect(result['mixed_array[1]']).toBe(42);
|
|
157
|
-
expect(result['mixed_array[2]']).toBe(true);
|
|
158
|
-
expect(result['mixed_array[3].nested']).toBe('Nested value');
|
|
159
|
-
});
|
|
160
|
-
it('should serialize arrays back correctly', () => {
|
|
161
|
-
const translationData = {
|
|
162
|
-
'items[0]': 'Primer elemento',
|
|
163
|
-
'items[1]': 'Segundo elemento',
|
|
164
|
-
'items[2]': 'Tercer elemento',
|
|
165
|
-
'complex_items[0].title': 'Elemento 1',
|
|
166
|
-
'complex_items[0].description': 'Descripción 1',
|
|
167
|
-
'complex_items[1].title': 'Elemento 2',
|
|
168
|
-
'complex_items[1].description': 'Descripción 2',
|
|
169
|
-
_metadata: {
|
|
170
|
-
format: 'json',
|
|
171
|
-
originalStructure: {
|
|
172
|
-
items: ['First item', 'Second item', 'Third item'],
|
|
173
|
-
complex_items: [
|
|
174
|
-
{ title: 'Item 1', description: 'Description 1' },
|
|
175
|
-
{ title: 'Item 2', description: 'Description 2' }
|
|
176
|
-
]
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
};
|
|
180
|
-
const result = handler.serialize(translationData);
|
|
181
|
-
const parsed = JSON.parse(result);
|
|
182
|
-
expect(parsed.items).toHaveLength(3);
|
|
183
|
-
expect(parsed.items[0]).toBe('Primer elemento');
|
|
184
|
-
expect(parsed.items[1]).toBe('Segundo elemento');
|
|
185
|
-
expect(parsed.items[2]).toBe('Tercer elemento');
|
|
186
|
-
expect(parsed.complex_items[0].title).toBe('Elemento 1');
|
|
187
|
-
expect(parsed.complex_items[0].description).toBe('Descripción 1');
|
|
188
|
-
expect(parsed.complex_items[1].title).toBe('Elemento 2');
|
|
189
|
-
expect(parsed.complex_items[1].description).toBe('Descripción 2');
|
|
190
|
-
});
|
|
191
|
-
});
|
|
192
|
-
describe('metadata preservation', () => {
|
|
193
|
-
const jsonWithMixedTypes = `{
|
|
194
|
-
"strings": {
|
|
195
|
-
"title": "My Title",
|
|
196
|
-
"description": "My Description"
|
|
197
|
-
},
|
|
198
|
-
"numbers": {
|
|
199
|
-
"count": 42,
|
|
200
|
-
"price": 19.99
|
|
201
|
-
},
|
|
202
|
-
"booleans": {
|
|
203
|
-
"enabled": true,
|
|
204
|
-
"visible": false
|
|
205
|
-
},
|
|
206
|
-
"null_value": null
|
|
207
|
-
}`;
|
|
208
|
-
it('should preserve non-string values during parsing', () => {
|
|
209
|
-
const result = handler.parse(jsonWithMixedTypes);
|
|
210
|
-
expect(result['strings.title']).toBe('My Title');
|
|
211
|
-
expect(result['strings.description']).toBe('My Description');
|
|
212
|
-
expect(result['numbers.count']).toBe(42);
|
|
213
|
-
expect(result['numbers.price']).toBe(19.99);
|
|
214
|
-
expect(result['booleans.enabled']).toBe(true);
|
|
215
|
-
expect(result['booleans.visible']).toBe(false);
|
|
216
|
-
expect(result['null_value']).toBe(null);
|
|
217
|
-
});
|
|
218
|
-
it('should preserve non-string values during serialization', () => {
|
|
219
|
-
const translationData = {
|
|
220
|
-
'strings.title': 'Mi Título',
|
|
221
|
-
'strings.description': 'Mi Descripción',
|
|
222
|
-
'numbers.count': 42,
|
|
223
|
-
'numbers.price': 19.99,
|
|
224
|
-
'booleans.enabled': true,
|
|
225
|
-
'booleans.visible': false,
|
|
226
|
-
'null_value': null,
|
|
227
|
-
_metadata: {
|
|
228
|
-
format: 'json',
|
|
229
|
-
originalStructure: {
|
|
230
|
-
strings: { title: 'My Title', description: 'My Description' },
|
|
231
|
-
numbers: { count: 42, price: 19.99 },
|
|
232
|
-
booleans: { enabled: true, visible: false },
|
|
233
|
-
null_value: null
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
};
|
|
237
|
-
const result = handler.serialize(translationData);
|
|
238
|
-
const parsed = JSON.parse(result);
|
|
239
|
-
expect(parsed.strings.title).toBe('Mi Título');
|
|
240
|
-
expect(parsed.strings.description).toBe('Mi Descripción');
|
|
241
|
-
expect(parsed.numbers.count).toBe(42);
|
|
242
|
-
expect(parsed.numbers.price).toBe(19.99);
|
|
243
|
-
expect(parsed.booleans.enabled).toBe(true);
|
|
244
|
-
expect(parsed.booleans.visible).toBe(false);
|
|
245
|
-
expect(parsed.null_value).toBe(null);
|
|
246
|
-
});
|
|
247
|
-
});
|
|
248
|
-
describe('validation', () => {
|
|
249
|
-
it('should validate correct JSON structure', () => {
|
|
250
|
-
const validData = {
|
|
251
|
-
title: 'Test Title',
|
|
252
|
-
nested: {
|
|
253
|
-
message: 'Test Message'
|
|
254
|
-
}
|
|
255
|
-
};
|
|
256
|
-
const result = handler.validateStructure(validData);
|
|
257
|
-
expect(result.isValid).toBe(true);
|
|
258
|
-
expect(result.errors).toHaveLength(0);
|
|
259
|
-
});
|
|
260
|
-
it('should detect invalid JSON structure', () => {
|
|
261
|
-
const invalidData = 'not an object';
|
|
262
|
-
const result = handler.validateStructure(invalidData);
|
|
263
|
-
expect(result.isValid).toBe(false);
|
|
264
|
-
expect(result.errors.length).toBeGreaterThan(0);
|
|
265
|
-
expect(result.errors[0].code).toBe('INVALID_STRUCTURE');
|
|
266
|
-
});
|
|
267
|
-
it('should warn about non-translatable values', () => {
|
|
268
|
-
const dataWithNonStrings = {
|
|
269
|
-
title: 'Translatable string',
|
|
270
|
-
count: 42,
|
|
271
|
-
enabled: true,
|
|
272
|
-
config: {
|
|
273
|
-
timeout: 5000,
|
|
274
|
-
message: 'Another translatable string'
|
|
275
|
-
}
|
|
276
|
-
};
|
|
277
|
-
const result = handler.validateStructure(dataWithNonStrings);
|
|
278
|
-
expect(result.isValid).toBe(true);
|
|
279
|
-
expect(result.warnings.length).toBeGreaterThan(0);
|
|
280
|
-
const nonTranslatableWarnings = result.warnings.filter(w => w.code === 'NON_TRANSLATABLE_VALUE');
|
|
281
|
-
expect(nonTranslatableWarnings.length).toBeGreaterThan(0);
|
|
282
|
-
});
|
|
283
|
-
it('should handle empty JSON', () => {
|
|
284
|
-
const emptyData = {};
|
|
285
|
-
const result = handler.validateStructure(emptyData);
|
|
286
|
-
expect(result.isValid).toBe(true);
|
|
287
|
-
expect(result.warnings.length).toBeGreaterThan(0);
|
|
288
|
-
expect(result.warnings[0].code).toBe('EMPTY_JSON');
|
|
289
|
-
});
|
|
290
|
-
it('should detect circular references', () => {
|
|
291
|
-
const circularData = { name: 'test' };
|
|
292
|
-
circularData.self = circularData;
|
|
293
|
-
const result = handler.validateStructure(circularData);
|
|
294
|
-
expect(result.isValid).toBe(false);
|
|
295
|
-
expect(result.errors.length).toBeGreaterThan(0);
|
|
296
|
-
expect(result.errors[0].code).toBe('CIRCULAR_REFERENCE');
|
|
297
|
-
});
|
|
298
|
-
});
|
|
299
|
-
describe('formatting options', () => {
|
|
300
|
-
it('should apply custom indentation', () => {
|
|
301
|
-
const data = {
|
|
302
|
-
title: 'Test Title',
|
|
303
|
-
message: 'Test Message'
|
|
304
|
-
};
|
|
305
|
-
const result = handler.serialize(data, { indentation: 4 });
|
|
306
|
-
expect(result).toContain(' "title"');
|
|
307
|
-
expect(result).toContain(' "message"');
|
|
308
|
-
});
|
|
309
|
-
it('should use string indentation', () => {
|
|
310
|
-
const data = {
|
|
311
|
-
title: 'Test Title',
|
|
312
|
-
message: 'Test Message'
|
|
313
|
-
};
|
|
314
|
-
const result = handler.serialize(data, { indentation: '\t' });
|
|
315
|
-
expect(result).toContain('\t"title"');
|
|
316
|
-
expect(result).toContain('\t"message"');
|
|
317
|
-
});
|
|
318
|
-
it('should use default indentation when not specified', () => {
|
|
319
|
-
const data = {
|
|
320
|
-
title: 'Test Title'
|
|
321
|
-
};
|
|
322
|
-
const result = handler.serialize(data);
|
|
323
|
-
// Default should be 2 spaces
|
|
324
|
-
expect(result).toContain(' "title"');
|
|
325
|
-
});
|
|
326
|
-
});
|
|
327
|
-
describe('error handling', () => {
|
|
328
|
-
it('should throw error for invalid JSON content', () => {
|
|
329
|
-
const invalidJson = '{ invalid json content }';
|
|
330
|
-
expect(() => handler.parse(invalidJson)).toThrow('Failed to parse JSON');
|
|
331
|
-
});
|
|
332
|
-
it('should throw error for malformed JSON', () => {
|
|
333
|
-
const malformedJson = '{"key": "value",}'; // trailing comma
|
|
334
|
-
expect(() => handler.parse(malformedJson)).toThrow('Failed to parse JSON');
|
|
335
|
-
});
|
|
336
|
-
});
|
|
337
|
-
describe('getFileExtension', () => {
|
|
338
|
-
it('should return .json extension', () => {
|
|
339
|
-
expect(handler.getFileExtension()).toBe('.json');
|
|
340
|
-
});
|
|
341
|
-
});
|
|
342
|
-
describe('reconstruction from flat structure', () => {
|
|
343
|
-
it('should reconstruct nested structure from dot notation', () => {
|
|
344
|
-
const flatData = {
|
|
345
|
-
'app.name': 'Test App',
|
|
346
|
-
'app.version': '1.0.0',
|
|
347
|
-
'messages.welcome': 'Welcome!',
|
|
348
|
-
'messages.errors.network': 'Network error'
|
|
349
|
-
};
|
|
350
|
-
const result = handler.serialize(flatData);
|
|
351
|
-
const parsed = JSON.parse(result);
|
|
352
|
-
expect(parsed.app.name).toBe('Test App');
|
|
353
|
-
expect(parsed.app.version).toBe('1.0.0');
|
|
354
|
-
expect(parsed.messages.welcome).toBe('Welcome!');
|
|
355
|
-
expect(parsed.messages.errors.network).toBe('Network error');
|
|
356
|
-
});
|
|
357
|
-
it('should reconstruct arrays from bracket notation', () => {
|
|
358
|
-
const flatData = {
|
|
359
|
-
'items[0]': 'First',
|
|
360
|
-
'items[1]': 'Second',
|
|
361
|
-
'complex[0].title': 'Title 1',
|
|
362
|
-
'complex[0].desc': 'Description 1',
|
|
363
|
-
'complex[1].title': 'Title 2'
|
|
364
|
-
};
|
|
365
|
-
const result = handler.serialize(flatData);
|
|
366
|
-
const parsed = JSON.parse(result);
|
|
367
|
-
expect(parsed.items).toHaveLength(2);
|
|
368
|
-
expect(parsed.items[0]).toBe('First');
|
|
369
|
-
expect(parsed.items[1]).toBe('Second');
|
|
370
|
-
expect(parsed.complex).toHaveLength(2);
|
|
371
|
-
expect(parsed.complex[0].title).toBe('Title 1');
|
|
372
|
-
expect(parsed.complex[0].desc).toBe('Description 1');
|
|
373
|
-
expect(parsed.complex[1].title).toBe('Title 2');
|
|
374
|
-
});
|
|
375
|
-
});
|
|
376
|
-
describe('enhanced JSON metadata preservation', () => {
|
|
377
|
-
let nestedJsonContent;
|
|
378
|
-
beforeAll(() => {
|
|
379
|
-
nestedJsonContent = fs.readFileSync(path.join(__dirname, 'test-data', 'nested-json.json'), 'utf-8');
|
|
380
|
-
});
|
|
381
|
-
it('should preserve complete metadata for complex nested structures', () => {
|
|
382
|
-
const result = handler.parse(nestedJsonContent);
|
|
383
|
-
expect(result._metadata).toBeDefined();
|
|
384
|
-
expect(result._metadata?.format).toBe('json');
|
|
385
|
-
expect(result._metadata?.originalStructure).toBeDefined();
|
|
386
|
-
expect(result._metadata?.preserveComments).toBe(false);
|
|
387
|
-
expect(result._metadata?.preserveAttributes).toBe(false);
|
|
388
|
-
// Verify original structure is preserved
|
|
389
|
-
const original = result._metadata?.originalStructure;
|
|
390
|
-
expect(original.app.name).toBe('My Application');
|
|
391
|
-
expect(original.app.config.timeout).toBe(5000);
|
|
392
|
-
expect(original.messages.errors.network).toBe('Network connection failed');
|
|
393
|
-
expect(original.arrays.items).toHaveLength(3);
|
|
394
|
-
expect(original.arrays.complex_items).toHaveLength(2);
|
|
395
|
-
});
|
|
396
|
-
it('should correctly flatten deeply nested structures with mixed types', () => {
|
|
397
|
-
const result = handler.parse(nestedJsonContent);
|
|
398
|
-
// Test string values
|
|
399
|
-
expect(result['app.name']).toBe('My Application');
|
|
400
|
-
expect(result['app.version']).toBe('1.0.0');
|
|
401
|
-
expect(result['messages.welcome']).toBe('Welcome to our app!');
|
|
402
|
-
expect(result['messages.errors.network']).toBe('Network connection failed');
|
|
403
|
-
expect(result['messages.errors.validation']).toBe('Please check your input');
|
|
404
|
-
expect(result['messages.success.login']).toBe('Login successful');
|
|
405
|
-
// Test non-string values are preserved
|
|
406
|
-
expect(result['app.config.timeout']).toBe(5000);
|
|
407
|
-
expect(result['app.config.retries']).toBe(3);
|
|
408
|
-
expect(result['app.config.debug']).toBe(true);
|
|
409
|
-
// Test array handling
|
|
410
|
-
expect(result['arrays.items[0]']).toBe('First item');
|
|
411
|
-
expect(result['arrays.items[1]']).toBe('Second item');
|
|
412
|
-
expect(result['arrays.items[2]']).toBe('Third item');
|
|
413
|
-
// Test complex array items
|
|
414
|
-
expect(result['arrays.complex_items[0].title']).toBe('Item 1');
|
|
415
|
-
expect(result['arrays.complex_items[0].description']).toBe('Description 1');
|
|
416
|
-
expect(result['arrays.complex_items[0].priority']).toBe(1);
|
|
417
|
-
expect(result['arrays.complex_items[1].title']).toBe('Item 2');
|
|
418
|
-
expect(result['arrays.complex_items[1].description']).toBe('Description 2');
|
|
419
|
-
expect(result['arrays.complex_items[1].priority']).toBe(2);
|
|
420
|
-
// Test mixed array
|
|
421
|
-
expect(result['arrays.mixed_array[0]']).toBe('String item');
|
|
422
|
-
expect(result['arrays.mixed_array[1]']).toBe(42);
|
|
423
|
-
expect(result['arrays.mixed_array[2]']).toBe(true);
|
|
424
|
-
expect(result['arrays.mixed_array[3].nested']).toBe('Nested value');
|
|
425
|
-
});
|
|
426
|
-
it('should perfectly reconstruct original structure after translation', () => {
|
|
427
|
-
const parsed = handler.parse(nestedJsonContent);
|
|
428
|
-
// Simulate translation by modifying string values
|
|
429
|
-
const translated = {
|
|
430
|
-
...parsed,
|
|
431
|
-
'app.name': 'Mi Aplicación',
|
|
432
|
-
'app.version': '1.0.0', // Keep version unchanged
|
|
433
|
-
'messages.welcome': '¡Bienvenido a nuestra aplicación!',
|
|
434
|
-
'messages.errors.network': 'Falló la conexión de red',
|
|
435
|
-
'messages.errors.validation': 'Por favor verifica tu entrada',
|
|
436
|
-
'messages.errors.server': 'Ocurrió un error del servidor',
|
|
437
|
-
'messages.success.login': 'Inicio de sesión exitoso',
|
|
438
|
-
'messages.success.logout': 'Cierre de sesión exitoso',
|
|
439
|
-
'buttons.submit': 'Enviar',
|
|
440
|
-
'buttons.cancel': 'Cancelar',
|
|
441
|
-
'buttons.retry': 'Reintentar',
|
|
442
|
-
'arrays.items[0]': 'Primer elemento',
|
|
443
|
-
'arrays.items[1]': 'Segundo elemento',
|
|
444
|
-
'arrays.items[2]': 'Tercer elemento',
|
|
445
|
-
'arrays.complex_items[0].title': 'Elemento 1',
|
|
446
|
-
'arrays.complex_items[0].description': 'Descripción 1',
|
|
447
|
-
'arrays.complex_items[1].title': 'Elemento 2',
|
|
448
|
-
'arrays.complex_items[1].description': 'Descripción 2',
|
|
449
|
-
'arrays.mixed_array[0]': 'Elemento de cadena',
|
|
450
|
-
'arrays.mixed_array[3].nested': 'Valor anidado'
|
|
451
|
-
};
|
|
452
|
-
const serialized = handler.serialize(translated);
|
|
453
|
-
const reconstructed = JSON.parse(serialized);
|
|
454
|
-
// Verify structure is maintained
|
|
455
|
-
expect(reconstructed.app.name).toBe('Mi Aplicación');
|
|
456
|
-
expect(reconstructed.app.version).toBe('1.0.0');
|
|
457
|
-
expect(reconstructed.app.config.timeout).toBe(5000); // Non-string preserved
|
|
458
|
-
expect(reconstructed.app.config.retries).toBe(3);
|
|
459
|
-
expect(reconstructed.app.config.debug).toBe(true);
|
|
460
|
-
expect(reconstructed.messages.welcome).toBe('¡Bienvenido a nuestra aplicación!');
|
|
461
|
-
expect(reconstructed.messages.errors.network).toBe('Falló la conexión de red');
|
|
462
|
-
expect(reconstructed.messages.success.login).toBe('Inicio de sesión exitoso');
|
|
463
|
-
expect(reconstructed.buttons.submit).toBe('Enviar');
|
|
464
|
-
expect(reconstructed.buttons.cancel).toBe('Cancelar');
|
|
465
|
-
// Verify arrays are reconstructed correctly
|
|
466
|
-
expect(reconstructed.arrays.items).toHaveLength(3);
|
|
467
|
-
expect(reconstructed.arrays.items[0]).toBe('Primer elemento');
|
|
468
|
-
expect(reconstructed.arrays.items[1]).toBe('Segundo elemento');
|
|
469
|
-
expect(reconstructed.arrays.items[2]).toBe('Tercer elemento');
|
|
470
|
-
expect(reconstructed.arrays.complex_items).toHaveLength(2);
|
|
471
|
-
expect(reconstructed.arrays.complex_items[0].title).toBe('Elemento 1');
|
|
472
|
-
expect(reconstructed.arrays.complex_items[0].description).toBe('Descripción 1');
|
|
473
|
-
expect(reconstructed.arrays.complex_items[0].priority).toBe(1); // Non-string preserved
|
|
474
|
-
expect(reconstructed.arrays.complex_items[1].title).toBe('Elemento 2');
|
|
475
|
-
expect(reconstructed.arrays.complex_items[1].description).toBe('Descripción 2');
|
|
476
|
-
expect(reconstructed.arrays.complex_items[1].priority).toBe(2);
|
|
477
|
-
expect(reconstructed.arrays.mixed_array).toHaveLength(4);
|
|
478
|
-
expect(reconstructed.arrays.mixed_array[0]).toBe('Elemento de cadena');
|
|
479
|
-
expect(reconstructed.arrays.mixed_array[1]).toBe(42); // Non-string preserved
|
|
480
|
-
expect(reconstructed.arrays.mixed_array[2]).toBe(true); // Non-string preserved
|
|
481
|
-
expect(reconstructed.arrays.mixed_array[3].nested).toBe('Valor anidado');
|
|
482
|
-
});
|
|
483
|
-
it('should handle edge cases in nested structures', () => {
|
|
484
|
-
const edgeCaseJson = `{
|
|
485
|
-
"empty_object": {},
|
|
486
|
-
"empty_array": [],
|
|
487
|
-
"null_value": null,
|
|
488
|
-
"nested": {
|
|
489
|
-
"empty_nested": {},
|
|
490
|
-
"array_with_nulls": [null, "valid", null],
|
|
491
|
-
"deeply": {
|
|
492
|
-
"nested": {
|
|
493
|
-
"value": "deep value"
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
}
|
|
497
|
-
}`;
|
|
498
|
-
const result = handler.parse(edgeCaseJson);
|
|
499
|
-
expect(result['null_value']).toBe(null);
|
|
500
|
-
expect(result['nested.array_with_nulls[0]']).toBe(null);
|
|
501
|
-
expect(result['nested.array_with_nulls[1]']).toBe('valid');
|
|
502
|
-
expect(result['nested.array_with_nulls[2]']).toBe(null);
|
|
503
|
-
expect(result['nested.deeply.nested.value']).toBe('deep value');
|
|
504
|
-
// Serialize and verify structure is maintained
|
|
505
|
-
const serialized = handler.serialize(result);
|
|
506
|
-
const parsed = JSON.parse(serialized);
|
|
507
|
-
expect(parsed.empty_object).toEqual({});
|
|
508
|
-
expect(parsed.empty_array).toEqual([]);
|
|
509
|
-
expect(parsed.null_value).toBe(null);
|
|
510
|
-
expect(parsed.nested.empty_nested).toEqual({});
|
|
511
|
-
expect(parsed.nested.array_with_nulls).toEqual([null, 'valid', null]);
|
|
512
|
-
expect(parsed.nested.deeply.nested.value).toBe('deep value');
|
|
513
|
-
});
|
|
514
|
-
it('should preserve formatting options in metadata', () => {
|
|
515
|
-
const result = handler.parse(nestedJsonContent);
|
|
516
|
-
// Test custom indentation
|
|
517
|
-
const serializedWithTabs = handler.serialize(result, { indentation: '\t' });
|
|
518
|
-
expect(serializedWithTabs).toContain('\t"app"');
|
|
519
|
-
const serializedWith4Spaces = handler.serialize(result, { indentation: 4 });
|
|
520
|
-
expect(serializedWith4Spaces).toContain(' "app"');
|
|
521
|
-
// Test default indentation
|
|
522
|
-
const serializedDefault = handler.serialize(result);
|
|
523
|
-
expect(serializedDefault).toContain(' "app"'); // Default 2 spaces
|
|
524
|
-
});
|
|
525
|
-
});
|
|
526
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|