-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcli-style-rules.integration.test.ts
More file actions
639 lines (544 loc) · 20.1 KB
/
Copy pathcli-style-rules.integration.test.ts
File metadata and controls
639 lines (544 loc) · 20.1 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
/**
* Integration Tests for Style Rules CLI Command
* Tests CLI argument parsing, HTTP request structure with nock, and error handling
*/
import nock from 'nock';
import { DeepLClient } from '../../src/api/deepl-client.js';
import { StyleRulesService } from '../../src/services/style-rules.js';
import { StyleRulesCommand } from '../../src/cli/commands/style-rules.js';
import { createTestConfigDir, makeRunCLI, DEEPL_FREE_API_URL } from '../helpers';
describe('Style Rules CLI Integration', () => {
const testConfig = createTestConfigDir('style-rules');
const { runCLI } = makeRunCLI(testConfig.path);
afterAll(() => {
testConfig.cleanup();
});
describe('deepl style-rules --help', () => {
it('should display help for style-rules command', () => {
const output = runCLI('deepl style-rules --help');
expect(output).toContain('Usage:');
expect(output).toContain('style-rules');
expect(output).toContain('Manage DeepL style rules');
expect(output).toContain('list');
});
it('should display help for style-rules list subcommand', () => {
const output = runCLI('deepl style-rules list --help');
expect(output).toContain('List all style rules');
expect(output).toContain('--detailed');
expect(output).toContain('--page');
expect(output).toContain('--page-size');
expect(output).toContain('--format');
});
});
describe('deepl style-rules without API key', () => {
beforeEach(() => {
try {
runCLI('deepl auth clear', { stdio: 'pipe' });
} catch {
// Ignore if already cleared
}
});
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|not set/i);
}
});
it('should require API key for style-rules list --detailed', () => {
expect.assertions(1);
try {
runCLI('deepl style-rules list --detailed', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).toMatch(/API key|auth|not set/i);
}
});
it('should require API key for style-rules list with pagination', () => {
expect.assertions(1);
try {
runCLI('deepl style-rules list --page 1 --page-size 10', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).toMatch(/API key|auth|not set/i);
}
});
});
describe('option flags validation', () => {
it('should accept --detailed flag without error', () => {
try {
runCLI('deepl style-rules list --detailed', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).not.toMatch(/unknown.*option.*detailed/i);
expect(output).toMatch(/API key|auth/i);
}
});
it('should accept --page flag without error', () => {
try {
runCLI('deepl style-rules list --page 2', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).not.toMatch(/unknown.*option.*page/i);
expect(output).toMatch(/API key|auth/i);
}
});
it('should accept --page-size flag without error', () => {
try {
runCLI('deepl style-rules list --page-size 10', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).not.toMatch(/unknown.*option.*page-size/i);
expect(output).toMatch(/API key|auth/i);
}
});
it('should accept --format json flag without error', () => {
try {
runCLI('deepl style-rules list --format json', { stdio: 'pipe' });
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).not.toMatch(/unknown.*option.*format/i);
expect(output).toMatch(/API key|auth/i);
}
});
it('should accept all flags combined', () => {
try {
runCLI('deepl style-rules list --detailed --page 1 --page-size 5 --format json', { 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('command structure', () => {
it('should be listed in main help', () => {
const output = runCLI('deepl --help');
expect(output).toContain('style-rules');
});
it('should describe as Pro API only', () => {
const output = runCLI('deepl style-rules --help');
expect(output).toContain('Pro API only');
});
it('should have list as a subcommand', () => {
const output = runCLI('deepl style-rules --help');
expect(output).toContain('list');
expect(output).toContain('List all style rules');
});
});
});
describe('Style Rules API Integration', () => {
const API_KEY = 'test-api-key-123:fx';
const FREE_API_URL = DEEPL_FREE_API_URL;
let client: DeepLClient;
let styleRulesCommand: StyleRulesCommand;
beforeEach(() => {
client = new DeepLClient(API_KEY);
styleRulesCommand = new StyleRulesCommand(new StyleRulesService(client));
});
afterEach(() => {
client.destroy();
nock.cleanAll();
});
describe('list - happy path', () => {
it('should make GET request to /v3/style_rules', async () => {
const scope = nock(FREE_API_URL)
.get('/v3/style_rules')
.reply(200, {
style_rules: [
{
style_id: 'abc-123',
name: 'Business Writing',
language: 'en',
version: 1,
creation_time: '2024-01-01T00:00:00Z',
updated_time: '2024-01-02T00:00:00Z',
},
],
});
const rules = await styleRulesCommand.list();
expect(rules).toHaveLength(1);
expect(rules[0]?.styleId).toBe('abc-123');
expect(rules[0]?.name).toBe('Business Writing');
expect(rules[0]?.language).toBe('en');
expect(rules[0]?.version).toBe(1);
expect(rules[0]?.creationTime).toBe('2024-01-01T00:00:00Z');
expect(rules[0]?.updatedTime).toBe('2024-01-02T00:00:00Z');
expect(scope.isDone()).toBe(true);
});
it('should return multiple style rules', async () => {
const scope = nock(FREE_API_URL)
.get('/v3/style_rules')
.reply(200, {
style_rules: [
{
style_id: 'abc-123',
name: 'Business Writing',
language: 'en',
version: 1,
creation_time: '2024-01-01T00:00:00Z',
updated_time: '2024-01-02T00:00:00Z',
},
{
style_id: 'def-456',
name: 'Academic Style',
language: 'de',
version: 2,
creation_time: '2024-02-01T00:00:00Z',
updated_time: '2024-02-15T00:00:00Z',
},
],
});
const rules = await styleRulesCommand.list();
expect(rules).toHaveLength(2);
expect(rules[0]?.name).toBe('Business Writing');
expect(rules[1]?.name).toBe('Academic Style');
expect(rules[1]?.language).toBe('de');
expect(scope.isDone()).toBe(true);
});
it('should handle empty style rules list', async () => {
const scope = nock(FREE_API_URL)
.get('/v3/style_rules')
.reply(200, {
style_rules: [],
});
const rules = await styleRulesCommand.list();
expect(rules).toHaveLength(0);
expect(scope.isDone()).toBe(true);
});
it('should format empty results correctly', async () => {
nock(FREE_API_URL)
.get('/v3/style_rules')
.reply(200, { style_rules: [] });
const rules = await styleRulesCommand.list();
const output = styleRulesCommand.formatStyleRulesList(rules);
expect(output).toBe('No style rules found.');
});
it('should format style rules list output correctly', async () => {
nock(FREE_API_URL)
.get('/v3/style_rules')
.reply(200, {
style_rules: [
{
style_id: 'abc-123',
name: 'Business Writing',
language: 'en',
version: 1,
creation_time: '2024-01-01T00:00:00Z',
updated_time: '2024-01-02T00:00:00Z',
},
],
});
const rules = await styleRulesCommand.list();
const output = styleRulesCommand.formatStyleRulesList(rules);
expect(output).toContain('Found 1 style rule(s)');
expect(output).toContain('Business Writing');
expect(output).toContain('abc-123');
expect(output).toContain('en');
});
});
describe('list --detailed', () => {
it('should pass detailed=true query parameter', async () => {
const scope = nock(FREE_API_URL)
.get('/v3/style_rules')
.query({ detailed: true })
.reply(200, {
style_rules: [
{
style_id: 'abc-123',
name: 'Business Writing',
language: 'en',
version: 1,
creation_time: '2024-01-01T00:00:00Z',
updated_time: '2024-01-02T00:00:00Z',
configured_rules: ['no_passive_voice', 'short_sentences'],
custom_instructions: [
{ label: 'Voice', prompt: 'Use active voice' },
{ label: 'Length', prompt: 'Keep sentences under 20 words' },
],
},
],
});
const rules = await styleRulesCommand.list({ detailed: true });
expect(rules).toHaveLength(1);
const rule = rules[0] as any;
expect(rule.configuredRules).toEqual(['no_passive_voice', 'short_sentences']);
expect(rule.customInstructions).toEqual([
{ label: 'Voice', prompt: 'Use active voice' },
{ label: 'Length', prompt: 'Keep sentences under 20 words' },
]);
expect(scope.isDone()).toBe(true);
});
it('should format detailed style rules with configured rules and instructions', async () => {
nock(FREE_API_URL)
.get('/v3/style_rules')
.query({ detailed: true })
.reply(200, {
style_rules: [
{
style_id: 'abc-123',
name: 'Business Writing',
language: 'en',
version: 1,
creation_time: '2024-01-01T00:00:00Z',
updated_time: '2024-01-02T00:00:00Z',
configured_rules: ['rule_a', 'rule_b'],
custom_instructions: [
{ label: 'Instruction one', prompt: 'Do this' },
{ label: 'Instruction two', prompt: 'Do that' },
],
},
],
});
const rules = await styleRulesCommand.list({ detailed: true });
const output = styleRulesCommand.formatStyleRulesList(rules);
expect(output).toContain('rule_a, rule_b');
expect(output).toContain('Instruction one');
expect(output).toContain('Instruction two');
});
});
describe('list with pagination', () => {
it('should pass page query parameter', async () => {
const scope = nock(FREE_API_URL)
.get('/v3/style_rules')
.query({ page: 2 })
.reply(200, { style_rules: [] });
await styleRulesCommand.list({ page: 2 });
expect(scope.isDone()).toBe(true);
});
it('should pass page_size query parameter', async () => {
const scope = nock(FREE_API_URL)
.get('/v3/style_rules')
.query({ page_size: 10 })
.reply(200, { style_rules: [] });
await styleRulesCommand.list({ pageSize: 10 });
expect(scope.isDone()).toBe(true);
});
it('should pass both page and page_size query parameters', async () => {
const scope = nock(FREE_API_URL)
.get('/v3/style_rules')
.query({ page: 1, page_size: 5 })
.reply(200, {
style_rules: [
{
style_id: 'abc-123',
name: 'Paginated Rule',
language: 'en',
version: 1,
creation_time: '2024-01-01T00:00:00Z',
updated_time: '2024-01-02T00:00:00Z',
},
],
});
const rules = await styleRulesCommand.list({ page: 1, pageSize: 5 });
expect(rules).toHaveLength(1);
expect(rules[0]?.name).toBe('Paginated Rule');
expect(scope.isDone()).toBe(true);
});
it('should combine pagination with detailed option', async () => {
const scope = nock(FREE_API_URL)
.get('/v3/style_rules')
.query({ detailed: true, page: 1, page_size: 10 })
.reply(200, {
style_rules: [
{
style_id: 'abc-123',
name: 'Full Options Rule',
language: 'fr',
version: 3,
creation_time: '2024-01-01T00:00:00Z',
updated_time: '2024-01-02T00:00:00Z',
configured_rules: ['concise'],
custom_instructions: [{ label: 'Brevity', prompt: 'Be brief' }],
},
],
});
const rules = await styleRulesCommand.list({ detailed: true, page: 1, pageSize: 10 });
expect(rules).toHaveLength(1);
const rule = rules[0] as any;
expect(rule.language).toBe('fr');
expect(rule.configuredRules).toEqual(['concise']);
expect(scope.isDone()).toBe(true);
});
});
describe('JSON format output', () => {
it('should format rules as valid JSON', async () => {
nock(FREE_API_URL)
.get('/v3/style_rules')
.reply(200, {
style_rules: [
{
style_id: 'abc-123',
name: 'JSON Rule',
language: 'en',
version: 1,
creation_time: '2024-01-01T00:00:00Z',
updated_time: '2024-01-02T00:00:00Z',
},
],
});
const rules = await styleRulesCommand.list();
const jsonOutput = styleRulesCommand.formatStyleRulesJson(rules);
const parsed = JSON.parse(jsonOutput);
expect(parsed).toHaveLength(1);
expect(parsed[0].styleId).toBe('abc-123');
expect(parsed[0].name).toBe('JSON Rule');
expect(parsed[0].language).toBe('en');
expect(parsed[0].version).toBe(1);
});
it('should format empty rules as empty JSON array', async () => {
nock(FREE_API_URL)
.get('/v3/style_rules')
.reply(200, { style_rules: [] });
const rules = await styleRulesCommand.list();
const jsonOutput = styleRulesCommand.formatStyleRulesJson(rules);
expect(JSON.parse(jsonOutput)).toEqual([]);
});
});
describe('HTTP request structure', () => {
it('should include Authorization header', async () => {
const scope = nock(FREE_API_URL, {
reqheaders: {
authorization: `DeepL-Auth-Key ${API_KEY}`,
},
})
.get('/v3/style_rules')
.reply(200, { style_rules: [] });
await styleRulesCommand.list();
expect(scope.isDone()).toBe(true);
});
it('should use GET method for listing style rules', async () => {
const scope = nock(FREE_API_URL)
.get('/v3/style_rules')
.reply(200, { style_rules: [] });
await styleRulesCommand.list();
expect(scope.isDone()).toBe(true);
});
it('should not send request body for GET request', async () => {
let receivedBody: any = undefined;
const scope = nock(FREE_API_URL)
.get('/v3/style_rules')
.reply(function (_uri: string, body: any) {
receivedBody = body;
return [200, { style_rules: [] }];
});
await styleRulesCommand.list();
expect(scope.isDone()).toBe(true);
expect(receivedBody).toBe('');
});
it('should use Pro API URL for pro keys', async () => {
const proClient = new DeepLClient('test-pro-api-key');
const proCommand = new StyleRulesCommand(new StyleRulesService(proClient));
const scope = nock('https://api.deepl.com')
.get('/v3/style_rules')
.reply(200, { style_rules: [] });
await proCommand.list();
proClient.destroy();
expect(scope.isDone()).toBe(true);
});
});
describe('response field mapping', () => {
it('should map snake_case API fields to camelCase', async () => {
nock(FREE_API_URL)
.get('/v3/style_rules')
.reply(200, {
style_rules: [
{
style_id: 'test-id-001',
name: 'Mapped Rule',
language: 'en',
version: 5,
creation_time: '2024-06-01T10:00:00Z',
updated_time: '2024-06-15T14:30:00Z',
},
],
});
const rules = await styleRulesCommand.list();
expect(rules[0]).toEqual({
styleId: 'test-id-001',
name: 'Mapped Rule',
language: 'en',
version: 5,
creationTime: '2024-06-01T10:00:00Z',
updatedTime: '2024-06-15T14:30:00Z',
});
});
it('should map detailed fields correctly', async () => {
nock(FREE_API_URL)
.get('/v3/style_rules')
.query({ detailed: true })
.reply(200, {
style_rules: [
{
style_id: 'test-id-002',
name: 'Detailed Mapped Rule',
language: 'de',
version: 2,
creation_time: '2024-03-01T00:00:00Z',
updated_time: '2024-03-10T00:00:00Z',
configured_rules: ['formal_tone', 'no_contractions'],
custom_instructions: [{ label: 'Formality', prompt: 'Always use formal language' }],
},
],
});
const rules = await styleRulesCommand.list({ detailed: true });
const rule = rules[0] as any;
expect(rule.styleId).toBe('test-id-002');
expect(rule.configuredRules).toEqual(['formal_tone', 'no_contractions']);
expect(rule.customInstructions).toEqual([{ label: 'Formality', prompt: 'Always use formal language' }]);
});
});
// error handling must be the last describe block — replyWithError in nock v14
// emits async socket errors that leak into subsequent tests
describe('error handling', () => {
let noRetryClient: DeepLClient;
let noRetryCommand: StyleRulesCommand;
beforeEach(() => {
noRetryClient = new DeepLClient(API_KEY, { maxRetries: 0 });
noRetryCommand = new StyleRulesCommand(new StyleRulesService(noRetryClient));
});
afterEach(() => {
noRetryClient.destroy();
});
it('should handle 403 authentication error', async () => {
nock(FREE_API_URL)
.get('/v3/style_rules')
.reply(403, { message: 'Invalid API key' });
await expect(noRetryCommand.list()).rejects.toThrow('Authentication failed');
});
it('should handle 429 rate limit error', async () => {
nock(FREE_API_URL)
.get('/v3/style_rules')
.reply(429, { message: 'Too many requests' });
await expect(noRetryCommand.list()).rejects.toThrow('Rate limit exceeded');
});
it('should handle 503 service unavailable error', async () => {
nock(FREE_API_URL)
.get('/v3/style_rules')
.reply(503, { message: 'Service unavailable' });
await expect(noRetryCommand.list()).rejects.toThrow('Service temporarily unavailable');
});
it('should handle 456 quota exceeded error', async () => {
nock(FREE_API_URL)
.get('/v3/style_rules')
.reply(456, { message: 'Quota exceeded' });
await expect(noRetryCommand.list()).rejects.toThrow('Quota exceeded');
});
it('should handle unexpected API response format', async () => {
nock(FREE_API_URL)
.get('/v3/style_rules')
.reply(500, { error: 'Internal server error' });
await expect(noRetryCommand.list()).rejects.toThrow();
});
// replyWithError must be last — nock v14 emits async socket errors that
// leak into subsequent tests despite abortPendingRequests()
it('should handle network errors', async () => {
nock(FREE_API_URL)
.get('/v3/style_rules')
.replyWithError('Connection refused');
await expect(noRetryCommand.list()).rejects.toThrow();
});
});
});