-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcli-translate.integration.test.ts
More file actions
955 lines (860 loc) · 34.7 KB
/
Copy pathcli-translate.integration.test.ts
File metadata and controls
955 lines (860 loc) · 34.7 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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
/**
* Integration Tests for Translate CLI Command
* Tests the translate command CLI behavior, argument validation, and error handling
*/
import { execSync } from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import { createTestConfigDir, createTestDir, makeRunCLI } from '../helpers';
describe('Translate CLI Integration', () => {
const testConfig = createTestConfigDir('test-translate');
const testFiles = createTestDir('translate-files');
const testConfigDir = testConfig.path;
const testDir = testFiles.path;
const { runCLI } = makeRunCLI(testConfig.path);
// Cache help outputs to avoid redundant process spawns (~25 duplicate calls eliminated)
let translateHelp: string;
let mainHelp: string;
let styleRulesHelp: string;
let styleRulesListHelp: string;
beforeAll(() => {
translateHelp = runCLI('deepl translate --help');
mainHelp = runCLI('deepl --help');
styleRulesHelp = runCLI('deepl style-rules --help');
styleRulesListHelp = runCLI('deepl style-rules list --help');
});
afterAll(() => {
testConfig.cleanup();
testFiles.cleanup();
});
describe('deepl translate --help', () => {
it('should display help for translate command', () => {
const output = translateHelp;
expect(output).toContain('Usage:');
expect(output).toContain('deepl translate');
expect(output).toContain('Translate text, files, or directories');
expect(output).toContain('--to');
expect(output).toContain('--from');
expect(output).toContain('--output');
expect(output).toContain('--formality');
expect(output).toContain('--preserve-code');
expect(output).toContain('--context');
expect(output).toContain('--split-sentences');
expect(output).toContain('--tag-handling');
expect(output).toContain('--model-type');
});
});
describe('deepl translate without API key', () => {
it('should require API key to be configured', () => {
expect.assertions(1);
try {
runCLI('deepl translate "Hello" --to es', { excludeApiKey: true });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
// Should indicate API key is required
expect(output).toMatch(/API key|auth|not set/i);
}
});
});
describe('required arguments validation', () => {
it('should require --to flag or config default for translation', () => {
expect.assertions(1);
try {
runCLI('deepl translate "Hello"', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).toMatch(/target language|--to/i);
}
});
it('should accept text argument with --to flag', () => {
const helpOutput = translateHelp;
// Verify command structure accepts text argument
expect(helpOutput).toContain('[text]');
expect(helpOutput).toContain('--to <language>');
});
});
describe('default target language from config', () => {
const runCLINoApiKey = (command: string, options: { stdio?: any } = {}): string => {
const env: Record<string, string | undefined> = { ...process.env, DEEPL_CONFIG_DIR: testConfigDir };
delete env['DEEPL_API_KEY'];
return execSync(command, {
encoding: 'utf-8',
env,
...options,
});
};
it('should show error mentioning both --to and config when neither is set', () => {
// Clear any config that might have targetLangs set
const configPath = path.join(testConfigDir, 'config.json');
if (fs.existsSync(configPath)) {
fs.unlinkSync(configPath);
}
expect.assertions(2);
try {
runCLINoApiKey('deepl translate "Hello"', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).toMatch(/--to/);
expect(output).toMatch(/deepl init/);
}
});
it('should use config default target language when --to is omitted', () => {
const configPath = path.join(testConfigDir, 'config.json');
const config = {
auth: {},
api: {},
defaults: {
targetLangs: ['es'],
formality: 'default',
preserveFormatting: true,
},
cache: { enabled: true, maxSize: 1073741824, ttl: 2592000 },
output: { format: 'text', verbose: false, color: true },
watch: { debounceMs: 500, autoCommit: false, pattern: '*.md' },
};
fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf-8');
expect.assertions(1);
try {
// Without --to, should fall back to config default 'es'
// Will still fail because no API key, but should NOT fail on missing --to
runCLINoApiKey('deepl translate "Hello"', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
// Should fail on API key, not on missing target language
expect(output).toMatch(/API key|auth/i);
}
});
});
describe('file translation validation', () => {
it('should require --output flag for file translation', () => {
// Create a test file
const testFile = path.join(testDir, 'test.txt');
fs.writeFileSync(testFile, 'Hello world', 'utf-8');
expect.assertions(1);
try {
runCLI(`deepl translate "${testFile}" --to es`, { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
// Will fail on API key or output requirement
expect(output).toMatch(/API key|auth|output/i);
}
});
it('should accept file path with --to and --output flags', () => {
const testFile = path.join(testDir, 'test2.txt');
const outputFile = path.join(testDir, 'output.txt');
fs.writeFileSync(testFile, 'Hello', 'utf-8');
expect.assertions(2);
try {
// This will fail without API key, but should recognize valid arguments
runCLI(`deepl translate "${testFile}" --to es --output "${outputFile}"`, { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
// Should fail on API key, not argument validation
expect(output).toMatch(/API key|auth/i);
expect(output).not.toMatch(/output.*required/i);
}
});
it('should handle non-existent file gracefully', () => {
const nonExistentFile = path.join(testDir, 'does-not-exist.txt');
expect.assertions(1);
try {
runCLI(`deepl translate "${nonExistentFile}" --to es --output output.txt`, { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
// Should indicate file not found or API key missing
expect(output).toMatch(/not found|does not exist|API key|auth/i);
}
});
});
describe('directory translation validation', () => {
it('should require --output flag for directory translation', () => {
// Create a test directory with files
const testSubDir = path.join(testDir, 'subdir');
fs.mkdirSync(testSubDir, { recursive: true });
fs.writeFileSync(path.join(testSubDir, 'file1.txt'), 'Content 1', 'utf-8');
expect.assertions(1);
try {
runCLI(`deepl translate "${testSubDir}" --to es`, { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
// Will fail on API key or output requirement
expect(output).toMatch(/API key|auth|output/i);
}
});
it('should accept directory path with --to and --output flags', () => {
const testSubDir2 = path.join(testDir, 'subdir2');
const outputDir = path.join(testDir, 'output-dir');
fs.mkdirSync(testSubDir2, { recursive: true });
fs.writeFileSync(path.join(testSubDir2, 'file1.txt'), 'Content', 'utf-8');
expect.assertions(1);
try {
// This will fail without API key, but should recognize valid arguments
runCLI(`deepl translate "${testSubDir2}" --to es --output "${outputDir}"`, { stdio: 'pipe' });
// CLI accepted the arguments without error
expect(true).toBe(true);
} catch (error: any) {
const output = error.stderr ?? error.stdout;
// Should fail on API key, not argument validation
expect(output).toMatch(/API key|auth/i);
}
});
});
describe('option flags validation', () => {
it.each([
{ flag: '--from <language>', description: 'Source language' },
{ flag: '--formality <level>', descriptionPattern: /more.*less/i },
{ flag: '--preserve-code', description: 'code blocks' },
{ flag: '--context <text>', description: 'context' },
{ flag: '--split-sentences <mode>', descriptionPattern: /on.*off.*nonewlines/i },
{ flag: '--tag-handling <mode>', descriptionPattern: /xml.*html/i },
{ flag: '--model-type <type>', descriptionPattern: /quality_optimized|latency_optimized/i },
{ flag: '--no-recursive', description: 'subdirectories' },
{ flag: '--pattern <pattern>', description: 'Glob pattern' },
{ flag: '--concurrency <number>', description: 'parallel' },
{ flag: '--api-url <url>', description: 'Custom API endpoint' },
])('should accept $flag flag', ({ flag, description, descriptionPattern }) => {
const helpOutput = translateHelp;
expect(helpOutput).toContain(flag);
if (description) {
expect(helpOutput).toContain(description);
}
if (descriptionPattern) {
expect(helpOutput).toMatch(descriptionPattern);
}
});
});
describe('multiple target languages', () => {
it('should accept comma-separated target languages', () => {
const helpOutput = translateHelp;
expect(helpOutput).toContain('--to <language>');
expect(helpOutput).toContain('comma-separated');
});
it('should validate comma-separated format', () => {
// Help should indicate comma-separated format is supported
const helpOutput = translateHelp;
expect(helpOutput).toContain('comma-separated');
});
});
describe('command structure', () => {
it('should be available as a command', () => {
const helpOutput = mainHelp;
// Translate command should be listed in main help
expect(helpOutput).toContain('translate');
expect(helpOutput).toContain('Translate text, files, or directories');
});
it('should accept text as optional argument', () => {
const output = translateHelp;
// Text should be optional (can use stdin)
// Shown in usage line as [text]
expect(output).toContain('translate [options] [text]');
expect(output).toContain('Arguments:');
});
it('should support stdin input', () => {
const output = translateHelp;
// Help should mention stdin
expect(output).toMatch(/stdin|read from stdin/i);
});
});
describe('error messages', () => {
it('should show clear error when --to omitted and no config default', () => {
// Clear config to ensure no default targetLangs
const configPath = path.join(testConfigDir, 'config.json');
if (fs.existsSync(configPath)) {
fs.unlinkSync(configPath);
}
const env = { ...process.env, DEEPL_CONFIG_DIR: testConfigDir };
(env as Record<string, string | undefined>)['DEEPL_API_KEY'] = undefined;
expect.assertions(1);
try {
execSync('deepl translate "Hello"', {
encoding: 'utf-8',
env,
stdio: 'pipe',
});
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).toMatch(/--to|target/i);
}
});
it('should show clear error for file without output', () => {
const testFile = path.join(testDir, 'error-test.txt');
fs.writeFileSync(testFile, 'Test', 'utf-8');
expect.assertions(1);
try {
runCLI(`deepl translate "${testFile}" --to es`, { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).toMatch(/API key|auth|output/i);
}
});
});
describe('file type support', () => {
it('should support .txt files', () => {
const txtFile = path.join(testDir, 'test.txt');
const outputFile = path.join(testDir, 'output.txt');
fs.writeFileSync(txtFile, 'Test content', 'utf-8');
expect.assertions(2);
try {
// Will fail without API key but should recognize file type
runCLI(`deepl translate "${txtFile}" --to es --output "${outputFile}"`, { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
// Should fail on auth, not unsupported file type
expect(output).toMatch(/API key|auth/i);
expect(output).not.toMatch(/unsupported.*file/i);
}
});
it('should support .md files', () => {
const mdFile = path.join(testDir, 'test.md');
const outputFile = path.join(testDir, 'output.md');
fs.writeFileSync(mdFile, '# Test\n\nContent', 'utf-8');
expect.assertions(2);
try {
// Will fail without API key but should recognize file type
runCLI(`deepl translate "${mdFile}" --to es --output "${outputFile}"`, { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
// Should fail on auth, not unsupported file type
expect(output).toMatch(/API key|auth/i);
expect(output).not.toMatch(/unsupported.*file/i);
}
});
});
describe('output format', () => {
it('should support --format option', () => {
// Help should show --format option
const output = translateHelp;
expect(output).toContain('--format <format>');
expect(output).toContain('json');
expect(output).toContain('text');
});
});
describe('billed characters', () => {
it('should show --show-billed-characters flag in help', () => {
const helpOutput = translateHelp;
expect(helpOutput).toContain('--show-billed-characters');
expect(helpOutput).toMatch(/billed.*character/i);
expect(helpOutput).toMatch(/cost.*transparency/i);
});
it('should accept --show-billed-characters flag without error', () => {
// Verify the flag is recognized (will fail on API key but shouldn't error on unknown flag)
expect.assertions(2);
try {
runCLI('deepl translate "Hello" --to es --show-billed-characters', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
// Should fail on API key, not on unknown option
expect(output).not.toMatch(/unknown.*option.*show-billed-characters/i);
expect(output).toMatch(/API key|auth/i);
}
});
});
describe('document minification', () => {
it('should show --enable-minification flag in help', () => {
const helpOutput = translateHelp;
expect(helpOutput).toContain('--enable-minification');
expect(helpOutput).toMatch(/minification/i);
expect(helpOutput).toMatch(/pptx|docx/i);
});
it('should accept --enable-minification flag without error', () => {
// Create a test PPTX file (mock)
const pptxFile = path.join(testDir, 'presentation.pptx');
const outputFile = path.join(testDir, 'output.pptx');
fs.writeFileSync(pptxFile, 'Mock PPTX content', 'utf-8');
expect.assertions(2);
try {
runCLI(`deepl translate "${pptxFile}" --to es --output "${outputFile}" --enable-minification`, { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
// Should fail on API key, not on unknown option
expect(output).not.toMatch(/unknown.*option.*enable-minification/i);
expect(output).toMatch(/API key|auth/i);
}
});
it('should recognize --enable-minification flag in command', () => {
// Verify the flag doesn't cause "unknown option" error
const pptxFile = path.join(testDir, 'test.pptx');
const outputFile = path.join(testDir, 'out.pptx');
fs.writeFileSync(pptxFile, 'content', 'utf-8');
expect.assertions(1);
try {
runCLI(`deepl translate "${pptxFile}" --to fr --output "${outputFile}" --enable-minification`, { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout ?? error.message;
// Should not contain unknown option error
expect(output).not.toMatch(/unknown.*option/i);
}
});
});
describe('XML tag handling parameters', () => {
it.each([
{
flag: '--outline-detection',
patterns: [/xml.*structure.*detection/i, /tag-handling.*xml/i],
},
{
flag: '--splitting-tags',
patterns: [/xml.*tags.*split/i],
},
{
flag: '--non-splitting-tags',
patterns: [/should not be used to split sentences/i],
normalizeWhitespace: true,
},
{
flag: '--ignore-tags',
patterns: [/ignore/i],
},
])('should show $flag flag in help', ({ flag, patterns, normalizeWhitespace }) => {
const helpOutput = translateHelp;
expect(helpOutput).toContain(flag);
for (const pattern of patterns) {
const text = normalizeWhitespace ? helpOutput.replace(/\s+/g, ' ') : helpOutput;
expect(text).toMatch(pattern);
}
});
it.each([
{
flag: '--outline-detection',
args: '--outline-detection true',
},
{
flag: '--splitting-tags',
args: '--splitting-tags "br,hr,div"',
},
{
flag: '--non-splitting-tags',
args: '--non-splitting-tags "code,pre"',
},
{
flag: '--ignore-tags',
args: '--ignore-tags "script,style"',
},
])('should accept $flag flag without error', ({ flag, args }) => {
expect.assertions(2);
try {
runCLI(`deepl translate "<p>Hello</p>" --to es --tag-handling xml ${args}`, { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).not.toMatch(new RegExp(`unknown.*option.*${flag.replace('--', '')}`, 'i'));
expect(output).toMatch(/API key|auth/i);
}
});
it('should accept all XML tag handling flags together', () => {
expect.assertions(2);
try {
runCLI('deepl translate "<p>Hello</p>" --to es --tag-handling xml --outline-detection false --splitting-tags "br,hr" --non-splitting-tags "code" --ignore-tags "script"', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).not.toMatch(/unknown.*option/i);
expect(output).toMatch(/API key|auth/i);
}
});
});
describe('table output format', () => {
it('should show table format option in help', () => {
const helpOutput = translateHelp;
expect(helpOutput).toContain('--format <format>');
expect(helpOutput).toMatch(/json/i);
});
it('should accept --format table flag without error', () => {
expect.assertions(3);
try {
runCLI('deepl translate "Hello" --to es,fr,de --format table', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
// Should fail on API key, not on unknown format
expect(output).not.toMatch(/unknown.*format.*table/i);
expect(output).not.toMatch(/invalid.*format/i);
expect(output).toMatch(/API key|auth/i);
}
});
it('should recognize table format as valid option', () => {
expect.assertions(2);
try {
runCLI('deepl translate "Test" --to es,fr --format table', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout ?? error.message;
// Should not contain invalid format error
expect(output).not.toMatch(/invalid.*format/i);
expect(output).not.toMatch(/unknown.*option/i);
}
});
it('should work with multiple target languages', () => {
expect.assertions(2);
try {
runCLI('deepl translate "Hello world" --to es,fr,de,ja --format table', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
// Should not reject the format or language combination
expect(output).not.toMatch(/invalid.*format/i);
expect(output).not.toMatch(/unknown.*option/i);
}
});
it('should accept table format with other options', () => {
expect.assertions(2);
try {
runCLI('deepl translate "Test" --to es,fr --format table --formality more --context "Business email"', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
// Should accept combination of options
expect(output).not.toMatch(/invalid.*format/i);
expect(output).toMatch(/API key|auth/i);
}
});
it('should accept table format with --show-billed-characters', () => {
expect.assertions(3);
try {
runCLI('deepl translate "Test" --to es,fr,de --format table --show-billed-characters --no-cache', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
// Should accept both flags together
expect(output).not.toMatch(/invalid.*format/i);
expect(output).not.toMatch(/unknown.*option/i);
expect(output).toMatch(/API key|auth/i);
}
});
});
describe('--custom-instruction flag', () => {
it('should display --custom-instruction in help text', () => {
const output = translateHelp;
expect(output).toContain('--custom-instruction');
});
it('should accept a single --custom-instruction flag', () => {
expect.assertions(2);
try {
runCLI('deepl translate "Hello" --to es --custom-instruction "Use informal tone"', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
// Should fail on API key, not flag parsing
expect(output).not.toMatch(/unknown.*option/i);
expect(output).toMatch(/API key|auth/i);
}
});
it('should accept multiple --custom-instruction flags', () => {
expect.assertions(2);
try {
runCLI('deepl translate "Hello" --to es --custom-instruction "Use informal tone" --custom-instruction "Preserve brand names"', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
// Should fail on API key, not flag parsing
expect(output).not.toMatch(/unknown.*option/i);
expect(output).toMatch(/API key|auth/i);
}
});
it('should accept --custom-instruction with other options', () => {
expect.assertions(2);
try {
runCLI('deepl translate "Hello" --to es --formality more --custom-instruction "Keep it formal" --model-type quality_optimized', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).not.toMatch(/unknown.*option/i);
expect(output).toMatch(/API key|auth/i);
}
});
});
describe('--style-id flag', () => {
it('should show --style-id in help text', () => {
const result = translateHelp;
expect(result).toContain('--style-id');
});
it('should accept --style-id flag', () => {
expect.assertions(2);
try {
runCLI('deepl translate "Hello" --to es --style-id "abc-123-def"', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).not.toMatch(/unknown.*option/i);
expect(output).toMatch(/API key|auth/i);
}
});
it('should accept --style-id with other options', () => {
expect.assertions(2);
try {
runCLI('deepl translate "Hello" --to es --style-id "abc-123" --formality more', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).not.toMatch(/unknown.*option/i);
expect(output).toMatch(/API key|auth/i);
}
});
});
describe('--enable-beta-languages flag', () => {
it('should show --enable-beta-languages in help text', () => {
const result = translateHelp;
expect(result).toContain('--enable-beta-languages');
});
it('should accept --enable-beta-languages flag', () => {
expect.assertions(2);
try {
runCLI('deepl translate "Hello" --to es --enable-beta-languages', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).not.toMatch(/unknown.*option/i);
expect(output).toMatch(/API key|auth/i);
}
});
it('should accept --enable-beta-languages with other options', () => {
expect.assertions(2);
try {
runCLI('deepl translate "Hello" --to es --enable-beta-languages --formality more', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).not.toMatch(/unknown.*option/i);
expect(output).toMatch(/API key|auth/i);
}
});
});
describe('style-rules command', () => {
it('should show style-rules in main help', () => {
const result = mainHelp;
expect(result).toContain('style-rules');
});
it('should show list subcommand in style-rules help', () => {
const result = styleRulesHelp;
expect(result).toContain('list');
});
it('should show --detailed flag in style-rules list help', () => {
const result = styleRulesListHelp;
expect(result).toContain('--detailed');
expect(result).toContain('--page');
expect(result).toContain('--page-size');
expect(result).toContain('--format');
});
it('should require API key for style-rules list', () => {
expect.assertions(1);
try {
runCLI('deepl style-rules list', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).toMatch(/API key|auth/i);
}
});
});
describe('expanded language support', () => {
it('should accept extended language codes', () => {
expect.assertions(2);
try {
runCLI('deepl translate "Hello" --to sw', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
// Should fail on API key, not language validation
expect(output).not.toMatch(/Invalid target language/i);
expect(output).toMatch(/API key|auth/i);
}
});
it('should accept ES-419 target variant', () => {
expect.assertions(2);
try {
runCLI('deepl translate "Hello" --to es-419', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).not.toMatch(/Invalid target language/i);
expect(output).toMatch(/API key|auth/i);
}
});
it('should accept zh-hans and zh-hant variants', () => {
expect.assertions(2);
try {
runCLI('deepl translate "Hello" --to zh-hans', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).not.toMatch(/Invalid target language/i);
expect(output).toMatch(/API key|auth/i);
}
});
it('should accept newly added core languages (he, vi)', () => {
expect.assertions(2);
try {
runCLI('deepl translate "Hello" --to he', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).not.toMatch(/Invalid target language/i);
expect(output).toMatch(/API key|auth/i);
}
});
});
describe('--tag-handling-version flag', () => {
it('should show --tag-handling-version in help text', () => {
const result = translateHelp;
expect(result).toContain('--tag-handling-version');
});
it('should accept --tag-handling-version with --tag-handling', () => {
expect.assertions(2);
try {
runCLI('deepl translate "<p>Hello</p>" --to es --tag-handling html --tag-handling-version v2', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).not.toMatch(/unknown.*option/i);
expect(output).toMatch(/API key|auth/i);
}
});
});
describe('--api-url HTTPS enforcement', () => {
const runCLIWithKey = (command: string): string => {
return execSync(command, {
encoding: 'utf-8',
timeout: 3000,
env: {
...process.env,
DEEPL_CONFIG_DIR: testConfigDir,
DEEPL_API_KEY: 'fake-key-for-url-validation',
},
});
};
it('should reject http:// URLs for remote hosts', () => {
expect.assertions(2);
try {
runCLIWithKey('deepl translate "Hello" --to es --api-url http://evil-server.com/v2');
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).toMatch(/Insecure HTTP URL rejected/i);
expect(output).toMatch(/credential exposure/i);
}
});
it('should accept https:// URLs', () => {
expect.assertions(1);
try {
runCLIWithKey('deepl translate "Hello" --to es --api-url https://api-free.deepl.com/v2');
} catch (error: any) {
const output = error.stderr ?? error.stdout;
// Should NOT fail on URL validation; may fail on auth or network
expect(output).not.toMatch(/Insecure HTTP URL rejected/i);
}
});
it('should allow http://localhost for local testing', () => {
expect.assertions(1);
try {
runCLIWithKey('deepl translate "Hello" --to es --api-url http://localhost:3000/v2');
} catch (error: any) {
const output = error.stderr ?? error.stdout;
// Should NOT fail on URL validation; may fail on connection
expect(output).not.toMatch(/Insecure HTTP URL rejected/i);
}
});
it('should allow http://127.0.0.1 for local testing', () => {
expect.assertions(1);
try {
runCLIWithKey('deepl translate "Hello" --to es --api-url http://127.0.0.1:5000/v2');
} catch (error: any) {
const output = error.stderr ?? error.stdout;
// Should NOT fail on URL validation; may fail on connection
expect(output).not.toMatch(/Insecure HTTP URL rejected/i);
}
});
});
describe('choices validation for enum options', () => {
it('should reject invalid --formality value', () => {
expect.assertions(8);
try {
runCLI('deepl translate "Hello" --to es --formality super_formal', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).toMatch(/--formality/);
expect(output).toMatch(/invalid/i);
expect(output).toMatch(/Allowed choices/i);
expect(output).toContain('default');
expect(output).toContain('more');
expect(output).toContain('less');
expect(output).toContain('prefer_more');
expect(output).toContain('prefer_less');
}
});
it('should accept all valid --formality values', () => {
const validValues = ['default', 'more', 'less', 'prefer_more', 'prefer_less'];
expect.assertions(validValues.length);
for (const value of validValues) {
try {
runCLI(`deepl translate "Hello" --to es --formality ${value}`, { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout ?? '';
expect(output).not.toMatch(/invalid.*Allowed choices/i);
}
}
});
it('should reject invalid --tag-handling value', () => {
expect.assertions(5);
try {
runCLI('deepl translate "Hello" --to es --tag-handling json', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).toMatch(/--tag-handling/);
expect(output).toMatch(/invalid/i);
expect(output).toMatch(/Allowed choices/i);
expect(output).toContain('xml');
expect(output).toContain('html');
}
});
it('should accept valid --tag-handling values', () => {
const validValues = ['xml', 'html'];
expect.assertions(validValues.length);
for (const value of validValues) {
try {
runCLI(`deepl translate "<p>Hello</p>" --to es --tag-handling ${value}`, { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout ?? '';
expect(output).not.toMatch(/invalid.*Allowed choices/i);
}
}
});
it('should reject invalid --model-type value', () => {
expect.assertions(6);
try {
runCLI('deepl translate "Hello" --to es --model-type fast', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).toMatch(/--model-type/);
expect(output).toMatch(/invalid/i);
expect(output).toMatch(/Allowed choices/i);
expect(output).toContain('quality_optimized');
expect(output).toContain('prefer_quality_optimized');
expect(output).toContain('latency_optimized');
}
});
it('should accept valid --model-type values', () => {
const validValues = ['quality_optimized', 'prefer_quality_optimized', 'latency_optimized'];
expect.assertions(validValues.length);
for (const value of validValues) {
try {
runCLI(`deepl translate "Hello" --to es --model-type ${value}`, { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout ?? '';
expect(output).not.toMatch(/invalid.*Allowed choices/i);
}
}
});
it('should reject invalid --split-sentences value', () => {
expect.assertions(6);
try {
runCLI('deepl translate "Hello" --to es --split-sentences always', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).toMatch(/--split-sentences/);
expect(output).toMatch(/invalid/i);
expect(output).toMatch(/Allowed choices/i);
expect(output).toContain('on');
expect(output).toContain('off');
expect(output).toContain('nonewlines');
}
});
it('should accept valid --split-sentences values', () => {
const validValues = ['on', 'off', 'nonewlines'];
expect.assertions(validValues.length);
for (const value of validValues) {
try {
runCLI(`deepl translate "Hello" --to es --split-sentences ${value}`, { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout ?? '';
expect(output).not.toMatch(/invalid.*Allowed choices/i);
}
}
});
it('should show choices in help text for constrained options', () => {
const helpOutput = translateHelp;
expect(helpOutput).toMatch(/--formality.*choices/is);
expect(helpOutput).toMatch(/--tag-handling.*choices/is);
expect(helpOutput).toMatch(/--model-type.*choices/is);
expect(helpOutput).toMatch(/--split-sentences.*choices/is);
});
});
});