|
9 | 9 |
|
10 | 10 | import json |
11 | 11 | import pytest |
12 | | -from unittest.mock import MagicMock |
| 12 | +from unittest.mock import MagicMock, patch |
13 | 13 |
|
14 | 14 |
|
15 | 15 | @pytest.fixture |
@@ -131,6 +131,29 @@ def test_get_ports(self, client): |
131 | 131 | assert data["success"] is True |
132 | 132 | assert "ports" in data |
133 | 133 |
|
| 134 | + @patch("serial_utils.serial.tools.list_ports.comports") |
| 135 | + @patch("serial_utils.glob.glob") |
| 136 | + def test_get_ports_filters_ttys(self, mock_glob, mock_comports, client): |
| 137 | + """Test get ports response does not include /dev/ttyS* devices.""" |
| 138 | + mock_comports.return_value = [ |
| 139 | + MagicMock(device="/dev/ttyUSB0", description="USB Serial"), |
| 140 | + MagicMock(device="/dev/ttyS2", description="Legacy UART"), |
| 141 | + MagicMock(device="/dev/ttyACM0", description="CDC ACM"), |
| 142 | + ] |
| 143 | + mock_glob.return_value = ["/dev/ttyCH341USB0"] |
| 144 | + |
| 145 | + response = client.get("/api/ports") |
| 146 | + assert response.status_code == 200 |
| 147 | + data = response.get_json() |
| 148 | + assert data["success"] is True |
| 149 | + |
| 150 | + devices = [port["device"] for port in data["ports"]] |
| 151 | + assert "/dev/ttyUSB0" in devices |
| 152 | + assert "/dev/ttyACM0" in devices |
| 153 | + assert "/dev/ttyCH341USB0" in devices |
| 154 | + assert "/dev/ttyS2" not in devices |
| 155 | + assert all(not device.startswith("/dev/ttyS") for device in devices) |
| 156 | + |
134 | 157 |
|
135 | 158 | class TestStatusRoute: |
136 | 159 | """Test status API route.""" |
|
0 commit comments