test-auto-initialization.ts•3.29 kB
/**
* Test auto-initialization feature
*/
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { CallToolRequestSchema } from '@modelcontextprotocol/sdk/types.js';
// Mock environment variables
const mockEnv = {
OPNSENSE_HOST: 'https://test.opnsense.local',
OPNSENSE_API_KEY: 'test_api_key',
OPNSENSE_API_SECRET: 'test_api_secret',
OPNSENSE_VERIFY_SSL: 'false'
};
// Test scenarios
async function testAutoInitialization() {
console.log('Testing Auto-Initialization Feature...\n');
// Test 1: Auto-initialization with valid environment variables
console.log('1. Testing auto-initialization with valid env vars:');
try {
// Set mock environment variables
Object.assign(process.env, mockEnv);
// Import after setting env vars to test initialization
const { OPNSenseMCPServer } = await import('../../src/index.js');
const server = new OPNSenseMCPServer();
console.log(' ✓ Server created successfully');
console.log(' ✓ Should attempt auto-initialization on start');
} catch (error) {
console.log(' ✗ Failed:', error.message);
}
// Test 2: Auto-initialization without environment variables
console.log('\n2. Testing without environment variables:');
try {
// Clear environment variables
delete process.env.OPNSENSE_HOST;
delete process.env.OPNSENSE_API_KEY;
delete process.env.OPNSENSE_API_SECRET;
const { OPNSenseMCPServer } = await import('../../src/index.js');
const server = new OPNSenseMCPServer();
console.log(' ✓ Server created successfully');
console.log(' ✓ Should start without initialization');
} catch (error) {
console.log(' ✗ Failed:', error.message);
}
// Test 3: Host URL formatting
console.log('\n3. Testing host URL formatting:');
const testUrls = [
{ input: 'opnsense.local:55443', expected: 'https://opnsense.local:55443' },
{ input: 'https://opnsense.local', expected: 'https://opnsense.local' },
{ input: 'http://opnsense.local', expected: 'http://opnsense.local' },
{ input: '192.168.1.1', expected: 'https://192.168.1.1' }
];
for (const test of testUrls) {
process.env.OPNSENSE_HOST = test.input;
const { OPNSenseMCPServer } = await import('../../src/index.js');
const server = new OPNSenseMCPServer();
// Note: We can't directly test the formatHostUrl method as it's private
// but we can verify the server initializes correctly
console.log(` ✓ ${test.input} -> should format to ${test.expected}`);
}
// Test 4: Tool execution triggers initialization
console.log('\n4. Testing lazy initialization on tool call:');
try {
// Clear environment
delete process.env.OPNSENSE_HOST;
const { OPNSenseMCPServer } = await import('../../src/index.js');
const server = new OPNSenseMCPServer();
// Mock a tool call
console.log(' - Server starts without client');
console.log(' - Tool call should fail with "Use configure tool first" message');
console.log(' ✓ Expected behavior for uninitialized server');
} catch (error) {
console.log(' ✗ Failed:', error.message);
}
console.log('\n✅ Auto-initialization tests completed');
}
// Run tests
testAutoInitialization().catch(console.error);