7bfe862f53
Add discover_printer(), _mac_of(), _port_open(), _sweep_arp(), _cache_ip() with PRINTER_MAC/PRINTER_SUBNET/CACHE_FILE config + unit tests. Also adds conftest.py to work around broken symlink that blocked pytest collection. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
40 lines
1.7 KiB
Python
40 lines
1.7 KiB
Python
import os, sys
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
import pos_briefing as pb
|
|
|
|
|
|
def test_cache_hit_returns_cached_ip(monkeypatch, tmp_path):
|
|
cache = tmp_path / "last_ip"
|
|
cache.write_text("192.168.0.50")
|
|
monkeypatch.setattr(pb, "CACHE_FILE", cache)
|
|
monkeypatch.setattr(pb, "PRINTER_MAC", "50:57:9c:eb:0f:07")
|
|
monkeypatch.setattr(pb, "_mac_of", lambda ip: "50:57:9c:eb:0f:07" if ip == "192.168.0.50" else None)
|
|
monkeypatch.setattr(pb, "_port_open", lambda ip, port, timeout=3: True)
|
|
assert pb.discover_printer() == "192.168.0.50"
|
|
|
|
|
|
def test_sweep_finds_printer_on_cache_miss(monkeypatch, tmp_path):
|
|
cache = tmp_path / "last_ip"
|
|
monkeypatch.setattr(pb, "CACHE_FILE", cache)
|
|
monkeypatch.setattr(pb, "PRINTER_IP", "192.168.0.99") # stale seed
|
|
monkeypatch.setattr(pb, "PRINTER_MAC", "50:57:9c:eb:0f:07")
|
|
monkeypatch.setattr(pb, "PRINTER_SUBNET", "192.168.0")
|
|
monkeypatch.setattr(pb, "_mac_of", lambda ip: None) # seed/cache miss
|
|
monkeypatch.setattr(pb, "_sweep_arp", lambda: {"50:57:9c:eb:0f:07": "192.168.0.184"})
|
|
monkeypatch.setattr(pb, "_port_open", lambda ip, port, timeout=3: True)
|
|
assert pb.discover_printer() == "192.168.0.184"
|
|
assert cache.read_text().strip() == "192.168.0.184"
|
|
|
|
|
|
def test_not_found_raises(monkeypatch, tmp_path):
|
|
monkeypatch.setattr(pb, "CACHE_FILE", tmp_path / "last_ip")
|
|
monkeypatch.setattr(pb, "PRINTER_IP", "")
|
|
monkeypatch.setattr(pb, "PRINTER_MAC", "50:57:9c:eb:0f:07")
|
|
monkeypatch.setattr(pb, "_mac_of", lambda ip: None)
|
|
monkeypatch.setattr(pb, "_sweep_arp", lambda: {})
|
|
try:
|
|
pb.discover_printer()
|
|
assert False, "expected RuntimeError"
|
|
except RuntimeError as e:
|
|
assert "printer-not-found" in str(e)
|