Files
projectAIpopular/tests/test_settings.py
T

51 lines
1.6 KiB
Python

"""SettingsStore 单测(封闭:临时路径)。"""
import json
from gateway.settings import DEFAULTS, SettingsStore, _coerce
def test_defaults(tmp_path):
s = SettingsStore(path=tmp_path / "s.json")
d = s.to_dict()
assert d["worker"]["backend"] == "llama_server"
assert "pipeline" in d and "architect" in d
def test_update_and_coerce(tmp_path):
s = SettingsStore(path=tmp_path / "s.json")
s.update({"worker": {"backend": "openai", "temperature": "0.4", "max_fix_attempts": "3"}})
d = s.to_dict()
assert d["worker"]["backend"] == "openai"
assert d["worker"]["temperature"] == 0.4 # 字符串转 float
assert d["worker"]["max_fix_attempts"] == 3 # 字符串转 int
def test_ignores_unknown_keys(tmp_path):
s = SettingsStore(path=tmp_path / "s.json")
s.update({"worker": {"not_a_key": 1}, "unknown_section": {"x": 1}})
d = s.to_dict()
assert "not_a_key" not in d["worker"]
assert "unknown_section" not in d
def test_persistence_roundtrip(tmp_path):
p = tmp_path / "s.json"
s = SettingsStore(path=p)
s.update({"worker": {"temperature": 0.5}})
s2 = SettingsStore(path=p) # 重新加载
assert s2.to_dict()["worker"]["temperature"] == 0.5
def test_reset(tmp_path):
s = SettingsStore(path=tmp_path / "s.json")
s.update({"worker": {"backend": "mock"}})
d = s.reset()
assert d["worker"]["backend"] == "llama_server"
def test_coerce():
assert _coerce("true", True) is True
assert _coerce("42", 0) == 42
assert _coerce("bad", 0) == 0
assert _coerce(1.5, 0.0) == 1.5