config.test.ts•5.18 kB
import { describe, test, expect, beforeEach } from '@jest/globals';
import { loadConfig, loadMCPServerConfig, isToolEnabled, getEnabledTools } from '../src/utils/config.js';
describe('Configuration Management', () => {
const originalEnv = process.env;
beforeEach(() => {
// Reset environment
jest.resetModules();
process.env = { ...originalEnv };
});
afterEach(() => {
process.env = originalEnv;
});
describe('loadConfig', () => {
test('should load config from environment variables', () => {
process.env.ACI_APIC_URL = 'https://test-apic.com';
process.env.ACI_USERNAME = 'testuser';
process.env.ACI_PASSWORD = 'testpass';
process.env.ACI_VALIDATE_CERTS = 'true';
process.env.ACI_TIMEOUT = '15000';
const config = loadConfig();
expect(config.apicUrl).toBe('https://test-apic.com');
expect(config.username).toBe('testuser');
expect(config.password).toBe('testpass');
expect(config.validateCerts).toBe(true);
expect(config.timeout).toBe(15000);
});
test('should throw error when username is missing', () => {
process.env.ACI_APIC_URL = 'https://test-apic.com';
delete process.env.ACI_USERNAME;
expect(() => loadConfig()).toThrow('ACI username not configured');
});
test('should throw error when no authentication method is provided', () => {
process.env.ACI_APIC_URL = 'https://test-apic.com';
process.env.ACI_USERNAME = 'testuser';
delete process.env.ACI_PASSWORD;
delete process.env.ACI_PRIVATE_KEY_PATH;
expect(() => loadConfig()).toThrow('ACI password or private key not configured');
});
test('should use default values when not specified', () => {
process.env.ACI_APIC_URL = 'https://test-apic.com';
process.env.ACI_USERNAME = 'testuser';
process.env.ACI_PASSWORD = 'testpass';
const config = loadConfig();
expect(config.validateCerts).toBe(false);
expect(config.timeout).toBe(30000);
});
});
describe('loadMCPServerConfig', () => {
test('should load default core configuration', () => {
const config = loadMCPServerConfig();
expect(config.serverConfig.name).toBe('aci');
expect(config.serverConfig.toolSelectionMode).toBe('whitelist');
expect(config.serverConfig.enableAllTools).toBe(false);
expect(config.toolConfiguration.enabledTools).toContain('list_tenants');
expect(config.toolConfiguration.enabledTools).toContain('get_fabric_health');
});
test('should enable all tools when ACI_TOOL_MODE=all', () => {
process.env.ACI_TOOL_MODE = 'all';
const config = loadMCPServerConfig();
expect(config.serverConfig.enableAllTools).toBe(true);
expect(config.toolConfiguration.enabledTools.length).toBeGreaterThan(35);
});
test('should set tenant focus from environment', () => {
process.env.ACI_TENANT_FOCUS = 'production';
const config = loadMCPServerConfig();
expect(config.serverConfig.tenantFocus).toBe('production');
});
});
describe('Tool Management', () => {
test('should correctly identify enabled tools', () => {
const config = {
serverConfig: {
name: 'aci',
toolSelectionMode: 'whitelist' as const,
enableAllTools: false
},
toolConfiguration: {
enabledTools: ['list_tenants', 'get_tenant'],
disabledTools: []
}
};
expect(isToolEnabled('list_tenants', config)).toBe(true);
expect(isToolEnabled('create_tenant', config)).toBe(false);
});
test('should handle blacklist mode correctly', () => {
const config = {
serverConfig: {
name: 'aci',
toolSelectionMode: 'blacklist' as const,
enableAllTools: false
},
toolConfiguration: {
enabledTools: [],
disabledTools: ['create_tenant', 'delete_tenant']
}
};
expect(isToolEnabled('list_tenants', config)).toBe(true);
expect(isToolEnabled('create_tenant', config)).toBe(false);
expect(isToolEnabled('delete_tenant', config)).toBe(false);
});
test('should handle enableAllTools correctly', () => {
const config = {
serverConfig: {
name: 'aci',
toolSelectionMode: 'whitelist' as const,
enableAllTools: true
},
toolConfiguration: {
enabledTools: [],
disabledTools: ['dangerous_tool']
}
};
expect(isToolEnabled('list_tenants', config)).toBe(true);
expect(isToolEnabled('dangerous_tool', config)).toBe(false);
});
test('should get correct list of enabled tools', () => {
const config = {
serverConfig: {
name: 'aci',
toolSelectionMode: 'whitelist' as const,
enableAllTools: false
},
toolConfiguration: {
enabledTools: ['list_tenants', 'get_tenant', 'create_tenant'],
disabledTools: []
}
};
const enabledTools = getEnabledTools(config);
expect(enabledTools).toEqual(['list_tenants', 'get_tenant', 'create_tenant']);
});
});
});