#!/usr/bin/env node
/**
* Test the fixed URL construction logic
*/
const { UmbrellaApiClient } = require('../../dist/api-client.js');
async function testUrlConstruction() {
console.log('\n🔧 Testing URL Construction Fix');
console.log('=' .repeat(60));
const baseURL = 'https://api.umbrellacost.io/api';
const frontendBaseURL = 'https://api-front.umbrellacost.io/api';
const apiClient = new UmbrellaApiClient(baseURL, frontendBaseURL);
console.log('Base URL:', baseURL);
console.log('Frontend Base URL:', frontendBaseURL);
console.log('');
// Test mock requests to see URL construction
const testCases = [
{
name: 'Regular endpoint (should use base URL)',
path: '/invoices/caui',
customerAccountKey: null,
expectedUrl: 'https://api.umbrellacost.io/api/invoices/caui'
},
{
name: 'Recommendations endpoint without customer (should use base URL)',
path: '/recommendationsNew/heatmap/summary',
customerAccountKey: null,
expectedUrl: 'https://api.umbrellacost.io/api/recommendationsNew/heatmap/summary'
},
{
name: 'Recommendations endpoint with customer (should use frontend URL)',
path: '/recommendationsNew/heatmap/summary',
customerAccountKey: '22676',
expectedUrl: 'https://api-front.umbrellacost.io/api/recommendationsNew/heatmap/summary'
},
{
name: 'Legacy recommendations with customer (should use frontend URL)',
path: '/recommendations/list',
customerAccountKey: '24223',
expectedUrl: 'https://api-front.umbrellacost.io/api/recommendations/list'
}
];
for (const testCase of testCases) {
console.log(`\n📋 Test: ${testCase.name}`);
console.log(`Path: ${testCase.path}`);
console.log(`Customer Account Key: ${testCase.customerAccountKey || 'None'}`);
console.log(`Expected URL: ${testCase.expectedUrl}`);
// This would be the URL that gets constructed
const isRecommendationsEndpoint = testCase.path.includes('/recommendationsNew/') || testCase.path.includes('/recommendations/');
const useFrontendApi = isRecommendationsEndpoint && testCase.customerAccountKey;
const apiUrl = useFrontendApi ? frontendBaseURL : baseURL;
const finalUrl = `${apiUrl}${testCase.path}`;
console.log(`Actual URL: ${finalUrl}`);
console.log(`✅ Match: ${finalUrl === testCase.expectedUrl ? 'YES' : 'NO'}`);
if (finalUrl !== testCase.expectedUrl) {
console.log('❌ URL MISMATCH!');
}
}
console.log('\n' + '=' .repeat(60));
console.log('✅ URL construction test completed');
}
// Run the test
testUrlConstruction().catch(err => {
console.error('❌ Test failed:', err);
process.exit(1);
});