test_message_gate.py
8.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
"""MessageGate 激活识别测试。"""
import pytest
from unittest.mock import MagicMock, AsyncMock
from app.agent_core.message_gate import MessageGate, GateDecision
def test_message_gate_can_be_constructed():
"""MessageGate 应能通过依赖注入的方式构造。"""
gate = MessageGate(
config_loader=MagicMock(),
session_service=MagicMock(),
post_reply_hook=MagicMock(),
agent_hub=MagicMock(),
main_agent_key="11",
model=MagicMock(),
)
assert gate is not None
def test_gate_decision_only_intent():
"""GateDecision 只有 intent 字段。"""
d = GateDecision(intent="chat")
assert d.intent == "chat"
assert not hasattr(d, "rewritten_query")
assert not hasattr(d, "reply")
@pytest.mark.asyncio
async def test_classify_returns_chat_for_empty_pending():
"""空 pending → 当前临时直接放行,返回 pass。"""
gate = MessageGate(
config_loader=MagicMock(),
session_service=MagicMock(),
post_reply_hook=MagicMock(),
agent_hub=MagicMock(),
main_agent_key="11",
model=MagicMock(),
)
decision = await gate._classify_and_rewrite(pending=[])
assert decision.intent == "pass"
@pytest.mark.asyncio
async def test_classify_activation_returns_chat_by_default():
"""直接放行模式:返回 pass(临时跳过 LLM 分类)。"""
gate = MessageGate(
config_loader=MagicMock(),
session_service=MagicMock(),
post_reply_hook=MagicMock(),
agent_hub=MagicMock(),
main_agent_key="11",
model=MagicMock(),
)
pending = [{"msg_type": "text", "msg_content": "谢谢大家"}]
decision = await gate._classify_and_rewrite(pending=pending)
assert decision.intent == "pass"
@pytest.mark.asyncio
async def test_handle_chat_adds_user_message_to_history():
"""_handle_chat 追加用户消息到 conversation_history。"""
from app.infrastructure.session_store import SessionStore, clear_default_store_for_tests
clear_default_store_for_tests()
store = SessionStore()
sid = "test-chat-session"
# SessionMemory.add() 要求 session 已存在
store.set(sid, {"conversation_history": [], "intent_session": ""})
mock_session_service = MagicMock()
mock_session_service.save_to_db = MagicMock()
gate = MessageGate(
config_loader=MagicMock(),
session_service=mock_session_service,
post_reply_hook=MagicMock(),
agent_hub=MagicMock(),
main_agent_key="11",
model=MagicMock(),
)
pending = [{"msg_type": "text", "msg_content": "好的"}]
await gate._handle_chat(session_id=sid, pending=pending)
data = store.get(sid)
history = data.get("conversation_history", [])
assert len(history) == 1
assert history[0]["role"] == "user"
assert history[0]["content"] == "好的"
mock_session_service.save_to_db.assert_called_once_with(sid)
@pytest.mark.asyncio
async def test_handle_actionable_calls_agent_hub():
"""_handle_actionable 用预提取的 query/file_url 调用 agent_hub.process_message。"""
mock_agent_hub = MagicMock()
mock_agent_hub.process_message = AsyncMock()
gate = MessageGate(
config_loader=MagicMock(),
session_service=MagicMock(),
post_reply_hook=MagicMock(),
agent_hub=mock_agent_hub,
main_agent_key="11",
model=MagicMock(),
)
await gate._handle_actionable(
session_id="test-session",
room_id="room-1",
user_id="user-1",
query="创建任务",
file_url=None,
bot_id_mapping={"11": "bot_main"},
)
mock_agent_hub.process_message.assert_called_once()
call_kwargs = mock_agent_hub.process_message.call_args.kwargs
assert call_kwargs["user_query"] == "创建任务"
assert call_kwargs["file_url"] is None
# ── _extract_query_and_file_url 单元测试 ──
def test_extract_query_and_file_url_text_only():
"""仅包含文本消息时,提取 query 且 file_url 为 None。"""
from app.agent_core.message_gate import MessageGate
pending = [{"msg_type": "text", "msg_content": "帮我整理纪要"}]
query, file_url = MessageGate._extract_query_and_file_url(pending)
assert query == "帮我整理纪要"
assert file_url is None
def test_extract_query_and_file_url_with_file():
"""混合文本+文件消息时,提取 query 和 file_url。"""
from app.agent_core.message_gate import MessageGate
pending = [
{"msg_type": "text", "msg_content": "分析这份报表"},
{"msg_type": "file", "msg_content": "https://oss.example.com/report.xlsx"},
]
query, file_url = MessageGate._extract_query_and_file_url(pending)
assert "分析这份报表" in query
assert file_url == "https://oss.example.com/report.xlsx"
def test_extract_query_and_file_url_append_url():
"""文本中已含 URL 时,不重复追加。"""
from app.agent_core.message_gate import MessageGate
pending = [
{"msg_type": "text", "msg_content": "看下这个 https://example.com/doc"},
]
query, file_url = MessageGate._extract_query_and_file_url(pending)
assert "https://example.com/doc" in query
assert query.count("https://example.com/doc") == 1
assert file_url is None
def test_extract_query_and_file_url_empty():
"""空 pending 返回空 query。"""
from app.agent_core.message_gate import MessageGate
query, file_url = MessageGate._extract_query_and_file_url([])
assert query == ""
assert file_url is None
@pytest.mark.asyncio
async def test_handle_busy_writes_to_intent_session():
"""_handle_busy 写入 intent_session 且不污染 conversation_history。"""
from app.infrastructure.session_store import SessionStore, clear_default_store_for_tests
clear_default_store_for_tests()
store = SessionStore()
sid = "test-busy-session"
store.set(sid, {"conversation_history": [], "intent_session": []})
mock_post_reply = MagicMock()
mock_post_reply.execute = AsyncMock()
gate = MessageGate(
config_loader=MagicMock(),
session_service=MagicMock(),
post_reply_hook=mock_post_reply,
agent_hub=MagicMock(),
main_agent_key="11",
model=MagicMock(),
)
pending = [{"msg_type": "text", "msg_content": "帮我查一下"}]
await gate._handle_busy(
session_id=sid,
room_id="room-1",
user_id="user-1",
pending=pending,
bot_id_mapping={"11": "bot_main"},
)
data = store.get(sid)
# 不污染 conversation_history
assert data["conversation_history"] == []
# 写入 intent_session
assert len(data["intent_session"]) == 1
assert data["intent_session"][0]["role"] == "user"
assert data["intent_session"][0]["content"] == "帮我查一下"
# 发送了 BUSY_REPLY
mock_post_reply.execute.assert_called_once()
call_context = mock_post_reply.execute.call_args[0][0]
assert "正在执行任务中" in call_context["response_content"]
@pytest.mark.asyncio
async def test_handle_busy_accumulates_in_intent_session():
"""多次 busy 调用应累积消息到 intent_session。"""
from app.infrastructure.session_store import SessionStore, clear_default_store_for_tests
clear_default_store_for_tests()
store = SessionStore()
sid = "test-busy-accum"
store.set(sid, {"conversation_history": [], "intent_session": []})
mock_post_reply = MagicMock()
mock_post_reply.execute = AsyncMock()
gate = MessageGate(
config_loader=MagicMock(),
session_service=MagicMock(),
post_reply_hook=mock_post_reply,
agent_hub=MagicMock(),
main_agent_key="11",
model=MagicMock(),
)
await gate._handle_busy(sid, "room", "u1",
[{"msg_type": "text", "msg_content": "第一条"}],
{"11": "bot"})
await gate._handle_busy(sid, "room", "u1",
[{"msg_type": "text", "msg_content": "第二条"}],
{"11": "bot"})
data = store.get(sid)
assert len(data["intent_session"]) == 2
assert data["intent_session"][0]["content"] == "第一条"
assert data["intent_session"][1]["content"] == "第二条"
assert data["conversation_history"] == []