"""Tests for the admin-configured「按类型聚合」module → agent-tag config. Backs the zzJC / zzXD module left sidebar (需求 0616 #3/#4): an admin picks which agent tags belong to the module, persisted in ``system_settings.json``. """ from __future__ import annotations import json from pathlib import Path import pytest from deerflow.config import system_settings as ss @pytest.fixture def tmp_runtime_home(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Path: """Redirect ``runtime_home()`` to a per-test tmp dir.""" def fake_home() -> Path: return tmp_path monkeypatch.setattr(ss, "runtime_home", fake_home) return tmp_path def test_load_returns_empty_modules_when_missing(tmp_runtime_home: Path) -> None: settings = ss.load_system_settings() assert settings.agent_modules.modules == {} assert ss.get_module_tag_ids(settings.agent_modules, "zzjc") == [] def test_set_module_tag_ids_dedupes_and_strips() -> None: base = ss.AgentModuleSettings() updated = ss.set_module_tag_ids(base, "zzjc", [" t1 ", "t2", "t1", "", " "]) assert updated.modules["zzjc"] == ["t1", "t2"] # Original is untouched (returns a copy). assert base.modules == {} def test_set_module_tag_ids_keeps_other_modules() -> None: base = ss.set_module_tag_ids(ss.AgentModuleSettings(), "zzjc", ["t1"]) both = ss.set_module_tag_ids(base, "zzxd", ["t2", "t3"]) assert ss.get_module_tag_ids(both, "zzjc") == ["t1"] assert ss.get_module_tag_ids(both, "zzxd") == ["t2", "t3"] def test_save_then_load_roundtrips(tmp_runtime_home: Path) -> None: current = ss.load_system_settings() updated = ss.set_module_tag_ids(current.agent_modules, "zzjc", ["tag-a", "tag-b"]) ss.save_system_settings(current.model_copy(update={"agent_modules": updated})) file_path = tmp_runtime_home / "system_settings.json" assert file_path.is_file() saved = json.loads(file_path.read_text(encoding="utf-8")) assert saved["agent_modules"]["modules"]["zzjc"] == ["tag-a", "tag-b"] reloaded = ss.load_system_settings() assert ss.get_module_tag_ids(reloaded.agent_modules, "zzjc") == ["tag-a", "tag-b"]