from __future__ import annotations from nanobot.channels.registry import load_channel_plugin from nanobot.config.schema import Config from nanobot.webui.settings_system import ( coerce_channel_value, save_channel_config_values, system_settings_payload, update_agent_system_settings, ) def test_system_domain_owns_runtime_dto_and_agent_updates(tmp_path) -> None: config = Config() changed, restart_required = update_agent_system_settings( config, { "timezone": ["Asia/Shanghai"], "tool_hint_max_length": ["120"], }, ) payload = system_settings_payload( config, config_path=tmp_path / "config.json", version="0.3.0", ) assert changed is True assert restart_required is True assert config.agents.defaults.timezone == "Asia/Shanghai" assert config.agents.defaults.timezone_mode == "manual" assert config.agents.defaults.tool_hint_max_length == 120 assert payload["runtime"]["config_path"] == str(tmp_path / "config.json") assert payload["version"] == {"current": "0.3.0"} assert payload["docs"]["version"] == "0.3.0" assert set(payload) == {"runtime", "runtime_config", "usage", "advanced", "version", "docs"} def test_system_domain_validates_channel_field_values() -> None: assert coerce_channel_value("allow_from", "alice, bob", "list") == [ "alice", "bob", ] assert coerce_channel_value("enabled", "yes", "bool") is True assert coerce_channel_value("port", "8765", "int") == 8765 assert coerce_channel_value("delay", "0.6", "float") == 0.6 assert coerce_channel_value("overrides", '{"123": "open"}', "json") == { "123": "open" } assert coerce_channel_value("token", None, "secret") == "" def test_channel_secret_is_cleared_only_by_explicit_null() -> None: config = Config.model_validate({ "channels": {"matrix": {"password": "saved-password"}}, }) saved = save_channel_config_values( config, "matrix", {"channels.matrix.password": None}, load_channel_plugin=load_channel_plugin, ) assert saved == ["channels.matrix.password"] assert config.channels.matrix["password"] == ""