from types import SimpleNamespace from app.gateway.services import ( apply_knowledge_space_strict_scope, enforce_knowledge_space_strict_run_config, knowledge_space_strict_excluded_tools, ) def test_strict_scope_preserves_existing_and_blocks_configured_web_tools() -> None: config = SimpleNamespace( tools=[ SimpleNamespace(name="company_search", group="web"), SimpleNamespace(name="calculator", group="utility"), ] ) excluded = knowledge_space_strict_excluded_tools(["custom_disabled"], app_config=config) assert "custom_disabled" in excluded assert "company_search" in excluded assert "web_search" in excluded assert "browser_fetch_page" in excluded assert "calculator" not in excluded assert "llmwiki_search" not in excluded def test_selected_knowledge_space_activates_fail_closed_external_retrieval_block() -> None: context = {"excluded_tools": ["custom_disabled"]} config = SimpleNamespace( tools=[ SimpleNamespace(name="company_search", group="web"), SimpleNamespace(name="calculator", group="utility"), ] ) activated = apply_knowledge_space_strict_scope( context, selection_requested=True, knowledge_base_ids=[], assistant_knowledge_base_ids=[], app_config=config, ) assert activated is True assert context["knowledge_space_strict"] is True assert "web_search" in context["excluded_tools"] assert "browser_fetch_page" in context["excluded_tools"] assert "company_search" in context["excluded_tools"] assert "llmwiki_search" not in context["excluded_tools"] assert "calculator" not in context["excluded_tools"] def test_selected_scope_overrides_forged_client_runtime_config() -> None: config = { "configurable": { "knowledge_space_strict": False, "excluded_tools": [], "llmwiki_knowledge_base_ids": ["forged"], "assistant_knowledge_base_ids": ["forged-assistant"], }, "context": { "knowledge_space_strict": False, "excluded_tools": ["client-disabled"], "llmwiki_knowledge_base_ids": ["forged"], "assistant_knowledge_base_ids": ["forged-assistant"], }, } enforce_knowledge_space_strict_run_config( config, knowledge_base_ids=["wiki-authorized"], assistant_knowledge_base_ids=["assistant-authorized"], excluded_tools=["server-disabled"], app_config=SimpleNamespace(tools=[]), ) for scope in ("configurable", "context"): assert config[scope]["knowledge_space_strict"] is True assert config[scope]["llmwiki_knowledge_base_ids"] == [ "wiki-authorized" ] assert config[scope]["assistant_knowledge_base_ids"] == [ "assistant-authorized" ] assert "web_search" in config[scope]["excluded_tools"] assert "server-disabled" in config[scope]["excluded_tools"]