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.
Files changed (40) hide show
  1. package/README.MD +240 -3
  2. package/build/package.json +30 -16
  3. package/build/src/bin/cli.d.ts +1 -1
  4. package/build/src/bin/cli.js +82 -53
  5. package/build/src/config.d.ts +12 -1
  6. package/build/src/files.js +2 -2
  7. package/build/src/huggingface-local.d.ts +11 -0
  8. package/build/src/huggingface-local.js +71 -0
  9. package/build/src/huggingface.d.ts +12 -0
  10. package/build/src/huggingface.js +76 -0
  11. package/build/src/index.js +17 -0
  12. package/build/src/lib.js +9 -1
  13. package/build/src/openai.js +1 -0
  14. package/package.json +30 -16
  15. package/build/tests/arb-handler.test.d.ts +0 -1
  16. package/build/tests/arb-handler.test.js +0 -694
  17. package/build/tests/cli-integration.test.d.ts +0 -1
  18. package/build/tests/cli-integration.test.js +0 -352
  19. package/build/tests/csv-handler.test.d.ts +0 -1
  20. package/build/tests/csv-handler.test.js +0 -507
  21. package/build/tests/error-handling.test.d.ts +0 -1
  22. package/build/tests/error-handling.test.js +0 -456
  23. package/build/tests/files-integration.test.d.ts +0 -1
  24. package/build/tests/files-integration.test.js +0 -421
  25. package/build/tests/json-handler.test.d.ts +0 -1
  26. package/build/tests/json-handler.test.js +0 -526
  27. package/build/tests/po-handler.test.d.ts +0 -1
  28. package/build/tests/po-handler.test.js +0 -768
  29. package/build/tests/properties-handler.test.d.ts +0 -1
  30. package/build/tests/properties-handler.test.js +0 -316
  31. package/build/tests/setup.d.ts +0 -1
  32. package/build/tests/setup.js +0 -5
  33. package/build/tests/xliff-handler.test.d.ts +0 -1
  34. package/build/tests/xliff-handler.test.js +0 -459
  35. package/build/tests/xmb-xtb-handler.test.d.ts +0 -1
  36. package/build/tests/xmb-xtb-handler.test.js +0 -758
  37. package/build/tests/xml-handler.test.d.ts +0 -1
  38. package/build/tests/xml-handler.test.js +0 -570
  39. package/build/tests/yaml-handler.test.d.ts +0 -1
  40. package/build/tests/yaml-handler.test.js +0 -156
@@ -1,507 +0,0 @@
1
- import { CsvHandler } from '../src/format/csv-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 { TsvHandler } from '../src/format/tsv-handler.js';
7
- import * as fs from 'fs';
8
- import * as path from 'path';
9
- describe('CsvHandler', () => {
10
- let handler;
11
- beforeEach(() => {
12
- handler = new CsvHandler();
13
- });
14
- describe('canHandle', () => {
15
- it('should handle .csv files', () => {
16
- expect(handler.canHandle('test.csv')).toBe(true);
17
- expect(handler.canHandle('translations.csv')).toBe(true);
18
- });
19
- it('should not handle non-csv files', () => {
20
- expect(handler.canHandle('test.tsv')).toBe(false);
21
- expect(handler.canHandle('test.json')).toBe(false);
22
- expect(handler.canHandle('test.xml')).toBe(false);
23
- });
24
- it('should validate CSV content when provided', () => {
25
- const validCsv = 'key,value\napp_name,My App\nwelcome,Hello';
26
- expect(handler.canHandle('test.csv', validCsv)).toBe(true);
27
- // Test with content that doesn't have consistent structure
28
- const inconsistentCsv = 'key,value\napp_name\nwelcome,Hello,extra,fields,here';
29
- expect(handler.canHandle('test.csv', inconsistentCsv)).toBe(false);
30
- });
31
- });
32
- describe('CSV parsing', () => {
33
- let simpleCsvContent;
34
- let multiLanguageCsvContent;
35
- let quotedFieldsCsvContent;
36
- beforeAll(() => {
37
- simpleCsvContent = fs.readFileSync(path.join(__dirname, 'test-data', 'simple.csv'), 'utf-8');
38
- multiLanguageCsvContent = fs.readFileSync(path.join(__dirname, 'test-data', 'multi-language.csv'), 'utf-8');
39
- quotedFieldsCsvContent = fs.readFileSync(path.join(__dirname, 'test-data', 'quoted-fields.csv'), 'utf-8');
40
- });
41
- it('should parse simple CSV correctly', () => {
42
- const result = handler.parse(simpleCsvContent);
43
- expect(result.app_name).toBe('My Application');
44
- expect(result.welcome_message).toBe('Welcome to our app!');
45
- expect(result.button_text).toBe('Click here');
46
- expect(result.error_message).toBe('Something went wrong');
47
- });
48
- it('should preserve CSV metadata', () => {
49
- const result = handler.parse(simpleCsvContent);
50
- expect(result._metadata).toBeDefined();
51
- expect(result._metadata?.format).toBe('csv');
52
- expect(result._metadata?.csvDelimiter).toBe(',');
53
- expect(result._metadata?.keyColumn).toBe('key');
54
- expect(result._metadata?.valueColumn).toBe('value');
55
- expect(result._metadata?.hasHeaders).toBe(true);
56
- expect(result._metadata?.columns).toEqual(['key', 'value']);
57
- });
58
- it('should handle quoted fields correctly', () => {
59
- const result = handler.parse(quotedFieldsCsvContent);
60
- expect(result.simple_text).toBe('Hello World');
61
- expect(result.quoted_text).toBe('Hello, World!');
62
- expect(result.text_with_quotes).toBe('She said "Hello" to me');
63
- // The multiline text parsing may vary based on line terminator handling
64
- expect(result.multiline_text).toContain('This is a');
65
- expect(result.text_with_commas).toBe('Red, Green, Blue');
66
- expect(result.text_with_semicolon).toBe('Name: John; Age: 30');
67
- });
68
- it('should handle multi-language columns', () => {
69
- const result = handler.parse(multiLanguageCsvContent);
70
- // Check main translation (first language column)
71
- expect(result.app_name).toBe('My Application');
72
- expect(result.welcome_message).toBe('Welcome to our app!');
73
- // Check metadata for multi-language support
74
- expect(result._metadata?.multiLanguageColumns).toBeDefined();
75
- expect(result._metadata?.multiLanguageColumns?.en).toBe('en');
76
- expect(result._metadata?.multiLanguageColumns?.es).toBe('es');
77
- expect(result._metadata?.multiLanguageColumns?.fr).toBe('fr');
78
- // Check multi-language data
79
- expect(result._metadata?.multiLanguageData?.es?.app_name).toBe('Mi Aplicación');
80
- expect(result._metadata?.multiLanguageData?.fr?.app_name).toBe('Mon Application');
81
- });
82
- });
83
- describe('TSV parsing', () => {
84
- let tsvContent;
85
- beforeAll(() => {
86
- tsvContent = fs.readFileSync(path.join(__dirname, 'test-data', 'sample.tsv'), 'utf-8');
87
- });
88
- it('should detect tab delimiter correctly', () => {
89
- const result = handler.parse(tsvContent);
90
- expect(result._metadata?.csvDelimiter).toBe('\t');
91
- expect(result.app_name).toBe('My Application');
92
- expect(result.welcome_message).toBe('Welcome to our app!');
93
- });
94
- });
95
- describe('header detection', () => {
96
- let noHeadersCsvContent;
97
- let customColumnsCsvContent;
98
- beforeAll(() => {
99
- noHeadersCsvContent = fs.readFileSync(path.join(__dirname, 'test-data', 'no-headers.csv'), 'utf-8');
100
- customColumnsCsvContent = fs.readFileSync(path.join(__dirname, 'test-data', 'custom-columns.csv'), 'utf-8');
101
- });
102
- it('should detect when headers are present', () => {
103
- const result = handler.parse(customColumnsCsvContent);
104
- expect(result._metadata?.hasHeaders).toBe(true);
105
- expect(result._metadata?.keyColumn).toBe('identifier');
106
- expect(result._metadata?.valueColumn).toBe('text');
107
- expect(result._metadata?.columns).toEqual(['identifier', 'text', 'description']);
108
- });
109
- it('should handle CSV without headers', () => {
110
- const result = handler.parse(noHeadersCsvContent, { hasHeaders: false });
111
- expect(result._metadata?.hasHeaders).toBe(false);
112
- expect(result._metadata?.keyColumn).toBe('column_1');
113
- expect(result._metadata?.valueColumn).toBe('column_2');
114
- });
115
- it('should auto-detect key and value columns', () => {
116
- const result = handler.parse(customColumnsCsvContent);
117
- expect(result._metadata?.keyColumn).toBe('identifier');
118
- expect(result._metadata?.valueColumn).toBe('text');
119
- expect(result.app_name).toBe('My Application');
120
- expect(result.welcome_message).toBe('Welcome to our app!');
121
- });
122
- });
123
- describe('column mapping', () => {
124
- it('should use custom column mapping when provided', () => {
125
- const csvContent = 'id,message,note\napp_name,My App,App title\nwelcome,Hello,Greeting';
126
- const result = handler.parse(csvContent, {
127
- keyColumn: 'id',
128
- valueColumn: 'message'
129
- });
130
- expect(result.app_name).toBe('My App');
131
- expect(result.welcome).toBe('Hello');
132
- });
133
- it('should handle multi-language column mapping', () => {
134
- const multiLanguageCsvContent = fs.readFileSync(path.join(__dirname, 'test-data', 'multi-language.csv'), 'utf-8');
135
- const result = handler.parse(multiLanguageCsvContent, {
136
- multiLanguageColumns: {
137
- 'english': 'en',
138
- 'spanish': 'es',
139
- 'french': 'fr'
140
- }
141
- });
142
- expect(result._metadata?.multiLanguageData?.english?.app_name).toBe('My Application');
143
- expect(result._metadata?.multiLanguageData?.spanish?.app_name).toBe('Mi Aplicación');
144
- expect(result._metadata?.multiLanguageData?.french?.app_name).toBe('Mon Application');
145
- });
146
- });
147
- describe('CSV serialization', () => {
148
- it('should serialize simple data correctly', () => {
149
- const data = {
150
- app_name: 'Mi Aplicación',
151
- welcome_message: '¡Bienvenido!',
152
- button_text: 'Haz clic aquí',
153
- _metadata: {
154
- format: 'csv',
155
- csvDelimiter: ',',
156
- keyColumn: 'key',
157
- valueColumn: 'value',
158
- hasHeaders: true,
159
- columns: ['key', 'value']
160
- }
161
- };
162
- const result = handler.serialize(data);
163
- const lines = result.trim().split('\n');
164
- expect(lines[0]).toBe('key,value');
165
- expect(lines[1]).toBe('app_name,Mi Aplicación');
166
- expect(lines[2]).toBe('welcome_message,¡Bienvenido!');
167
- expect(lines[3]).toBe('button_text,Haz clic aquí');
168
- });
169
- it('should handle quoted fields in serialization', () => {
170
- const data = {
171
- simple: 'Hello',
172
- with_comma: 'Hello, World',
173
- with_quotes: 'She said "Hello"',
174
- with_newline: 'Line 1\nLine 2'
175
- };
176
- const result = handler.serialize(data);
177
- expect(result).toContain('simple,Hello');
178
- expect(result).toContain('with_comma,"Hello, World"');
179
- expect(result).toContain('with_quotes,"She said ""Hello"""');
180
- expect(result).toContain('with_newline,"Line 1\nLine 2"');
181
- });
182
- it('should preserve original row structure when available', () => {
183
- const multiLanguageCsvContent = fs.readFileSync(path.join(__dirname, 'test-data', 'multi-language.csv'), 'utf-8');
184
- const originalData = handler.parse(multiLanguageCsvContent);
185
- // Modify some translations
186
- originalData.app_name = 'Mi Aplicación Modificada';
187
- originalData.welcome_message = '¡Bienvenido modificado!';
188
- const result = handler.serialize(originalData);
189
- const lines = result.trim().split('\n');
190
- expect(lines[0]).toBe('key,en,es,fr');
191
- expect(lines[1]).toContain('app_name');
192
- expect(lines[2]).toContain('welcome_message');
193
- // The serializer preserves original structure, so it shows the original English value
194
- expect(result).toContain('app_name');
195
- expect(result).toContain('welcome_message');
196
- });
197
- });
198
- describe('validation', () => {
199
- it('should validate correct CSV structure', () => {
200
- const validData = {
201
- app_name: 'My App',
202
- welcome: 'Hello',
203
- _metadata: {
204
- format: 'csv',
205
- keyColumn: 'key',
206
- valueColumn: 'value',
207
- columns: ['key', 'value']
208
- }
209
- };
210
- const result = handler.validateStructure(validData);
211
- expect(result.isValid).toBe(true);
212
- expect(result.errors).toHaveLength(0);
213
- });
214
- it('should detect invalid structure', () => {
215
- const invalidData = 'not an object';
216
- const result = handler.validateStructure(invalidData);
217
- expect(result.isValid).toBe(false);
218
- expect(result.errors.length).toBeGreaterThan(0);
219
- expect(result.errors[0].code).toBe('INVALID_STRUCTURE');
220
- });
221
- it('should warn about non-string values', () => {
222
- const dataWithNonStrings = {
223
- title: 'Valid string',
224
- count: 42,
225
- enabled: true
226
- };
227
- const result = handler.validateStructure(dataWithNonStrings);
228
- expect(result.isValid).toBe(true);
229
- expect(result.warnings.length).toBeGreaterThan(0);
230
- const nonStringWarnings = result.warnings.filter(w => w.code === 'NON_STRING_VALUE');
231
- expect(nonStringWarnings.length).toBeGreaterThan(0);
232
- });
233
- it('should warn about missing columns', () => {
234
- const dataWithoutColumns = {
235
- title: 'Test',
236
- _metadata: {
237
- format: 'csv'
238
- }
239
- };
240
- const result = handler.validateStructure(dataWithoutColumns);
241
- expect(result.isValid).toBe(true);
242
- expect(result.warnings.length).toBeGreaterThan(0);
243
- const missingKeyWarning = result.warnings.find(w => w.code === 'MISSING_KEY_COLUMN');
244
- const missingValueWarning = result.warnings.find(w => w.code === 'MISSING_VALUE_COLUMN');
245
- expect(missingKeyWarning).toBeDefined();
246
- expect(missingValueWarning).toBeDefined();
247
- });
248
- it('should handle empty CSV data', () => {
249
- const emptyData = {};
250
- const result = handler.validateStructure(emptyData);
251
- expect(result.isValid).toBe(true);
252
- expect(result.warnings.length).toBeGreaterThan(0);
253
- expect(result.warnings[0].code).toBe('EMPTY_CSV');
254
- });
255
- });
256
- describe('delimiter detection', () => {
257
- it('should detect comma delimiter', () => {
258
- const csvContent = 'key,value\ntest,hello';
259
- const result = handler.parse(csvContent);
260
- expect(result._metadata?.csvDelimiter).toBe(',');
261
- });
262
- it('should detect tab delimiter', () => {
263
- const tsvContent = 'key\tvalue\ntest\thello';
264
- const result = handler.parse(tsvContent);
265
- expect(result._metadata?.csvDelimiter).toBe('\t');
266
- });
267
- it('should detect semicolon delimiter', () => {
268
- const csvContent = 'key;value\ntest;hello';
269
- const result = handler.parse(csvContent);
270
- expect(result._metadata?.csvDelimiter).toBe(';');
271
- });
272
- it('should detect pipe delimiter', () => {
273
- const csvContent = 'key|value\ntest|hello';
274
- const result = handler.parse(csvContent);
275
- expect(result._metadata?.csvDelimiter).toBe('|');
276
- });
277
- });
278
- describe('dialect options', () => {
279
- it('should handle Excel dialect', () => {
280
- const csvContent = 'key,value\ntest,"hello, world"';
281
- const result = handler.parse(csvContent, { dialect: 'excel' });
282
- expect(result.test).toBe('hello, world');
283
- });
284
- it('should handle Unix dialect', () => {
285
- const csvContent = 'key,value\ntest,"hello, world"';
286
- const result = handler.parse(csvContent, { dialect: 'unix' });
287
- expect(result.test).toBe('hello, world');
288
- });
289
- it('should handle RFC4180 dialect', () => {
290
- const csvContent = 'key,value\ntest,"hello, world"';
291
- const result = handler.parse(csvContent, { dialect: 'rfc4180' });
292
- expect(result.test).toBe('hello, world');
293
- });
294
- it('should use custom dialect options', () => {
295
- const csvContent = 'key,value\ntest,hello world';
296
- const result = handler.parse(csvContent, {
297
- dialect: 'custom',
298
- quote: "'",
299
- escape: "\\",
300
- trimFields: false
301
- });
302
- expect(result.test).toBe('hello world');
303
- });
304
- });
305
- describe('error handling', () => {
306
- it('should handle malformed CSV gracefully', () => {
307
- const malformedCsv = 'key,value\n"unclosed quote,test';
308
- // The CSV handler is designed to be lenient and handle malformed content
309
- const result = handler.parse(malformedCsv);
310
- expect(result).toBeDefined();
311
- expect(typeof result).toBe('object');
312
- });
313
- it('should handle missing key column gracefully', () => {
314
- const csvContent = 'col1,col2\nvalue1,value2';
315
- // When a nonexistent key column is specified, it should fall back to auto-detection
316
- const result = handler.parse(csvContent, { keyColumn: 'nonexistent' });
317
- expect(result).toBeDefined();
318
- expect(typeof result).toBe('object');
319
- });
320
- it('should handle missing value column gracefully', () => {
321
- const csvContent = 'key,col2\nvalue1,value2';
322
- // When a nonexistent value column is specified, it should fall back to auto-detection
323
- const result = handler.parse(csvContent, { valueColumn: 'nonexistent' });
324
- expect(result).toBeDefined();
325
- expect(typeof result).toBe('object');
326
- });
327
- it('should throw error for empty CSV', () => {
328
- const emptyCsv = '';
329
- expect(() => handler.parse(emptyCsv)).toThrow('CSV file contains no data rows');
330
- });
331
- });
332
- describe('getFileExtension', () => {
333
- it('should return .csv extension', () => {
334
- expect(handler.getFileExtension()).toBe('.csv');
335
- });
336
- });
337
- describe('round-trip consistency', () => {
338
- it('should maintain data integrity through parse-serialize cycle', () => {
339
- const originalCsv = fs.readFileSync(path.join(__dirname, 'test-data', 'simple.csv'), 'utf-8');
340
- const parsed = handler.parse(originalCsv);
341
- const serialized = handler.serialize(parsed);
342
- const reparsed = handler.parse(serialized);
343
- expect(reparsed.app_name).toBe(parsed.app_name);
344
- expect(reparsed.welcome_message).toBe(parsed.welcome_message);
345
- expect(reparsed.button_text).toBe(parsed.button_text);
346
- expect(reparsed.error_message).toBe(parsed.error_message);
347
- });
348
- it('should maintain multi-language data through parse-serialize cycle', () => {
349
- const originalCsv = fs.readFileSync(path.join(__dirname, 'test-data', 'multi-language.csv'), 'utf-8');
350
- const parsed = handler.parse(originalCsv);
351
- const serialized = handler.serialize(parsed);
352
- const reparsed = handler.parse(serialized);
353
- expect(reparsed._metadata?.multiLanguageData?.es?.app_name).toBe(parsed._metadata?.multiLanguageData?.es?.app_name);
354
- expect(reparsed._metadata?.multiLanguageData?.fr?.welcome_message).toBe(parsed._metadata?.multiLanguageData?.fr?.welcome_message);
355
- });
356
- it('should handle quoted fields consistently', () => {
357
- const originalCsv = fs.readFileSync(path.join(__dirname, 'test-data', 'quoted-fields.csv'), 'utf-8');
358
- const parsed = handler.parse(originalCsv);
359
- const serialized = handler.serialize(parsed);
360
- const reparsed = handler.parse(serialized);
361
- expect(reparsed.text_with_quotes).toBe(parsed.text_with_quotes);
362
- expect(reparsed.multiline_text).toBe(parsed.multiline_text);
363
- expect(reparsed.text_with_commas).toBe(parsed.text_with_commas);
364
- });
365
- });
366
- });
367
- describe('TsvHandler', () => {
368
- let handler;
369
- beforeEach(() => {
370
- handler = new TsvHandler();
371
- });
372
- describe('canHandle', () => {
373
- it('should handle .tsv files', () => {
374
- expect(handler.canHandle('test.tsv')).toBe(true);
375
- expect(handler.canHandle('translations.tsv')).toBe(true);
376
- });
377
- it('should not handle non-tsv files', () => {
378
- expect(handler.canHandle('test.csv')).toBe(false);
379
- expect(handler.canHandle('test.json')).toBe(false);
380
- expect(handler.canHandle('test.xml')).toBe(false);
381
- });
382
- it('should validate TSV content when provided', () => {
383
- const validTsv = 'key\tvalue\napp_name\tMy App\nwelcome\tHello';
384
- const invalidTsv = 'key,value\napp_name,My App'; // CSV format, not TSV
385
- expect(handler.canHandle('test.tsv', validTsv)).toBe(true);
386
- expect(handler.canHandle('test.tsv', invalidTsv)).toBe(false);
387
- });
388
- });
389
- describe('TSV parsing', () => {
390
- let tsvContent;
391
- beforeAll(() => {
392
- tsvContent = fs.readFileSync(path.join(__dirname, 'test-data', 'sample.tsv'), 'utf-8');
393
- });
394
- it('should parse TSV correctly', () => {
395
- const result = handler.parse(tsvContent);
396
- expect(result.app_name).toBe('My Application');
397
- expect(result.welcome_message).toBe('Welcome to our app!');
398
- expect(result.button_text).toBe('Click here');
399
- expect(result.error_message).toBe('Something went wrong');
400
- });
401
- it('should preserve TSV metadata', () => {
402
- const result = handler.parse(tsvContent);
403
- expect(result._metadata).toBeDefined();
404
- expect(result._metadata?.format).toBe('tsv');
405
- expect(result._metadata?.csvDelimiter).toBe('\t');
406
- expect(result._metadata?.keyColumn).toBe('key');
407
- expect(result._metadata?.valueColumn).toBe('value');
408
- expect(result._metadata?.hasHeaders).toBe(true);
409
- expect(result._metadata?.columns).toEqual(['key', 'value']);
410
- });
411
- it('should handle tab-separated values with spaces', () => {
412
- const tsvWithSpaces = 'key\tvalue\napp name\tMy Application Name\nwelcome message\tWelcome to our application!';
413
- const result = handler.parse(tsvWithSpaces);
414
- expect(result['app name']).toBe('My Application Name');
415
- expect(result['welcome message']).toBe('Welcome to our application!');
416
- });
417
- it('should handle multi-language TSV', () => {
418
- const multiLangTsv = 'key\ten\tes\tfr\napp_name\tMy Application\tMi Aplicación\tMon Application\nwelcome\tWelcome!\t¡Bienvenido!\tBienvenue!';
419
- const result = handler.parse(multiLangTsv);
420
- expect(result.app_name).toBe('My Application');
421
- expect(result._metadata?.multiLanguageData?.es?.app_name).toBe('Mi Aplicación');
422
- expect(result._metadata?.multiLanguageData?.fr?.app_name).toBe('Mon Application');
423
- });
424
- });
425
- describe('TSV serialization', () => {
426
- it('should serialize with tab delimiter', () => {
427
- const data = {
428
- app_name: 'Mi Aplicación',
429
- welcome_message: '¡Bienvenido!',
430
- button_text: 'Haz clic aquí'
431
- };
432
- const result = handler.serialize(data);
433
- const lines = result.trim().split('\n');
434
- expect(lines[0]).toBe('key\tvalue');
435
- expect(lines[1]).toBe('app_name\tMi Aplicación');
436
- expect(lines[2]).toBe('welcome_message\t¡Bienvenido!');
437
- expect(lines[3]).toBe('button_text\tHaz clic aquí');
438
- });
439
- it('should handle values with commas in TSV', () => {
440
- const data = {
441
- simple: 'Hello',
442
- with_comma: 'Hello, World',
443
- with_semicolon: 'Name: John; Age: 30'
444
- };
445
- const result = handler.serialize(data);
446
- // TSV should not quote values with commas since tabs are the delimiter
447
- expect(result).toContain('simple\tHello');
448
- expect(result).toContain('with_comma\tHello, World');
449
- expect(result).toContain('with_semicolon\tName: John; Age: 30');
450
- });
451
- it('should quote values with tabs in TSV', () => {
452
- const data = {
453
- simple: 'Hello',
454
- with_tab: 'Hello\tWorld',
455
- with_newline: 'Line 1\nLine 2'
456
- };
457
- const result = handler.serialize(data);
458
- expect(result).toContain('simple\tHello');
459
- expect(result).toContain('with_tab\t"Hello\tWorld"');
460
- expect(result).toContain('with_newline\t"Line 1\nLine 2"');
461
- });
462
- });
463
- describe('validation', () => {
464
- it('should use CSV validation logic', () => {
465
- const validData = {
466
- app_name: 'My App',
467
- welcome: 'Hello'
468
- };
469
- const result = handler.validateStructure(validData);
470
- expect(result.isValid).toBe(true);
471
- expect(result.errors).toHaveLength(0);
472
- });
473
- it('should detect invalid structure', () => {
474
- const invalidData = 'not an object';
475
- const result = handler.validateStructure(invalidData);
476
- expect(result.isValid).toBe(false);
477
- expect(result.errors.length).toBeGreaterThan(0);
478
- expect(result.errors[0].code).toBe('INVALID_STRUCTURE');
479
- });
480
- });
481
- describe('getFileExtension', () => {
482
- it('should return .tsv extension', () => {
483
- expect(handler.getFileExtension()).toBe('.tsv');
484
- });
485
- });
486
- describe('TSV round-trip consistency', () => {
487
- it('should maintain data integrity through parse-serialize cycle', () => {
488
- const originalTsv = fs.readFileSync(path.join(__dirname, 'test-data', 'sample.tsv'), 'utf-8');
489
- const parsed = handler.parse(originalTsv);
490
- const serialized = handler.serialize(parsed);
491
- const reparsed = handler.parse(serialized);
492
- expect(reparsed.app_name).toBe(parsed.app_name);
493
- expect(reparsed.welcome_message).toBe(parsed.welcome_message);
494
- expect(reparsed.button_text).toBe(parsed.button_text);
495
- expect(reparsed.error_message).toBe(parsed.error_message);
496
- });
497
- it('should handle tab-separated values with special characters', () => {
498
- const tsvWithSpecialChars = 'key\tvalue\ntest1\tHello, World!\ntest2\tSimple text\ntest3\tValue with quotes';
499
- const parsed = handler.parse(tsvWithSpecialChars);
500
- const serialized = handler.serialize(parsed);
501
- const reparsed = handler.parse(serialized);
502
- expect(reparsed.test1).toBe('Hello, World!');
503
- expect(reparsed.test2).toBe('Simple text');
504
- expect(reparsed.test3).toBe('Value with quotes');
505
- });
506
- });
507
- });
@@ -1 +0,0 @@
1
- export {};