From 971ba30032c1d629b3f3cd3c7f7bba4f970632d5 Mon Sep 17 00:00:00 2001 From: qzl Date: Mon, 2 Mar 2026 16:05:30 +0800 Subject: [PATCH] test: add unit tests for load_user_agent_catalog function - test_user_agent_catalog_file_exists_and_has_required_fields: verifies catalog file exists with correct structure - test_load_user_agent_catalog_raises_on_invalid_structure: verifies invalid YAML is properly rejected --- .../tests/unit/core/test_agent_init_data.py | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/backend/tests/unit/core/test_agent_init_data.py b/backend/tests/unit/core/test_agent_init_data.py index cbd1359..dfb0bab 100644 --- a/backend/tests/unit/core/test_agent_init_data.py +++ b/backend/tests/unit/core/test_agent_init_data.py @@ -141,3 +141,48 @@ async def test_initialize_data_rolls_back_on_invalid_factory_mapping( Base.metadata.remove(users_table) await engine.dispose() + + +def test_user_agent_catalog_file_exists_and_has_required_fields() -> None: + catalog_path = ( + Path(__file__).resolve().parents[3] + / "src" + / "core" + / "config" + / "static" + / "database" + / "user_agent_catalog.yaml" + ) + + assert catalog_path.exists(), f"Catalog file not found: {catalog_path}" + + catalog = init_data.load_user_agent_catalog(catalog_path) + + assert "agents" in catalog + assert isinstance(catalog["agents"], list) + assert len(catalog["agents"]) == 3 + + for agent in catalog["agents"]: + assert "agent_type" in agent + assert "llm_model_code" in agent + assert "status" in agent + assert "config" in agent + assert isinstance(agent["config"], dict) + + +def test_load_user_agent_catalog_raises_on_invalid_structure( + tmp_path: Path, +) -> None: + catalog_path = tmp_path / "user_agent_catalog.yaml" + catalog_path.write_text( + """ +agents: + - agent_type: TEST + llm_model_code: test-model + status: ACTIVE +""".strip(), + encoding="utf-8", + ) + + with pytest.raises(ValueError, match="Invalid user agent catalog"): + init_data.load_user_agent_catalog(catalog_path)