153 lines
5.6 KiB
JavaScript
153 lines
5.6 KiB
JavaScript
#!/usr/bin/env node
|
||
/**
|
||
* Test script to verify dynamic path handling in API client
|
||
* This script tests that the API client correctly uses PUBLIC_URL for dynamic paths
|
||
*/
|
||
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
|
||
class ApiClientPathTester {
|
||
constructor() {
|
||
this.clientPath = path.join(__dirname, 'src', 'api-client.ts');
|
||
}
|
||
|
||
/**
|
||
* Test that all API methods use getApiPath for dynamic paths
|
||
*/
|
||
testDynamicPaths() {
|
||
console.log('🧪 Testing API Client Dynamic Path Usage');
|
||
console.log('=========================================\n');
|
||
|
||
try {
|
||
const clientContent = fs.readFileSync(this.clientPath, 'utf8');
|
||
|
||
// Find all API method calls
|
||
const apiCallRegex = /this\.request<[^>]*>\(([^,]+),/g;
|
||
const hardcodedPathRegex = /this\.request<[^>]*>\("\/ai-voicebot/g;
|
||
const dynamicPathRegex = /this\.request<[^>]*>\(this\.getApiPath\(/g;
|
||
|
||
let match;
|
||
const allApiCalls = [];
|
||
const hardcodedCalls = [];
|
||
const dynamicCalls = [];
|
||
|
||
// Find all API calls
|
||
while ((match = apiCallRegex.exec(clientContent)) !== null) {
|
||
allApiCalls.push(match[1].trim());
|
||
}
|
||
|
||
// Find hardcoded calls (bad)
|
||
const hardcodedMatches = [...clientContent.matchAll(hardcodedPathRegex)];
|
||
|
||
// Find dynamic calls (good)
|
||
const dynamicMatches = [...clientContent.matchAll(dynamicPathRegex)];
|
||
|
||
console.log('📊 Results:');
|
||
console.log(` • Total API method calls: ${allApiCalls.length}`);
|
||
console.log(` • Using this.getApiPath(): ${dynamicMatches.length}`);
|
||
console.log(` • Using hardcoded paths: ${hardcodedMatches.length}`);
|
||
|
||
if (hardcodedMatches.length === 0) {
|
||
console.log('\n✅ PASS: No hardcoded /ai-voicebot paths found!');
|
||
console.log(' All API calls use dynamic path construction.');
|
||
} else {
|
||
console.log('\n❌ FAIL: Found hardcoded paths that should use getApiPath():');
|
||
// Show some context for each hardcoded path
|
||
hardcodedMatches.forEach((match, index) => {
|
||
const start = Math.max(0, match.index - 50);
|
||
const end = Math.min(clientContent.length, match.index + 100);
|
||
const context = clientContent.slice(start, end).replace(/\n/g, '\\n');
|
||
console.log(` ${index + 1}. ...${context}...`);
|
||
});
|
||
}
|
||
|
||
// Check that imports are correct
|
||
const hasCommonImport = clientContent.includes('import { base } from "./Common"');
|
||
const hasGetApiPathMethod = clientContent.includes('getApiPath(schemaPath: string)');
|
||
|
||
console.log('\n🔍 Implementation Details:');
|
||
console.log(` • Imports base from Common.ts: ${hasCommonImport ? '✅' : '❌'}`);
|
||
console.log(` • Has getApiPath() method: ${hasGetApiPathMethod ? '✅' : '❌'}`);
|
||
|
||
if (hasCommonImport && hasGetApiPathMethod && hardcodedMatches.length === 0) {
|
||
console.log('\n🎉 All tests passed! API client correctly uses dynamic paths.');
|
||
return true;
|
||
} else {
|
||
console.log('\n⚠️ Some issues found. See details above.');
|
||
return false;
|
||
}
|
||
|
||
} catch (error) {
|
||
console.error('❌ Test failed:', error.message);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Test that the evolution checker also uses dynamic paths
|
||
*/
|
||
testEvolutionChecker() {
|
||
console.log('\n🧪 Testing Evolution Checker Dynamic Path Usage');
|
||
console.log('===============================================\n');
|
||
|
||
try {
|
||
const checkerPath = path.join(__dirname, 'src', 'api-evolution-checker.ts');
|
||
const checkerContent = fs.readFileSync(checkerPath, 'utf8');
|
||
|
||
const hasCommonImport = checkerContent.includes('import { base } from \'./Common\'') ||
|
||
checkerContent.includes('import { base } from "./Common"');
|
||
const usesDynamicSchema = checkerContent.includes('`${base}/openapi-schema.json`');
|
||
const usesDynamicProxy = checkerContent.includes('`${base}/{path}`');
|
||
|
||
console.log('📊 Evolution Checker Results:');
|
||
console.log(` • Imports base from Common.ts: ${hasCommonImport ? '✅' : '❌'}`);
|
||
console.log(` • Uses dynamic schema path: ${usesDynamicSchema ? '✅' : '❌'}`);
|
||
console.log(` • Uses dynamic proxy check: ${usesDynamicProxy ? '✅' : '❌'}`);
|
||
|
||
if (hasCommonImport && usesDynamicSchema && usesDynamicProxy) {
|
||
console.log('\n✅ Evolution checker correctly uses dynamic paths!');
|
||
return true;
|
||
} else {
|
||
console.log('\n⚠️ Evolution checker has some hardcoded paths.');
|
||
return false;
|
||
}
|
||
|
||
} catch (error) {
|
||
console.error('❌ Evolution checker test failed:', error.message);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Run all tests
|
||
*/
|
||
run() {
|
||
const apiClientTest = this.testDynamicPaths();
|
||
const evolutionTest = this.testEvolutionChecker();
|
||
|
||
console.log('\n📋 Summary');
|
||
console.log('===========');
|
||
console.log(`API Client: ${apiClientTest ? '✅ PASS' : '❌ FAIL'}`);
|
||
console.log(`Evolution Checker: ${evolutionTest ? '✅ PASS' : '❌ FAIL'}`);
|
||
|
||
if (apiClientTest && evolutionTest) {
|
||
console.log('\n🎉 All dynamic path tests passed!');
|
||
console.log('The system correctly uses PUBLIC_URL for dynamic path construction.');
|
||
return 0;
|
||
} else {
|
||
console.log('\n⚠️ Some tests failed. Check the details above.');
|
||
return 1;
|
||
}
|
||
}
|
||
}
|
||
|
||
// Run the test
|
||
if (require.main === module) {
|
||
const tester = new ApiClientPathTester();
|
||
const exitCode = tester.run();
|
||
process.exit(exitCode);
|
||
}
|
||
|
||
module.exports = ApiClientPathTester;
|