feat: discover printer by MAC (survives DHCP IP changes)

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>
This commit is contained in:
Mortdecai
2026-06-07 13:20:34 -04:00
parent fa580ec936
commit 7bfe862f53
4 changed files with 109 additions and 1 deletions
+39
View File
@@ -0,0 +1,39 @@
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)