-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcli-translate-workflow.test.ts
More file actions
814 lines (653 loc) · 25.2 KB
/
Copy pathcli-translate-workflow.test.ts
File metadata and controls
814 lines (653 loc) · 25.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
/**
* Integration Tests for Translation Workflows
* Tests complete translation workflows including preservation, caching, and file operations
*/
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import { TranslationService } from '../../src/services/translation.js';
import { FileTranslationService } from '../../src/services/file-translation.js';
import { DeepLClient, LanguageInfo } from '../../src/api/deepl-client.js';
import { ConfigService } from '../../src/storage/config.js';
import { CacheService } from '../../src/storage/cache.js';
import { Language } from '../../src/types/index.js';
import { TEST_API_KEY, createMockConfigService, createMockCacheService } from '../helpers';
describe('Translation Workflow Integration', () => {
const API_KEY = TEST_API_KEY;
let translationService: TranslationService;
let fileTranslationService: FileTranslationService;
let client: DeepLClient;
let mockConfig: ConfigService;
let mockCache: CacheService;
let tmpDir: string;
beforeEach(() => {
// Create temp directory for test files
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'deepl-workflow-test-'));
// Set up services
client = new DeepLClient(API_KEY);
mockConfig = createMockConfigService({
get: jest.fn(() => ({
auth: {},
api: { baseUrl: '' },
defaults: { targetLangs: [], formality: 'default', preserveFormatting: false },
cache: { enabled: true },
output: { format: 'text', color: true },
proxy: {},
})),
getValue: jest.fn((key: string) => {
if (key === 'cache.enabled') {return true;}
return undefined;
}),
});
mockCache = createMockCacheService();
translationService = new TranslationService(client, mockConfig, mockCache);
fileTranslationService = new FileTranslationService(translationService);
});
afterEach(() => {
// Cleanup temp directory
if (fs.existsSync(tmpDir)) {
fs.rmSync(tmpDir, { recursive: true, force: true });
}
});
describe('TranslationService - Basic Translation', () => {
it('should require non-empty text', async () => {
await expect(
translationService.translate('', { targetLang: 'es' })
).rejects.toThrow('Text cannot be empty');
});
it('should require target language', async () => {
await expect(
translationService.translate('Hello', { targetLang: '' as any })
).rejects.toThrow('Target language is required');
});
it('should translate basic text', async () => {
// Mock API response
jest.spyOn(client, 'translate').mockResolvedValue({
text: 'Hola',
detectedSourceLang: 'en',
});
const result = await translationService.translate('Hello', { targetLang: 'es' });
expect(result.text).toBe('Hola');
expect(result.detectedSourceLang).toBe('en');
});
it('should use config defaults for translation options', async () => {
mockConfig = createMockConfigService({
get: jest.fn(() => ({
auth: {},
api: { baseUrl: '' },
defaults: {
targetLangs: [],
sourceLang: 'en',
formality: 'more',
preserveFormatting: true,
},
cache: { enabled: true },
output: { format: 'text', color: true },
proxy: {},
})),
getValue: jest.fn(() => true),
});
translationService = new TranslationService(client, mockConfig, mockCache);
const translateSpy = jest.spyOn(client, 'translate').mockResolvedValue({
text: 'Hola',
});
await translationService.translate('Hello', { targetLang: 'es' });
expect(translateSpy).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
sourceLang: 'en',
formality: 'more',
preserveFormatting: true,
})
);
});
});
describe('TranslationService - Code Preservation', () => {
it('should preserve inline code blocks', async () => {
jest.spyOn(client, 'translate').mockResolvedValue({
text: 'Use the __CODE_0__ function',
});
const result = await translationService.translate(
'Use the `console.log()` function',
{ targetLang: 'es' },
{ preserveCode: true }
);
expect(result.text).toContain('`console.log()`');
});
it('should preserve multi-line code blocks', async () => {
jest.spyOn(client, 'translate').mockResolvedValue({
text: 'Here is an example: __CODE_0__',
});
const result = await translationService.translate(
'Here is an example:\n```js\nconst x = 1;\n```',
{ targetLang: 'es' },
{ preserveCode: true }
);
expect(result.text).toContain('```js\nconst x = 1;\n```');
});
it('should preserve multiple code blocks', async () => {
jest.spyOn(client, 'translate').mockResolvedValue({
text: '__CODE_2__ and __CODE_0__ and __CODE_1__',
});
const result = await translationService.translate(
'`foo` and `bar` and ```baz```',
{ targetLang: 'es' },
{ preserveCode: true }
);
// Order of code blocks is preserved after restoration
expect(result.text).toContain('`foo`');
expect(result.text).toContain('`bar`');
expect(result.text).toContain('```baz```');
});
});
describe('TranslationService - Variable Preservation', () => {
it('should preserve ${} variables', async () => {
jest.spyOn(client, 'translate').mockResolvedValue({
text: 'Hello __VAR_0__',
});
const result = await translationService.translate(
'Hello ${name}',
{ targetLang: 'es' }
);
expect(result.text).toContain('${name}');
});
it('should preserve {} variables', async () => {
jest.spyOn(client, 'translate').mockResolvedValue({
text: 'Hello __VAR_0__',
});
const result = await translationService.translate(
'Hello {name}',
{ targetLang: 'es' }
);
expect(result.text).toContain('{name}');
});
it('should preserve %s and %d placeholders', async () => {
jest.spyOn(client, 'translate').mockResolvedValue({
text: 'The value is __VAR_0__ and __VAR_1__',
});
const result = await translationService.translate(
'The value is %s and %d',
{ targetLang: 'es' }
);
expect(result.text).toContain('%s');
expect(result.text).toContain('%d');
});
it('should preserve multiple variables', async () => {
jest.spyOn(client, 'translate').mockResolvedValue({
text: '__VAR_0__ says: __VAR_1__ is __VAR_2__',
});
const result = await translationService.translate(
'${user} says: {item} is %s',
{ targetLang: 'es' }
);
expect(result.text).toContain('${user}');
expect(result.text).toContain('{item}');
expect(result.text).toContain('%s');
});
});
describe('TranslationService - Combined Preservation', () => {
it('should preserve both code blocks and variables', async () => {
jest.spyOn(client, 'translate').mockResolvedValue({
text: 'Use __CODE_0__ with __VAR_0__',
});
const result = await translationService.translate(
'Use `console.log()` with ${variable}',
{ targetLang: 'es' },
{ preserveCode: true }
);
expect(result.text).toContain('`console.log()`');
expect(result.text).toContain('${variable}');
});
});
describe('TranslationService - Caching', () => {
it('should use cached result when available', async () => {
const cachedResult = {
text: 'Hola (cached)',
detectedSourceLang: 'en',
};
(mockCache.get as jest.Mock).mockReturnValue(cachedResult);
const translateSpy = jest.spyOn(client, 'translate');
const result = await translationService.translate('Hello', { targetLang: 'es' });
expect(result.text).toBe('Hola (cached)');
expect(translateSpy).not.toHaveBeenCalled(); // API not called
expect(mockCache.get).toHaveBeenCalled();
});
it('should cache new translations', async () => {
jest.spyOn(client, 'translate').mockResolvedValue({
text: 'Hola',
});
await translationService.translate('Hello', { targetLang: 'es' });
expect(mockCache.set).toHaveBeenCalled();
});
it('should skip cache when cache is disabled', async () => {
mockConfig = {
get: () => ({ defaults: {} }),
getValue: (key: string) => {
if (key === 'cache.enabled') {return false;}
return undefined;
},
} as ConfigService;
translationService = new TranslationService(client, mockConfig, mockCache);
jest.spyOn(client, 'translate').mockResolvedValue({
text: 'Hola',
});
await translationService.translate('Hello', { targetLang: 'es' });
expect(mockCache.get).not.toHaveBeenCalled();
expect(mockCache.set).not.toHaveBeenCalled();
});
it('should skip cache when skipCache option is set', async () => {
jest.spyOn(client, 'translate').mockResolvedValue({
text: 'Hola',
});
await translationService.translate(
'Hello',
{ targetLang: 'es' },
{ skipCache: true }
);
expect(mockCache.get).not.toHaveBeenCalled();
expect(mockCache.set).not.toHaveBeenCalled();
});
});
describe('TranslationService - Multi-Target Translation', () => {
it('should translate to multiple languages in parallel', async () => {
jest.spyOn(client, 'translate')
.mockResolvedValueOnce({ text: 'Hola' })
.mockResolvedValueOnce({ text: 'Bonjour' })
.mockResolvedValueOnce({ text: 'Hallo' });
const results = await translationService.translateToMultiple(
'Hello',
['es', 'fr', 'de']
);
expect(results).toHaveLength(3);
expect(results[0]).toEqual({ targetLang: 'es', text: 'Hola' });
expect(results[1]).toEqual({ targetLang: 'fr', text: 'Bonjour' });
expect(results[2]).toEqual({ targetLang: 'de', text: 'Hallo' });
});
it('should require at least one target language', async () => {
await expect(
translationService.translateToMultiple('Hello', [])
).rejects.toThrow('At least one target language is required');
});
it('should pass skipCache option to underlying translate calls', async () => {
const translateSpy = jest.spyOn(translationService, 'translate').mockResolvedValue({
text: 'Hola',
});
await translationService.translateToMultiple(
'Hello',
['es'],
{ skipCache: true }
);
expect(translateSpy).toHaveBeenCalledWith(
'Hello',
expect.objectContaining({ targetLang: 'es' }),
{ skipCache: true }
);
});
});
describe('TranslationService - Batch Translation', () => {
it('should translate multiple texts in batch', async () => {
jest.spyOn(client, 'translateBatch').mockResolvedValue([
{ text: 'Hola' },
{ text: 'Adiós' },
]);
const results = await translationService.translateBatch(
['Hello', 'Goodbye'],
{ targetLang: 'es' }
);
expect(results).toHaveLength(2);
expect(results[0]?.text).toBe('Hola');
expect(results[1]?.text).toBe('Adiós');
});
it('should return empty array for empty input', async () => {
const results = await translationService.translateBatch([], { targetLang: 'es' });
expect(results).toEqual([]);
});
it('should use cached results when available', async () => {
(mockCache.get as jest.Mock)
.mockReturnValueOnce({ text: 'Hola (cached)' })
.mockReturnValueOnce(null);
jest.spyOn(client, 'translateBatch').mockResolvedValue([
{ text: 'Adiós' },
]);
const results = await translationService.translateBatch(
['Hello', 'Goodbye'],
{ targetLang: 'es' }
);
expect(results).toHaveLength(2);
expect(results[0]?.text).toBe('Hola (cached)');
expect(results[1]?.text).toBe('Adiós');
// Only non-cached text sent to API
expect(client.translateBatch).toHaveBeenCalledWith(
['Goodbye'],
expect.any(Object)
);
});
it('should cache newly translated results', async () => {
jest.spyOn(client, 'translateBatch').mockResolvedValue([
{ text: 'Hola' },
{ text: 'Adiós' },
]);
await translationService.translateBatch(
['Hello', 'Goodbye'],
{ targetLang: 'es' }
);
expect(mockCache.set).toHaveBeenCalledTimes(2);
});
it('should handle batch API errors gracefully', async () => {
jest.spyOn(client, 'translateBatch').mockRejectedValue(new Error('API error'));
await expect(
translationService.translateBatch(['Hello'], { targetLang: 'es' })
).rejects.toThrow('API error');
});
it('should chunk large batches (>50 texts)', async () => {
// Create 60 unique texts to avoid cache key collisions
const texts = Array.from({ length: 60 }, (_, i) => `Hello ${i}`);
jest.spyOn(client, 'translateBatch')
.mockResolvedValueOnce(Array.from({ length: 50 }, (_, i) => ({ text: `Hola ${i}` })))
.mockResolvedValueOnce(Array.from({ length: 10 }, (_, i) => ({ text: `Hola ${i + 50}` })));
const results = await translationService.translateBatch(texts, { targetLang: 'es' });
expect(results).toHaveLength(60);
expect(client.translateBatch).toHaveBeenCalledTimes(2);
});
});
describe('TranslationService - Usage and Languages', () => {
it('should get usage statistics with computed fields', async () => {
jest.spyOn(client, 'getUsage').mockResolvedValue({
characterCount: 5000,
characterLimit: 10000,
});
const usage = await translationService.getUsage();
expect(usage.characterCount).toBe(5000);
expect(usage.characterLimit).toBe(10000);
expect(usage.percentageUsed).toBe(50);
expect(usage.remaining).toBe(5000);
});
it('should get supported source languages', async () => {
const mockLanguages: LanguageInfo[] = [
{ language: 'en' as Language, name: 'English' },
{ language: 'de' as Language, name: 'German' },
];
jest.spyOn(client, 'getSupportedLanguages').mockResolvedValue(mockLanguages);
const languages = await translationService.getSupportedLanguages('source');
expect(languages).toEqual(mockLanguages);
});
it('should get supported target languages', async () => {
const mockLanguages: LanguageInfo[] = [
{ language: 'es' as Language, name: 'Spanish' },
{ language: 'fr' as Language, name: 'French' },
];
jest.spyOn(client, 'getSupportedLanguages').mockResolvedValue(mockLanguages);
const languages = await translationService.getSupportedLanguages('target');
expect(languages).toEqual(mockLanguages);
});
it('should cache language results for 24 hours', async () => {
const mockLanguages: LanguageInfo[] = [{ language: 'en' as Language, name: 'English' }];
const getSpy = jest.spyOn(client, 'getSupportedLanguages').mockResolvedValue(mockLanguages);
// First call
await translationService.getSupportedLanguages('source');
// Second call (should use cache)
await translationService.getSupportedLanguages('source');
expect(getSpy).toHaveBeenCalledTimes(1); // API called only once
});
});
describe('FileTranslationService - Single File Translation', () => {
it('should translate a text file', async () => {
const inputPath = path.join(tmpDir, 'input.txt');
const outputPath = path.join(tmpDir, 'output.txt');
fs.writeFileSync(inputPath, 'Hello world');
jest.spyOn(translationService, 'translate').mockResolvedValue({
text: 'Hola mundo',
});
await fileTranslationService.translateFile(
inputPath,
outputPath,
{ targetLang: 'es' }
);
expect(fs.existsSync(outputPath)).toBe(true);
const output = fs.readFileSync(outputPath, 'utf-8');
expect(output).toBe('Hola mundo');
});
it('should translate a markdown file', async () => {
const inputPath = path.join(tmpDir, 'input.md');
const outputPath = path.join(tmpDir, 'output.md');
fs.writeFileSync(inputPath, '# Hello\n\nWorld');
jest.spyOn(translationService, 'translate').mockResolvedValue({
text: '# Hola\n\nMundo',
});
await fileTranslationService.translateFile(
inputPath,
outputPath,
{ targetLang: 'es' }
);
expect(fs.existsSync(outputPath)).toBe(true);
});
it('should create output directory if it does not exist', async () => {
const inputPath = path.join(tmpDir, 'input.txt');
const outputPath = path.join(tmpDir, 'nested', 'dir', 'output.txt');
fs.writeFileSync(inputPath, 'Hello');
jest.spyOn(translationService, 'translate').mockResolvedValue({
text: 'Hola',
});
await fileTranslationService.translateFile(
inputPath,
outputPath,
{ targetLang: 'es' }
);
expect(fs.existsSync(outputPath)).toBe(true);
});
it('should throw error for non-existent input file', async () => {
await expect(
fileTranslationService.translateFile(
'/nonexistent/file.txt',
'/tmp/output.txt',
{ targetLang: 'es' }
)
).rejects.toThrow('Input file not found');
});
it('should throw error for unsupported file type', async () => {
const inputPath = path.join(tmpDir, 'image.png');
fs.writeFileSync(inputPath, Buffer.from([0x89, 0x50, 0x4e, 0x47]));
await expect(
fileTranslationService.translateFile(
inputPath,
'/tmp/output.txt',
{ targetLang: 'es' }
)
).rejects.toThrow('Unsupported file type');
});
it('should throw error for empty file', async () => {
const inputPath = path.join(tmpDir, 'empty.txt');
fs.writeFileSync(inputPath, '');
await expect(
fileTranslationService.translateFile(
inputPath,
'/tmp/output.txt',
{ targetLang: 'es' }
)
).rejects.toThrow('Cannot translate empty file');
});
it('should pass preserveCode option to translation service', async () => {
const inputPath = path.join(tmpDir, 'code.md');
const outputPath = path.join(tmpDir, 'output.md');
fs.writeFileSync(inputPath, 'Use `console.log()`');
const translateSpy = jest.spyOn(translationService, 'translate').mockResolvedValue({
text: 'Use `console.log()`',
});
await fileTranslationService.translateFile(
inputPath,
outputPath,
{ targetLang: 'es' },
{ preserveCode: true }
);
expect(translateSpy).toHaveBeenCalledWith(
expect.any(String),
expect.any(Object),
{ preserveCode: true }
);
});
});
describe('FileTranslationService - Multiple Target Languages', () => {
it('should translate file to multiple languages', async () => {
const inputPath = path.join(tmpDir, 'input.txt');
fs.writeFileSync(inputPath, 'Hello');
jest.spyOn(translationService, 'translateToMultiple').mockResolvedValue([
{ targetLang: 'es', text: 'Hola' },
{ targetLang: 'fr', text: 'Bonjour' },
]);
const results = await fileTranslationService.translateFileToMultiple(
inputPath,
['es', 'fr']
);
expect(results).toHaveLength(2);
expect(results[0]).toEqual({ targetLang: 'es', text: 'Hola' });
expect(results[1]).toEqual({ targetLang: 'fr', text: 'Bonjour' });
});
it('should write output files when outputDir is specified', async () => {
const inputPath = path.join(tmpDir, 'input.txt');
const outputDir = path.join(tmpDir, 'output');
fs.writeFileSync(inputPath, 'Hello');
jest.spyOn(translationService, 'translateToMultiple').mockResolvedValue([
{ targetLang: 'es', text: 'Hola' },
{ targetLang: 'fr', text: 'Bonjour' },
]);
const results = await fileTranslationService.translateFileToMultiple(
inputPath,
['es', 'fr'],
{ outputDir }
);
expect(results).toHaveLength(2);
expect(results[0]?.outputPath).toBe(path.join(outputDir, 'input.es.txt'));
expect(results[1]?.outputPath).toBe(path.join(outputDir, 'input.fr.txt'));
const outputPath0 = results[0]?.outputPath;
const outputPath1 = results[1]?.outputPath;
expect(outputPath0).toBeDefined();
expect(outputPath1).toBeDefined();
if (outputPath0 && outputPath1) {
expect(fs.existsSync(outputPath0)).toBe(true);
expect(fs.existsSync(outputPath1)).toBe(true);
expect(fs.readFileSync(outputPath0, 'utf-8')).toBe('Hola');
expect(fs.readFileSync(outputPath1, 'utf-8')).toBe('Bonjour');
}
});
it('should throw error for non-existent input file', async () => {
await expect(
fileTranslationService.translateFileToMultiple(
'/nonexistent/file.txt',
['es']
)
).rejects.toThrow('Input file not found');
});
it('should throw error for unsupported file type', async () => {
const inputPath = path.join(tmpDir, 'image.png');
fs.writeFileSync(inputPath, Buffer.from([0x89, 0x50]));
await expect(
fileTranslationService.translateFileToMultiple(
inputPath,
['es']
)
).rejects.toThrow('Unsupported file type');
});
it('should throw error for empty file', async () => {
const inputPath = path.join(tmpDir, 'empty.txt');
fs.writeFileSync(inputPath, '');
await expect(
fileTranslationService.translateFileToMultiple(
inputPath,
['es']
)
).rejects.toThrow('Cannot translate empty file');
});
});
describe('FileTranslationService - File Type Validation', () => {
it('should support .txt files', () => {
expect(fileTranslationService.isSupportedFile('test.txt')).toBe(true);
});
it('should support .md files', () => {
expect(fileTranslationService.isSupportedFile('test.md')).toBe(true);
});
it('should support case-insensitive extensions', () => {
expect(fileTranslationService.isSupportedFile('test.TXT')).toBe(true);
expect(fileTranslationService.isSupportedFile('test.MD')).toBe(true);
});
it('should reject unsupported file types', () => {
expect(fileTranslationService.isSupportedFile('test.pdf')).toBe(false);
expect(fileTranslationService.isSupportedFile('test.docx')).toBe(false);
expect(fileTranslationService.isSupportedFile('test.png')).toBe(false);
});
it('should reject directories', () => {
expect(fileTranslationService.isSupportedFile(tmpDir)).toBe(false);
});
it('should get list of supported file types', () => {
const types = fileTranslationService.getSupportedFileTypes();
expect(types).toContain('.txt');
expect(types).toContain('.md');
});
});
describe('Translation Workflow - Advanced Options', () => {
it('should translate with formality option', async () => {
const translateSpy = jest.spyOn(client, 'translate').mockResolvedValue({
text: 'Hola',
});
await translationService.translate('Hello', {
targetLang: 'es',
formality: 'more',
});
expect(translateSpy).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({ formality: 'more' })
);
});
it('should translate with source language specified', async () => {
const translateSpy = jest.spyOn(client, 'translate').mockResolvedValue({
text: 'Hola',
});
await translationService.translate('Hello', {
targetLang: 'es',
sourceLang: 'en',
});
expect(translateSpy).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({ sourceLang: 'en' })
);
});
it('should translate with glossary', async () => {
const translateSpy = jest.spyOn(client, 'translate').mockResolvedValue({
text: 'Hola',
});
await translationService.translate('Hello', {
targetLang: 'es',
glossaryId: 'glossary-123',
});
expect(translateSpy).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({ glossaryId: 'glossary-123' })
);
});
it('should translate with context', async () => {
const translateSpy = jest.spyOn(client, 'translate').mockResolvedValue({
text: 'Hola',
});
await translationService.translate('Hello', {
targetLang: 'es',
context: 'greeting',
});
expect(translateSpy).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({ context: 'greeting' })
);
});
it('should translate with preserveFormatting', async () => {
const translateSpy = jest.spyOn(client, 'translate').mockResolvedValue({
text: 'Hola',
});
await translationService.translate('Hello', {
targetLang: 'es',
preserveFormatting: true,
});
expect(translateSpy).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({ preserveFormatting: true })
);
});
});
});