import { Command } from 'commander'; import { BaseCommand } from './BaseCommand'; import { SuggestTestsUseCaseImpl } from '../../domain/usecases/SuggestTestsUseCase'; export class TestSuggestCommand extends BaseCommand { constructor() { super('test-suggest', 'Suggest and generate unit tests'); this.option('--framework ', 'Test framework (auto-detected from file if not specified)') .option('--test-type ', 'Test type (unit,integration,e2e,performance,security)', 'unit') .option('--language ', 'Programming language (auto-detected from file if not specified)') .option('--include-setup', 'Include test setup code') .option('--include-teardown', 'Include test teardown code') .option('--coverage-target ', 'Target test coverage percentage', '80'); } protected async execute(options: any, command: Command): Promise { const useCase = new SuggestTestsUseCaseImpl( this.gitAdapter, this.testGeneratorAdapter, this.outputAdapter ); const testOptions: any = { baseRef: options.baseRef, headRef: options.headRef, includeStaged: options.staged, includeUnstaged: options.unstaged, filePatterns: this.parseFilePatterns(options.filePatterns) || [], excludePatterns: this.parseExcludePatterns(options.excludePatterns) || [], outputFormat: this.getOutputFormat(options) as 'html' | 'json' | 'markdown' | 'console' | 'file', // Only set defaults if not using file patterns (to allow auto-detection) framework: options.framework || (this.parseFilePatterns(options.filePatterns)?.length === 0 ? this.config.test.framework : undefined), testType: options.testType || (this.parseFilePatterns(options.filePatterns)?.length === 0 ? this.config.test.testType : 'unit'), language: options.language || (this.parseFilePatterns(options.filePatterns)?.length === 0 ? this.config.test.language : undefined), includeSetup: options.includeSetup || this.config.test.includeSetup, includeTeardown: options.includeTeardown || this.config.test.includeTeardown, coverageTarget: options.coverageTarget ? parseInt(options.coverageTarget) : this.config.test.coverageTarget, }; const outputPath = this.getOutputPath(options); if (outputPath) { testOptions.outputPath = outputPath; } if (this.isVerbose(options)) { console.log('Generating test suggestions...'); console.log('Options:', testOptions); } try { const suggestions = await useCase.execute(testOptions); if (this.isVerbose(options)) { console.log(`Generated ${suggestions.length} test suggestions.`); } } catch (error) { throw new Error(`Test suggestion generation failed: ${error instanceof Error ? error.message : 'Unknown error'}`); } } }