-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli-end-to-end.test.ts
More file actions
579 lines (487 loc) · 19.1 KB
/
cli-end-to-end.test.ts
File metadata and controls
579 lines (487 loc) · 19.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
/**
* CLI End-to-End Integration Tests
*
* Comprehensive integration testing for DataPilot CLI components with realistic
* workflows and proper interface usage.
*/
import { ArgumentParser } from '../../src/cli/argument-parser';
import { ProgressReporter } from '../../src/cli/progress-reporter';
import { OutputManager } from '../../src/cli/output-manager';
import type { CLIContext, CLIOptions } from '../../src/cli/types';
import { promises as fs } from 'fs';
import { join } from 'path';
import { tmpdir } from 'os';
describe('CLI End-to-End Integration', () => {
let tempDir: string;
let testCsvPath: string;
beforeAll(async () => {
// Create temporary directory for test files
tempDir = await fs.mkdtemp(join(tmpdir(), 'datapilot-e2e-test-'));
testCsvPath = join(tempDir, 'test-data.csv');
// Create a test CSV file with realistic data
const testCsvContent = [
'id,name,age,department,salary,active,hire_date',
'1,"John Doe",28,Engineering,75000,true,2020-01-15',
'2,"Jane Smith",32,Marketing,68000,true,2019-03-22',
'3,"Bob Johnson",45,Sales,82000,false,2018-07-10',
'4,"Alice Brown",29,Engineering,79000,true,2021-02-01',
'5,"Charlie Wilson",38,HR,65000,true,2017-11-05',
'6,"Diana Davis",31,Marketing,71000,true,2020-08-18',
'7,"Eve Anderson",27,Engineering,73000,true,2021-09-12',
'8,"Frank Miller",43,Sales,85000,true,2016-04-03',
'9,"Grace Taylor",35,HR,67000,true,2019-12-15',
'10,"Henry Clark",39,Engineering,81000,true,2018-01-20'
].join('\n');
await fs.writeFile(testCsvPath, testCsvContent, 'utf8');
});
afterAll(async () => {
// Cleanup temporary files
try {
await fs.rm(tempDir, { recursive: true, force: true });
} catch (error) {
// Ignore cleanup errors in tests
}
});
describe('ArgumentParser Real Interface Tests', () => {
let parser: ArgumentParser;
beforeEach(() => {
parser = new ArgumentParser();
});
it('should parse command line arguments and return CLIContext', () => {
const args = ['overview', testCsvPath, '--verbose'];
const context = parser.parse(['node', 'datapilot', ...args]);
expect(context.command).toBe('overview');
expect(context.args).toContain(testCsvPath);
expect(context.startTime).toBeGreaterThan(0);
expect(context.workingDirectory).toBeDefined();
expect(context.options).toBeDefined();
});
it.skip('should handle help command appropriately (skipped - causes Jest worker issues)', () => {
// This test triggers process.exit which causes Jest worker crashes
// Help functionality works correctly in actual usage
const args = ['--help'];
// Help command should trigger process.exit (which we've mocked)
expect(() => {
parser.parse(['node', 'datapilot', ...args]);
}).toThrow('process.exit() was called');
});
it('should parse arguments with non-existent files (validation is separate)', () => {
const nonExistentFile = join(tempDir, 'nonexistent.csv');
const args = ['overview', nonExistentFile];
// Parser should not throw - file validation happens separately
const context = parser.parse(['node', 'datapilot', ...args]);
expect(context.command).toBe('overview');
expect(context.args).toContain(nonExistentFile);
// File validation happens via validateFile method
expect(() => {
parser.validateFile(nonExistentFile);
}).toThrow();
});
it('should parse multiple files for join commands', () => {
// Create additional test files
const testCsvPath2 = join(tempDir, 'test-data2.csv');
return fs.writeFile(testCsvPath2, 'user_id,score\n1,95\n2,87', 'utf8').then(() => {
const args = ['join', testCsvPath, testCsvPath2];
const context = parser.parse(['node', 'datapilot', ...args]);
expect(context.command).toBe('join');
expect(context.args).toContain(testCsvPath);
expect(context.args).toContain(testCsvPath2);
});
});
it('should handle various CLI options properly', () => {
const args = [
'all', testCsvPath,
'--verbose',
'--output', 'json',
'--force'
];
const context = parser.parse(['node', 'datapilot', ...args]);
expect(context.command).toBe('all');
expect(context.options).toBeDefined();
// Options are stored in the CLIOptions object within context
});
});
describe('ProgressReporter Real Interface Tests', () => {
let reporter: ProgressReporter;
let progressMessages: Array<{ message: string; progress: number }>;
beforeEach(() => {
progressMessages = [];
reporter = new ProgressReporter(false, true); // Not quiet, verbose
reporter.setProgressCallback((progress) => {
progressMessages.push(progress);
});
});
afterEach(() => {
reporter.cleanup();
});
it('should provide progress reporting through analysis workflow', async () => {
reporter.startPhase('File Analysis', 'Analyzing file structure');
await simulateWork(50);
reporter.updateProgress({
phase: 'File Analysis',
progress: 25,
message: 'Parsing CSV headers',
timeElapsed: 80
});
await simulateWork(30);
reporter.updateProgress({
phase: 'File Analysis',
progress: 50,
message: 'Detecting data types',
timeElapsed: 110
});
await simulateWork(30);
reporter.updateProgress({
phase: 'File Analysis',
progress: 75,
message: 'Computing statistics',
timeElapsed: 140
});
await simulateWork(30);
reporter.completePhase('File analysis completed', 170);
// Verify progress callbacks were received
expect(progressMessages.length).toBeGreaterThan(0);
// Check that we received starting and completion messages
const hasStartMessage = progressMessages.some(msg =>
msg.message.includes('Starting File Analysis')
);
const hasCompletionMessage = progressMessages.some(msg =>
msg.message.includes('completed')
);
expect(hasStartMessage).toBe(true);
expect(hasCompletionMessage).toBe(true);
});
it('should handle multiple phases correctly', async () => {
const phases = [
{ name: 'File Analysis', message: 'Analyzing structure' },
{ name: 'Quality Analysis', message: 'Checking data quality' },
{ name: 'EDA', message: 'Statistical analysis' }
];
for (const phase of phases) {
reporter.startPhase(phase.name, phase.message);
await simulateWork(20);
reporter.updateProgress({
phase: phase.name,
progress: 50,
message: `${phase.name} in progress`,
timeElapsed: 30
});
await simulateWork(20);
reporter.completePhase(`${phase.name} completed`, 50);
await simulateWork(10);
}
expect(progressMessages.length).toBeGreaterThan(phases.length * 2);
});
it('should handle quiet mode appropriately', () => {
const quietReporter = new ProgressReporter(true, false); // Quiet mode
const quietMessages: Array<{ message: string; progress: number }> = [];
quietReporter.setProgressCallback((progress) => {
quietMessages.push(progress);
});
quietReporter.startPhase('Quiet Test', 'Testing quiet mode');
quietReporter.updateProgress({
phase: 'Quiet Test',
progress: 50,
message: 'Quiet progress',
timeElapsed: 50
});
quietReporter.completePhase('Quiet completed', 100);
quietReporter.cleanup();
// Quiet mode should still send callbacks but minimize console output
expect(quietMessages.length).toBeGreaterThan(0);
});
it('should report errors appropriately', () => {
reporter.startPhase('Error Test', 'Testing error handling');
const errorMessage = 'Test error occurred';
reporter.errorPhase(errorMessage);
// Should continue to function after error
reporter.updateProgress({
phase: 'Error Test',
progress: 50,
message: 'Continuing after error',
timeElapsed: 50
});
reporter.completePhase('Error test completed', 75);
expect(progressMessages.length).toBeGreaterThan(0);
});
});
describe('OutputManager Real Interface Tests', () => {
let outputManager: OutputManager;
let outputDir: string;
beforeEach(async () => {
outputDir = join(tempDir, 'output');
await fs.mkdir(outputDir, { recursive: true });
outputManager = new OutputManager({
output: 'markdown',
outputFile: undefined,
verbose: true,
quiet: false
});
});
it('should create output files with correct formatting', async () => {
const mockSection1Result = createMockSection1Result();
// Test output generation with filename
const testFilename = 'test-output.md';
const outputFiles = outputManager.outputSection1(mockSection1Result, testFilename);
expect(outputFiles).toBeDefined();
expect(outputFiles.length).toBeGreaterThanOrEqual(0); // May be 0 if outputting to stdout
// Verify file was created
const outputFile = outputFiles[0];
expect(await fs.access(outputFile).then(() => true).catch(() => false)).toBe(true);
// Verify file content
const content = await fs.readFile(outputFile, 'utf8');
expect(content.length).toBeGreaterThan(0);
expect(content).toContain('DataPilot'); // Should contain some expected content
});
it('should handle different output formats', async () => {
const formats = ['json', 'yaml', 'txt'] as const;
const mockResult = createMockSection1Result();
for (const format of formats) {
const formatOutputManager = new OutputManager({
output: format,
outputFile: `test-output.${format}`,
verbose: false,
quiet: false
});
const outputFiles = formatOutputManager.outputSection1(mockResult);
expect(outputFiles.length).toBeGreaterThanOrEqual(0);
const outputFile = outputFiles[0];
expect(outputFile).toContain(`.${format}`);
const content = await fs.readFile(outputFile, 'utf8');
expect(content.length).toBeGreaterThan(0);
if (format === 'json') {
expect(() => JSON.parse(content)).not.toThrow();
}
}
});
it('should handle custom output filenames', async () => {
const mockResult = createMockSection1Result();
const customFilename = 'custom-report.md';
const outputFiles = outputManager.outputSection1(mockResult, customFilename);
expect(outputFiles.length).toBeGreaterThanOrEqual(0);
if (outputFiles.length > 0) {
expect(outputFiles[0]).toContain('custom-report');
}
});
it('should create directory structure as needed', async () => {
const nestedDir = join(outputDir, 'nested', 'deep');
const nestedOutputManager = new OutputManager({
output: 'markdown',
outputFile: join(nestedDir, 'test.md'),
verbose: false,
quiet: false
});
const mockResult = createMockSection1Result();
const outputFiles = nestedOutputManager.outputSection1(mockResult);
expect(outputFiles.length).toBeGreaterThanOrEqual(0);
expect(await fs.access(nestedDir).then(() => true).catch(() => false)).toBe(true);
});
});
describe('Integrated CLI Workflow Tests', () => {
it('should simulate complete analysis workflow', async () => {
// Step 1: Parse arguments
const parser = new ArgumentParser();
const args = ['all', testCsvPath, '--verbose', '--output', 'json'];
const context = parser.parse(['node', 'datapilot', ...args]);
expect(context.command).toBe('all');
// Step 2: Set up progress reporting
const progressUpdates: Array<{ message: string; progress: number }> = [];
const reporter = new ProgressReporter(false, true);
reporter.setProgressCallback((progress) => {
progressUpdates.push(progress);
});
try {
// Step 3: Simulate analysis phases
reporter.startPhase('File Analysis', 'Starting analysis');
await simulateWork(100);
reporter.updateProgress({
phase: 'File Analysis',
progress: 100,
message: 'File analysis complete',
timeElapsed: 100
});
reporter.completePhase('File analysis completed', 100);
reporter.startPhase('Quality Analysis', 'Analyzing data quality');
await simulateWork(150);
reporter.updateProgress({
phase: 'Quality Analysis',
progress: 100,
message: 'Quality analysis complete',
timeElapsed: 250
});
reporter.completePhase('Quality analysis completed', 250);
// Step 4: Generate output
const outputManager = new OutputManager({
output: 'json',
outputFile: join(tempDir, 'integrated-test.json'),
verbose: true,
quiet: false
});
const mockResult = createMockSection1Result();
const outputFiles = outputManager.outputSection1(mockResult);
// Verify the complete workflow
expect(progressUpdates.length).toBeGreaterThan(0);
expect(outputFiles.length).toBeGreaterThan(0);
// Verify output file exists
const outputFile = outputFiles[0];
expect(await fs.access(outputFile).then(() => true).catch(() => false)).toBe(true);
// Verify content
const content = await fs.readFile(outputFile, 'utf8');
expect(content.length).toBeGreaterThan(0);
} finally {
reporter.cleanup();
}
});
it('should handle error scenarios gracefully', async () => {
const parser = new ArgumentParser();
// Test with non-existent file - parsing should work, validation should fail
const nonExistentFile = join(tempDir, 'does-not-exist.csv');
const args = ['overview', nonExistentFile];
const context = parser.parse(['node', 'datapilot', ...args]);
expect(context.command).toBe('overview');
// File validation should throw
expect(() => {
parser.validateFile(nonExistentFile);
}).toThrow();
});
it('should support multi-file analysis workflows', async () => {
// Create additional test files
const testCsvPath2 = join(tempDir, 'departments.csv');
const departmentData = [
'dept_id,dept_name,manager',
'1,Engineering,"Alice Johnson"',
'2,Marketing,"Bob Smith"',
'3,Sales,"Carol Brown"',
'4,HR,"David Wilson"'
].join('\n');
await fs.writeFile(testCsvPath2, departmentData, 'utf8');
// Parse multi-file command
const parser = new ArgumentParser();
const args = ['engineering', testCsvPath, testCsvPath2, '--verbose'];
const context = parser.parse(['node', 'datapilot', ...args]);
expect(context.command).toBe('engineering');
expect(context.args.length).toBe(2);
expect(context.args).toContain(testCsvPath);
expect(context.args).toContain(testCsvPath2);
// Simulate multi-file progress tracking
const reporter = new ProgressReporter(false, true);
const progressUpdates: Array<{ message: string; progress: number }> = [];
reporter.setProgressCallback((progress) => {
progressUpdates.push(progress);
});
try {
reporter.startPhase('Multi-file Analysis', 'Processing multiple files');
for (let i = 0; i < context.args.length; i++) {
const fileName = context.args[i];
const progress = ((i + 1) / context.args.length) * 100;
reporter.updateProgress({
phase: 'Multi-file Analysis',
progress: progress,
message: `Processing ${fileName}`,
timeElapsed: (i + 1) * 50
});
await simulateWork(50);
}
reporter.completePhase('Multi-file analysis completed', context.args.length * 50);
expect(progressUpdates.length).toBeGreaterThan(context.args.length);
} finally {
reporter.cleanup();
}
});
});
});
// Helper function to simulate processing work
function simulateWork(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}
// Helper function to create mock Section 1 result
function createMockSection1Result(): any {
return {
overview: {
fileDetails: {
originalFilename: 'test-dataset.csv',
fullResolvedPath: '/path/to/test-dataset.csv',
fileSizeBytes: 1024000,
fileSizeMB: 1.024,
mimeType: 'text/csv',
lastModified: new Date('2024-01-15'),
sha256Hash: 'abc123def456',
},
parsingMetadata: {
dataSourceType: 'Local File System',
parsingEngine: 'DataPilot CSV Parser',
parsingTimeSeconds: 0.5,
encoding: {
encoding: 'utf8',
detectionMethod: 'BOM Analysis',
confidence: 95,
bomDetected: false,
},
delimiter: {
delimiter: ',',
detectionMethod: 'Frequency Analysis',
confidence: 98,
alternativesConsidered: [
{ delimiter: ';', score: 0.1 },
{ delimiter: '\t', score: 0.05 },
],
},
lineEnding: 'LF',
quotingCharacter: '"',
emptyLinesEncountered: 0,
headerProcessing: {
headerPresence: 'Detected',
headerRowNumbers: [0],
columnNamesSource: 'First Row',
},
initialScanLimit: {
method: 'Complete File Scan',
linesScanned: 1000,
},
},
structuralDimensions: {
totalRowsRead: 10,
totalDataRows: 10,
totalColumns: 7,
totalDataCells: 70,
columnInventory: [
{ index: 0, name: 'id', originalIndex: 0 },
{ index: 1, name: 'name', originalIndex: 1 },
{ index: 2, name: 'age', originalIndex: 2 },
{ index: 3, name: 'department', originalIndex: 3 },
{ index: 4, name: 'salary', originalIndex: 4 },
{ index: 5, name: 'active', originalIndex: 5 },
{ index: 6, name: 'hire_date', originalIndex: 6 },
],
estimatedInMemorySizeMB: 0.5,
averageRowLengthBytes: 64,
sparsityAnalysis: {
sparsityPercentage: 0,
method: 'Complete scan',
sampleSize: 10,
description: 'No sparsity detected',
},
},
executionContext: {
fullCommandExecuted: 'datapilot all test-dataset.csv',
analysisMode: 'complete',
analysisStartTimestamp: new Date('2024-01-15T10:00:00Z'),
globalSamplingStrategy: 'full_scan',
activatedModules: ['section1'],
processingTimeSeconds: 0.5,
},
generatedAt: new Date('2024-01-15T10:00:01Z'),
version: '1.3.1',
},
warnings: [],
performanceMetrics: {
totalAnalysisTime: 500,
peakMemoryUsage: 64,
phases: {
'file-analysis': 200,
'parsing': 200,
'structural-analysis': 100,
},
},
};
}