Files
douyin_guaji-main/tests/test_pick_best_match.py
T
CodexandClaude Opus 4.7 5966f461c3 feat(wifi_adb): 启动重连失败时自动扫描子网 + 智能匹配
新增 pick_best_match(devs, last) 纯函数,按优先级匹配上次设备:
1. target 完全一致(IP 没换)
2. 同 model + 同 port(换 IP 但同一台手机)—— 多台同型号时返回 None 让用户选
3. 同主机段 + 同 port(弱兜底,仅唯一时返回)
4. 唯一一台 connected(扫到就一台时认定)

GUI _try_reconnect_last_wifi 流程升级:
- verify_adb_target 失败(不可达)→ 自动 discover_adb_devices 扫子网
- pick_best_match 命中 → 选中 + 更新 last_wifi_target
- 命中且 IP 已变 → 日志写"上次目标 X 已变 → 智能匹配到 Y(已更新记忆)"
- 没命中 → 列出候选 + 提示点'扫描 WiFi'手选

实际场景:换 WiFi 网段(家里 → 公司 / 192.168.1 → 192.168.5)后
GUI 启动时自动找回同一台手机,无需任何手动操作。

新增 15 个 pick_best_match 单元测试覆盖所有规则 + 边界。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 20:46:23 +08:00

137 lines
5.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""pick_best_match 智能匹配单元测试。"""
from __future__ import annotations
import pytest
from gui.gui_core_pkg.wifi_adb_scan import DiscoveredDevice, pick_best_match
def _dev(ip: str, model: str = "", state: str = "connected", port: int = 5555):
return DiscoveredDevice(ip=ip, port=port, adb_state=state, model=model)
class TestPickBestMatch:
# ============================================================
# 规则 1:target 完全一致
# ============================================================
def test_exact_target_match(self):
d1 = _dev("192.168.5.2", "PEPM00")
d2 = _dev("192.168.5.3", "Other")
result = pick_best_match([d1, d2], {"target": "192.168.5.2:5555"})
assert result is d1
def test_exact_match_takes_priority_over_model(self):
"""target 命中时,即使 model 也匹配别的设备,仍返回 target 命中的那个。"""
d1 = _dev("192.168.5.2", "Other") # target 命中,model 不一样
d2 = _dev("192.168.5.3", "PEPM00") # target 不命中,model 一样
result = pick_best_match(
[d1, d2], {"target": "192.168.5.2:5555", "model": "PEPM00"}
)
assert result is d1
# ============================================================
# 规则 2:同 model
# ============================================================
def test_model_match_when_target_changed(self):
"""换 IP 但仍是同型号手机。"""
d1 = _dev("10.0.0.5", "PEPM00")
d2 = _dev("10.0.0.7", "Other")
result = pick_best_match(
[d1, d2], {"target": "192.168.5.2:5555", "model": "PEPM00"}
)
assert result is d1
def test_multiple_same_model_returns_none(self):
"""多台同型号手机时无法判断,返回 None 让用户手选。"""
d1 = _dev("10.0.0.5", "PEPM00")
d2 = _dev("10.0.0.7", "PEPM00")
result = pick_best_match(
[d1, d2], {"target": "192.168.5.2:5555", "model": "PEPM00"}
)
assert result is None
def test_model_must_match_port_too(self):
"""同 model 但 port 不一样不算匹配。"""
d1 = _dev("10.0.0.5", "PEPM00", port=5556)
d2 = _dev("10.0.0.7", "Other", port=5555)
# last 的 port 是 5555,d1 是 5556 → model 不匹配
# d2 model 也不匹配 → 走规则 4 唯一 connected
result = pick_best_match(
[d1, d2], {"target": "192.168.5.2:5555", "model": "PEPM00"}
)
# 只有 d2 是 5555 端口;但 d1 d2 都是 connected → 走不到规则 4,返回 None
assert result is None
# ============================================================
# 规则 4:唯一一台 connected
# ============================================================
def test_only_one_connected_returned(self):
d1 = _dev("192.168.5.2", "PEPM00")
result = pick_best_match([d1], {"target": "10.0.0.1:5555", "model": "Other"})
assert result is d1
def test_only_one_connected_among_unauthorized(self):
"""连接列表里有 unauthorized 的设备不影响"唯一 connected"判定。"""
d1 = _dev("192.168.5.2", "PEPM00")
d2 = _dev("192.168.5.3", "", state="unauthorized")
result = pick_best_match([d1, d2], {"target": "10.0.0.1:5555"})
assert result is d1
# ============================================================
# 规则 3:同主机段(弱兜底)
# ============================================================
def test_same_subnet_prefix_when_unique(self):
"""同主机段 + 唯一一台 → 命中弱兜底规则 3。"""
d1 = _dev("192.168.5.7", "ModelA") # 同段
d2 = _dev("10.0.0.5", "ModelB") # 不同段
# last 是 192.168.5.* model 不匹配,唯一性也不成立(2 台 connected)
# 但只有 d1 同段 → 命中规则 3
result = pick_best_match(
[d1, d2], {"target": "192.168.5.99:5555", "model": "Different"}
)
assert result is d1
# ============================================================
# 边界
# ============================================================
def test_empty_devs(self):
assert pick_best_match([], {"target": "192.168.5.2:5555"}) is None
def test_no_connected_returns_none(self):
d1 = _dev("192.168.5.2", "", state="unauthorized")
d2 = _dev("192.168.5.3", "", state="failed")
assert pick_best_match([d1, d2], {"target": "192.168.5.2:5555"}) is None
def test_none_last(self):
d1 = _dev("192.168.5.2", "PEPM00")
# 唯一 connected 仍触发规则 4
assert pick_best_match([d1], None) is d1
def test_empty_last_dict(self):
d1 = _dev("192.168.5.2", "PEPM00")
assert pick_best_match([d1], {}) is d1
def test_invalid_last_type(self):
"""传入非 dict 时应当宽容,按空 last 处理。"""
d1 = _dev("192.168.5.2", "PEPM00")
# 类型断言: pick_best_match 接受 Optional[dict],传非 dict 应该也容错
assert pick_best_match([d1], "not a dict") is d1 # type: ignore[arg-type]
def test_target_without_port(self):
"""last_target 缺 port 时按默认 5555 处理。"""
d1 = _dev("192.168.5.2", "PEPM00")
result = pick_best_match([d1], {"target": "192.168.5.2"})
# 仍命中规则 4 唯一 connected(target 完全一致比较失败但唯一性兜底)
assert result is d1
def test_target_with_invalid_port_format(self):
"""target 'host:abc' 不应抛异常。"""
d1 = _dev("192.168.5.2", "PEPM00")
result = pick_best_match([d1], {"target": "192.168.5.2:abc"})
assert result is d1