-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_bot.py
More file actions
802 lines (734 loc) · 26.7 KB
/
Copy pathtest_bot.py
File metadata and controls
802 lines (734 loc) · 26.7 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
"""Tests for Bot class with example messages from Symphony, Slack, and Discord."""
from csp_bot import Bot, Message
from csp_bot.structs import BotCommand, CommandVariant, User
# ============================================================================
# Example raw messages from each backend
# ============================================================================
class TestMessageParsing:
"""Tests for Bot.parse_msg with different backend message formats."""
def test_parse_symphony_message_with_entities(self, bot: Bot):
"""Test parsing Symphony message with entity mentions."""
# Symphony messages use HTML with span entities
msg = Message(
user="John Doe",
user_id="123456789",
msg='<div data-format="PresentationML" data-version="2.0"><span class="entity" data-entity-id="0">@CSPBot</span> /help <span class="entity" data-entity-id="1">@Alice</span></div>',
channel="Test Room",
tags=["123456789", "987654321"],
backend="symphony",
)
text, entities = Bot.parse_msg(msg)
assert "/help" in text
assert "@CSPBot" in text or "@Alice" in text
assert len(entities) == 2
def test_parse_symphony_plain_message(self, bot: Bot):
"""Test parsing Symphony plain text message."""
msg = Message(
user="John Doe",
user_id="123456789",
msg='<div data-format="PresentationML" data-version="2.0">Hello world</div>',
channel="Test Room",
tags=[],
backend="symphony",
)
text, entities = Bot.parse_msg(msg)
assert "Hello world" in text
assert len(entities) == 0
def test_parse_symphony_reply_message(self, bot: Bot):
"""Test parsing Symphony message that's a reply (has separator)."""
msg = Message(
user="John Doe",
user_id="123456789",
msg='<div>Original message_———————————<span class="entity" data-entity-id="0">@CSPBot</span> /help</div>',
channel="Test Room",
tags=["123456789"],
backend="symphony",
)
text, entities = Bot.parse_msg(msg)
# The reply separator should be handled
assert "@CSPBot" in text or "/help" in text
def test_parse_slack_message_with_mentions(self, bot: Bot):
"""Test parsing Slack message with user mentions."""
# Slack uses <@USERID> format for mentions
msg = Message(
user="alice",
user_id="U123456789",
msg="<@U000BOT> /help <@U987654321>",
channel="general",
tags=["U000BOT", "U987654321"],
backend="slack",
)
text, entities = Bot.parse_msg(msg)
assert text == "<@U000BOT> /help <@U987654321>"
assert len(entities) == 2
assert "@U000BOT" in entities
assert "@U987654321" in entities
def test_parse_slack_plain_message(self, bot: Bot):
"""Test parsing Slack plain text message."""
msg = Message(
user="alice",
user_id="U123456789",
msg="Hello world without mentions",
channel="general",
tags=[],
backend="slack",
)
text, entities = Bot.parse_msg(msg)
assert text == "Hello world without mentions"
assert len(entities) == 0
def test_parse_discord_message_with_mentions(self, bot: Bot):
"""Test parsing Discord message with user mentions."""
# Discord also uses <@USERID> format
msg = Message(
user="bob",
user_id="123456789012345678",
msg="<@000000000000000001> /echo <@999999999999999999>",
channel="general",
tags=["000000000000000001", "999999999999999999"],
backend="discord",
)
text, entities = Bot.parse_msg(msg)
assert text == "<@000000000000000001> /echo <@999999999999999999>"
assert len(entities) == 2
assert "@000000000000000001" in entities
assert "@999999999999999999" in entities
def test_parse_discord_plain_message(self, bot: Bot):
"""Test parsing Discord plain text message."""
msg = Message(
user="bob",
user_id="123456789012345678",
msg="Just a regular message",
channel="general",
tags=[],
backend="discord",
)
text, entities = Bot.parse_msg(msg)
assert text == "Just a regular message"
assert len(entities) == 0
class TestIsMsgToBot:
"""Tests for Bot.is_msg_to_bot detection logic."""
def test_slack_message_with_bot_tag(self, bot: Bot):
"""Test detecting bot mention in Slack message."""
# Bot tag is "UBOT123" as mocked in conftest
msg = Message(
user="alice",
user_id="U123456789",
msg="<@UBOT123> /help",
channel="general",
tags=["UBOT123"],
backend="slack",
)
is_to_bot, channel, text, entities = bot.is_msg_to_bot(msg)
assert is_to_bot is True
assert channel == msg.channel
def test_slack_direct_message(self, bot: Bot):
"""Test DM detection for Slack."""
msg = Message(
user="alice",
user_id="U123456789",
msg="/help",
channel="IM",
tags=[],
backend="slack",
)
is_to_bot, channel, text, entities = bot.is_msg_to_bot(msg)
# DMs to bot should be detected
assert is_to_bot is True
assert channel == msg.user
def test_discord_message_with_bot_tag(self, bot: Bot):
"""Test detecting bot mention in Discord message."""
# Bot tag is "test_bot" for Discord
msg = Message(
user="bob",
user_id="123456789012345678",
msg="<@test_bot> /echo hello",
channel="general",
tags=["test_bot"],
backend="discord",
)
is_to_bot, channel, text, entities = bot.is_msg_to_bot(msg)
assert is_to_bot is True
def test_discord_direct_message(self, bot: Bot):
"""Test DM detection for Discord."""
msg = Message(
user="bob",
user_id="123456789012345678",
msg="/help",
channel="IM",
tags=[],
backend="discord",
)
is_to_bot, channel, text, entities = bot.is_msg_to_bot(msg)
assert is_to_bot is True
assert channel == msg.user
def test_message_not_to_bot_slack(self, bot: Bot):
"""Test message not directed to bot in Slack."""
msg = Message(
user="alice",
user_id="U123456789",
msg="Just a regular chat message",
channel="general",
tags=[],
backend="slack",
)
is_to_bot, channel, text, entities = bot.is_msg_to_bot(msg)
assert is_to_bot is False
def test_message_not_to_bot_discord(self, bot: Bot):
"""Test message not directed to bot in Discord."""
msg = Message(
user="bob",
user_id="123456789012345678",
msg="Just a regular chat message",
channel="general",
tags=[],
backend="discord",
)
is_to_bot, channel, text, entities = bot.is_msg_to_bot(msg)
assert is_to_bot is False
class TestBot:
"""Tests for Bot command extraction and execution."""
def test_extract_bot_commands(self, bot: Bot):
"""Test extracting commands from message."""
# This is a placeholder - proper test requires command registration
...
def test_extract_bot_commands_ignore_message(self, bot: Bot):
"""Test that non-command messages are ignored."""
commands = bot.extract_bot_commands(
message=Message(
user="user",
msg="ignore",
channel="test_channel",
tags=[],
backend="slack",
),
channel="test_channel",
text="ignore",
entities=[],
)
assert commands is None
def test_extract_bot_commands_bad_message(self, bot: Bot):
"""Test handling of malformed messages."""
commands = bot.extract_bot_commands(
message=Message(
user="user",
msg="\U00001010",
channel="test_channel",
tags=[],
backend="slack",
),
channel="test_channel",
text="\U00001010",
entities=[],
)
assert commands is None
def test_extract_bot_commands_with_slash_command(self, bot: Bot):
"""Test extracting slash command from Slack message."""
commands = bot.extract_bot_commands(
message=Message(
user="alice",
user_id="U123456789",
msg="/help",
channel="general",
tags=[],
backend="slack",
),
channel="general",
text="/help",
entities=[],
)
# help is a registered command
assert commands is not None
def test_extract_bot_commands_with_bot_prefix(self, bot: Bot):
"""Test command with @bot prefix."""
commands = bot.extract_bot_commands(
message=Message(
user="alice",
user_id="U123456789",
msg="@test_bot /help",
channel="general",
tags=["test_bot"],
backend="slack",
),
channel="general",
text="@test_bot /help",
entities=[],
)
# Should recognize the help command
assert commands is not None
def test_extract_bot_commands_unregistered_command(self, bot: Bot):
"""Test that unregistered commands return None."""
commands = bot.extract_bot_commands(
message=Message(
user="alice",
user_id="U123456789",
msg="/nonexistent_command",
channel="general",
tags=[],
backend="slack",
),
channel="general",
text="/nonexistent_command",
entities=[],
)
# Unregistered commands should return None
assert commands is None
def test_run_bot_command(self, bot: Bot):
"""Test running a bot command."""
...
class TestBotCommandsFromCommandString:
"""Tests for Bot.bot_commands_from_command_string parsing."""
def test_simple_command(self, bot: Bot):
"""Test parsing a simple command without arguments."""
msg = Message(
user="alice",
user_id="U123",
msg="/help",
channel="general",
tags=[],
backend="slack",
)
result = bot.bot_commands_from_command_string(
tokens=["/help"],
message=msg,
channel="general",
)
assert result is not None
command, args, target_channel, target_tags = result
assert command == "help"
assert args == []
assert target_channel == "general"
assert target_tags == []
def test_command_with_arguments(self, bot: Bot):
"""Test parsing command with arguments."""
msg = Message(
user="alice",
user_id="U123",
msg="/echo hello world",
channel="general",
tags=[],
backend="slack",
)
result = bot.bot_commands_from_command_string(
tokens=["/echo", "hello", "world"],
message=msg,
channel="general",
)
assert result is not None
command, args, target_channel, target_tags = result
assert command == "echo"
assert args == ["hello", "world"]
assert target_channel == "general"
def test_command_with_channel_override(self, bot: Bot):
"""Test parsing command with /channel directive."""
msg = Message(
user="alice",
user_id="U123",
msg="/echo /channel other-room hello",
channel="general",
tags=[],
backend="slack",
)
result = bot.bot_commands_from_command_string(
tokens=["/echo", "/channel", "other-room", "hello"],
message=msg,
channel="general",
)
assert result is not None
command, args, target_channel, target_tags = result
assert command == "echo"
assert target_channel == "other-room"
assert args == ["hello"]
def test_command_with_room_override(self, bot: Bot):
"""Test parsing command with /room directive (deprecated)."""
msg = Message(
user="alice",
user_id="U123",
msg="/echo /room other-room hello",
channel="general",
tags=[],
backend="symphony",
)
result = bot.bot_commands_from_command_string(
tokens=["/echo", "/room", "other-room", "hello"],
message=msg,
channel="general",
)
assert result is not None
command, args, target_channel, target_tags = result
assert target_channel == "other-room"
def test_command_with_entity_slack(self, bot: Bot):
"""Test parsing command with entity mentions for Slack."""
msg = Message(
user="alice",
user_id="U123",
msg="/echo <@U456>",
channel="general",
tags=["Bob Smith"], # Slack tags contain display names, not IDs
backend="slack",
)
# For Slack: entity_text is "@USER_ID", tag_value is display name
entity_map = {"ENTITY_0": ("@U456", "Bob Smith")}
result = bot.bot_commands_from_command_string(
tokens=["/echo", "ENTITY_0"],
message=msg,
channel="general",
entity_map=entity_map,
)
assert result is not None
command, args, target_channel, target_tags = result
assert command == "echo"
assert len(target_tags) == 1
# For Slack: id comes from entity_text (stripped @), name comes from tag_value
assert target_tags[0].id == "U456"
assert target_tags[0].name == "Bob Smith"
def test_command_with_entity_symphony(self, bot: Bot):
"""Test parsing command with entity mentions for Symphony."""
msg = Message(
user="John",
user_id="123456789",
msg="<span class='entity'>@Alice</span>",
channel="Test Room",
tags=["987654321"],
backend="symphony",
)
entity_map = {"ENTITY_0": ("@Alice", "987654321")}
result = bot.bot_commands_from_command_string(
tokens=["/echo", "ENTITY_0"],
message=msg,
channel="Test Room",
entity_map=entity_map,
)
assert result is not None
command, args, target_channel, target_tags = result
assert len(target_tags) == 1
# Symphony order is (name, id) in entity_map
assert target_tags[0].name == "@Alice"
assert target_tags[0].id == "987654321"
def test_command_with_entity_discord(self, bot: Bot):
"""Test parsing command with entity mentions for Discord."""
msg = Message(
user="bob",
user_id="111111111111111111",
msg="/echo <@222222222222222222>",
channel="general",
tags=["222222222222222222"],
backend="discord",
)
entity_map = {"ENTITY_0": ("@bob", "222222222222222222")}
result = bot.bot_commands_from_command_string(
tokens=["/echo", "ENTITY_0"],
message=msg,
channel="general",
entity_map=entity_map,
)
assert result is not None
command, args, target_channel, target_tags = result
assert len(target_tags) == 1
def test_empty_tokens(self, bot: Bot):
"""Test handling of empty token list."""
msg = Message(
user="alice",
user_id="U123",
msg="",
channel="general",
tags=[],
backend="slack",
)
result = bot.bot_commands_from_command_string(
tokens=[],
message=msg,
channel="general",
)
assert result is None
def test_unregistered_command(self, bot: Bot):
"""Test handling of unregistered command."""
msg = Message(
user="alice",
user_id="U123",
msg="/unknown_command",
channel="general",
tags=[],
backend="slack",
)
result = bot.bot_commands_from_command_string(
tokens=["/unknown_command"],
message=msg,
channel="general",
)
assert result is None
def test_malformed_channel_directive(self, bot: Bot):
"""Test handling of malformed /channel directive."""
msg = Message(
user="alice",
user_id="U123",
msg="/echo /channel",
channel="general",
tags=[],
backend="slack",
)
# /channel without a room name
result = bot.bot_commands_from_command_string(
tokens=["/echo", "/channel"],
message=msg,
channel="general",
)
# Should still return something, just with original channel
assert result is not None
class TestIsAuthorized:
"""Tests for Bot.is_authorized authorization check."""
def test_slack_always_authorized(self, bot: Bot):
"""Test that Slack users are always authorized (not implemented)."""
msg = Message(
user="alice",
user_id="U123456789",
msg="/help",
channel="general",
tags=[],
backend="slack",
)
assert bot.is_authorized(msg) is True
def test_discord_always_authorized(self, bot: Bot):
"""Test that Discord users are always authorized (not implemented)."""
msg = Message(
user="bob",
user_id="123456789012345678",
msg="/help",
channel="general",
tags=[],
backend="discord",
)
assert bot.is_authorized(msg) is True
class TestHelpCommand:
"""Tests for help command execution."""
def test_help_command_execution(self, bot: Bot):
"""Test that help command returns a message."""
command_instance = BotCommand(
command="help",
args=tuple(),
source=User(name="alice", id="U123", backend="slack"),
channel="general",
backend="slack",
variant=CommandVariant.REPLY,
message=Message(
user="alice",
user_id="U123",
msg="/help",
channel="general",
tags=[],
backend="slack",
),
)
result = bot.run_bot_command(command_instance)
assert result is not None
# Help command returns messages
assert len(result) >= 1
assert all(isinstance(r, Message) for r in result)
def test_help_command_for_specific_command(self, bot: Bot):
"""Test help for a specific command."""
command_instance = BotCommand(
command="help",
args=("echo",),
source=User(name="alice", id="U123", backend="slack"),
channel="general",
backend="slack",
variant=CommandVariant.REPLY,
message=Message(
user="alice",
user_id="U123",
msg="/help echo",
channel="general",
tags=[],
backend="slack",
),
)
result = bot.run_bot_command(command_instance)
assert result is not None
class TestEchoCommand:
"""Tests for echo command execution."""
def test_echo_command_execution(self, bot: Bot):
"""Test that echo command echoes the message."""
command_instance = BotCommand(
command="echo",
args=("hello", "world"),
source=User(name="alice", id="U123", backend="slack"),
channel="general",
backend="slack",
variant=CommandVariant.REPLY,
message=Message(
user="alice",
user_id="U123",
msg="/echo hello world",
channel="general",
tags=[],
backend="slack",
),
)
result = bot.run_bot_command(command_instance)
assert result is not None
# Echo command should return the echoed message
assert len(result) >= 1
response_text = result[0].msg
assert "hello" in response_text or "world" in response_text
class TestStatusCommand:
"""Tests for status command execution."""
def test_status_command_execution(self, bot: Bot):
"""Test that status command returns bot status."""
command_instance = BotCommand(
command="status",
args=tuple(),
source=User(name="alice", id="U123", backend="slack"),
channel="general",
backend="slack",
variant=CommandVariant.REPLY,
message=Message(
user="alice",
user_id="U123",
msg="/status",
channel="general",
tags=[],
backend="slack",
),
)
# Status command requires preexecute to set _adapters
# Call preexecute first to set up the command state
status_cmd = bot._commands["status"]
status_cmd.preexecute(command_instance, bot)
result = bot.run_bot_command(command_instance)
assert result is not None
class TestBotEdgeCases:
"""Tests for edge cases and error handling."""
def test_parse_msg_empty_message(self, bot: Bot):
"""Test parsing empty message."""
msg = Message(
user="alice",
user_id="U123",
msg="",
channel="general",
tags=[],
backend="slack",
)
text, entities = Bot.parse_msg(msg)
assert text == ""
assert len(entities) == 0
def test_parse_msg_only_whitespace(self, bot: Bot):
"""Test parsing whitespace-only message."""
msg = Message(
user="alice",
user_id="U123",
msg=" ",
channel="general",
tags=[],
backend="slack",
)
text, entities = Bot.parse_msg(msg)
assert text.strip() == ""
def test_is_msg_to_bot_with_at_prefix(self, bot: Bot):
"""Test bot detection with @bot_name format (no angle brackets)."""
msg = Message(
user="alice",
user_id="U123456789",
msg="@UBOT123 /help",
channel="general",
tags=["UBOT123"],
backend="slack",
)
is_to_bot, channel, text, entities = bot.is_msg_to_bot(msg)
assert is_to_bot is True
def test_extract_commands_with_multiple_entities(self, bot: Bot):
"""Test command extraction with multiple user mentions."""
msg = Message(
user="alice",
user_id="U123",
msg="/echo <@U456> <@U789>",
channel="general",
tags=["U456", "U789"],
backend="slack",
)
commands = bot.extract_bot_commands(
message=msg,
channel="general",
text="/echo <@U456> <@U789>",
entities=["@U456", "@U789"],
)
assert commands is not None
def test_extract_commands_with_dm_channel_detection(self, bot: Bot):
"""Test that DM channel triggers is_to_bot."""
msg = Message(
user="alice",
user_id="U123456789",
msg="/echo test",
channel="IM",
tags=[],
backend="slack",
)
is_to_bot, channel, text, entities = bot.is_msg_to_bot(msg)
assert is_to_bot is True
# Channel should be set to user for DMs
assert channel == "alice"
def test_command_with_at_entity_malformed_room(self, bot: Bot):
"""Test command with /room followed by @user (malformed)."""
msg = Message(
user="alice",
user_id="U123",
msg="/echo /room @someone",
channel="general",
tags=[],
backend="slack",
)
result = bot.bot_commands_from_command_string(
tokens=["/echo", "/room", "@someone"],
message=msg,
channel="general",
)
# Should still return result, just with original channel
assert result is not None
command, args, target_channel, _ = result
assert command == "echo"
def test_command_with_at_entity_malformed_channel(self, bot: Bot):
"""Test command with /channel followed by @user (malformed)."""
msg = Message(
user="alice",
user_id="U123",
msg="/echo /channel @someone",
channel="general",
tags=[],
backend="slack",
)
result = bot.bot_commands_from_command_string(
tokens=["/echo", "/channel", "@someone"],
message=msg,
channel="general",
)
# Should still return result, just with original channel
assert result is not None
command, args, target_channel, _ = result
assert command == "echo"
def test_command_with_trailing_args(self, bot: Bot):
"""Test command with multiple trailing arguments."""
msg = Message(
user="alice",
user_id="U123",
msg="/echo hello world how are you",
channel="general",
tags=[],
backend="slack",
)
result = bot.bot_commands_from_command_string(
tokens=["/echo", "hello", "world", "how", "are", "you"],
message=msg,
channel="general",
)
assert result is not None
command, args, target_channel, _ = result
assert command == "echo"
assert args == ["hello", "world", "how", "are", "you"]
class TestScheduleCommand:
"""Tests for schedule command basics."""
def test_schedule_command_registered(self, bot: Bot):
"""Test that schedule command is registered."""
assert "schedule" in bot._commands
def test_schedule_command_help(self, bot: Bot):
"""Test schedule command help text."""
schedule_cmd = bot._commands["schedule"]
help_text = schedule_cmd.help()
assert "schedule" in help_text.lower()