1+ import backend .services .agent_service as agent_service
2+ from backend .services .agent_service import update_agent_info_impl
3+ from backend .services .agent_service import get_creating_sub_agent_info_impl
4+ from backend .services .agent_service import list_all_agent_info_impl
5+ from backend .services .agent_service import get_agent_info_impl
6+ from backend .services .agent_service import get_creating_sub_agent_id_service
7+ from backend .services .agent_service import get_enable_tool_id_by_agent_id
8+ from backend .services .agent_service import (
9+ get_agent_call_relationship_impl ,
10+ delete_agent_impl ,
11+ export_agent_impl ,
12+ export_agent_by_agent_id ,
13+ import_agent_by_agent_id ,
14+ insert_related_agent_impl ,
15+ load_default_agents_json_file ,
16+ clear_agent_memory ,
17+ import_agent_impl ,
18+ get_agent_id_by_name ,
19+ save_messages ,
20+ prepare_agent_run ,
21+ run_agent_stream ,
22+ stop_agent_tasks ,
23+ )
124from fastapi import Request
225from consts .model import ExportAndImportAgentInfo , ExportAndImportDataFormat , MCPInfo , AgentRequest
326import sys
3760sys .modules ['nexent.memory' ] = MagicMock ()
3861sys .modules ['nexent.memory.memory_service' ] = MagicMock ()
3962
63+ # Mock monitoring modules
64+ monitoring_manager_mock = MagicMock ()
65+
66+ # Define a decorator that simply returns the original function unchanged
67+
68+
69+ def pass_through_decorator (* args , ** kwargs ):
70+ def decorator (func ):
71+ return func
72+ return decorator
73+
74+
75+ monitoring_manager_mock .monitor_endpoint = pass_through_decorator
76+ monitoring_manager_mock .monitor_llm_call = pass_through_decorator
77+ monitoring_manager_mock .setup_fastapi_app = MagicMock (return_value = True )
78+ monitoring_manager_mock .configure = MagicMock ()
79+ monitoring_manager_mock .add_span_event = MagicMock ()
80+ monitoring_manager_mock .set_span_attributes = MagicMock ()
81+
82+ # Mock nexent.monitor modules
83+ sys .modules ['nexent.monitor' ] = MagicMock ()
84+ sys .modules ['nexent.monitor.monitoring' ] = MagicMock ()
85+ sys .modules ['nexent.monitor' ].get_monitoring_manager = lambda : monitoring_manager_mock
86+ sys .modules ['nexent.monitor' ].monitoring_manager = monitoring_manager_mock
87+
4088# Mock other dependencies
4189sys .modules ['agents' ] = MagicMock ()
4290sys .modules ['agents.create_agent_info' ] = MagicMock ()
53101sys .modules ['utils.auth_utils' ] = MagicMock ()
54102sys .modules ['utils.memory_utils' ] = MagicMock ()
55103sys .modules ['utils.thread_utils' ] = MagicMock ()
104+ # Mock utils.monitoring to return our monitoring_manager_mock
105+ utils_monitoring_mock = MagicMock ()
106+ utils_monitoring_mock .monitoring_manager = monitoring_manager_mock
107+ utils_monitoring_mock .setup_fastapi_app = MagicMock (return_value = True )
108+ sys .modules ['utils.monitoring' ] = utils_monitoring_mock
56109sys .modules ['agents.agent_run_manager' ] = MagicMock ()
57110sys .modules ['agents.preprocess_manager' ] = MagicMock ()
58111sys .modules ['nexent.core.agents.run_agent' ] = MagicMock ()
59112
60- from backend .services .agent_service import (
61- get_agent_call_relationship_impl ,
62- delete_agent_impl ,
63- export_agent_impl ,
64- export_agent_by_agent_id ,
65- import_agent_by_agent_id ,
66- insert_related_agent_impl ,
67- load_default_agents_json_file ,
68- clear_agent_memory ,
69- import_agent_impl ,
70- get_agent_id_by_name ,
71- save_messages ,
72- prepare_agent_run ,
73- run_agent_stream ,
74- stop_agent_tasks ,
75- )
76- from backend .services .agent_service import get_enable_tool_id_by_agent_id
77- from backend .services .agent_service import get_creating_sub_agent_id_service
78- from backend .services .agent_service import get_agent_info_impl
79- from backend .services .agent_service import list_all_agent_info_impl
80- from backend .services .agent_service import get_creating_sub_agent_info_impl
81- from backend .services .agent_service import update_agent_info_impl
82113
83114original_agent_model = sys .modules ['nexent.core.agents.agent_model' ]
84115sys .modules ['nexent.core.agents.agent_model' ] = MagicMock ()
@@ -1616,7 +1647,6 @@ async def test_import_agent_impl_mcp_exists_different_url(mock_get_current_user_
16161647 mock_import_agent .assert_called_once ()
16171648
16181649
1619-
16201650@patch ('backend.services.agent_service.add_remote_mcp_server_list' , new_callable = AsyncMock )
16211651@patch ('backend.services.agent_service.get_mcp_server_by_name_and_tenant' )
16221652@patch ('backend.services.agent_service.check_mcp_name_exists' )
@@ -1938,7 +1968,8 @@ def test_stop_agent_tasks(mock_preprocess_manager, mock_agent_run_manager):
19381968 assert result ["status" ] == "success"
19391969 assert "successfully stopped agent run and preprocess tasks" in result ["message" ]
19401970
1941- mock_agent_run_manager .stop_agent_run .assert_called_once_with (123 , "test_user" )
1971+ mock_agent_run_manager .stop_agent_run .assert_called_once_with (
1972+ 123 , "test_user" )
19421973
19431974 # Test only agent stopped
19441975 mock_agent_run_manager .stop_agent_run .return_value = True
@@ -2305,8 +2336,6 @@ def test_get_agent_call_relationship_impl_tool_name_fallback(mock_query_sub_agen
23052336# Additional tests for newer logic in agent_service.py
23062337#############################
23072338
2308- import backend .services .agent_service as agent_service
2309-
23102339
23112340@pytest .mark .asyncio
23122341async def test__stream_agent_chunks_persists_and_unregisters (monkeypatch ):
@@ -2326,7 +2355,8 @@ async def fake_agent_run(*_, **__):
23262355 yield "chunk1"
23272356 yield "chunk2"
23282357
2329- monkeypatch .setitem (sys .modules , "nexent.core.agents.run_agent" , MagicMock ())
2358+ monkeypatch .setitem (
2359+ sys .modules , "nexent.core.agents.run_agent" , MagicMock ())
23302360 monkeypatch .setattr (
23312361 "backend.services.agent_service.agent_run" , fake_agent_run , raising = False
23322362 )
@@ -2674,6 +2704,7 @@ async def test_generate_stream_with_memory_unexpected_exception_emits_error(monk
26742704 assert out and out [0 ].startswith (
26752705 "data: {" ) and "\" type\" : \" error\" " in out [0 ]
26762706
2707+
26772708async def test_generate_stream_no_memory_registers_and_streams (monkeypatch ):
26782709 """generate_stream_no_memory should prepare run info, register it and stream data without memory tokens."""
26792710 # Prepare AgentRequest & Request
@@ -2698,7 +2729,7 @@ async def test_generate_stream_no_memory_registers_and_streams(monkeypatch):
26982729 AsyncMock (return_value = MagicMock ()),
26992730 raising = False ,
27002731 )
2701-
2732+
27022733 registered = {}
27032734
27042735 def fake_register (conv_id , run_info , user_id ):
@@ -2819,7 +2850,8 @@ async def test_generate_stream_with_memory_emits_tokens_and_unregisters(monkeypa
28192850 # Enable memory switch in preview
28202851 monkeypatch .setattr (
28212852 "backend.services.agent_service.build_memory_context" ,
2822- MagicMock (return_value = MagicMock (user_config = MagicMock (memory_switch = True ))),
2853+ MagicMock (return_value = MagicMock (
2854+ user_config = MagicMock (memory_switch = True ))),
28232855 raising = False ,
28242856 )
28252857
@@ -2893,7 +2925,8 @@ async def test_generate_stream_with_memory_fallback_on_failure(monkeypatch):
28932925 # Enable memory
28942926 monkeypatch .setattr (
28952927 "backend.services.agent_service.build_memory_context" ,
2896- MagicMock (return_value = MagicMock (user_config = MagicMock (memory_switch = True ))),
2928+ MagicMock (return_value = MagicMock (
2929+ user_config = MagicMock (memory_switch = True ))),
28972930 raising = False ,
28982931 )
28992932
@@ -2944,7 +2977,7 @@ def fake_unregister(task_id):
29442977async def test_list_all_agent_info_impl_with_disabled_agents ():
29452978 """
29462979 Test list_all_agent_info_impl with disabled agents.
2947-
2980+
29482981 This test verifies that:
29492982 1. Agents with enabled=False are skipped and not included in the result
29502983 2. Only enabled agents are processed and returned
@@ -2997,7 +3030,7 @@ async def test_list_all_agent_info_impl_with_disabled_agents():
29973030 assert result [0 ]["name" ] == "Enabled Agent 1"
29983031 assert result [0 ]["display_name" ] == "Display Enabled Agent 1"
29993032 assert result [0 ]["is_available" ] == True
3000-
3033+
30013034 assert result [1 ]["agent_id" ] == 3
30023035 assert result [1 ]["name" ] == "Enabled Agent 2"
30033036 assert result [1 ]["display_name" ] == "Display Enabled Agent 2"
@@ -3019,7 +3052,7 @@ async def test_list_all_agent_info_impl_with_disabled_agents():
30193052async def test_list_all_agent_info_impl_all_disabled_agents ():
30203053 """
30213054 Test list_all_agent_info_impl with all agents disabled.
3022-
3055+
30233056 This test verifies that:
30243057 1. When all agents are disabled, an empty list is returned
30253058 2. No tool queries are made since no agents are processed
@@ -3059,4 +3092,4 @@ async def test_list_all_agent_info_impl_all_disabled_agents():
30593092 mock_query_agents .assert_called_once_with (tenant_id = "test_tenant" )
30603093 # No tool queries should be made since no agents are enabled
30613094 mock_search_tools .assert_not_called ()
3062- mock_check_tools .assert_not_called ()
3095+ mock_check_tools .assert_not_called ()
0 commit comments