66from unittest .mock import Mock , MagicMock
77from jupyterlab_chat .models import Message
88from jupyterlab_chat .ychat import YChat
9- from jupyter_ai_router .router import MessageRouter
9+ from jupyter_ai_router .router import MessageRouter , matches_pattern
1010from jupyter_ai_router .utils import get_first_word , is_persona
1111
1212
@@ -131,34 +131,27 @@ def test_cleanup(self):
131131 assert len (self .router .slash_cmd_observers ) == 0
132132 assert len (self .router .chat_msg_observers ) == 0
133133
134- def test_observe_slash_cmd_patterns (self ):
135- """Test registering specific slash command callback with patterns."""
136- room_id = "test-room"
137- command_pattern = "help"
138- self .router .observe_slash_cmd_msg (room_id , command_pattern , self .mock_specific_cmd_callback )
139-
140- assert command_pattern in self .router .slash_cmd_observers [room_id ]
141- assert self .mock_specific_cmd_callback in self .router .slash_cmd_observers [room_id ][command_pattern ]
142134
143- def test_command_matches_exact (self ):
135+ def test_matches_pattern_exact (self ):
144136 """Test exact command matching."""
145- assert self .router ._command_matches ("/help" , "help" ) is True
146- assert self .router ._command_matches ("/help" , "status" ) is False
147-
148- def test_command_matches_wildcard (self ):
149- """Test wildcard command matching."""
150- assert self .router ._command_matches ("/ai-generate" , "ai-*" ) is True
151- assert self .router ._command_matches ("/ai-review" , "ai-*" ) is True
152- assert self .router ._command_matches ("/help" , "ai-*" ) is False
153- assert self .router ._command_matches ("/export-csv" , "export-*" ) is True
154-
155- def test_command_matches_regex (self ):
156- """Test regex command matching."""
137+ assert matches_pattern ("help" , "help" ) is True
138+ assert matches_pattern ("help" , "status" ) is False
139+
140+ def test_matches_pattern_regex (self ):
141+ """Test regex pattern matching."""
142+ # Pattern with .* (formerly wildcard)
143+ assert matches_pattern ("ai-generate" , "ai-.*" ) is True
144+ assert matches_pattern ("ai-review" , "ai-.*" ) is True
145+ assert matches_pattern ("help" , "ai-.*" ) is False
146+ assert matches_pattern ("export-csv" , "export-.*" ) is True
147+
148+ def test_matches_pattern_regex_groups (self ):
149+ """Test regex command matching with groups."""
157150 pattern = r"export-(json|csv|xml)"
158- assert self . router . _command_matches ( "/ export-json" , pattern ) is True
159- assert self . router . _command_matches ( "/ export-csv" , pattern ) is True
160- assert self . router . _command_matches ( "/ export-xml" , pattern ) is True
161- assert self . router . _command_matches ( "/ export-pdf" , pattern ) is False
151+ assert matches_pattern ( " export-json" , pattern ) is True
152+ assert matches_pattern ( " export-csv" , pattern ) is True
153+ assert matches_pattern ( " export-xml" , pattern ) is True
154+ assert matches_pattern ( " export-pdf" , pattern ) is False
162155
163156 def test_specific_command_routing_exact (self ):
164157 """Test routing of specific slash commands with exact match."""
@@ -183,10 +176,10 @@ def test_specific_command_routing_exact(self):
183176 self .router ._route_message (room_id , status_msg )
184177 self .mock_specific_cmd_callback .assert_not_called ()
185178
186- def test_specific_command_routing_wildcard (self ):
187- """Test routing of specific slash commands with wildcard pattern."""
179+ def test_specific_command_routing_regex (self ):
180+ """Test routing of specific slash commands with regex pattern."""
188181 room_id = "test-room"
189- self .router .observe_slash_cmd_msg (room_id , "ai-*" , self .mock_specific_cmd_callback )
182+ self .router .observe_slash_cmd_msg (room_id , "ai-. *" , self .mock_specific_cmd_callback )
190183
191184 # Test matching commands
192185 generate_msg = Message (id = "1" , body = "/ai-generate code" , sender = "user" , time = 123 )
@@ -267,7 +260,7 @@ def test_multiple_patterns_different_commands(self):
267260 export_callback = Mock ()
268261
269262 self .router .observe_slash_cmd_msg (room_id , "help" , help_callback )
270- self .router .observe_slash_cmd_msg (room_id , "export-*" , export_callback )
263+ self .router .observe_slash_cmd_msg (room_id , "export-. *" , export_callback )
271264
272265 help_msg = Message (id = "1" , body = "/help topic" , sender = "user" , time = 123 )
273266 self .router ._route_message (room_id , help_msg )
@@ -304,7 +297,7 @@ def test_specific_command_error_handling(self):
304297 def test_invalid_regex_pattern (self ):
305298 """Test handling of invalid regex patterns."""
306299 # Invalid regex should not match anything
307- assert self . router . _command_matches ( "/ help" , "[invalid" ) is False
300+ assert matches_pattern ( " help" , "[invalid" ) is False
308301
309302 def test_message_trimming_and_command_cleaning (self ):
310303 """Test that messages are properly trimmed and commands cleaned."""
0 commit comments