fix(wifi-adb): improve scan with multi-subnet fallback and mdns discovery

This commit is contained in:
Your Name committed 2026-06-01 09:40:44 +08:00
1 parent de21921d03
commit f76232f2fc
6 files changed
+235 -32

No files matched your search

+18
View File
@@ -135,3 +135,21 @@ def test_wifi_auto_reconnect_without_adb_returns_422(monkeypatch):
assert status == 422
assert obj.get("error") == "adb not found"
def test_wifi_scan_includes_tried_subnets(monkeypatch):
monkeypatch.setattr(routes, "find_adb_executable", lambda: "adb")
monkeypatch.setattr(routes, "list_adb_devices", lambda adb: []) # noqa: ARG001
monkeypatch.setattr(routes, "read_config", lambda: {})
monkeypatch.setattr(routes, "write_config", lambda cfg: None) # noqa: ARG001
import gui.gui_core_pkg as core_pkg
monkeypatch.setattr(core_pkg, "parse_port_spec", lambda spec: [5555]) # noqa: ARG001
monkeypatch.setattr(core_pkg, "discover_adb_devices", lambda *a, **k: [])
monkeypatch.setattr(core_pkg, "get_local_subnet", lambda: "192.168.10.0/24")
monkeypatch.setattr(core_pkg, "get_local_subnets", lambda: ["192.168.10.0/24", "10.0.0.0/24"])
status, obj = _post("/api/wifi/scan", {}, monkeypatch)
assert status == 200
assert obj.get("ok") is True
assert obj.get("subnets") == ["192.168.10.0/24", "10.0.0.0/24"]
+54
View File
@@ -79,6 +79,13 @@ class TestGetLocalSubnet:
assert scan.get_local_subnet() is None
class TestGetLocalSubnets:
def test_dedup_and_outbound_first(self):
with patch.object(scan, "_list_private_ipv4s", return_value=["192.168.10.108", "10.0.0.23", "192.168.10.20"]):
subnets = scan.get_local_subnets()
assert subnets == ["192.168.10.0/24", "10.0.0.0/24"]
# ============================================================
# probe_adb_port
# ============================================================
@@ -252,6 +259,23 @@ class TestVerifyAdbTarget:
assert state == "failed"
class TestDiscoverMdnsTargets:
def test_parse_ipv4_targets(self):
sample = """
List of discovered mdns services
adb-1234 _adb-tls-connect._tcp. 192.168.10.66:40075
adb-5678 _adb._tcp. 10.0.0.9:5555
garbage line
"""
with patch.object(scan, "_run_adb", return_value=(0, sample)):
targets = scan.discover_mdns_targets("/adb")
assert targets == [("10.0.0.9", 5555), ("192.168.10.66", 40075)]
def test_empty_on_error(self):
with patch.object(scan, "_run_adb", return_value=(1, "failed")):
assert scan.discover_mdns_targets("/adb") == []
# ============================================================
# discover_adb_devices —— 主入口集成测试
# ============================================================
@@ -332,6 +356,36 @@ class TestDiscoverAdbDevices:
assert captured["ports"] == [5555, 37001]
def test_auto_multi_subnet_fallback_scans_all(self):
calls = []
def fake_scan_ports(ips, **kwargs):
calls.append(list(ips))
# 第一网段无设备,第二网段有一个
if "192.168.10.2" in ips:
return []
return [("10.0.0.9", 5555)]
with patch.object(scan, "get_local_subnets", return_value=["192.168.10.0/30", "10.0.0.8/30"]), \
patch.object(scan, "scan_open_ports", side_effect=fake_scan_ports), \
patch.object(scan, "verify_adb_target", return_value=("connected", "ok")), \
patch.object(scan, "query_device_model", return_value="PEPM00"):
devs = scan.discover_adb_devices("/adb")
assert len(calls) == 2
assert len(devs) == 1
assert devs[0].target == "10.0.0.9:5555"
def test_merges_mdns_targets_before_port_scan(self):
with patch.object(scan, "discover_mdns_targets", return_value=[("192.168.10.66", 40075)]), \
patch.object(scan, "get_local_subnets", return_value=["192.168.10.0/30"]), \
patch.object(scan, "scan_open_ports", return_value=[]), \
patch.object(scan, "verify_adb_target", side_effect=lambda adb, t: ("connected", f"ok:{t}")), \
patch.object(scan, "query_device_model", return_value="PEPM00"):
devs = scan.discover_adb_devices("/adb")
assert len(devs) == 1
assert devs[0].target == "192.168.10.66:40075"
def test_returns_devs_with_correct_ports(self):
"""两个 (ip, port) 分别返回各自端口的 DiscoveredDevice。"""
with patch.object(