| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- import os
- import tempfile
- import unittest
- from config import GatewayConfig
- class GatewayConfigCompatTest(unittest.TestCase):
- def test_gateway_config_accepts_documented_fms_env_names_when_dotenv_missing(self):
- env = {
- 'FMS_API_BASE': 'http://gateway.example.test',
- 'FMS_CLIENT_TYPE': 'workbuddy',
- 'FMS_TIMEOUT_MS': '15000',
- 'FMS_LOG_LEVEL': 'debug',
- }
- config = GatewayConfig.from_env(env, dotenv_path=os.path.join(tempfile.gettempdir(), 'missing-fms-mcp.env'))
- self.assertEqual('http://gateway.example.test', config.auth_base_url)
- self.assertEqual('http://gateway.example.test', config.tools_base_url)
- self.assertEqual('workbuddy', config.client_type)
- self.assertEqual(15, config.timeout_seconds)
- self.assertEqual('debug', config.log_level)
- def test_gateway_config_reads_dotenv_file_when_env_missing(self):
- with tempfile.TemporaryDirectory() as tmp_dir:
- dotenv_path = os.path.join(tmp_dir, '.env')
- with open(dotenv_path, 'w', encoding='utf-8') as file:
- file.write('FMS_API_BASE=http://dotenv.example.test\n')
- file.write('FMS_CLIENT_TYPE=workbuddy\n')
- file.write('FMS_TIMEOUT_SECONDS=12\n')
- file.write('FMS_LOG_LEVEL=warning\n')
- file.write('FMS_TOKEN_STORE_PATH=.runtime-token.json\n')
- config = GatewayConfig.from_env(env={}, dotenv_path=dotenv_path)
- self.assertEqual('http://dotenv.example.test', config.auth_base_url)
- self.assertEqual('http://dotenv.example.test', config.tools_base_url)
- self.assertEqual('workbuddy', config.client_type)
- self.assertEqual(12, config.timeout_seconds)
- self.assertEqual('warning', config.log_level)
- self.assertEqual('.runtime-token.json', config.token_store_path)
- def test_gateway_config_prefers_dotenv_over_environment(self):
- with tempfile.TemporaryDirectory() as tmp_dir:
- dotenv_path = os.path.join(tmp_dir, '.env')
- with open(dotenv_path, 'w', encoding='utf-8') as file:
- file.write('FMS_AUTH_BASE=http://dotenv-auth.example.test\n')
- file.write('FMS_TOOLS_BASE=http://dotenv-tools.example.test\n')
- file.write('FMS_CLIENT_TYPE=workbuddy-dotenv\n')
- file.write('FMS_TIMEOUT_SECONDS=12\n')
- file.write('FMS_LOG_LEVEL=warning\n')
- file.write('FMS_TOKEN_STORE_PATH=.dotenv-token.json\n')
- config = GatewayConfig.from_env(
- env={
- 'FMS_AUTH_BASE': 'http://env-auth.example.test',
- 'FMS_TOOLS_BASE': 'http://env-tools.example.test',
- 'FMS_CLIENT_TYPE': 'workbuddy-env',
- 'FMS_TIMEOUT_SECONDS': '30',
- 'FMS_LOG_LEVEL': 'debug',
- 'FMS_TOKEN_STORE_PATH': '.env-token.json',
- },
- dotenv_path=dotenv_path,
- )
- self.assertEqual('http://dotenv-auth.example.test', config.auth_base_url)
- self.assertEqual('http://dotenv-tools.example.test', config.tools_base_url)
- self.assertEqual('workbuddy-dotenv', config.client_type)
- self.assertEqual(12, config.timeout_seconds)
- self.assertEqual('warning', config.log_level)
- self.assertEqual('.dotenv-token.json', config.token_store_path)
- def test_public_gateway_config_reads_session_ttl_and_mode(self):
- config = GatewayConfig.from_env(env={
- 'FMS_API_BASE': 'https://base.example.com',
- 'FMS_GATEWAY_MODE': 'public',
- 'FMS_GATEWAY_SESSION_TTL_SECONDS': '600',
- 'FMS_REDIS_PREFIX': 'fms:mcp:gateway:',
- }, dotenv_path='missing.env')
- self.assertEqual('public', config.gateway_mode)
- self.assertEqual(600, config.gateway_session_ttl_seconds)
- self.assertEqual('fms:mcp:gateway:', config.redis_prefix)
- def test_timeout_ms_conversion_in_preferred_env(self):
- """Test FMS_TIMEOUT_MS conversion in preferred env"""
- config = GatewayConfig.from_env(env={
- 'FMS_API_BASE': 'https://base.example.com',
- 'FMS_TIMEOUT_MS': '5000', # 5000ms = 5 seconds
- }, dotenv_path='missing.env')
- self.assertEqual(5, config.timeout_seconds)
- def test_timeout_ms_minimum_value(self):
- """Test that FMS_TIMEOUT_MS converts to at least 1 second"""
- config = GatewayConfig.from_env(env={
- 'FMS_API_BASE': 'https://base.example.com',
- 'FMS_TIMEOUT_MS': '500', # 500ms should become 1 second
- }, dotenv_path='missing.env')
- self.assertEqual(1, config.timeout_seconds)
- if __name__ == '__main__':
- unittest.main()
|