Files
douyi_guaji_main/tests/test_web_gui_routes_logic.py
T

138 lines
3.5 KiB
Python

"""web_gui_pkg.routes 关键逻辑单测(无需启动 HTTP 服务)。"""
from __future__ import annotations
from types import SimpleNamespace
import pytest
from gui.web_gui_pkg import routes
class _DummyHandler:
def __init__(self, body=None, csrf_token: str = "token"):
self.headers = {"X-CSRF-Token": csrf_token}
self._body = body if body is not None else {}
self.calls = []
def _read_json(self):
return self._body
def _send_json(self, obj, status: int = 200):
self.calls.append((status, obj))
return {"status": status, "obj": obj}
def send_error(self, status):
self.calls.append((status, {"error": "send_error"}))
return None
class _DummyRunner:
def get_stats(self, include_events=False): # noqa: ARG002
return {"ok": True}
def is_running(self):
return False
def get_reservations(self):
return {}
def get_high_value_rooms(self):
return {}
def force_stop(self):
return None
def stop(self):
return None
def send_back_key(self, _device_id): # noqa: ARG002
return True
def start_main_run(self, _cfg): # noqa: ARG002
return None
def _post(path: str, body: dict, monkeypatch, csrf: str = "token"):
handler = _DummyHandler(body=body, csrf_token=csrf)
runner = _DummyRunner()
u = SimpleNamespace(path=path, query="")
routes.handle_post(handler, u, runner, "token")
assert handler.calls, f"{path} should produce a response"
return handler.calls[-1]
@pytest.mark.parametrize(
"raw,expected",
[
(True, True),
(False, False),
("true", True),
("TRUE", True),
("1", True),
("yes", True),
("on", True),
("false", False),
("FALSE", False),
("0", False),
("no", False),
("off", False),
("", False),
(0, False),
(1, True),
],
)
def test_coerce_bool(raw, expected):
assert routes._coerce_bool(raw) is expected
def test_config_bool_string_false_written_as_false(monkeypatch):
written = {}
monkeypatch.setattr(routes, "read_config", lambda: {"enable_streaming_screenshot": True})
monkeypatch.setattr(routes, "write_config", lambda cfg: written.update(cfg))
status, obj = _post(
"/api/config",
{"config": {"enable_streaming_screenshot": "false"}},
monkeypatch,
)
assert status == 200
assert obj.get("ok") is True
assert written.get("enable_streaming_screenshot") is False
@pytest.mark.parametrize(
"ports_raw,expected_invalid",
[
(None, False),
("", False),
("5555", False),
("5555,5556", False),
("5555-5560", False),
(["5555", 5556], False),
("abc", True),
("5555,abc", True),
("1-2-3", True),
(70000, True),
(["x"], True),
],
)
def test_is_invalid_ports_spec(ports_raw, expected_invalid):
assert routes._is_invalid_ports_spec(ports_raw) is expected_invalid
def test_wifi_scan_invalid_ports_returns_400(monkeypatch):
monkeypatch.setattr(routes, "find_adb_executable", lambda: "adb")
status, obj = _post("/api/wifi/scan", {"ports": "abc"}, monkeypatch)
assert status == 400
assert obj.get("error") == "invalid ports"
def test_wifi_auto_reconnect_without_adb_returns_422(monkeypatch):
monkeypatch.setattr(routes, "find_adb_executable", lambda: "")
status, obj = _post("/api/wifi/auto-reconnect", {}, monkeypatch)
assert status == 422
assert obj.get("error") == "adb not found"