77 lines
2.2 KiB
Python
77 lines
2.2 KiB
Python
import json
|
|
import os
|
|
|
|
from common.env_manager import EnvManager
|
|
from common.settings_manager import SettingsManager
|
|
|
|
|
|
def test_env_manager_basic(tmp_path):
|
|
# Setup temp env files
|
|
env_dir = tmp_path / "envs"
|
|
env_dir.mkdir()
|
|
|
|
manager = EnvManager(str(env_dir))
|
|
|
|
# Test set_credentials
|
|
manager.set_credentials("test", {"KEY1": "VAL1", "KEY2": "VAL2"})
|
|
|
|
path = manager.get_env_path("test")
|
|
assert os.path.exists(path)
|
|
|
|
with open(path, "r") as f:
|
|
content = f.read()
|
|
# set_key might quote values
|
|
assert "KEY1='VAL1'" in content or "KEY1=VAL1" in content
|
|
|
|
# Test load_service_env
|
|
manager.load_service_env("test")
|
|
assert os.environ["KEY1"] == "VAL1"
|
|
|
|
# Test get_status
|
|
status = manager.get_status("test", ["KEY1", "KEY3"])
|
|
assert status["configured"] is False
|
|
assert "KEY3" in status["missing_keys"]
|
|
|
|
status = manager.get_status("test", ["KEY1", "KEY2"])
|
|
assert status["configured"] is True
|
|
|
|
def test_settings_manager_basic(tmp_path):
|
|
data_dir = tmp_path / "data"
|
|
data_dir.mkdir()
|
|
|
|
manager = SettingsManager(str(data_dir))
|
|
|
|
# Test save_profile
|
|
profile_data = {"fitness_goals": "Lose weight", "focus_days": ["Monday"]}
|
|
manager.save_profile(profile_data)
|
|
|
|
profile_path = os.path.join(str(data_dir), "user_profile.json")
|
|
assert os.path.exists(profile_path)
|
|
|
|
with open(profile_path, "r") as f:
|
|
saved = json.load(f)
|
|
assert saved["fitness_goals"] == "Lose weight"
|
|
|
|
# Test get_profile (mapped to load_profile)
|
|
loaded = manager.load_profile()
|
|
assert loaded["fitness_goals"] == "Lose weight"
|
|
assert "dietary_preferences" in loaded # Default value
|
|
|
|
# Test get_context_string
|
|
ctx = manager.get_context_string()
|
|
assert "Lose weight" in ctx
|
|
assert "Dietary Preferences" in ctx
|
|
|
|
def test_settings_manager_error_handling(tmp_path):
|
|
data_dir = tmp_path / "data_error"
|
|
data_dir.mkdir()
|
|
profile_path = data_dir / "user_profile.json"
|
|
|
|
# Write corrupted JSON
|
|
with open(profile_path, "w") as f:
|
|
f.write("invalid json")
|
|
|
|
manager = SettingsManager(str(data_dir))
|
|
profile = manager.load_profile()
|
|
assert profile == {}
|