-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcli-structured-file-translate.integration.test.ts
More file actions
393 lines (319 loc) · 13.6 KB
/
Copy pathcli-structured-file-translate.integration.test.ts
File metadata and controls
393 lines (319 loc) · 13.6 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
/**
* Integration Tests for Structured File (JSON/YAML) Translation
* Tests both CLI argument validation (via subprocess) and service-level integration (in-process)
*/
import * as fs from 'fs';
import * as path from 'path';
import nock from 'nock';
import { TranslationService } from '../../src/services/translation';
import { FileTranslationService } from '../../src/services/file-translation';
import { DeepLClient } from '../../src/api/deepl-client';
import { ConfigService } from '../../src/storage/config';
import { CacheService } from '../../src/storage/cache';
import { createTestConfigDir, createTestDir, makeRunCLI, DEEPL_FREE_API_URL } from '../helpers';
describe('Structured File Translation CLI Integration', () => {
const testConfig = createTestConfigDir('test-structured');
const testFiles = createTestDir('structured-files');
const testDir = testFiles.path;
const { runCLI } = makeRunCLI(testConfig.path, { apiKey: 'test-api-key-123' });
beforeAll(() => {
fs.writeFileSync(
path.join(testConfig.path, 'config.json'),
JSON.stringify({
auth: { apiKey: 'test-api-key-123' },
api: { baseUrl: 'https://api-free.deepl.com/v2' },
defaults: { sourceLang: undefined, targetLangs: [], formality: 'default', preserveFormatting: true },
cache: { enabled: false, maxSize: 1073741824, ttl: 2592000 },
})
);
});
afterAll(() => {
nock.cleanAll();
testConfig.cleanup();
testFiles.cleanup();
});
afterEach(() => {
nock.cleanAll();
});
describe('CLI argument validation', () => {
it('should accept JSON file with --to and --output flags', () => {
const testFile = path.join(testDir, 'validation.json');
fs.writeFileSync(testFile, JSON.stringify({ key: 'test' }, null, 2));
try {
runCLI(`deepl translate "${testFile}" --to es --output /tmp/out.json`);
} catch (error: any) {
const output = error.stderr ?? error.stdout;
// Should fail on API auth, not argument validation or file type
expect(output).not.toMatch(/Unsupported file type/i);
}
});
it('should accept YAML file with --to and --output flags', () => {
const testFile = path.join(testDir, 'validation.yaml');
fs.writeFileSync(testFile, 'key: test\n');
try {
runCLI(`deepl translate "${testFile}" --to es --output /tmp/out.yaml`);
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).not.toMatch(/Unsupported file type/i);
}
});
it('should accept .yml file with --to and --output flags', () => {
const testFile = path.join(testDir, 'validation.yml');
fs.writeFileSync(testFile, 'key: test\n');
try {
runCLI(`deepl translate "${testFile}" --to es --output /tmp/out.yml`);
} catch (error: any) {
const output = error.stderr ?? error.stdout;
expect(output).not.toMatch(/Unsupported file type/i);
}
});
it('should validate target language for structured files', () => {
const testFile = path.join(testDir, 'lang-val.json');
fs.writeFileSync(testFile, JSON.stringify({ key: 'test' }, null, 2));
expect(() => {
runCLI(`deepl translate "${testFile}" --to INVALID --output /tmp/out.json`);
}).toThrow();
});
it('should require --output flag for structured file translation', () => {
const testFile = path.join(testDir, 'no-output.json');
fs.writeFileSync(testFile, JSON.stringify({ key: 'test' }, null, 2));
expect(() => {
runCLI(`deepl translate "${testFile}" --to es`);
}).toThrow();
});
it('should handle empty JSON object without API call', () => {
const inputPath = path.join(testDir, 'empty.json');
const outputPath = path.join(testDir, 'empty-es.json');
fs.writeFileSync(inputPath, '{}');
const output = runCLI(`deepl translate "${inputPath}" --to es --output "${outputPath}"`);
expect(output).toContain('Translated');
const result = JSON.parse(fs.readFileSync(outputPath, 'utf-8'));
expect(result).toEqual({});
});
it('should reject invalid JSON files', () => {
const testFile = path.join(testDir, 'invalid.json');
fs.writeFileSync(testFile, '{ not valid json }');
expect(() => {
runCLI(`deepl translate "${testFile}" --to es --output /tmp/out.json`);
}).toThrow();
});
it('should reject empty JSON files', () => {
const testFile = path.join(testDir, 'empty-file.json');
fs.writeFileSync(testFile, '');
expect(() => {
runCLI(`deepl translate "${testFile}" --to es --output /tmp/out.json`);
}).toThrow();
});
});
describe('service-level integration (in-process with nock)', () => {
const API_KEY = 'test-api-key-123:fx';
const FREE_API_URL = DEEPL_FREE_API_URL;
let fileTranslationService: FileTranslationService;
let cacheService: CacheService;
let client: DeepLClient;
beforeEach(() => {
const svcDir = path.join(testDir, `svc-${Date.now()}`);
fs.mkdirSync(svcDir, { recursive: true });
const configPath = path.join(svcDir, 'config.json');
const cachePath = path.join(svcDir, 'cache.db');
const config = new ConfigService(configPath);
cacheService = new CacheService({ dbPath: cachePath, maxSize: 1024 * 100 });
client = new DeepLClient(API_KEY);
const translationService = new TranslationService(client, config, cacheService);
fileTranslationService = new FileTranslationService(translationService);
});
afterEach(() => {
client.destroy();
try { cacheService.close(); } catch { /* ignore */ }
});
it('should translate flat JSON file via FileTranslationService', async () => {
const inputPath = path.join(testDir, 'svc-en.json');
const outputPath = path.join(testDir, 'svc-es.json');
fs.writeFileSync(inputPath, JSON.stringify({
greeting: 'Hello',
farewell: 'Goodbye',
}, null, 2));
nock(FREE_API_URL)
.post('/v2/translate')
.reply(200, {
translations: [
{ text: 'Hola', detected_source_language: 'EN' },
{ text: 'Adiós', detected_source_language: 'EN' },
],
});
await fileTranslationService.translateFile(inputPath, outputPath, { targetLang: 'es' });
const result = JSON.parse(fs.readFileSync(outputPath, 'utf-8'));
expect(result.greeting).toBe('Hola');
expect(result.farewell).toBe('Adiós');
});
it('should translate nested JSON preserving structure', async () => {
const inputPath = path.join(testDir, 'svc-nested.json');
const outputPath = path.join(testDir, 'svc-nested-es.json');
fs.writeFileSync(inputPath, JSON.stringify({
nav: { home: 'Home', about: 'About' },
footer: { copyright: 'All rights reserved' },
version: 2,
}, null, 2));
nock(FREE_API_URL)
.post('/v2/translate')
.reply(200, {
translations: [
{ text: 'Inicio', detected_source_language: 'EN' },
{ text: 'Acerca de', detected_source_language: 'EN' },
{ text: 'Todos los derechos reservados', detected_source_language: 'EN' },
],
});
await fileTranslationService.translateFile(inputPath, outputPath, { targetLang: 'es' });
const result = JSON.parse(fs.readFileSync(outputPath, 'utf-8'));
expect(result.nav.home).toBe('Inicio');
expect(result.nav.about).toBe('Acerca de');
expect(result.footer.copyright).toBe('Todos los derechos reservados');
expect(result.version).toBe(2);
});
it('should preserve JSON indentation', async () => {
const inputPath = path.join(testDir, 'svc-indent.json');
const outputPath = path.join(testDir, 'svc-indent-es.json');
fs.writeFileSync(inputPath, JSON.stringify({ key: 'Hello' }, null, 4) + '\n');
nock(FREE_API_URL)
.post('/v2/translate')
.reply(200, {
translations: [
{ text: 'Hola', detected_source_language: 'EN' },
],
});
await fileTranslationService.translateFile(inputPath, outputPath, { targetLang: 'es' });
const raw = fs.readFileSync(outputPath, 'utf-8');
expect(raw).toContain(' "key"');
expect(raw.endsWith('\n')).toBe(true);
});
it('should translate YAML file via FileTranslationService', async () => {
const inputPath = path.join(testDir, 'svc-en.yaml');
const outputPath = path.join(testDir, 'svc-es.yaml');
fs.writeFileSync(inputPath, 'greeting: Hello\nfarewell: Goodbye\n');
nock(FREE_API_URL)
.post('/v2/translate')
.reply(200, {
translations: [
{ text: 'Hola', detected_source_language: 'EN' },
{ text: 'Adiós', detected_source_language: 'EN' },
],
});
await fileTranslationService.translateFile(inputPath, outputPath, { targetLang: 'es' });
const raw = fs.readFileSync(outputPath, 'utf-8');
expect(raw).toContain('greeting: Hola');
expect(raw).toContain('farewell: Adiós');
});
it('should preserve YAML comments', async () => {
const inputPath = path.join(testDir, 'svc-comments.yaml');
const outputPath = path.join(testDir, 'svc-comments-es.yaml');
fs.writeFileSync(inputPath, [
'# Main heading',
'greeting: Hello # inline',
'',
].join('\n'));
nock(FREE_API_URL)
.post('/v2/translate')
.reply(200, {
translations: [
{ text: 'Hola', detected_source_language: 'EN' },
],
});
await fileTranslationService.translateFile(inputPath, outputPath, { targetLang: 'es' });
const raw = fs.readFileSync(outputPath, 'utf-8');
expect(raw).toContain('# Main heading');
expect(raw).toContain('# inline');
});
it('should send only string values to the API', async () => {
const inputPath = path.join(testDir, 'svc-api-check.json');
const outputPath = path.join(testDir, 'svc-api-check-es.json');
fs.writeFileSync(inputPath, JSON.stringify({
title: 'Hello World',
count: 42,
}, null, 2));
const scope = nock(FREE_API_URL)
.post('/v2/translate', (body: any) => {
// text is sent as URL-encoded; nock parses it for us
const texts = Array.isArray(body.text) ? body.text : [body.text];
expect(texts).toEqual(['Hello World']);
expect(body.target_lang).toBe('ES');
return true;
})
.reply(200, {
translations: [
{ text: 'Hola Mundo', detected_source_language: 'EN' },
],
});
await fileTranslationService.translateFile(inputPath, outputPath, { targetLang: 'es' });
expect(scope.isDone()).toBe(true);
});
it('should pass formality option through to API', async () => {
const inputPath = path.join(testDir, 'svc-formality.json');
const outputPath = path.join(testDir, 'svc-formality-es.json');
fs.writeFileSync(inputPath, JSON.stringify({ msg: 'Hello' }, null, 2));
const scope = nock(FREE_API_URL)
.post('/v2/translate', (body: any) => {
expect(body.formality).toBe('more');
return true;
})
.reply(200, {
translations: [
{ text: 'Hallo', detected_source_language: 'EN' },
],
});
await fileTranslationService.translateFile(inputPath, outputPath, {
targetLang: 'de',
formality: 'more',
});
expect(scope.isDone()).toBe(true);
});
it('should handle API errors gracefully', async () => {
const inputPath = path.join(testDir, 'svc-error.json');
const outputPath = path.join(testDir, 'svc-error-es.json');
fs.writeFileSync(inputPath, JSON.stringify({ key: 'Hello' }, null, 2));
nock(FREE_API_URL)
.post('/v2/translate')
.reply(403, { message: 'Forbidden' });
await expect(
fileTranslationService.translateFile(inputPath, outputPath, { targetLang: 'es' })
).rejects.toThrow();
});
it('should translate .yml files', async () => {
const inputPath = path.join(testDir, 'svc-en.yml');
const outputPath = path.join(testDir, 'svc-es.yml');
fs.writeFileSync(inputPath, 'title: Welcome\n');
nock(FREE_API_URL)
.post('/v2/translate')
.reply(200, {
translations: [
{ text: 'Bienvenido', detected_source_language: 'EN' },
],
});
await fileTranslationService.translateFile(inputPath, outputPath, { targetLang: 'es' });
const raw = fs.readFileSync(outputPath, 'utf-8');
expect(raw).toContain('title: Bienvenido');
});
it('should translate file to multiple languages', async () => {
const inputPath = path.join(testDir, 'svc-multi.json');
fs.writeFileSync(inputPath, JSON.stringify({ greeting: 'Hello' }, null, 2));
nock(FREE_API_URL)
.post('/v2/translate', (body: any) => body.target_lang === 'ES')
.reply(200, {
translations: [{ text: 'Hola', detected_source_language: 'EN' }],
})
.post('/v2/translate', (body: any) => body.target_lang === 'FR')
.reply(200, {
translations: [{ text: 'Bonjour', detected_source_language: 'EN' }],
});
const results = await fileTranslationService.translateFileToMultiple(
inputPath,
['es', 'fr'],
{ outputDir: testDir }
);
expect(results).toHaveLength(2);
expect(results[0]?.targetLang).toBe('es');
expect(results[1]?.targetLang).toBe('fr');
const esResult = JSON.parse(fs.readFileSync(results[0]!.outputPath!, 'utf-8'));
expect(esResult.greeting).toBe('Hola');
});
});
});