Spaces:
Runtime error
Runtime error
| import { testConnection as testFetcher } from './services/appwriteDataFetcher.js'; | |
| import candidateService from './services/candidateService.js'; | |
| import appwriteService from './services/AppwriteService.js'; | |
| import imageProxy from './services/imageProxy.js'; | |
| import cacheService from './services/cacheService.js'; | |
| async function runTests() { | |
| console.log('π§ͺ Starting Verification Tests...\n'); | |
| // 1. Test AppwriteService directly | |
| console.log('1οΈβ£ Testing AppwriteService...'); | |
| const serviceStats = appwriteService.getStats(); | |
| console.log(' Stats:', serviceStats); | |
| if (serviceStats.enabled) { | |
| console.log(' β AppwriteService is enabled'); | |
| } else { | |
| console.error(' β AppwriteService is disabled (check .env)'); | |
| } | |
| // 2. Test appwriteDataFetcher (Backward Compatibility) | |
| console.log('\n2οΈβ£ Testing appwriteDataFetcher (Backward Compatibility)...'); | |
| const fetcherResult = await testFetcher(); | |
| if (fetcherResult.healthy) { | |
| console.log(' β appwriteDataFetcher connection successful'); | |
| } else { | |
| console.error(' β appwriteDataFetcher failed:', fetcherResult.message); | |
| } | |
| // 3. Test candidateService | |
| console.log('\n3οΈβ£ Testing candidateService...'); | |
| const candidateHealth = await candidateService.healthCheck(); | |
| if (candidateHealth.healthy) { | |
| console.log(' β candidateService health check passed'); | |
| } else { | |
| console.error(' β candidateService health check failed:', candidateHealth.error); | |
| } | |
| // 4. Test Real Candidate Fetch | |
| console.log('\n4οΈβ£ Testing Real Candidate Fetch...'); | |
| try { | |
| const candidateData = await candidateService.getCandidateData( | |
| 'Sangeeta Kumari Singh Deo', | |
| 'Bolangir', | |
| 'Bharatiya Janata Party' | |
| ); | |
| if (candidateData && !candidateData.error) { | |
| console.log(' β Real candidate fetch successful'); | |
| console.log(' Data keys:', Object.keys(candidateData)); | |
| } else { | |
| console.error(' β Real candidate fetch failed:', candidateData?.error || 'Unknown error'); | |
| if (candidateData?.data?.error) { | |
| console.error(' Inner error:', candidateData.data.error); | |
| } | |
| } | |
| } catch (error) { | |
| console.error(' β Real candidate fetch threw error:', error.message); | |
| } | |
| // 5. Test Image Proxy | |
| console.log('\n5οΈβ£ Testing Image Proxy...'); | |
| try { | |
| // Mock environment variables | |
| process.env.RAILWAY_STATIC_URL = 'https://production-app.com/'; | |
| // Test 'dev' alias | |
| process.env.NODE_ENV = 'dev'; | |
| const testUrl = 'https://example.com/image.jpg'; | |
| const localBaseUrl = 'http://localhost:3000'; | |
| // Pass localBaseUrl as argument, simulating api.js behavior | |
| const proxyUrl = imageProxy.createProxyUrl(testUrl, localBaseUrl); | |
| console.log(` Original: ${testUrl}`); | |
| console.log(` Proxy (Dev): ${proxyUrl}`); | |
| if (proxyUrl.startsWith(localBaseUrl)) { | |
| console.log(' β Correctly used local URL in development mode (alias: dev)'); | |
| } else { | |
| console.error(' β Failed to use local URL in development mode (alias: dev)'); | |
| } | |
| // Test Production Mode | |
| process.env.NODE_ENV = 'production'; | |
| const prodProxyUrl = imageProxy.createProxyUrl(testUrl, localBaseUrl); | |
| console.log(` Proxy (Prod): ${prodProxyUrl}`); | |
| if (prodProxyUrl.startsWith('https://production-app.com')) { | |
| console.log(' β Correctly used Railway URL in production mode'); | |
| } else { | |
| console.error(' β Failed to use Railway URL in production mode'); | |
| } | |
| } catch (error) { | |
| console.error(' β Image proxy test failed:', error.message); | |
| } | |
| // 6. Test Cache Service (Polling) | |
| console.log('\n6οΈβ£ Testing Cache Service (Polling)...'); | |
| try { | |
| const requestId = 'test-poll-id'; | |
| const pollData = { status: 'ready', data: { foo: 'bar' } }; | |
| // Set cache | |
| cacheService.set('poll', requestId, pollData); | |
| // Get cache | |
| const cached = cacheService.get('poll', requestId); | |
| if (cached && cached.data.foo === 'bar') { | |
| console.log(' β Cache service handles "poll" type correctly'); | |
| } else { | |
| console.error(' β Cache service failed to retrieve "poll" data'); | |
| } | |
| // Check TTL | |
| const ttl = cacheService.getTTL('poll'); | |
| if (ttl === 5 * 60 * 1000) { | |
| console.log(' β Poll TTL is correct (5 mins)'); | |
| } else { | |
| console.error(` β Poll TTL is incorrect: ${ttl}`); | |
| } | |
| } catch (error) { | |
| console.error(' β Cache service test failed:', error.message); | |
| } | |
| console.log('\nπ Verification Complete'); | |
| } | |
| runTests().catch(console.error); | |