#!/usr/bin/env node
/**
* SIMPLE TEST TO VERIFY MSP CUSTOMER DETECTION FIX
* =================================================
*
* This test verifies that OAuth sessions properly set authMethod and that
* MSP customer detection runs correctly for Bank Leumi queries.
*/
const { spawn } = require('child_process');
const path = require('path');
console.log('🔬 TESTING MSP CUSTOMER DETECTION FIX');
console.log('='.repeat(60));
async function runDirectTest() {
console.log('🚀 Starting direct MCP server test...\n');
// Start the MCP server directly
const serverProcess = spawn('node', [path.join(__dirname, '../../build/index.js')], {
stdio: 'pipe',
env: {
...process.env,
UMBRELLA_BASE_URL: 'https://umbrellacost.io',
DEBUG: 'true'
}
});
let serverOutput = '';
let serverErrors = '';
serverProcess.stdout.on('data', (data) => {
const text = data.toString();
serverOutput += text;
if (text.includes('Server info') || text.includes('[AUTH]') || text.includes('[MSP-CHECK]')) {
process.stdout.write('SERVER: ' + text);
}
});
serverProcess.stderr.on('data', (data) => {
const text = data.toString();
serverErrors += text;
if (text.includes('[AUTH]') || text.includes('[MSP-CHECK]') || text.includes('authMethod') || text.includes('customer detection')) {
process.stderr.write('SERVER: ' + text);
}
});
// Wait for server to start
await new Promise(resolve => setTimeout(resolve, 3000));
console.log('✅ MCP server started, now testing OAuth flow...\n');
// Test the OAuth authentication and MSP customer detection
const testScript = `
const { Client } = require('@modelcontextprotocol/sdk/client/index.js');
const { StdioClientTransport } = require('@modelcontextprotocol/sdk/client/stdio.js');
async function testMSPFlow() {
const transport = new StdioClientTransport({
command: 'node',
args: ['${path.join(__dirname, '../../build/index.js')}'],
env: {
...process.env,
UMBRELLA_BASE_URL: 'https://umbrellacost.io',
DEBUG: 'true'
}
});
const client = new Client(
{ name: 'msp-test-client', version: '1.0.0' },
{ capabilities: {} }
);
try {
await client.connect(transport);
console.log('✅ Connected to MCP server');
// Test OAuth authentication
console.log('\\n🔐 Testing OAuth authentication...');
const authResult = await client.request({
method: 'tools/call',
params: {
name: 'oauth_authenticate',
arguments: {
username: 'david+allcloud@umbrellacost.com',
password: process.env.UMBRELLA_PASSWORD || 'defaultPassword'
}
}
});
if (authResult.content?.[0]?.text?.includes('successfully')) {
console.log('✅ OAuth authentication successful');
} else {
console.log('❌ OAuth authentication failed');
console.log('Auth result:', JSON.stringify(authResult, null, 2));
return false;
}
// Test MSP customer detection
console.log('\\n🏦 Testing MSP customer detection for Bank Leumi...');
const recommendationsResult = await client.request({
method: 'tools/call',
params: {
name: 'api__api_v2_recommendations_list',
arguments: {
userQuery: 'show Bank Leumi BL Test Env cost recommendations'
}
}
});
console.log('\\n📊 RECOMMENDATIONS RESULT:');
console.log('='.repeat(40));
if (recommendationsResult.content?.[0]?.text) {
const resultText = recommendationsResult.content[0].text;
console.log(resultText);
if (resultText.includes('No recommendations data received')) {
console.log('\\n❌ FAILURE: MSP customer detection still not working');
return false;
} else if (resultText.includes('recommendations') || resultText.includes('cost') || resultText.includes('Bank Leumi')) {
console.log('\\n✅ SUCCESS: MSP customer detection working!');
return true;
} else {
console.log('\\n⚠️ UNCLEAR: Unexpected response format');
return false;
}
} else {
console.log('\\n❌ FAILURE: No response content received');
return false;
}
} catch (error) {
console.error('\\n❌ Test failed:', error.message);
return false;
} finally {
try {
await client.close();
} catch (e) {
// Ignore close errors
}
}
}
testMSPFlow().then(success => {
process.exit(success ? 0 : 1);
}).catch(error => {
console.error('Fatal error:', error);
process.exit(1);
});
`;
// Write and execute the test
require('fs').writeFileSync('/tmp/msp-test-flow.js', testScript);
return new Promise((resolve) => {
const testProcess = spawn('node', ['/tmp/msp-test-flow.js'], {
stdio: 'pipe',
env: { ...process.env }
});
let output = '';
let errorOutput = '';
testProcess.stdout.on('data', (data) => {
const text = data.toString();
output += text;
process.stdout.write(text);
});
testProcess.stderr.on('data', (data) => {
const text = data.toString();
errorOutput += text;
process.stderr.write(text);
});
testProcess.on('close', (code) => {
// Clean up
if (serverProcess.pid) {
serverProcess.kill('SIGTERM');
}
console.log('\\n' + '='.repeat(60));
if (code === 0) {
console.log('🎉 MSP CUSTOMER DETECTION FIX VERIFIED SUCCESSFULLY!');
console.log('✅ OAuth sessions properly detect authMethod');
console.log('✅ MSP customer detection runs correctly');
console.log('✅ Dynamic customer selection working');
} else {
console.log('❌ MSP customer detection still has issues');
console.log('🔧 Check server logs above for authMethod and customer detection details');
}
console.log('='.repeat(60));
resolve(code === 0);
});
});
}
runDirectTest().then(success => {
process.exit(success ? 0 : 1);
}).catch(error => {
console.error('❌ Test execution failed:', error.message);
process.exit(1);
});