veriqa-test-advisor
Version:
AI-powered regression test case advisor CLI tool with integrated Manual QA support
417 lines (351 loc) • 16.5 kB
JavaScript
/**
* VeriQA AI API Key Test Script
* Test करता है कि आपकी AI API keys सही से काम कर रही हैं
* Real AI suggestions generate करके demonstrate करता है
*/
// Load environment variables
require('dotenv').config();
const chalk = require('chalk');
const VeriQAAISuggestionEngine = require('../src/ai-suggestion-engine');
class AIAPIKeyTester {
constructor() {
this.engine = new VeriQAAISuggestionEngine();
this.testResults = {
openai: { tested: false, working: false, error: null },
anthropic: { tested: false, working: false, error: null },
gemini: { tested: false, working: false, error: null },
veriqa: { tested: false, working: false, error: null }
};
}
async runTests() {
console.log(chalk.cyan('\n🧪 VeriQA AI API Key Testing Started\n'));
console.log('=' .repeat(70));
// Test sample code for AI analysis
const sampleCode = {
file: 'src/auth/login.js',
content: `
async function loginUser(email, password) {
if (!email || !password) {
throw new Error('Email and password required');
}
const user = await findUserByEmail(email);
if (!user) {
throw new Error('User not found');
}
const isValidPassword = await bcrypt.compare(password, user.hashedPassword);
if (!isValidPassword) {
throw new Error('Invalid password');
}
const token = jwt.sign({ userId: user.id }, JWT_SECRET, { expiresIn: '24h' });
return { success: true, token, user: { id: user.id, email: user.email } };
}
`,
changes: ['Added password validation', 'Implemented JWT token generation']
};
console.log(chalk.blue('📋 Testing with Sample Authentication Code:'));
console.log(chalk.dim(`File: ${sampleCode.file}`));
console.log(chalk.dim('Changes: Password validation + JWT implementation'));
console.log();
// Test each AI provider
await this.testOpenAI(sampleCode);
await this.testAnthropic(sampleCode);
await this.testGemini(sampleCode);
await this.testVeriQAService(sampleCode);
// Show final results
this.showFinalResults();
}
async testOpenAI(sampleCode) {
console.log(chalk.yellow('🔍 Testing OpenAI API Key...'));
this.testResults.openai.tested = true;
try {
// Check if API key exists
const apiKey = process.env.OPENAI_API_KEY || this.engine.getAPIKey({ name: 'OpenAI GPT' });
if (!apiKey) {
this.testResults.openai.error = 'No API key found. Set OPENAI_API_KEY or use --setup-token';
console.log(chalk.red('❌ OpenAI: No API key configured'));
console.log(chalk.dim(' 💡 Solution: export OPENAI_API_KEY="your-key" or node index.js --setup-token openai your-key'));
return;
}
console.log(chalk.green('✅ OpenAI: API key found'));
console.log(chalk.blue('🤖 Getting OpenAI suggestions...'));
const suggestions = await this.engine.generateSuggestions([sampleCode], {
provider: 'openai',
type: 'security_testing'
});
if (suggestions.success && suggestions.suggestions.length > 0) {
this.testResults.openai.working = true;
console.log(chalk.green('🎉 OpenAI: Working perfectly!'));
console.log(chalk.cyan(`📊 Generated ${suggestions.suggestions.length} suggestions`));
// Show detailed suggestions
console.log(chalk.white('📝 AI Suggestions Generated:'));
suggestions.suggestions.slice(0, 2).forEach((suggestion, index) => {
console.log(chalk.green(` ${index + 1}. ${suggestion.description}`));
console.log(chalk.yellow(` Priority: ${suggestion.priority || 'medium'}`));
console.log(chalk.blue(` Confidence: ${((suggestion.confidence || 0.8) * 100).toFixed(0)}%`));
if (suggestion.code) {
const codePreview = suggestion.code.split('\n')[0].trim();
console.log(chalk.gray(` Code: ${codePreview}...`));
}
console.log();
});
} else {
this.testResults.openai.error = 'API call failed or no suggestions returned';
console.log(chalk.yellow('⚠️ OpenAI: API key valid but response issues'));
}
} catch (error) {
this.testResults.openai.error = error.message;
console.log(chalk.red(`❌ OpenAI Error: ${error.message}`));
}
console.log();
}
async testAnthropic(sampleCode) {
console.log(chalk.yellow('🔍 Testing Anthropic Claude API Key...'));
this.testResults.anthropic.tested = true;
try {
const apiKey = process.env.ANTHROPIC_API_KEY || this.engine.getAPIKey({ name: 'Anthropic Claude' });
if (!apiKey) {
this.testResults.anthropic.error = 'No API key found. Set ANTHROPIC_API_KEY or use --setup-token';
console.log(chalk.red('❌ Anthropic: No API key configured'));
console.log(chalk.dim(' 💡 Solution: export ANTHROPIC_API_KEY="your-key" or node index.js --setup-token anthropic your-key'));
return;
}
console.log(chalk.green('✅ Anthropic: API key found'));
console.log(chalk.blue('🤖 Getting Claude suggestions...'));
const suggestions = await this.engine.generateSuggestions([sampleCode], {
provider: 'anthropic',
type: 'edge_case_testing'
});
if (suggestions.success && suggestions.suggestions.length > 0) {
this.testResults.anthropic.working = true;
console.log(chalk.green('🎉 Anthropic: Working perfectly!'));
console.log(chalk.cyan(`📊 Generated ${suggestions.suggestions.length} suggestions`));
// Show detailed suggestions
console.log(chalk.white('📝 Claude AI Suggestions:'));
suggestions.suggestions.slice(0, 2).forEach((suggestion, index) => {
console.log(chalk.green(` ${index + 1}. ${suggestion.description}`));
console.log(chalk.yellow(` Priority: ${suggestion.priority || 'medium'}`));
console.log(chalk.blue(` Type: ${suggestion.type || 'test'}`));
if (suggestion.reasoning) {
console.log(chalk.gray(` Reasoning: ${suggestion.reasoning}`));
}
console.log();
});
} else {
this.testResults.anthropic.error = 'API call failed or no suggestions returned';
console.log(chalk.yellow('⚠️ Anthropic: API key valid but response issues'));
}
} catch (error) {
this.testResults.anthropic.error = error.message;
console.log(chalk.red(`❌ Anthropic Error: ${error.message}`));
}
console.log();
}
async testGemini(sampleCode) {
console.log(chalk.yellow('🔍 Testing Google Gemini API Key...'));
this.testResults.gemini.tested = true;
try {
// Check environment variable first, then engine method
const apiKey = process.env.GEMINI_API_KEY || this.engine.getAPIKey({ name: 'Google Gemini' });
if (!apiKey) {
this.testResults.gemini.error = 'No API key found. Set GEMINI_API_KEY or use --setup-token';
console.log(chalk.red('❌ Gemini: No API key configured'));
console.log(chalk.dim(' 💡 Get free key: https://makersuite.google.com/app/apikey'));
console.log(chalk.dim(' 💡 Setup: node index.js --setup-token gemini your-key'));
return;
}
console.log(chalk.green('✅ Gemini: API key found'));
console.log(chalk.blue('🤖 Getting Gemini suggestions...'));
const suggestions = await this.engine.generateSuggestions([sampleCode], {
provider: 'gemini',
type: 'comprehensive_testing'
});
if (suggestions.success && suggestions.suggestions.length > 0) {
this.testResults.gemini.working = true;
console.log(chalk.green('🎉 Gemini: Working perfectly!'));
console.log(chalk.cyan(`📊 Generated ${suggestions.suggestions.length} suggestions`));
console.log(chalk.cyan(`⚡ Model: ${suggestions.model || 'gemini-pro'}`));
console.log(chalk.cyan(`🎯 Confidence: ${((suggestions.metadata?.confidence || 0.8) * 100).toFixed(1)}%`));
// Show detailed suggestions
console.log(chalk.white('📝 Gemini AI Suggestions:'));
suggestions.suggestions.forEach((suggestion, index) => {
console.log(chalk.green(` ${index + 1}. ${suggestion.description}`));
console.log(chalk.yellow(` Priority: ${suggestion.priority || 'medium'}`));
console.log(chalk.blue(` Type: ${suggestion.type || 'test'}`));
console.log(chalk.magenta(` Confidence: ${((suggestion.confidence || 0.8) * 100).toFixed(0)}%`));
if (suggestion.code) {
console.log(chalk.white(' Generated Code:'));
const codeLines = suggestion.code.split('\n').slice(0, 3);
codeLines.forEach(line => {
if (line.trim()) {
console.log(chalk.gray(` ${line}`));
}
});
if (suggestion.code.split('\n').length > 3) {
console.log(chalk.gray(' ... (more code available)'));
}
}
if (suggestion.reasoning) {
console.log(chalk.cyan(` AI Reasoning: ${suggestion.reasoning}`));
}
console.log();
});
} else {
this.testResults.gemini.error = 'API call failed or no suggestions returned';
console.log(chalk.yellow('⚠️ Gemini: API key valid but response issues'));
}
} catch (error) {
this.testResults.gemini.error = error.message;
console.log(chalk.red(`❌ Gemini Error: ${error.message}`));
}
console.log();
}
async testVeriQAService(sampleCode) {
console.log(chalk.yellow('🔍 Testing VeriQA Service...'));
this.testResults.veriqa.tested = true;
try {
console.log(chalk.blue('🌐 Getting VeriQA service suggestions...'));
const suggestions = await this.engine.generateSuggestions([sampleCode], {
provider: 'veriqa',
type: 'regression_testing'
});
if (suggestions.success) {
this.testResults.veriqa.working = true;
console.log(chalk.green('🎉 VeriQA Service: Working perfectly!'));
console.log(chalk.cyan(`📊 Generated ${suggestions.suggestions?.length || 0} suggestions`));
console.log(chalk.yellow('💡 Using intelligent fallback suggestions (upgrade for real AI)'));
// Show detailed VeriQA suggestions
if (suggestions.suggestions && suggestions.suggestions.length > 0) {
console.log(chalk.white('📝 VeriQA Smart Suggestions:'));
suggestions.suggestions.forEach((suggestion, index) => {
console.log(chalk.green(` ${index + 1}. ${suggestion.description}`));
console.log(chalk.yellow(` Priority: ${suggestion.priority || 'medium'}`));
console.log(chalk.blue(` Type: ${suggestion.type || 'unit_test'}`));
if (suggestion.code) {
console.log(chalk.white(' Sample Test Code:'));
const codeLines = suggestion.code.split('\n').slice(0, 2);
codeLines.forEach(line => {
if (line.trim()) {
console.log(chalk.gray(` ${line.trim()}`));
}
});
}
console.log();
});
// Show upgrade message
console.log(chalk.blue('🚀 Want Real AI? Setup your API key:'));
console.log(chalk.dim(' node index.js --setup-token gemini YOUR_GEMINI_KEY'));
}
} else {
this.testResults.veriqa.error = 'Service unavailable';
console.log(chalk.yellow('⚠️ VeriQA Service: Issues detected'));
}
} catch (error) {
this.testResults.veriqa.error = error.message;
console.log(chalk.red(`❌ VeriQA Service Error: ${error.message}`));
}
console.log();
}
showFinalResults() {
console.log('=' .repeat(70));
console.log(chalk.cyan('📊 Final Test Results:'));
console.log();
let workingCount = 0;
let totalTested = 0;
// OpenAI Results
if (this.testResults.openai.tested) {
totalTested++;
const status = this.testResults.openai.working ?
chalk.green('✅ WORKING') :
chalk.red('❌ NOT WORKING');
console.log(`🤖 OpenAI GPT: ${status}`);
if (this.testResults.openai.working) workingCount++;
if (this.testResults.openai.error) {
console.log(chalk.dim(` Error: ${this.testResults.openai.error}`));
}
}
// Anthropic Results
if (this.testResults.anthropic.tested) {
totalTested++;
const status = this.testResults.anthropic.working ?
chalk.green('✅ WORKING') :
chalk.red('❌ NOT WORKING');
console.log(`🧠 Anthropic Claude: ${status}`);
if (this.testResults.anthropic.working) workingCount++;
if (this.testResults.anthropic.error) {
console.log(chalk.dim(` Error: ${this.testResults.anthropic.error}`));
}
}
// Gemini Results
if (this.testResults.gemini.tested) {
totalTested++;
const status = this.testResults.gemini.working ?
chalk.green('✅ WORKING') :
chalk.red('❌ NOT WORKING');
console.log(`💎 Google Gemini: ${status}`);
if (this.testResults.gemini.working) workingCount++;
if (this.testResults.gemini.error) {
console.log(chalk.dim(` Error: ${this.testResults.gemini.error}`));
}
}
// VeriQA Results
if (this.testResults.veriqa.tested) {
totalTested++;
const status = this.testResults.veriqa.working ?
chalk.green('✅ WORKING') :
chalk.red('❌ NOT WORKING');
console.log(`🚀 VeriQA Service: ${status}`);
if (this.testResults.veriqa.working) workingCount++;
}
console.log();
console.log(chalk.cyan(`📈 Success Rate: ${workingCount}/${totalTested} (${((workingCount/totalTested)*100).toFixed(1)}%)`));
// Recommendations
console.log();
console.log(chalk.yellow('💡 Recommendations:'));
if (workingCount === 0) {
console.log(chalk.red('🚨 No AI providers working! Setup at least one API key:'));
console.log(' • Gemini (Free): https://makersuite.google.com/app/apikey');
console.log(' • OpenAI: https://platform.openai.com/api-keys');
console.log(' • Anthropic: https://console.anthropic.com/');
} else if (workingCount < totalTested) {
console.log(chalk.yellow('⚡ Some providers need setup. Get more AI power by configuring all:'));
if (!this.testResults.gemini.working) {
console.log(' • Setup Gemini: node index.js --setup-token gemini YOUR_KEY');
}
if (!this.testResults.openai.working) {
console.log(' • Setup OpenAI: node index.js --setup-token openai YOUR_KEY');
}
if (!this.testResults.anthropic.working) {
console.log(' • Setup Claude: node index.js --setup-token anthropic YOUR_KEY');
}
} else {
console.log(chalk.green('🎉 All AI providers working! You have maximum AI power!'));
console.log(' • Try: node index.js --regression --provider gemini');
console.log(' • Try: node index.js --ai --provider openai');
console.log(' • Try: node index.js --smart --provider anthropic');
}
console.log();
console.log(chalk.blue('🚀 Next Steps:'));
console.log('1. Setup missing API keys (if any)');
console.log('2. Run: node index.js --regression (get AI test suggestions)');
console.log('3. Run: node index.js --usage (check your usage stats)');
console.log('4. Run: node index.js --smart (full AI analysis)');
console.log();
console.log(chalk.green('✨ VeriQA AI Testing Complete!'));
}
}
// Demo execution
async function runDemo() {
console.log(chalk.magenta('🎯 VeriQA AI API Key Testing Demo'));
console.log(chalk.dim('This will test all your configured AI providers and show real suggestions'));
const tester = new AIAPIKeyTester();
await tester.runTests();
}
// Run if called directly
if (require.main === module) {
runDemo().catch(error => {
console.error(chalk.red('Demo failed:'), error.message);
process.exit(1);
});
}
module.exports = AIAPIKeyTester;