Skip to content

Commit 2b98f56

Browse files
committed
fix: cleanup tests
1 parent 737481f commit 2b98f56

File tree

7 files changed

+266
-112
lines changed

7 files changed

+266
-112
lines changed

codecov.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ coverage:
33
status:
44
project:
55
default:
6-
target: 78 # Automatically set coverage target
6+
target: 85 # Automatically set coverage target
77
threshold: 5% # Allow 5% drop in coverage

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,7 @@ dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
8282
"__init__.py" = ["F401", "E402"]
8383
# Ignore assert statements in tests
8484
"test_*.py" = ["S101", "E712"]
85-
"tests/*.py" = ["S101", "E712"]
85+
"tests/*.py" = ["S101", "E712"]
86+
87+
[tool.coverage.run]
88+
omit = ["mcphawk/models.py"]

tests/test_broadcast_ui.py

Lines changed: 0 additions & 75 deletions
This file was deleted.

tests/test_broadcaster.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import asyncio
2+
from unittest.mock import AsyncMock, MagicMock
3+
4+
import pytest
5+
6+
from mcphawk.web.broadcaster import active_clients, broadcast_new_log
7+
8+
9+
@pytest.mark.asyncio
10+
async def test_broadcast_new_log_with_clients():
11+
# Create mock websocket clients
12+
mock_client1 = AsyncMock()
13+
mock_client2 = AsyncMock()
14+
15+
# Add to active clients
16+
active_clients.append(mock_client1)
17+
active_clients.append(mock_client2)
18+
19+
try:
20+
# Broadcast a message
21+
test_log = {
22+
"timestamp": "2024-01-01T12:00:00Z",
23+
"src_ip": "127.0.0.1",
24+
"message": "test"
25+
}
26+
27+
await broadcast_new_log(test_log)
28+
29+
# Verify both clients received the message
30+
mock_client1.send_json.assert_called_once_with(test_log)
31+
mock_client2.send_json.assert_called_once_with(test_log)
32+
33+
finally:
34+
# Clean up
35+
active_clients.clear()
36+
37+
38+
@pytest.mark.asyncio
39+
async def test_broadcast_new_log_with_disconnected_client():
40+
# Create mock websocket clients
41+
mock_client1 = AsyncMock()
42+
mock_client2 = AsyncMock()
43+
44+
# Make client1 throw an exception (disconnected)
45+
mock_client1.send_json.side_effect = Exception("Disconnected")
46+
47+
# Add to active clients
48+
active_clients.append(mock_client1)
49+
active_clients.append(mock_client2)
50+
51+
try:
52+
# Broadcast a message
53+
test_log = {"test": "data"}
54+
55+
await broadcast_new_log(test_log)
56+
57+
# Client1 should be removed from active_clients
58+
assert mock_client1 not in active_clients
59+
assert mock_client2 in active_clients
60+
61+
# Client2 should still receive the message
62+
mock_client2.send_json.assert_called_once_with(test_log)
63+
64+
finally:
65+
# Clean up
66+
active_clients.clear()
67+
68+
69+
@pytest.mark.asyncio
70+
async def test_broadcast_new_log_no_clients():
71+
# Ensure list is empty
72+
active_clients.clear()
73+
74+
# Should not raise any errors
75+
await broadcast_new_log({"test": "data"})

tests/test_server.py

Lines changed: 0 additions & 31 deletions
This file was deleted.

tests/test_sniffer.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,6 @@ def test_packet_callback(dummy_server):
8686
assert "Berlin" in logged_msg
8787

8888

89-
def test_import():
90-
import mcphawk
91-
assert hasattr(mcphawk, "__version__")
92-
9389

9490
class TestAutoDetect:
9591
"""Test auto-detect mode functionality."""

0 commit comments

Comments
 (0)