-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_mention_bug.py
More file actions
292 lines (235 loc) · 9.02 KB
/
test_mention_bug.py
File metadata and controls
292 lines (235 loc) · 9.02 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/env python3
"""
Test the specific bug described in issue #26:
Bot still answers to replies replying to a message with a mention,
even when the reply itself doesn't have a mention.
"""
import os
import sys
import asyncio
from unittest.mock import MagicMock, AsyncMock
# Ensure the src directory is in Python path
current_dir = os.path.dirname(os.path.abspath(__file__))
src_dir = os.path.join(current_dir, 'src')
sys.path.insert(0, src_dir)
# Mock the nio module
class MockRoomMessageText:
def __init__(self, event_id, sender, body, source=None, server_timestamp=None):
self.event_id = event_id
self.sender = sender
self.body = body
self.source = source or {'content': {}}
self.server_timestamp = server_timestamp
class MockMatrixRoom:
def __init__(self, room_id):
self.room_id = room_id
class MockRoomGetEventResponse:
def __init__(self, event):
self.event = event
class MockAsyncClient:
def __init__(self):
self.user_id = "@testbot:matrix.org"
self.access_token = "fake_token"
self.device_id = "fake_device"
self.room_get_event = AsyncMock()
self.room_send = AsyncMock()
self.room_typing = AsyncMock()
# Mock the nio module
sys.modules['nio'] = MagicMock()
import nio
nio.AsyncClient = MockAsyncClient
nio.ClientConfig = MagicMock()
nio.Event = MagicMock()
nio.LoginResponse = MagicMock()
nio.MatrixRoom = MockMatrixRoom
nio.RoomMessageText = MockRoomMessageText
nio.SyncResponse = MagicMock()
nio.RoomGetEventResponse = MockRoomGetEventResponse
async def test_mention_bug_scenario():
"""
Test the specific bug scenario:
1. User A sends: "@askaosus how do I install Ubuntu?"
2. User B replies to User A: "I also need help with this"
3. Bot should NOT respond to User B's message in 'mention' mode
"""
print("=== Testing Mention Bug Scenario ===")
# Set up environment for mention mode
os.environ["MATRIX_HOMESERVER_URL"] = "https://matrix.test.org"
os.environ["MATRIX_USER_ID"] = "@testbot:matrix.test.org"
os.environ["MATRIX_PASSWORD"] = "test_password"
os.environ["LLM_API_KEY"] = "test_key"
os.environ["BOT_REPLY_BEHAVIOR"] = "mention"
from config import Config
from bot import AskaosusBot
config = Config()
bot = AskaosusBot(config)
bot.start_time = 0 # Process all messages
room = MockMatrixRoom("!test:matrix.org")
# Step 1: Original message from User A with bot mention
original_message_id = "$original_with_mention"
original_message = MockRoomMessageText(
event_id=original_message_id,
sender="@userA:matrix.org",
body="@askaosus how do I install Ubuntu?", # This has the mention
server_timestamp=1000
)
# Step 2: User B replies to User A's message WITHOUT mentioning the bot
# In Matrix, the reply body might include quoted content like:
# "> @askaosus how do I install Ubuntu?"
# "I also need help with this"
# This simulates how some Matrix clients format reply messages
reply_message_body = "> @askaosus how do I install Ubuntu?\n\nI also need help with this"
reply_message = MockRoomMessageText(
event_id="$reply_without_mention",
sender="@userB:matrix.org",
body=reply_message_body, # Contains quoted mention but no direct mention
source={
'content': {
'm.relates_to': {
'm.in_reply_to': {
'event_id': original_message_id
}
}
}
},
server_timestamp=2000
)
# Mock the original message fetch
bot.matrix_client.room_get_event.return_value = MockRoomGetEventResponse(original_message)
# Test: Bot should NOT respond because the reply itself doesn't mention the bot
result = await bot._should_respond(room, reply_message)
question, should_respond, reply_to_event_id = result
print(f"Original message: {original_message.body}")
print(f"Reply message: {reply_message.body}")
print(f"Bot should respond: {should_respond}")
if should_respond:
print("❌ BUG DETECTED: Bot is responding to reply without direct mention")
print(f" Question extracted: {question}")
return False
else:
print("✅ CORRECT: Bot is NOT responding to reply without direct mention")
return True
async def test_mention_bug_with_formatted_body():
"""
Test the same scenario but with properly formatted Matrix message
that separates the quoted content from the actual reply.
"""
print("\n=== Testing With Proper Matrix Formatted Body ===")
# Set up environment for mention mode
os.environ["BOT_REPLY_BEHAVIOR"] = "mention"
from config import Config
from bot import AskaosusBot
config = Config()
bot = AskaosusBot(config)
bot.start_time = 0
room = MockMatrixRoom("!test:matrix.org")
# Original message with mention
original_message_id = "$original_with_mention_2"
original_message = MockRoomMessageText(
event_id=original_message_id,
sender="@userA:matrix.org",
body="@askaosus how do I install Ubuntu?",
server_timestamp=1000
)
# Reply that has a clean body without quoted content
# This is what Matrix should provide in the formatted_body or clean body
reply_message = MockRoomMessageText(
event_id="$clean_reply",
sender="@userB:matrix.org",
body="I also need help with this", # Clean body without quoted mention
source={
'content': {
'm.relates_to': {
'm.in_reply_to': {
'event_id': original_message_id
}
}
}
},
server_timestamp=2000
)
# Mock the original message fetch
bot.matrix_client.room_get_event.return_value = MockRoomGetEventResponse(original_message)
# Test: Bot should NOT respond
result = await bot._should_respond(room, reply_message)
question, should_respond, reply_to_event_id = result
print(f"Original message: {original_message.body}")
print(f"Reply message (clean): {reply_message.body}")
print(f"Bot should respond: {should_respond}")
if should_respond:
print("❌ BUG: Bot is responding when it shouldn't")
return False
else:
print("✅ CORRECT: Bot correctly ignores clean reply without mention")
return True
async def test_mention_bug_with_direct_mention():
"""
Test that the bot DOES respond when the reply itself has a mention.
"""
print("\n=== Testing Reply With Direct Mention ===")
os.environ["BOT_REPLY_BEHAVIOR"] = "mention"
from config import Config
from bot import AskaosusBot
config = Config()
bot = AskaosusBot(config)
bot.start_time = 0
room = MockMatrixRoom("!test:matrix.org")
# Original message
original_message_id = "$original_3"
original_message = MockRoomMessageText(
event_id=original_message_id,
sender="@userA:matrix.org",
body="How do I install Ubuntu?", # No mention in original
server_timestamp=1000
)
# Reply with direct mention
reply_message = MockRoomMessageText(
event_id="$reply_with_mention",
sender="@userB:matrix.org",
body="@askaosus can you help with this?", # Direct mention
source={
'content': {
'm.relates_to': {
'm.in_reply_to': {
'event_id': original_message_id
}
}
}
},
server_timestamp=2000
)
# Mock the original message fetch
bot.matrix_client.room_get_event.return_value = MockRoomGetEventResponse(original_message)
# Test: Bot SHOULD respond
result = await bot._should_respond(room, reply_message)
question, should_respond, reply_to_event_id = result
print(f"Original message: {original_message.body}")
print(f"Reply message: {reply_message.body}")
print(f"Bot should respond: {should_respond}")
if should_respond:
print("✅ CORRECT: Bot responds to reply with direct mention")
return True
else:
print("❌ ERROR: Bot should respond to direct mention")
return False
async def main():
"""Run all bug reproduction tests."""
print("Testing mention bug scenarios...\n")
success = True
# Test the main bug scenario
if not await test_mention_bug_scenario():
success = False
# Test with clean formatted body
if not await test_mention_bug_with_formatted_body():
success = False
# Test that direct mentions still work
if not await test_mention_bug_with_direct_mention():
success = False
if success:
print("\n🎉 All tests passed!")
return 0
else:
print("\n❌ Some tests failed!")
return 1
if __name__ == "__main__":
sys.exit(asyncio.run(main()))