#!/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;