import assert from 'node:assert/strict';
import { describe, it, beforeEach, afterEach } from 'node:test';
import { ConfigManager } from './config.js';
describe('ConfigManager', () => {
    const originalEnv = { ...process.env };
    let configManager;
    beforeEach(() => {
        configManager = new ConfigManager();
        // 清理环境变量
        delete process.env.AWS_SERVER_URL;
        delete process.env.AWS_HEARTBEAT_INTERVAL;
        delete process.env.AWS_WORKSPACE_PATH;
        delete process.env.AWS_ROLE_NAME;
        delete process.env.AWS_AGENT_ID;
        delete process.env.AWS_PROJECT_NAME;
        delete process.env.AWS_PROMPT;
        delete process.env.AWS_MCP_HTTP_URL;
    });
    afterEach(() => {
        // 恢复环境变量
        process.env = { ...originalEnv };
    });
    describe('loadConfig', () => {
        it('loads default values when no env vars set', () => {
            configManager.loadConfig();
            const agentConfig = configManager.getAgentConfig();
            const serverConfig = configManager.getServerConfig();
            assert.equal(agentConfig.roleName, '');
            assert.equal(agentConfig.projectName, 'default');
            assert.equal(serverConfig.serverUrl, 'ws://localhost:8080/ws/agent');
            assert.equal(serverConfig.heartbeatInterval, 3000);
        });
        it('loads custom server URL from env', () => {
            process.env.AWS_SERVER_URL = 'wss://custom.example.com/ws/agent';
            configManager.loadConfig();
            const serverConfig = configManager.getServerConfig();
            assert.equal(serverConfig.serverUrl, 'wss://custom.example.com/ws/agent');
        });
        it('loads custom heartbeat interval from env', () => {
            process.env.AWS_HEARTBEAT_INTERVAL = '30000';
            configManager.loadConfig();
            const serverConfig = configManager.getServerConfig();
            assert.equal(serverConfig.heartbeatInterval, 30000);
        });
        it('loads workspace path from env', () => {
            process.env.AWS_WORKSPACE_PATH = '/custom/workspace';
            configManager.loadConfig();
            const agentConfig = configManager.getAgentConfig();
            assert.equal(agentConfig.workspacePath, '/custom/workspace');
        });
        it('uses cwd as default workspace path', () => {
            configManager.loadConfig();
            const agentConfig = configManager.getAgentConfig();
            assert.equal(agentConfig.workspacePath, process.cwd());
        });
        it('loads role name from env', () => {
            process.env.AWS_ROLE_NAME = 'backend-expert';
            configManager.loadConfig();
            const agentConfig = configManager.getAgentConfig();
            assert.equal(agentConfig.roleName, 'backend-expert');
        });
        it('loads agent ID from env', () => {
            process.env.AWS_AGENT_ID = 'agent-123';
            configManager.loadConfig();
            const agentConfig = configManager.getAgentConfig();
            assert.equal(agentConfig.agentId, 'agent-123');
        });
        it('loads project name from env', () => {
            process.env.AWS_PROJECT_NAME = 'my-project';
            configManager.loadConfig();
            const agentConfig = configManager.getAgentConfig();
            assert.equal(agentConfig.projectName, 'my-project');
        });
        it('loads prompt from env', () => {
            process.env.AWS_PROMPT = 'You are a helpful assistant';
            configManager.loadConfig();
            const agentConfig = configManager.getAgentConfig();
            assert.equal(agentConfig.prompt, 'You are a helpful assistant');
        });
        it('loads mcpHttpUrl from env', () => {
            process.env.AWS_MCP_HTTP_URL = 'http://custom:3000/mcp/call';
            configManager.loadConfig();
            const serverConfig = configManager.getServerConfig();
            assert.equal(serverConfig.mcpHttpUrl, 'http://custom:3000/mcp/call');
        });
        it('sets undefined mcpHttpUrl when not set', () => {
            configManager.loadConfig();
            const serverConfig = configManager.getServerConfig();
            assert.equal(serverConfig.mcpHttpUrl, undefined);
        });
    });
    describe('getAgentConfig', () => {
        it('throws error when config not loaded', () => {
            assert.throws(() => configManager.getAgentConfig(), /Configuration is not loaded/);
        });
        it('returns config after loadConfig called', () => {
            configManager.loadConfig();
            const config = configManager.getAgentConfig();
            assert.ok(config);
            assert.ok(typeof config === 'object');
        });
    });
    describe('getServerConfig', () => {
        it('throws error when config not loaded', () => {
            assert.throws(() => configManager.getServerConfig(), /Configuration is not loaded/);
        });
        it('returns config after loadConfig called', () => {
            configManager.loadConfig();
            const config = configManager.getServerConfig();
            assert.ok(config);
            assert.ok(typeof config === 'object');
            assert.equal(config.reconnectInterval, 200);
            assert.equal(config.maxReconnectAttempts, -1);
        });
    });
    describe('applyRuntimeLaunchBinding', () => {
        it('repairs duplicated scheduler ports from runtime bridge bindings', () => {
            configManager.loadConfig();
            configManager.applyRuntimeLaunchBinding({
                agentId: 'agent-a',
                schedulerBaseUrl: 'http://127.0.0.1:8080:8080',
            });
            const serverConfig = configManager.getServerConfig();
            assert.equal(serverConfig.serverUrl, 'ws://127.0.0.1:8080/ws/agent');
            assert.equal(serverConfig.mcpHttpUrl, 'http://127.0.0.1:8080/mcp/call');
        });
        it('preserves MCP HTTP subpaths from runtime bridge bindings', () => {
            configManager.loadConfig();
            configManager.applyRuntimeLaunchBinding({
                agentId: 'agent-a',
                serverUrl: 'http://example.com/api',
                mcpHttpUrl: 'http://example.com/api/mcp/call',
            });
            const serverConfig = configManager.getServerConfig();
            assert.equal(serverConfig.serverUrl, 'ws://example.com/api/ws/agent');
            assert.equal(serverConfig.mcpHttpUrl, 'http://example.com/api/mcp/call');
        });
    });
    describe('multiple loadConfig calls', () => {
        it('overwrites previous config on subsequent calls', () => {
            process.env.AWS_SERVER_URL = 'ws://first:8080/ws/agent';
            configManager.loadConfig();
            assert.equal(configManager.getServerConfig().serverUrl, 'ws://first:8080/ws/agent');
            process.env.AWS_SERVER_URL = 'ws://second:8080/ws/agent';
            configManager.loadConfig();
            assert.equal(configManager.getServerConfig().serverUrl, 'ws://second:8080/ws/agent');
        });
    });
});
//# sourceMappingURL=config.test.js.map