Files
led-controller/tests/test_pi_wifi_scan.py
2026-05-28 00:38:21 +12:00

67 lines
1.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.
#!/usr/bin/env python3
"""Tests for nmcli WiFi scan parsing."""
from __future__ import annotations
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "src"))
from util.pi_wifi import _unescape_nmcli # noqa: E402
def test_unescape_nmcli():
assert _unescape_nmcli("bridge\\:abc") == "bridge:abc"
assert _unescape_nmcli("plain") == "plain"
def test_interface_display_name_fallback(monkeypatch):
from util import pi_wifi
monkeypatch.setattr(
pi_wifi,
"_interface_display_name",
lambda device: "Test WiFi" if device == "wlan0" else device,
)
import subprocess
def fake_run(*args, **kwargs):
class _R:
stdout = "wlan0:wifi:connected:HomeNet\neth0:ethernet:connected:\n"
return _R()
monkeypatch.setattr(subprocess, "run", fake_run)
ifaces = pi_wifi.list_wifi_interfaces()
assert len(ifaces) == 1
assert ifaces[0]["device"] == "wlan0"
assert ifaces[0]["label"] == "Test WiFi"
assert ifaces[0]["connection"] == "HomeNet"
def test_scan_wifi_parses_terse_nmcli(monkeypatch):
import asyncio
from util import pi_wifi
sample = "\n".join(
[
"bridge-588c81a2fc18:84:",
"My Network:72:WPA2",
":50:WPA2",
"led:100:WPA2",
]
)
async def fake_run(*args, **kwargs):
return 0, sample, ""
monkeypatch.setattr(pi_wifi, "_run_nmcli", fake_run)
networks = asyncio.run(pi_wifi.scan_wifi("wlan0"))
ssids = [n["ssid"] for n in networks]
assert ssids == ["led", "bridge-588c81a2fc18", "My Network"]
assert networks[0]["signal"] == 100