auto-translate-json-library 1.5.4 → 2.0.2

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 (108) hide show
  1. package/README.MD +1141 -148
  2. package/build/package.json +97 -21
  3. package/build/src/aws.d.ts +1 -1
  4. package/build/src/aws.js +27 -38
  5. package/build/src/azure.d.ts +1 -1
  6. package/build/src/azure.js +50 -63
  7. package/build/src/bin/cli.d.ts +1 -1
  8. package/build/src/bin/cli.js +196 -37
  9. package/build/src/config.d.ts +1 -0
  10. package/build/src/config.js +1 -2
  11. package/build/src/deepl.d.ts +1 -1
  12. package/build/src/deepl.js +18 -64
  13. package/build/src/files.d.ts +11 -2
  14. package/build/src/files.js +356 -69
  15. package/build/src/folderFiles.d.ts +6 -3
  16. package/build/src/folderFiles.js +33 -44
  17. package/build/src/format/arb-handler.d.ts +33 -0
  18. package/build/src/format/arb-handler.js +679 -0
  19. package/build/src/format/base-format-handler.d.ts +75 -0
  20. package/build/src/format/base-format-handler.js +166 -0
  21. package/build/src/format/csv-handler.d.ts +38 -0
  22. package/build/src/format/csv-handler.js +604 -0
  23. package/build/src/format/index.d.ts +17 -0
  24. package/build/src/format/index.js +48 -0
  25. package/build/src/format/json-handler.d.ts +17 -0
  26. package/build/src/format/json-handler.js +294 -0
  27. package/build/src/format/po-handler.d.ts +17 -0
  28. package/build/src/format/po-handler.js +338 -0
  29. package/build/src/format/po-utils.d.ts +86 -0
  30. package/build/src/format/po-utils.js +385 -0
  31. package/build/src/format/pot-handler.d.ts +22 -0
  32. package/build/src/format/pot-handler.js +331 -0
  33. package/build/src/format/properties-handler.d.ts +26 -0
  34. package/build/src/format/properties-handler.js +500 -0
  35. package/build/src/format/tsv-handler.d.ts +13 -0
  36. package/build/src/format/tsv-handler.js +67 -0
  37. package/build/src/format/xliff-handler.d.ts +24 -0
  38. package/build/src/format/xliff-handler.js +433 -0
  39. package/build/src/format/xmb-handler.d.ts +33 -0
  40. package/build/src/format/xmb-handler.js +484 -0
  41. package/build/src/format/xmb-xtb-utils.d.ts +43 -0
  42. package/build/src/format/xmb-xtb-utils.js +180 -0
  43. package/build/src/format/xml-handler.d.ts +28 -0
  44. package/build/src/format/xml-handler.js +561 -0
  45. package/build/src/format/xtb-handler.d.ts +33 -0
  46. package/build/src/format/xtb-handler.js +518 -0
  47. package/build/src/format/yaml-handler.d.ts +26 -0
  48. package/build/src/format/yaml-handler.js +522 -0
  49. package/build/src/format-detector.d.ts +7 -0
  50. package/build/src/format-detector.js +58 -0
  51. package/build/src/format-handler-factory.d.ts +9 -0
  52. package/build/src/format-handler-factory.js +18 -0
  53. package/build/src/format.interface.d.ts +56 -0
  54. package/build/src/format.interface.js +1 -0
  55. package/build/src/google.d.ts +1 -1
  56. package/build/src/google.js +32 -49
  57. package/build/src/index.d.ts +4 -3
  58. package/build/src/index.js +15 -17
  59. package/build/src/lib.d.ts +1 -1
  60. package/build/src/lib.js +188 -133
  61. package/build/src/openai.d.ts +2 -2
  62. package/build/src/openai.js +59 -50
  63. package/build/src/translate.interface.d.ts +1 -1
  64. package/build/src/translate.interface.js +1 -2
  65. package/build/src/util.js +4 -9
  66. package/build/src/validation/enhanced-validation-result.d.ts +96 -0
  67. package/build/src/validation/enhanced-validation-result.js +198 -0
  68. package/build/src/validation/error-messages.d.ts +88 -0
  69. package/build/src/validation/error-messages.js +436 -0
  70. package/build/src/validation/error-recovery.d.ts +78 -0
  71. package/build/src/validation/error-recovery.js +593 -0
  72. package/build/src/validation/example-usage.d.ts +31 -0
  73. package/build/src/validation/example-usage.js +148 -0
  74. package/build/src/validation/format-rules.d.ts +37 -0
  75. package/build/src/validation/format-rules.js +550 -0
  76. package/build/src/validation/format-validator.d.ts +70 -0
  77. package/build/src/validation/format-validator.js +279 -0
  78. package/build/src/validation/index.d.ts +13 -0
  79. package/build/src/validation/index.js +30 -0
  80. package/build/src/validation/validation-service.d.ts +56 -0
  81. package/build/src/validation/validation-service.js +262 -0
  82. package/build/tests/arb-handler.test.d.ts +1 -0
  83. package/build/tests/arb-handler.test.js +694 -0
  84. package/build/tests/cli-integration.test.d.ts +1 -0
  85. package/build/tests/cli-integration.test.js +352 -0
  86. package/build/tests/csv-handler.test.d.ts +1 -0
  87. package/build/tests/csv-handler.test.js +507 -0
  88. package/build/tests/error-handling.test.d.ts +1 -0
  89. package/build/tests/error-handling.test.js +456 -0
  90. package/build/tests/files-integration.test.d.ts +1 -0
  91. package/build/tests/files-integration.test.js +421 -0
  92. package/build/tests/json-handler.test.d.ts +1 -0
  93. package/build/tests/json-handler.test.js +526 -0
  94. package/build/tests/po-handler.test.d.ts +1 -0
  95. package/build/tests/po-handler.test.js +768 -0
  96. package/build/tests/properties-handler.test.d.ts +1 -0
  97. package/build/tests/properties-handler.test.js +316 -0
  98. package/build/tests/setup.d.ts +1 -0
  99. package/build/tests/setup.js +5 -0
  100. package/build/tests/xliff-handler.test.d.ts +1 -0
  101. package/build/tests/xliff-handler.test.js +459 -0
  102. package/build/tests/xmb-xtb-handler.test.d.ts +1 -0
  103. package/build/tests/xmb-xtb-handler.test.js +758 -0
  104. package/build/tests/xml-handler.test.d.ts +1 -0
  105. package/build/tests/xml-handler.test.js +570 -0
  106. package/build/tests/yaml-handler.test.d.ts +1 -0
  107. package/build/tests/yaml-handler.test.js +156 -0
  108. package/package.json +97 -21
@@ -0,0 +1,694 @@
1
+ import { ArbHandler } from "../src/format/arb-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
+ describe("ArbHandler", () => {
7
+ let handler;
8
+ beforeEach(() => {
9
+ handler = new ArbHandler();
10
+ });
11
+ describe("canHandle", () => {
12
+ it("should handle .arb files", () => {
13
+ expect(handler.canHandle("test.arb")).toBe(true);
14
+ });
15
+ it("should not handle non-.arb files", () => {
16
+ expect(handler.canHandle("test.json")).toBe(false);
17
+ expect(handler.canHandle("test.xml")).toBe(false);
18
+ });
19
+ it("should validate ARB content structure with @@locale metadata", () => {
20
+ const validArbContent = JSON.stringify({
21
+ "@@locale": "en",
22
+ "hello": "Hello",
23
+ "@hello": {
24
+ "description": "A greeting"
25
+ }
26
+ });
27
+ expect(handler.canHandle("test.arb", validArbContent)).toBe(true);
28
+ });
29
+ it("should validate ARB content structure with resource metadata", () => {
30
+ const validArbContent = JSON.stringify({
31
+ "hello": "Hello",
32
+ "@hello": {
33
+ "description": "A greeting"
34
+ }
35
+ });
36
+ expect(handler.canHandle("test.arb", validArbContent)).toBe(true);
37
+ });
38
+ it("should reject invalid JSON content", () => {
39
+ const invalidContent = "{ invalid json }";
40
+ expect(handler.canHandle("test.arb", invalidContent)).toBe(false);
41
+ });
42
+ it("should reject non-ARB JSON structure", () => {
43
+ const nonArbContent = JSON.stringify({
44
+ "key1": "value1",
45
+ "key2": "value2"
46
+ });
47
+ expect(handler.canHandle("test.arb", nonArbContent)).toBe(false);
48
+ });
49
+ });
50
+ describe("parse - Basic ARB parsing", () => {
51
+ it("should parse basic ARB file with minimal structure", () => {
52
+ const arbContent = JSON.stringify({
53
+ "@@locale": "en",
54
+ "hello": "Hello",
55
+ "goodbye": "Goodbye"
56
+ });
57
+ const result = handler.parse(arbContent);
58
+ expect(result.hello).toBe("Hello");
59
+ expect(result.goodbye).toBe("Goodbye");
60
+ expect(result._metadata?.format).toBe("arb");
61
+ expect(result._metadata?.sourceLanguage).toBe("en");
62
+ expect(result._metadata?.arbMetadata?.["@@locale"]).toBe("en");
63
+ });
64
+ it("should parse ARB file with complete metadata", () => {
65
+ const arbContent = JSON.stringify({
66
+ "@@locale": "en_US",
67
+ "@@last_modified": "2023-01-01T00:00:00.000Z",
68
+ "@@author": "Test Author",
69
+ "@@context": "Test context",
70
+ "hello": "Hello",
71
+ "goodbye": "Goodbye",
72
+ "@hello": {
73
+ "description": "A greeting",
74
+ "type": "text"
75
+ },
76
+ "@goodbye": {
77
+ "description": "A farewell",
78
+ "context": "polite"
79
+ }
80
+ });
81
+ const result = handler.parse(arbContent);
82
+ expect(result.hello).toBe("Hello");
83
+ expect(result.goodbye).toBe("Goodbye");
84
+ expect(result._metadata?.format).toBe("arb");
85
+ expect(result._metadata?.sourceLanguage).toBe("en_US");
86
+ expect(result._metadata?.arbMetadata).toEqual({
87
+ "@@locale": "en_US",
88
+ "@@last_modified": "2023-01-01T00:00:00.000Z",
89
+ "@@author": "Test Author",
90
+ "@@context": "Test context"
91
+ });
92
+ expect(result._metadata?.resourceMetadata?.["@hello"]).toEqual({
93
+ description: "A greeting",
94
+ type: "text"
95
+ });
96
+ expect(result._metadata?.resourceMetadata?.["@goodbye"]).toEqual({
97
+ description: "A farewell",
98
+ context: "polite"
99
+ });
100
+ });
101
+ it("should handle ARB file without @@locale metadata", () => {
102
+ const arbContent = JSON.stringify({
103
+ "hello": "Hello",
104
+ "@hello": {
105
+ "description": "A greeting"
106
+ }
107
+ });
108
+ const result = handler.parse(arbContent);
109
+ expect(result.hello).toBe("Hello");
110
+ expect(result._metadata?.format).toBe("arb");
111
+ expect(result._metadata?.sourceLanguage).toBeUndefined();
112
+ });
113
+ it("should preserve original structure in metadata", () => {
114
+ const originalStructure = {
115
+ "@@locale": "en",
116
+ "hello": "Hello",
117
+ "@hello": {
118
+ "description": "A greeting"
119
+ }
120
+ };
121
+ const arbContent = JSON.stringify(originalStructure);
122
+ const result = handler.parse(arbContent);
123
+ expect(result._metadata?.originalStructure).toEqual(originalStructure);
124
+ });
125
+ it("should throw error for invalid JSON", () => {
126
+ const invalidContent = "{ invalid json }";
127
+ expect(() => handler.parse(invalidContent)).toThrow("Failed to parse ARB");
128
+ });
129
+ it("should throw error for invalid ARB structure", () => {
130
+ const nonArbContent = JSON.stringify({
131
+ "key1": "value1",
132
+ "key2": "value2"
133
+ });
134
+ expect(() => handler.parse(nonArbContent)).toThrow("Invalid ARB file structure");
135
+ });
136
+ });
137
+ describe("parse - ICU message format handling", () => {
138
+ it("should parse ARB with simple ICU placeholders", () => {
139
+ const arbContent = JSON.stringify({
140
+ "@@locale": "en",
141
+ "greeting": "Hello {name}!",
142
+ "@greeting": {
143
+ "description": "Personalized greeting",
144
+ "placeholders": {
145
+ "name": {
146
+ "type": "String",
147
+ "example": "John"
148
+ }
149
+ }
150
+ }
151
+ });
152
+ const result = handler.parse(arbContent);
153
+ expect(result.greeting).toBe("Hello {name}!");
154
+ expect(result._metadata?.icuAnalysis?.greeting?.hasIcuSyntax).toBe(true);
155
+ expect(result._metadata?.icuAnalysis?.greeting?.messageType).toBe("complex");
156
+ expect(result._metadata?.icuAnalysis?.greeting?.placeholders).toContain("name");
157
+ });
158
+ it("should parse ARB with plural ICU messages", () => {
159
+ const arbContent = JSON.stringify({
160
+ "@@locale": "en",
161
+ "itemCount": "{count, plural, =0 {no items} =1 {one item} other {# items}}",
162
+ "@itemCount": {
163
+ "description": "Number of items",
164
+ "placeholders": {
165
+ "count": {
166
+ "type": "int",
167
+ "example": "5"
168
+ }
169
+ }
170
+ }
171
+ });
172
+ const result = handler.parse(arbContent);
173
+ expect(result.itemCount).toBe("{count, plural, =0 {no items} =1 {one item} other {# items}}");
174
+ expect(result._metadata?.icuAnalysis?.itemCount?.hasIcuSyntax).toBe(true);
175
+ expect(result._metadata?.icuAnalysis?.itemCount?.messageType).toBe("plural");
176
+ expect(result._metadata?.icuAnalysis?.itemCount?.placeholders).toContain("count");
177
+ expect(result._metadata?.icuAnalysis?.itemCount?.pluralForms).toEqual(["=0", "=1", "other"]);
178
+ });
179
+ it("should parse ARB with select ICU messages", () => {
180
+ const arbContent = JSON.stringify({
181
+ "@@locale": "en",
182
+ "genderMessage": "{gender, select, male {He is} female {She is} other {They are}} here",
183
+ "@genderMessage": {
184
+ "description": "Gender-specific message",
185
+ "placeholders": {
186
+ "gender": {
187
+ "type": "String",
188
+ "example": "male"
189
+ }
190
+ }
191
+ }
192
+ });
193
+ const result = handler.parse(arbContent);
194
+ expect(result.genderMessage).toBe("{gender, select, male {He is} female {She is} other {They are}} here");
195
+ expect(result._metadata?.icuAnalysis?.genderMessage?.hasIcuSyntax).toBe(true);
196
+ expect(result._metadata?.icuAnalysis?.genderMessage?.messageType).toBe("select");
197
+ expect(result._metadata?.icuAnalysis?.genderMessage?.placeholders).toContain("gender");
198
+ expect(result._metadata?.icuAnalysis?.genderMessage?.selectOptions).toEqual(["male", "female", "other"]);
199
+ });
200
+ it("should parse ARB with complex nested ICU messages", () => {
201
+ const arbContent = JSON.stringify({
202
+ "@@locale": "en",
203
+ "complexMessage": "{count, plural, =0 {no {type}} =1 {one {type}} other {# {type, select, item {items} file {files} other {things}}}}",
204
+ "@complexMessage": {
205
+ "description": "Complex nested ICU message",
206
+ "placeholders": {
207
+ "count": {
208
+ "type": "int",
209
+ "example": "5"
210
+ },
211
+ "type": {
212
+ "type": "String",
213
+ "example": "item"
214
+ }
215
+ }
216
+ }
217
+ });
218
+ const result = handler.parse(arbContent);
219
+ expect(result.complexMessage).toBe("{count, plural, =0 {no {type}} =1 {one {type}} other {# {type, select, item {items} file {files} other {things}}}}");
220
+ expect(result._metadata?.icuAnalysis?.complexMessage?.hasIcuSyntax).toBe(true);
221
+ expect(result._metadata?.icuAnalysis?.complexMessage?.messageType).toBe("plural");
222
+ expect(result._metadata?.icuAnalysis?.complexMessage?.placeholders).toEqual(expect.arrayContaining(["count", "type"]));
223
+ });
224
+ it("should handle messages without ICU syntax", () => {
225
+ const arbContent = JSON.stringify({
226
+ "@@locale": "en",
227
+ "simpleMessage": "This is a simple message",
228
+ "@simpleMessage": {
229
+ "description": "A simple message without placeholders"
230
+ }
231
+ });
232
+ const result = handler.parse(arbContent);
233
+ expect(result.simpleMessage).toBe("This is a simple message");
234
+ expect(result._metadata?.icuAnalysis?.simpleMessage?.hasIcuSyntax).toBe(false);
235
+ expect(result._metadata?.icuAnalysis?.simpleMessage?.messageType).toBe("simple");
236
+ expect(result._metadata?.icuAnalysis?.simpleMessage?.placeholders).toEqual([]);
237
+ });
238
+ });
239
+ describe("parse - Metadata and placeholder preservation", () => {
240
+ it("should preserve all ARB metadata types", () => {
241
+ const arbContent = JSON.stringify({
242
+ "@@locale": "en_US",
243
+ "@@last_modified": "2023-01-01T00:00:00.000Z",
244
+ "@@author": "Test Author",
245
+ "@@context": "Test context",
246
+ "@@custom_metadata": "Custom value",
247
+ "hello": "Hello"
248
+ });
249
+ const result = handler.parse(arbContent);
250
+ expect(result._metadata?.arbMetadata).toEqual({
251
+ "@@locale": "en_US",
252
+ "@@last_modified": "2023-01-01T00:00:00.000Z",
253
+ "@@author": "Test Author",
254
+ "@@context": "Test context",
255
+ "@@custom_metadata": "Custom value"
256
+ });
257
+ });
258
+ it("should preserve complete resource metadata", () => {
259
+ const arbContent = JSON.stringify({
260
+ "@@locale": "en",
261
+ "greeting": "Hello {name}!",
262
+ "@greeting": {
263
+ "type": "text",
264
+ "description": "A personalized greeting",
265
+ "context": "informal",
266
+ "placeholders": {
267
+ "name": {
268
+ "type": "String",
269
+ "example": "John",
270
+ "description": "The person's name"
271
+ }
272
+ }
273
+ }
274
+ });
275
+ const result = handler.parse(arbContent);
276
+ expect(result._metadata?.resourceMetadata?.["@greeting"]).toEqual({
277
+ type: "text",
278
+ description: "A personalized greeting",
279
+ context: "informal",
280
+ placeholders: {
281
+ name: {
282
+ type: "String",
283
+ example: "John",
284
+ description: "The person's name"
285
+ }
286
+ }
287
+ });
288
+ });
289
+ it("should preserve complex placeholder metadata", () => {
290
+ const arbContent = JSON.stringify({
291
+ "@@locale": "en",
292
+ "orderStatus": "Order {orderId} for {customerName} is {status}",
293
+ "@orderStatus": {
294
+ "description": "Order status message",
295
+ "placeholders": {
296
+ "orderId": {
297
+ "type": "String",
298
+ "example": "ORD-12345",
299
+ "description": "Unique order identifier"
300
+ },
301
+ "customerName": {
302
+ "type": "String",
303
+ "example": "John Doe",
304
+ "description": "Customer full name"
305
+ },
306
+ "status": {
307
+ "type": "String",
308
+ "example": "shipped",
309
+ "description": "Current order status"
310
+ }
311
+ }
312
+ }
313
+ });
314
+ const result = handler.parse(arbContent);
315
+ expect(result._metadata?.resourceMetadata?.["@orderStatus"]?.placeholders).toEqual({
316
+ orderId: {
317
+ type: "String",
318
+ example: "ORD-12345",
319
+ description: "Unique order identifier"
320
+ },
321
+ customerName: {
322
+ type: "String",
323
+ example: "John Doe",
324
+ description: "Customer full name"
325
+ },
326
+ status: {
327
+ type: "String",
328
+ example: "shipped",
329
+ description: "Current order status"
330
+ }
331
+ });
332
+ });
333
+ it("should handle invalid resource metadata gracefully", () => {
334
+ const arbContent = JSON.stringify({
335
+ "@@locale": "en",
336
+ "hello": "Hello",
337
+ "@hello": "invalid metadata" // Should be an object
338
+ });
339
+ const result = handler.parse(arbContent);
340
+ expect(result.hello).toBe("Hello");
341
+ expect(result._metadata?.resourceMetadata?.["@hello"]).toEqual({});
342
+ });
343
+ it("should handle invalid placeholder metadata gracefully", () => {
344
+ const arbContent = JSON.stringify({
345
+ "@@locale": "en",
346
+ "greeting": "Hello {name}",
347
+ "@greeting": {
348
+ "description": "A greeting",
349
+ "placeholders": "invalid placeholders" // Should be an object
350
+ }
351
+ });
352
+ const result = handler.parse(arbContent);
353
+ expect(result.greeting).toBe("Hello {name}");
354
+ expect(result._metadata?.resourceMetadata?.["@greeting"]).toEqual({
355
+ description: "A greeting"
356
+ });
357
+ });
358
+ });
359
+ describe("serialize - Basic ARB serialization", () => {
360
+ it("should serialize ARB file with proper structure ordering", () => {
361
+ const data = {
362
+ hello: "Hello",
363
+ goodbye: "Goodbye",
364
+ _metadata: {
365
+ format: "arb",
366
+ arbMetadata: {
367
+ "@@locale": "en",
368
+ "@@author": "Test Author",
369
+ "@@last_modified": "2023-01-01T00:00:00.000Z"
370
+ },
371
+ resourceMetadata: {
372
+ "@hello": {
373
+ description: "A greeting"
374
+ }
375
+ }
376
+ }
377
+ };
378
+ const result = handler.serialize(data);
379
+ const parsed = JSON.parse(result);
380
+ // Check that @@locale comes first
381
+ const keys = Object.keys(parsed);
382
+ expect(keys[0]).toBe("@@locale");
383
+ expect(parsed["@@locale"]).toBe("en");
384
+ expect(parsed["@@author"]).toBe("Test Author");
385
+ expect(parsed.hello).toBe("Hello");
386
+ expect(parsed.goodbye).toBe("Goodbye");
387
+ expect(parsed["@hello"]).toEqual({
388
+ description: "A greeting"
389
+ });
390
+ expect(parsed["@@last_modified"]).toBeDefined();
391
+ });
392
+ it("should update @@last_modified timestamp by default", () => {
393
+ const data = {
394
+ hello: "Hello",
395
+ _metadata: {
396
+ format: "arb",
397
+ arbMetadata: {
398
+ "@@locale": "en",
399
+ "@@last_modified": "2020-01-01T00:00:00.000Z"
400
+ }
401
+ }
402
+ };
403
+ const result = handler.serialize(data);
404
+ const parsed = JSON.parse(result);
405
+ expect(parsed["@@last_modified"]).not.toBe("2020-01-01T00:00:00.000Z");
406
+ expect(new Date(parsed["@@last_modified"])).toBeInstanceOf(Date);
407
+ });
408
+ it("should preserve @@last_modified when updateTimestamp is false", () => {
409
+ const data = {
410
+ hello: "Hello",
411
+ _metadata: {
412
+ format: "arb",
413
+ arbMetadata: {
414
+ "@@locale": "en",
415
+ "@@last_modified": "2020-01-01T00:00:00.000Z"
416
+ }
417
+ }
418
+ };
419
+ const options = {
420
+ customSettings: {
421
+ updateTimestamp: false
422
+ }
423
+ };
424
+ const result = handler.serialize(data, options);
425
+ const parsed = JSON.parse(result);
426
+ expect(parsed["@@last_modified"]).toBe("2020-01-01T00:00:00.000Z");
427
+ });
428
+ it("should handle custom indentation", () => {
429
+ const data = {
430
+ hello: "Hello",
431
+ _metadata: {
432
+ format: "arb",
433
+ arbMetadata: {
434
+ "@@locale": "en"
435
+ }
436
+ }
437
+ };
438
+ const result = handler.serialize(data, { indentation: 4 });
439
+ expect(result).toContain(' "@@locale": "en"');
440
+ });
441
+ it("should serialize without metadata", () => {
442
+ const data = {
443
+ hello: "Hello",
444
+ goodbye: "Goodbye"
445
+ };
446
+ const result = handler.serialize(data);
447
+ const parsed = JSON.parse(result);
448
+ expect(parsed.hello).toBe("Hello");
449
+ expect(parsed.goodbye).toBe("Goodbye");
450
+ expect(Object.keys(parsed)).toEqual(["goodbye", "hello"]); // Sorted order
451
+ });
452
+ it("should throw error for serialization failure", () => {
453
+ const circularData = {};
454
+ circularData.self = circularData;
455
+ expect(() => handler.serialize(circularData)).toThrow("Failed to serialize ARB");
456
+ });
457
+ });
458
+ describe("serialize - ICU message format preservation", () => {
459
+ it("should preserve ICU plural messages", () => {
460
+ const data = {
461
+ itemCount: "{count, plural, =0 {no items} =1 {one item} other {# items}}",
462
+ _metadata: {
463
+ format: "arb",
464
+ arbMetadata: {
465
+ "@@locale": "en"
466
+ },
467
+ resourceMetadata: {
468
+ "@itemCount": {
469
+ description: "Number of items",
470
+ placeholders: {
471
+ count: {
472
+ type: "int",
473
+ example: "5"
474
+ }
475
+ }
476
+ }
477
+ }
478
+ }
479
+ };
480
+ const result = handler.serialize(data);
481
+ const parsed = JSON.parse(result);
482
+ expect(parsed.itemCount).toBe("{count, plural, =0 {no items} =1 {one item} other {# items}}");
483
+ expect(parsed["@itemCount"]).toEqual({
484
+ description: "Number of items",
485
+ placeholders: {
486
+ count: {
487
+ type: "int",
488
+ example: "5"
489
+ }
490
+ }
491
+ });
492
+ });
493
+ it("should preserve ICU select messages", () => {
494
+ const data = {
495
+ genderMessage: "{gender, select, male {He is} female {She is} other {They are}} here",
496
+ _metadata: {
497
+ format: "arb",
498
+ arbMetadata: {
499
+ "@@locale": "en"
500
+ },
501
+ resourceMetadata: {
502
+ "@genderMessage": {
503
+ description: "Gender-specific message",
504
+ placeholders: {
505
+ gender: {
506
+ type: "String",
507
+ example: "male"
508
+ }
509
+ }
510
+ }
511
+ }
512
+ }
513
+ };
514
+ const result = handler.serialize(data);
515
+ const parsed = JSON.parse(result);
516
+ expect(parsed.genderMessage).toBe("{gender, select, male {He is} female {She is} other {They are}} here");
517
+ expect(parsed["@genderMessage"]).toEqual({
518
+ description: "Gender-specific message",
519
+ placeholders: {
520
+ gender: {
521
+ type: "String",
522
+ example: "male"
523
+ }
524
+ }
525
+ });
526
+ });
527
+ });
528
+ describe("validateStructure", () => {
529
+ it("should validate valid ARB structure", () => {
530
+ const data = {
531
+ "@@locale": "en",
532
+ "hello": "Hello",
533
+ "@hello": {
534
+ "description": "A greeting"
535
+ }
536
+ };
537
+ const result = handler.validateStructure(data);
538
+ expect(result.isValid).toBe(true);
539
+ expect(result.errors).toHaveLength(0);
540
+ });
541
+ it("should reject non-object data", () => {
542
+ const result = handler.validateStructure("invalid data");
543
+ expect(result.isValid).toBe(false);
544
+ expect(result.errors[0].code).toBe("INVALID_STRUCTURE");
545
+ });
546
+ it("should warn about missing @@locale metadata", () => {
547
+ const data = {
548
+ "hello": "Hello"
549
+ };
550
+ const result = handler.validateStructure(data);
551
+ expect(result.warnings.some(w => w.code === "MISSING_LOCALE_METADATA")).toBe(true);
552
+ });
553
+ it("should validate ARB metadata types", () => {
554
+ const data = {
555
+ "@@locale": 123, // Should be string
556
+ "hello": "Hello"
557
+ };
558
+ const result = handler.validateStructure(data);
559
+ expect(result.isValid).toBe(false);
560
+ expect(result.errors.some(e => e.code === "INVALID_METADATA_TYPE")).toBe(true);
561
+ });
562
+ it("should warn about invalid locale format", () => {
563
+ const data = {
564
+ "@@locale": "invalid-locale-format",
565
+ "hello": "Hello"
566
+ };
567
+ const result = handler.validateStructure(data);
568
+ expect(result.warnings.some(w => w.code === "INVALID_LOCALE_FORMAT")).toBe(true);
569
+ });
570
+ it("should detect orphaned metadata", () => {
571
+ const data = {
572
+ "@@locale": "en",
573
+ "hello": "Hello",
574
+ "@goodbye": {
575
+ "description": "A farewell"
576
+ }
577
+ };
578
+ const result = handler.validateStructure(data);
579
+ expect(result.warnings.some(w => w.code === "ORPHANED_METADATA")).toBe(true);
580
+ });
581
+ it("should validate resource metadata structure", () => {
582
+ const data = {
583
+ "@@locale": "en",
584
+ "hello": "Hello",
585
+ "@hello": "invalid metadata" // Should be object
586
+ };
587
+ const result = handler.validateStructure(data);
588
+ expect(result.isValid).toBe(false);
589
+ expect(result.errors.some(e => e.code === "INVALID_RESOURCE_METADATA")).toBe(true);
590
+ });
591
+ it("should warn about unknown metadata properties", () => {
592
+ const data = {
593
+ "@@locale": "en",
594
+ "hello": "Hello",
595
+ "@hello": {
596
+ "description": "A greeting",
597
+ "unknownProperty": "value"
598
+ }
599
+ };
600
+ const result = handler.validateStructure(data);
601
+ expect(result.warnings.some(w => w.code === "UNKNOWN_METADATA_PROPERTY")).toBe(true);
602
+ });
603
+ it("should validate placeholder structure", () => {
604
+ const data = {
605
+ "@@locale": "en",
606
+ "greeting": "Hello {name}",
607
+ "@greeting": {
608
+ "placeholders": "invalid" // Should be object
609
+ }
610
+ };
611
+ const result = handler.validateStructure(data);
612
+ expect(result.isValid).toBe(false);
613
+ expect(result.errors.some(e => e.code === "INVALID_PLACEHOLDERS")).toBe(true);
614
+ });
615
+ it("should validate individual placeholder data", () => {
616
+ const data = {
617
+ "@@locale": "en",
618
+ "greeting": "Hello {name}",
619
+ "@greeting": {
620
+ "placeholders": {
621
+ "name": "invalid" // Should be object
622
+ }
623
+ }
624
+ };
625
+ const result = handler.validateStructure(data);
626
+ expect(result.isValid).toBe(false);
627
+ expect(result.errors.some(e => e.code === "INVALID_PLACEHOLDER_DATA")).toBe(true);
628
+ });
629
+ it("should detect ICU syntax errors - missing comma", () => {
630
+ const data = {
631
+ "@@locale": "en",
632
+ "badIcu": "{count plural =0 {no items}}", // Missing comma after count
633
+ };
634
+ const result = handler.validateStructure(data);
635
+ expect(result.isValid).toBe(false);
636
+ expect(result.errors.some(e => e.code === "ICU_MISSING_COMMA")).toBe(true);
637
+ });
638
+ it("should detect ICU syntax errors - bracket mismatch", () => {
639
+ const data = {
640
+ "@@locale": "en",
641
+ "badIcu": "{count, plural, =0 {no items} =1 {one item}", // Missing closing bracket
642
+ };
643
+ const result = handler.validateStructure(data);
644
+ expect(result.isValid).toBe(false);
645
+ expect(result.errors.some(e => e.code === "ICU_BRACKET_MISMATCH")).toBe(true);
646
+ });
647
+ it("should detect missing 'other' in plural forms", () => {
648
+ const data = {
649
+ "@@locale": "en",
650
+ "badPlural": "{count, plural, =0 {no items} =1 {one item}}", // Missing 'other'
651
+ };
652
+ const result = handler.validateStructure(data);
653
+ expect(result.isValid).toBe(false);
654
+ expect(result.errors.some(e => e.code === "ICU_MISSING_OTHER_PLURAL")).toBe(true);
655
+ });
656
+ it("should warn about missing 'other' in select forms", () => {
657
+ const data = {
658
+ "@@locale": "en",
659
+ "selectMessage": "{gender, select, male {He} female {She}}", // Missing 'other'
660
+ };
661
+ const result = handler.validateStructure(data);
662
+ expect(result.warnings.some(w => w.code === "ICU_MISSING_OTHER_SELECT")).toBe(true);
663
+ });
664
+ it("should detect placeholder metadata inconsistencies", () => {
665
+ const data = {
666
+ "@@locale": "en",
667
+ "greeting": "Hello {name}",
668
+ "@greeting": {
669
+ "placeholders": {
670
+ "wrongName": {
671
+ "type": "String"
672
+ }
673
+ }
674
+ }
675
+ };
676
+ const result = handler.validateStructure(data);
677
+ expect(result.warnings.some(w => w.code === "MISSING_PLACEHOLDER_METADATA")).toBe(true);
678
+ expect(result.warnings.some(w => w.code === "EXTRA_PLACEHOLDER_METADATA")).toBe(true);
679
+ });
680
+ it("should warn about ICU messages without placeholder metadata", () => {
681
+ const data = {
682
+ "@@locale": "en",
683
+ "greeting": "Hello {name}" // Has ICU but no metadata
684
+ };
685
+ const result = handler.validateStructure(data);
686
+ expect(result.warnings.some(w => w.code === "MISSING_PLACEHOLDER_METADATA")).toBe(true);
687
+ });
688
+ });
689
+ describe("getFileExtension", () => {
690
+ it("should return .arb extension", () => {
691
+ expect(handler.getFileExtension()).toBe(".arb");
692
+ });
693
+ });
694
+ });