"""Tests for the wx route's shared Agent Gallery category selection.""" from __future__ import annotations import json from pathlib import Path import pytest from fastapi import HTTPException from app.gateway.routers import system_settings as router from deerflow.config import system_settings as ss @pytest.fixture def tmp_runtime_home(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Path: monkeypatch.setattr(ss, "runtime_home", lambda: tmp_path) return tmp_path def test_wx_common_agents_defaults_to_original_recommendations(tmp_runtime_home: Path) -> None: assert ss.load_system_settings().wx_common_agents.agent_type is None def test_wx_common_agents_roundtrip(tmp_runtime_home: Path) -> None: current = ss.load_system_settings() updated = current.wx_common_agents.model_copy( update={"agent_type": "square:default"}, ) ss.save_system_settings(current.model_copy(update={"wx_common_agents": updated})) saved = json.loads((tmp_runtime_home / "system_settings.json").read_text("utf-8")) assert saved["wx_common_agents"]["agent_type"] == "square:default" assert ss.load_system_settings().wx_common_agents.agent_type == "square:default" def test_wx_category_validation_uses_effective_publish_squares() -> None: squares = ss.PublishSquaresSettings() assert router._normalize_wx_common_agent_type(None, squares) is None assert router._normalize_wx_common_agent_type("", squares) is None assert router._normalize_wx_common_agent_type("mine", squares) == "mine" assert router._normalize_wx_common_agent_type("sentiment", squares) == "sentiment" assert router._normalize_wx_common_agent_type("square:default", squares) == "square:default" with pytest.raises(HTTPException) as error: router._normalize_wx_common_agent_type("square:missing", squares) assert error.value.status_code == 422 @pytest.mark.asyncio async def test_wx_can_update_common_agent_category( monkeypatch: pytest.MonkeyPatch, ) -> None: async def current_user(_request: object): return type("User", (), {"email": "wx@example.com", "system_role": "user"})() monkeypatch.setattr(router, "get_optional_user_from_request", current_user) await router._require_wx_common_agents_manager(object()) @pytest.mark.asyncio async def test_other_user_cannot_update_common_agent_category( monkeypatch: pytest.MonkeyPatch, ) -> None: async def current_user(_request: object): return type("User", (), {"email": "someone@example.com", "system_role": "user"})() monkeypatch.setattr(router, "get_optional_user_from_request", current_user) with pytest.raises(HTTPException) as error: await router._require_wx_common_agents_manager(object()) assert error.value.status_code == 403