import time
import pytest
from utils import GP, check_shell_any_device, check_shell, load_gp
class TestHdcReconnect:
@pytest.mark.L0
def test_reconnect_no_target_key(self):
assert check_shell("reconnect", "[Fail]Usage: reconnect <target-key>", head=GP.hdc_exe)
@pytest.mark.L0
def test_reconnect_nonexistent_target(self):
assert check_shell("reconnect fake_serial_not_exist",
"[Fail]Target device fake_serial_not_exist not available", head=GP.hdc_exe)
@pytest.mark.L0
def test_reconnect_tcp_target_rejected(self):
"""Reconnect should reject non-USB (TCP) targets."""
result = check_shell("tconn 127.0.0.1:10178", fetch=True, head=GP.hdc_exe)
if "Connect OK" not in result:
pytest.skip("No TCP daemon available for test")
assert check_shell("reconnect 127.0.0.1:10178",
"[Fail]Reconnect only supports USB devices", head=GP.hdc_exe)
check_shell("tconn 127.0.0.1:10178 -remove", head=GP.hdc_exe)
@pytest.mark.L0
def test_reconnect_usb_device(self):
"""Reconnect a USB device and verify it comes back."""
output = check_shell("list targets", fetch=True, head=GP.hdc_exe)
if "[Empty]" in output:
pytest.skip("No USB device connected")
targets = [t.strip() for t in output.strip().split('\n') if t.strip()]
usb_target = None
for t in targets:
if ':' not in t:
usb_target = t.split()[0] if ' ' in t else t
break
if usb_target is None:
pytest.skip("No USB target found in list targets")
assert check_shell(f"reconnect {usb_target}", "Reconnecting", head=GP.hdc_exe)
check_shell_any_device(f"{GP.hdc_exe} wait", None, False)
time.sleep(2)
assert check_shell("list targets", usb_target, head=GP.hdc_exe)