1- import json
21from datetime import datetime , timezone
32from unittest .mock import MagicMock , patch
43
@@ -62,125 +61,119 @@ def test_logs_endpoint_with_limit(client, mock_fetch_logs):
6261
6362
6463def test_websocket_connection ():
65- with TestClient (app ) as client :
66- with client .websocket_connect ("/ws" ) as websocket :
67- # WebSocket should connect successfully
68- # The connection is automatically closed when exiting the context
69- pass
64+ with TestClient (app ) as client , client .websocket_connect ("/ws" ):
65+ # WebSocket should connect successfully
66+ # The connection is automatically closed when exiting the context
67+ pass
7068
7169
7270def test_websocket_ping_pong ():
73- import time
7471 import asyncio
75- with TestClient (app ) as client :
72+ with TestClient (app ) as client , patch ( "asyncio.wait_for" ) as mock_wait_for :
7673 # Mock asyncio.wait_for to simulate timeout
77- with patch ("asyncio.wait_for" ) as mock_wait_for :
78- mock_wait_for .side_effect = asyncio .TimeoutError ()
79-
80- with client .websocket_connect ("/ws" ) as websocket :
81- # This should trigger the timeout handling
82- try :
83- # The server should send a ping
84- data = websocket .receive_json ()
85- assert data == {"type" : "ping" }
86- except Exception :
87- # Connection might close, that's ok
88- pass
74+ mock_wait_for .side_effect = asyncio .TimeoutError ()
75+
76+ with client .websocket_connect ("/ws" ) as websocket :
77+ # This should trigger the timeout handling
78+ try :
79+ # The server should send a ping
80+ data = websocket .receive_json ()
81+ assert data == {"type" : "ping" }
82+ except Exception :
83+ # Connection might close, that's ok
84+ pass
8985
9086
9187def test_websocket_disconnect ():
92- with TestClient (app ) as client :
93- with client .websocket_connect ("/ws" ) as websocket :
94- # Force disconnect by closing
95- websocket .close ()
96- # Should handle gracefully
88+ with TestClient (app ) as client , client .websocket_connect ("/ws" ) as websocket :
89+ # Force disconnect by closing
90+ websocket .close ()
91+ # Should handle gracefully
9792
9893
9994def test_websocket_broadcast ():
100- with TestClient (app ) as client :
95+ with TestClient (app ) as client , client . websocket_connect ( "/ws" ) as websocket :
10196 # Test that websocket connects and can receive messages
102- with client .websocket_connect ("/ws" ) as websocket :
103- # Just verify the websocket connection works
104- assert websocket is not None
105- # Could send a test message if needed
106- # websocket.send_json({"type": "ping"})
97+ # Just verify the websocket connection works
98+ assert websocket is not None
99+ # Could send a test message if needed
100+ # websocket.send_json({"type": "ping"})
107101
108102
109103
110104
111105def test_start_sniffer_thread ():
112106 from mcphawk .web .server import _start_sniffer_thread
113-
114- with patch ("mcphawk.sniffer.start_sniffer" ) as mock_sniffer :
115- with patch ("threading.Thread" ) as mock_thread :
107+
108+ with patch ("mcphawk.sniffer.start_sniffer" ), patch ("threading.Thread" ) as mock_thread :
116109 mock_thread_instance = MagicMock ()
117110 mock_thread .return_value = mock_thread_instance
118-
111+
119112 _start_sniffer_thread ("tcp port 3000" , auto_detect = False , debug = True )
120-
113+
121114 # Verify thread was created with correct arguments
122115 mock_thread .assert_called_once ()
123116 args , kwargs = mock_thread .call_args
124117 assert kwargs ["daemon" ] is True
125118 assert callable (kwargs ["target" ])
126-
119+
127120 # Verify thread was started
128121 mock_thread_instance .start .assert_called_once ()
129122
130123
131124def test_run_web_with_sniffer ():
132125 from mcphawk .web .server import run_web
133-
134- with patch ("uvicorn.run" ) as mock_uvicorn :
135- with patch ("mcphawk.web.server._start_sniffer_thread" ) as mock_start_sniffer :
136- with patch ("mcphawk.logger.init_db" ) as mock_init_db :
137- run_web (
138- sniffer = True ,
139- host = "0.0.0.0" ,
140- port = 9000 ,
141- filter_expr = "tcp port 3000" ,
142- auto_detect = False ,
143- debug = True
144- )
145-
146- # Verify sniffer was started
147- mock_start_sniffer .assert_called_once_with ("tcp port 3000" , False , True )
148-
149- # Verify uvicorn was started with correct params
150- mock_uvicorn .assert_called_once ()
151- args , kwargs = mock_uvicorn .call_args
152- assert args [0 ] == app # First arg is the app
153- assert kwargs ["host" ] == "0.0.0.0"
154- assert kwargs ["port" ] == 9000
126+
127+ with patch ("uvicorn.run" ) as mock_uvicorn , \
128+ patch ("mcphawk.web.server._start_sniffer_thread" ) as mock_start_sniffer , \
129+ patch ("mcphawk.logger.init_db" ):
130+ run_web (
131+ sniffer = True ,
132+ host = "0.0.0.0" ,
133+ port = 9000 ,
134+ filter_expr = "tcp port 3000" ,
135+ auto_detect = False ,
136+ debug = True
137+ )
138+
139+ # Verify sniffer was started
140+ mock_start_sniffer .assert_called_once_with ("tcp port 3000" , False , True )
141+
142+ # Verify uvicorn was started with correct params
143+ mock_uvicorn .assert_called_once ()
144+ args , kwargs = mock_uvicorn .call_args
145+ assert args [0 ] == app # First arg is the app
146+ assert kwargs ["host" ] == "0.0.0.0"
147+ assert kwargs ["port" ] == 9000
155148
156149
157150def test_run_web_without_sniffer ():
158151 from mcphawk .web .server import run_web
159-
160- with patch ("uvicorn.run" ) as mock_uvicorn :
161- with patch ("mcphawk.web.server._start_sniffer_thread" ) as mock_start_sniffer :
162- with patch ("mcphawk.logger.init_db" ) as mock_init_db :
163- run_web (
164- sniffer = False ,
165- host = "127.0.0.1" ,
166- port = 8000 ,
167- debug = False
168- )
169-
170- # Verify sniffer was NOT started
171- mock_start_sniffer .assert_not_called ()
172-
173- # Verify uvicorn was started with correct params
174- mock_uvicorn .assert_called_once ()
175- args , kwargs = mock_uvicorn .call_args
176- assert args [0 ] == app # First arg is the app
177- assert kwargs ["host" ] == "127.0.0.1"
178- assert kwargs ["port" ] == 8000
152+
153+ with patch ("uvicorn.run" ) as mock_uvicorn , \
154+ patch ("mcphawk.web.server._start_sniffer_thread" ) as mock_start_sniffer , \
155+ patch ("mcphawk.logger.init_db" ):
156+ run_web (
157+ sniffer = False ,
158+ host = "127.0.0.1" ,
159+ port = 8000 ,
160+ debug = False
161+ )
162+
163+ # Verify sniffer was NOT started
164+ mock_start_sniffer .assert_not_called ()
165+
166+ # Verify uvicorn was started with correct params
167+ mock_uvicorn .assert_called_once ()
168+ args , kwargs = mock_uvicorn .call_args
169+ assert args [0 ] == app # First arg is the app
170+ assert kwargs ["host" ] == "127.0.0.1"
171+ assert kwargs ["port" ] == 8000
179172
180173
181174def test_run_web_sniffer_without_filter ():
182175 from mcphawk .web .server import run_web
183-
176+
184177 # Test that ValueError is raised when sniffer=True but no filter_expr
185178 with pytest .raises (ValueError , match = "filter_expr is required" ):
186- run_web (sniffer = True , filter_expr = None )
179+ run_web (sniffer = True , filter_expr = None )
0 commit comments