test_honcho_client_config.py
1 """Tests for Honcho client configuration.""" 2 3 import json 4 import os 5 import tempfile 6 from pathlib import Path 7 8 import pytest 9 10 from plugins.memory.honcho.client import HonchoClientConfig 11 12 13 class TestHonchoClientConfigAutoEnable: 14 """Test auto-enable behavior when API key is present.""" 15 16 def test_auto_enables_when_api_key_present_no_explicit_enabled(self, tmp_path): 17 """When API key exists and enabled is not set, should auto-enable.""" 18 config_path = tmp_path / "config.json" 19 config_path.write_text(json.dumps({ 20 "apiKey": "test-api-key-12345", 21 # Note: no "enabled" field 22 })) 23 24 cfg = HonchoClientConfig.from_global_config(config_path=config_path) 25 26 assert cfg.api_key == "test-api-key-12345" 27 assert cfg.enabled is True # Auto-enabled because API key exists 28 29 def test_respects_explicit_enabled_false(self, tmp_path): 30 """When enabled is explicitly False, should stay disabled even with API key.""" 31 config_path = tmp_path / "config.json" 32 config_path.write_text(json.dumps({ 33 "apiKey": "test-api-key-12345", 34 "enabled": False, # Explicitly disabled 35 })) 36 37 cfg = HonchoClientConfig.from_global_config(config_path=config_path) 38 39 assert cfg.api_key == "test-api-key-12345" 40 assert cfg.enabled is False # Respects explicit setting 41 42 def test_respects_explicit_enabled_true(self, tmp_path): 43 """When enabled is explicitly True, should be enabled.""" 44 config_path = tmp_path / "config.json" 45 config_path.write_text(json.dumps({ 46 "apiKey": "test-api-key-12345", 47 "enabled": True, 48 })) 49 50 cfg = HonchoClientConfig.from_global_config(config_path=config_path) 51 52 assert cfg.api_key == "test-api-key-12345" 53 assert cfg.enabled is True 54 55 def test_disabled_when_no_api_key_and_no_explicit_enabled(self, tmp_path): 56 """When no API key and enabled not set, should be disabled.""" 57 config_path = tmp_path / "config.json" 58 config_path.write_text(json.dumps({ 59 "workspace": "test", 60 # No apiKey, no enabled 61 })) 62 63 # Clear env var if set 64 env_key = os.environ.pop("HONCHO_API_KEY", None) 65 try: 66 cfg = HonchoClientConfig.from_global_config(config_path=config_path) 67 assert cfg.api_key is None 68 assert cfg.enabled is False # No API key = not enabled 69 finally: 70 if env_key: 71 os.environ["HONCHO_API_KEY"] = env_key 72 73 def test_auto_enables_with_env_var_api_key(self, tmp_path, monkeypatch): 74 """When API key is in env var (not config), should auto-enable.""" 75 config_path = tmp_path / "config.json" 76 config_path.write_text(json.dumps({ 77 "workspace": "test", 78 # No apiKey in config 79 })) 80 81 monkeypatch.setenv("HONCHO_API_KEY", "env-api-key-67890") 82 83 cfg = HonchoClientConfig.from_global_config(config_path=config_path) 84 85 assert cfg.api_key == "env-api-key-67890" 86 assert cfg.enabled is True # Auto-enabled from env var API key 87 88 def test_from_env_always_enabled(self, monkeypatch): 89 """from_env() should always set enabled=True.""" 90 monkeypatch.setenv("HONCHO_API_KEY", "env-test-key") 91 92 cfg = HonchoClientConfig.from_env() 93 94 assert cfg.api_key == "env-test-key" 95 assert cfg.enabled is True 96 97 def test_falls_back_to_env_when_no_config_file(self, tmp_path, monkeypatch): 98 """When config file doesn't exist, should fall back to from_env().""" 99 nonexistent = tmp_path / "nonexistent.json" 100 monkeypatch.setenv("HONCHO_API_KEY", "fallback-key") 101 102 cfg = HonchoClientConfig.from_global_config(config_path=nonexistent) 103 104 assert cfg.api_key == "fallback-key" 105 assert cfg.enabled is True # from_env() sets enabled=True