#!/usr/bin/env node
const https = require('https');
const BEARER_TOKEN = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkYXZpZCtzYW9sYUB1bWJyZWxsYWNvc3QuY29tIiwiY2xpZW50SWQiOiJjbGF1ZGUtbWNwLWNsaWVudC1iNzFhNzljNmY0YzQ0YzY1IiwiaWF0IjoxNzU4NzMzNzI0LCJleHAiOjE3NTg4MjAxMjR9.hVS1kxOWnN7vJdE6wbC-OTErcYgdLKzAv2qGE9CgCzpKV2C8C1xV-fKg5xqwhT5P0jF4aOZrw3_Z8YxrNeD6Qw3kZvGQrT7uY4Lm0I9sJ6HqeA4-rRzW8fT3NxPcUeKjVnB2lYdDhMm0Sz7X4qQ-5vFgL9xN2I6hTcPjOy8K';
const MCP_ENDPOINT = 'https://gibson-editions-goto-gaps.trycloudflare.com/mcp';
async function testAnomalyDetectionFix() {
console.log('🔧 Testing Timeout Fix for Anomaly Detection...\\n');
const requestData = {
jsonrpc: '2.0',
method: 'tools/call',
params: {
name: 'api_anomaly_detection',
arguments: {
isFull: true,
userQuery: 'Show me open cost anomalies'
}
},
id: 1
};
const postData = JSON.stringify(requestData);
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${BEARER_TOKEN}`,
'Content-Length': Buffer.byteLength(postData)
}
};
return new Promise((resolve, reject) => {
console.log(`📍 Making MCP request to: ${MCP_ENDPOINT}`);
console.log(`🔑 Testing with Bearer token...`);
console.log(`📦 Request: ${JSON.stringify(requestData, null, 2)}`);
const startTime = Date.now();
const req = https.request(MCP_ENDPOINT, options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
const endTime = Date.now();
const duration = endTime - startTime;
console.log(`\\n✅ Response received in ${duration}ms`);
console.log(`📊 Status Code: ${res.statusCode}`);
try {
const jsonResponse = JSON.parse(data);
if (jsonResponse.error) {
console.log(`\\n❌ MCP Error:`, jsonResponse.error);
console.log(` This indicates the timeout fix may not have resolved the issue`);
} else if (jsonResponse.result) {
console.log(`\\n✅ SUCCESS! Anomaly detection completed successfully`);
console.log(`📄 Result type:`, Array.isArray(jsonResponse.result.content) ? 'Array' : typeof jsonResponse.result.content);
if (Array.isArray(jsonResponse.result.content)) {
console.log(`📊 Number of response items: ${jsonResponse.result.content.length}`);
if (jsonResponse.result.content[0]) {
const firstItem = jsonResponse.result.content[0];
console.log(`📝 First item preview: ${JSON.stringify(firstItem).substring(0, 200)}...`);
}
}
}
resolve({
success: !jsonResponse.error,
duration,
response: jsonResponse
});
} catch (parseError) {
console.log(`\\n❌ JSON Parse Error:`, parseError.message);
console.log(`📄 Raw response (first 200 chars):`, data.substring(0, 200));
resolve({
success: false,
error: 'Parse error',
duration,
rawData: data.substring(0, 500)
});
}
});
});
req.on('error', (error) => {
const endTime = Date.now();
const duration = endTime - startTime;
console.log(`\\n❌ Request Error after ${duration}ms:`, error.message);
reject(error);
});
// Set a manual timeout to detect if our fix worked
req.setTimeout(150000, () => { // 2.5 minutes - longer than our new 2 minute timeout
console.log(`\\n⏰ Request timeout after 150 seconds - this suggests the server timeout fix may need adjustment`);
req.destroy();
});
req.write(postData);
req.end();
});
}
async function runTest() {
try {
console.log('='.repeat(60));
console.log('🧪 TIMEOUT FIX VERIFICATION TEST');
console.log('='.repeat(60));
console.log('Testing the fix for anomaly detection timeouts...');
console.log('Expected: No more "Network error: Unable to connect" messages');
console.log('Expected: Either success or improved error message about large datasets\\n');
const result = await testAnomalyDetectionFix();
console.log('\\n' + '='.repeat(60));
console.log('📋 TEST RESULTS');
console.log('='.repeat(60));
if (result.success) {
console.log('✅ TIMEOUT FIX SUCCESSFUL!');
console.log(' - No more generic "Network error" messages');
console.log(' - Anomaly detection completed successfully');
console.log(` - Response time: ${result.duration}ms`);
} else {
console.log('❌ Issues detected:');
if (result.error === 'Parse error') {
console.log(' - Response parsing failed (possible server error)');
} else {
console.log(` - ${result.error}`);
}
console.log(` - Response time: ${result.duration}ms`);
}
} catch (error) {
console.error('❌ Test failed with error:', error.message);
}
}
runTest();