-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-config-unit.js
More file actions
356 lines (304 loc) · 8.87 KB
/
test-config-unit.js
File metadata and controls
356 lines (304 loc) · 8.87 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
#!/usr/bin/env node
/**
* Unit tests for configuration system - tests without GUI
*/
const { spawn } = require('child_process');
const fs = require('fs').promises;
const path = require('path');
const os = require('os');
// Colors for terminal output
const colors = {
reset: '\x1b[0m',
green: '\x1b[32m',
red: '\x1b[31m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
gray: '\x1b[90m'
};
function log(message, color = 'reset') {
console.log(`${colors[color]}${message}${colors.reset}`);
}
class ConfigTester {
constructor() {
this.testResults = [];
this.testCount = 0;
this.passCount = 0;
}
async runTest(name, testFn) {
this.testCount++;
log(`\n▶ Test ${this.testCount}: ${name}`, 'blue');
try {
const result = await testFn();
if (result) {
this.passCount++;
log(` ✓ PASSED`, 'green');
this.testResults.push({ name, passed: true });
} else {
log(` ✗ FAILED`, 'red');
this.testResults.push({ name, passed: false });
}
} catch (error) {
log(` ✗ ERROR: ${error.message}`, 'red');
this.testResults.push({ name, passed: false, error: error.message });
}
}
/**
* Test config loading by running a simple node script that imports main.js logic
*/
async testConfigLoading() {
return this.runTest('Configuration loading and defaults', async () => {
// Create a test script that simulates the config loading
const testScript = `
const nconf = require('nconf');
const path = require('path');
// Same DEFAULT_CONFIG as in main.js
const DEFAULT_CONFIG = {
ai: {
provider: 'shell',
shell: {
command: 'echo',
args: ['[DTUI2]:'],
template: '{command} {args} "{prompt}"',
timeout: 10000,
streaming: false,
outputFormat: {
useCodeBlock: true,
codeBlockSyntax: 'shell'
}
}
},
terminal: {
shell: '/bin/bash',
columns: 80,
lines: 24
},
ui: {
theme: 'dark',
fontSize: 14
}
};
// Test config initialization
nconf.reset();
nconf.defaults(DEFAULT_CONFIG);
// Test that defaults are loaded
const provider = nconf.get('ai:provider');
const shellCommand = nconf.get('ai:shell:command');
const useCodeBlock = nconf.get('ai:shell:outputFormat:useCodeBlock');
console.log('Provider:', provider);
console.log('Shell command:', shellCommand);
console.log('Use code block:', useCodeBlock);
// Verify expected values
if (provider === 'shell' && shellCommand === 'echo' && useCodeBlock === true) {
console.log('SUCCESS: Default configuration loaded correctly');
process.exit(0);
} else {
console.log('FAILED: Configuration mismatch');
process.exit(1);
}
`;
// Write and run test script
const testFile = path.join(os.tmpdir(), 'config-test.js');
await fs.writeFile(testFile, testScript);
return new Promise((resolve) => {
const child = spawn('node', [testFile], {
stdio: 'pipe',
cwd: process.cwd()
});
let output = '';
child.stdout.on('data', (data) => {
output += data.toString();
});
child.on('close', (code) => {
log(` Test output: ${output.trim()}`, 'gray');
fs.unlink(testFile).catch(() => {});
resolve(code === 0);
});
});
});
}
/**
* Test environment variable override
*/
async testEnvironmentOverride() {
return this.runTest('Environment variable override', async () => {
const testScript = `
const nconf = require('nconf');
const DEFAULT_CONFIG = {
ai: {
provider: 'shell',
shell: {
command: 'echo',
args: ['[DTUI2]:']
}
}
};
// Simulate environment variable processing
const envOverrides = {};
const testEnvVars = {
'DTUI_CFG__ai__shell__command': 'printf',
'DTUI_CFG__ai__shell__args': '["[ENV_TEST]:"]'
};
Object.keys(testEnvVars)
.filter(key => key.startsWith('DTUI_CFG__'))
.forEach(key => {
const configPath = key.replace('DTUI_CFG__', '').toLowerCase().split('__');
let value = testEnvVars[key];
// Parse JSON strings
try {
if (value.startsWith('[')) {
value = JSON.parse(value);
}
} catch (e) {
// Keep as string
}
// Create nested structure
let current = envOverrides;
for (let i = 0; i < configPath.length - 1; i++) {
if (!current[configPath[i]]) {
current[configPath[i]] = {};
}
current = current[configPath[i]];
}
current[configPath[configPath.length - 1]] = value;
});
nconf.reset();
nconf.overrides(envOverrides);
nconf.defaults(DEFAULT_CONFIG);
// Test override values
const command = nconf.get('ai:shell:command');
const args = nconf.get('ai:shell:args');
console.log('Overridden command:', command);
console.log('Overridden args:', args);
if (command === 'printf' && args[0] === '[ENV_TEST]:') {
console.log('SUCCESS: Environment overrides work');
process.exit(0);
} else {
console.log('FAILED: Environment overrides not working');
process.exit(1);
}
`;
const testFile = path.join(os.tmpdir(), 'env-test.js');
await fs.writeFile(testFile, testScript);
return new Promise((resolve) => {
const child = spawn('node', [testFile], {
stdio: 'pipe',
cwd: process.cwd()
});
let output = '';
child.stdout.on('data', (data) => {
output += data.toString();
});
child.on('close', (code) => {
log(` Test output: ${output.trim()}`, 'gray');
fs.unlink(testFile).catch(() => {});
resolve(code === 0);
});
});
});
}
/**
* Test user config file loading
*/
async testUserConfigFile() {
return this.runTest('User config file loading', async () => {
// Create temporary user config
const tempConfigPath = path.join(os.tmpdir(), 'test-user-config.json');
const userConfig = {
ai: {
provider: 'shell',
shell: {
command: 'echo',
args: ['[USER_CONFIG]:'],
timeout: 15000
}
}
};
await fs.writeFile(tempConfigPath, JSON.stringify(userConfig, null, 2));
const testScript = `
const nconf = require('nconf');
const fsSync = require('fs');
const DEFAULT_CONFIG = {
ai: {
provider: 'shell',
shell: {
command: 'echo',
args: ['[DTUI2]:'],
timeout: 10000
}
}
};
// Simulate user config loading
const userConfigFile = '${tempConfigPath}';
nconf.reset();
if (userConfigFile && fsSync.existsSync(userConfigFile)) {
nconf.file('user', userConfigFile);
}
nconf.defaults(DEFAULT_CONFIG);
// Test values
const args = nconf.get('ai:shell:args');
const timeout = nconf.get('ai:shell:timeout');
console.log('Args from config:', args);
console.log('Timeout from config:', timeout);
if (args[0] === '[USER_CONFIG]:' && timeout === 15000) {
console.log('SUCCESS: User config loaded correctly');
process.exit(0);
} else {
console.log('FAILED: User config not loaded properly');
process.exit(1);
}
`;
const testFile = path.join(os.tmpdir(), 'user-config-test.js');
await fs.writeFile(testFile, testScript);
return new Promise((resolve) => {
const child = spawn('node', [testFile], {
stdio: 'pipe',
cwd: process.cwd()
});
let output = '';
child.stdout.on('data', (data) => {
output += data.toString();
});
child.on('close', async (code) => {
log(` Test output: ${output.trim()}`, 'gray');
await fs.unlink(testFile).catch(() => {});
await fs.unlink(tempConfigPath).catch(() => {});
resolve(code === 0);
});
});
});
}
async runAllTests() {
log('\n' + '='.repeat(60), 'blue');
log('DTUI2 Configuration Unit Tests', 'blue');
log('='.repeat(60), 'blue');
await this.testConfigLoading();
await this.testEnvironmentOverride();
await this.testUserConfigFile();
log('\n' + '='.repeat(60), 'blue');
log(`Test Results: ${this.passCount}/${this.testCount} passed`,
this.passCount === this.testCount ? 'green' : 'yellow');
log('='.repeat(60), 'blue');
// Print summary
log('\nSummary:', 'blue');
this.testResults.forEach((result, index) => {
const status = result.passed ? '✓' : '✗';
const color = result.passed ? 'green' : 'red';
log(` ${status} Test ${index + 1}: ${result.name}`, color);
if (result.error) {
log(` Error: ${result.error}`, 'red');
}
});
return this.passCount === this.testCount;
}
}
// Run tests if executed directly
if (require.main === module) {
const tester = new ConfigTester();
tester.runAllTests().then(success => {
process.exit(success ? 0 : 1);
}).catch(error => {
console.error('Test suite failed:', error);
process.exit(1);
});
}
module.exports = ConfigTester;