""" backend/tests/test_agent_runtime_context_registry.py 用途:验证 Context Adapter Registry 注册、code 解析与 build_chat_dify_inputs 分发。 依赖:agent_runtime_context.bootstrap、registry、pytest。 逻辑:迭代 2 验收 — Registry 含三类 adapter;未知 code 回退 generic;不再依赖名称 if/elif。 """ from __future__ import annotations from types import SimpleNamespace import pytest from app.services.agent_registry.resolver import resolve_agent_code from app.services.agent_runtime_context import AGENT_RUNTIME_CONTEXT_INPUT from app.services.agent_runtime_context.bootstrap import load_context_adapters from app.services.agent_runtime_context.registry import ( CONTEXT_ADAPTER_REGISTRY, build_chat_dify_inputs, register_context_adapter, resolve_context_adapter_code, ) @pytest.fixture(autouse=True) def _load_context_adapters(): """每条用例前确保 adapter 已注册。""" load_context_adapters() def test_context_adapter_registry_contains_seed_adapters(): """启动加载后 Registry 须含 generic、shipping_notice、file_check。""" assert {'generic', 'shipping_notice', 'file_check'}.issubset(set(CONTEXT_ADAPTER_REGISTRY.keys())) def test_resolve_context_adapter_code_falls_back_to_generic(): """未知 agent_code 应回退 generic adapter。""" assert resolve_context_adapter_code('kb_faq_agent') == 'generic' assert resolve_context_adapter_code('') == 'generic' def test_build_chat_dify_inputs_routes_by_agent_code_not_agent_name(monkeypatch): """分发应基于 resolve_agent_code,而非 is_shipping_notice_chat_agent 名称判断。""" agent = SimpleNamespace( name='知识库问答 Agent', type='api', extra_config={'agent_code': 'shipping_notice'}, agent_code='shipping_notice', runtime_provider='hybrid', conversation_mode='task_chat', ) monkeypatch.setattr( 'app.services.shipping_notice.context_adapter.build_dify_chat_inputs', lambda conversation_id, **kwargs: {'adapter': 'shipping', 'conversation_id': conversation_id}, ) inputs = build_chat_dify_inputs(agent, session_id='conv-by-code-001') assert inputs == {'adapter': 'shipping', 'conversation_id': 'conv-by-code-001'} def test_build_chat_dify_inputs_routes_file_check_by_registered_agent_code(monkeypatch): """agent_code=file_check 列已注册时应走 file_check adapter。""" agent = SimpleNamespace( name='RoHS 检核', type='api', extra_config={}, agent_code='file_check', runtime_provider='dify', conversation_mode='task_chat', bound_schedule_task_id=7, ) assert resolve_agent_code(agent) == 'file_check' monkeypatch.setattr( 'app.services.file_check_tools.context_adapter.build_file_check_dify_inputs', lambda current_agent, required, **kwargs: { 'adapter': 'file_check', AGENT_RUNTIME_CONTEXT_INPUT: '{"refs":{"schedule_task_id":"7"}}', 'required': required, }, ) inputs = build_chat_dify_inputs(agent, session_id='conv-file-check-002') assert inputs['adapter'] == 'file_check' assert AGENT_RUNTIME_CONTEXT_INPUT in inputs def test_register_context_adapter_allows_custom_code(): """register_context_adapter 装饰器应写入 Registry。""" @register_context_adapter('custom_test_adapter') def _custom_adapter(**kwargs): return {'custom': True} assert 'custom_test_adapter' in CONTEXT_ADAPTER_REGISTRY assert CONTEXT_ADAPTER_REGISTRY['custom_test_adapter'](agent=None, session_id='x') == {'custom': True}