-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcli-config-file.integration.test.ts
More file actions
265 lines (218 loc) · 10.2 KB
/
Copy pathcli-config-file.integration.test.ts
File metadata and controls
265 lines (218 loc) · 10.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
/**
* Integration tests for --config flag
* Tests using custom configuration files
*/
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import { makeRunCLI } from '../helpers';
describe('CLI --config flag integration', () => {
const testDir = path.join(os.tmpdir(), `.deepl-cli-config-test-${Date.now()}`);
const customConfigPath = path.join(testDir, 'custom-config.json');
const defaultConfigDir = path.join(testDir, '.deepl-cli-default');
const defaultConfigPath = path.join(defaultConfigDir, 'config.json');
const helpers = makeRunCLI(defaultConfigDir);
const runCLI = (command: string, env: Record<string, string> = {}) => {
return helpers.runCLI(command, { env });
};
const runCLIAll = (command: string, env: Record<string, string> = {}) => {
return helpers.runCLIAll(command, { env });
};
beforeEach(() => {
// Create test directory
if (!fs.existsSync(testDir)) {
fs.mkdirSync(testDir, { recursive: true });
}
// Create default config directory
if (!fs.existsSync(defaultConfigDir)) {
fs.mkdirSync(defaultConfigDir, { recursive: true });
}
// Set up default config with a test API key
const defaultConfig = {
auth: { apiKey: 'default-test-key' },
api: { baseUrl: 'https://api.deepl.com' },
defaults: { sourceLang: undefined, targetLangs: [], 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(defaultConfigPath, JSON.stringify(defaultConfig, null, 2));
// Set up custom config with different API key
const customConfig = {
auth: { apiKey: 'custom-test-key' },
api: { baseUrl: 'https://api-free.deepl.com' },
defaults: { sourceLang: 'en', targetLangs: ['es', 'fr'], formality: 'more', preserveFormatting: false },
cache: { enabled: false, maxSize: 104857600, ttl: 86400 },
output: { format: 'json', verbose: true, color: false },
watch: { debounceMs: 1000, autoCommit: true, pattern: '*.txt' },
};
fs.writeFileSync(customConfigPath, JSON.stringify(customConfig, null, 2));
});
afterEach(() => {
// Clean up test directory
if (fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true, force: true });
}
});
describe('config get command', () => {
it('should use default config when --config is not specified', () => {
const output = runCLI('deepl config get auth.apiKey');
expect(output.trim()).toBe('"defa...-key"');
});
it('should use custom config when --config is specified', () => {
const output = runCLI(`deepl --config "${customConfigPath}" config get auth.apiKey`);
expect(output.trim()).toBe('"cust...-key"');
});
it('should read nested config values from custom config', () => {
const output = runCLI(`deepl --config "${customConfigPath}" config get cache.enabled`);
expect(output.trim()).toBe('false');
});
it('should read array values from custom config', () => {
const output = runCLI(`deepl --config "${customConfigPath}" config get defaults.targetLangs`);
const parsed = JSON.parse(output.trim());
expect(parsed).toEqual(['es', 'fr']);
});
});
describe('config set command', () => {
it('should write to default config when --config is not specified', () => {
runCLI('deepl config set api.baseUrl https://custom.api.com');
const config = JSON.parse(fs.readFileSync(defaultConfigPath, 'utf-8'));
expect(config.api.baseUrl).toBe('https://custom.api.com');
});
it('should write to custom config when --config is specified', () => {
runCLI(`deepl --config "${customConfigPath}" config set api.baseUrl https://custom.api.com`);
const config = JSON.parse(fs.readFileSync(customConfigPath, 'utf-8'));
expect(config.api.baseUrl).toBe('https://custom.api.com');
// Verify default config is unchanged
const defaultConfig = JSON.parse(fs.readFileSync(defaultConfigPath, 'utf-8'));
expect(defaultConfig.api.baseUrl).toBe('https://api.deepl.com');
});
});
describe('config list command', () => {
it('should list default config when --config is not specified', () => {
const output = runCLI('deepl config list');
const config = JSON.parse(output.trim());
// API keys are masked in list output
expect(config.auth.apiKey).toBe('defa...-key');
expect(config.api.baseUrl).toBe('https://api.deepl.com');
});
it('should list custom config when --config is specified', () => {
const output = runCLI(`deepl --config "${customConfigPath}" config list`);
const config = JSON.parse(output.trim());
// API keys are masked in list output
expect(config.auth.apiKey).toBe('cust...-key');
expect(config.api.baseUrl).toBe('https://api-free.deepl.com');
expect(config.defaults.formality).toBe('more');
});
});
describe('auth command', () => {
it('should set API key in custom config file', () => {
// Use config set instead of auth set-key to bypass validation
runCLI(`deepl --config "${customConfigPath}" config set auth.apiKey new-custom-key`);
const config = JSON.parse(fs.readFileSync(customConfigPath, 'utf-8'));
expect(config.auth.apiKey).toBe('new-custom-key');
});
});
describe('error handling', () => {
it('should load defaults if config file does not exist', () => {
const nonExistentPath = path.join(testDir, 'nonexistent.json');
// Config file doesn't exist, so it should load defaults (apiKey is undefined)
const output = runCLI(`deepl --config "${nonExistentPath}" config get auth.apiKey`);
expect(output.trim()).toBe('null'); // undefined becomes null in JSON output
});
it('should fail if --config value is not provided', () => {
expect.assertions(1);
try {
runCLI('deepl --config config get auth.apiKey');
} catch (error: any) {
expect(error.status).toBeGreaterThan(0);
}
});
});
describe('integration with other commands', () => {
it('should use custom config API key for auth show', () => {
const output = runCLIAll(`deepl --config "${customConfigPath}" auth show`);
expect(output).toContain('cust');
expect(output).toContain('-key'); // Masked key
});
it('should create new config file if it does not exist', () => {
const newConfigPath = path.join(testDir, 'new-config.json');
expect(fs.existsSync(newConfigPath)).toBe(false);
// Use config set instead of auth set-key to bypass validation
runCLI(`deepl --config "${newConfigPath}" config set auth.apiKey brand-new-key`);
expect(fs.existsSync(newConfigPath)).toBe(true);
const config = JSON.parse(fs.readFileSync(newConfigPath, 'utf-8'));
expect(config.auth.apiKey).toBe('brand-new-key');
});
});
describe('config path security validation', () => {
it('should reject --config path without .json extension', () => {
const txtPath = path.join(testDir, 'config.txt');
fs.writeFileSync(txtPath, '{}');
expect.assertions(2);
try {
runCLI(`deepl --config "${txtPath}" config get auth.apiKey`);
} catch (error: any) {
expect(error.status).toBeGreaterThan(0);
expect(error.stderr ?? error.stdout).toContain('.json extension');
}
});
it('should reject --config path with no extension', () => {
const noExtPath = path.join(testDir, 'myconfig');
fs.writeFileSync(noExtPath, '{}');
expect.assertions(1);
try {
runCLI(`deepl --config "${noExtPath}" config get auth.apiKey`);
} catch (error: any) {
expect(error.status).toBeGreaterThan(0);
}
});
it('should reject --config path that is a symlink', () => {
const realConfig = path.join(testDir, 'real-config.json');
const symlinkConfig = path.join(testDir, 'link-config.json');
fs.writeFileSync(realConfig, JSON.stringify({ auth: {} }));
fs.symlinkSync(realConfig, symlinkConfig);
expect.assertions(2);
try {
runCLI(`deepl --config "${symlinkConfig}" config get auth.apiKey`);
} catch (error: any) {
expect(error.status).toBeGreaterThan(0);
expect(error.stderr ?? error.stdout).toContain('symlink');
}
});
it('should accept --config path with .json extension (case-insensitive)', () => {
const upperCasePath = path.join(testDir, 'config.JSON');
fs.writeFileSync(upperCasePath, JSON.stringify({
auth: { apiKey: 'upper-case-key' },
api: { baseUrl: 'https://api.deepl.com' },
defaults: { targetLangs: [], 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' },
}, null, 2));
const output = runCLI(`deepl --config "${upperCasePath}" config get auth.apiKey`);
expect(output.trim()).toContain('uppe');
});
it('should allow non-existent --config path with .json extension', () => {
const newPath = path.join(testDir, 'brand-new.json');
expect(fs.existsSync(newPath)).toBe(false);
const output = runCLI(`deepl --config "${newPath}" config get auth.apiKey`);
expect(output.trim()).toBe('null');
});
});
describe('precedence and isolation', () => {
it('should not affect default config when using custom config', () => {
const originalDefault = JSON.parse(fs.readFileSync(defaultConfigPath, 'utf-8'));
// Modify custom config
runCLI(`deepl --config "${customConfigPath}" config set api.baseUrl https://changed.com`);
// Check default config is unchanged
const currentDefault = JSON.parse(fs.readFileSync(defaultConfigPath, 'utf-8'));
expect(currentDefault).toEqual(originalDefault);
});
it('should handle relative paths for --config', () => {
const relativeConfigPath = path.relative(process.cwd(), customConfigPath);
const output = runCLI(`deepl --config "${relativeConfigPath}" config get auth.apiKey`);
expect(output.trim()).toBe('"cust...-key"');
});
});
});