@hhoangphuoc/escape-room-cli
Version:
A CLI for playing AI-generated escape room games. Install globally with: npm install -g @hhoangphuoc/escape-room-cli
72 lines (71 loc) • 2.93 kB
JavaScript
/**
* Standalone validation test utility for debugging API validation issues
* Usage: npx tsx source/utils/validationTest.ts
*/
import { checkBackendHealth, validateOpenAIApiKey } from './validation.js';
import { getApiUrl } from './apiConfig.js';
async function runValidationTests() {
console.log('='.repeat(60));
console.log('Escape Room CLI - Validation System Test');
console.log('='.repeat(60));
// Test 1: Check API URL configuration
console.log('\n1. API URL Configuration:');
console.log('---------------------------');
const apiUrl = getApiUrl();
console.log(`Current API URL: ${apiUrl}`);
console.log(`Environment LOCAL_API_URL: ${process.env['LOCAL_API_URL'] || 'not set'}`);
// Test 2: Backend health check
console.log('\n2. Backend Health Check:');
console.log('-------------------------');
const healthResult = await checkBackendHealth();
console.log(`Backend Available: ${healthResult.isAvailable}`);
console.log(`Response Time: ${healthResult.responseTime}ms`);
if (healthResult.error) {
console.log(`Error: ${healthResult.error}`);
}
// Test 3: Manual endpoint test
console.log('\n3. Manual Endpoint Test:');
console.log('-------------------------');
try {
const response = await fetch(`${apiUrl}/api/users/validate/openai-key`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ apiKey: 'sk-proj-test-key' })
});
console.log(`Response Status: ${response.status}`);
console.log(`Response Headers:`, Object.fromEntries(response.headers.entries()));
const contentType = response.headers.get('content-type');
if (contentType && contentType.includes('application/json')) {
const data = await response.json();
console.log(`Response Data:`, data);
}
else {
const text = await response.text();
console.log(`Response Text (first 300 chars):`, text.substring(0, 300));
}
}
catch (error) {
console.log(`Manual test failed:`, error);
}
// Test 4: Validation function test (with fake key)
console.log('\n4. Validation Function Test:');
console.log('-----------------------------');
try {
const validationResult = await validateOpenAIApiKey('sk-proj-test-key-for-validation-testing');
console.log(`Validation Result:`, validationResult);
}
catch (error) {
console.log(`Validation function failed:`, error);
}
console.log('\n' + '='.repeat(60));
console.log('Test completed. Check the output above for issues.');
console.log('='.repeat(60));
}
// Run tests if this script is executed directly
if (require.main === module) {
runValidationTests().catch(console.error);
}
export { runValidationTests };