fix: direct-IP fallback when printer_mac unset; force utf-8 in bridge subprocess

Final-review hardening:
- discover_printer falls back to direct IP reachability when printer_mac is
  empty (restores legacy behavior; avoids permanent unreachable on misconfig).
  Keeps the MAC check when a MAC IS set so we never print to a wrong DHCP tenant.
- bridge subprocess decodes child stdout as utf-8 explicitly, so a LANG=C systemd
  locale can't UnicodeDecodeError on the success checkmark and mis-report error.
- add tests for both no-MAC fallback paths (5 passed).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Mortdecai
2026-06-07 13:48:59 -04:00
parent 03aa1fc7c7
commit b9865be55f
3 changed files with 30 additions and 1 deletions
+21
View File
@@ -37,3 +37,24 @@ def test_not_found_raises(monkeypatch, tmp_path):
assert False, "expected RuntimeError"
except RuntimeError as e:
assert "printer-not-found" in str(e)
def test_no_mac_falls_back_to_direct_ip(monkeypatch, tmp_path):
# When printer_mac is unconfigured, a reachable seed IP is used directly.
monkeypatch.setattr(pb, "CACHE_FILE", tmp_path / "last_ip")
monkeypatch.setattr(pb, "PRINTER_IP", "192.168.0.184")
monkeypatch.setattr(pb, "PRINTER_MAC", "")
monkeypatch.setattr(pb, "_port_open", lambda ip, port, timeout=3: ip == "192.168.0.184")
assert pb.discover_printer() == "192.168.0.184"
def test_no_mac_unreachable_raises(monkeypatch, tmp_path):
monkeypatch.setattr(pb, "CACHE_FILE", tmp_path / "last_ip")
monkeypatch.setattr(pb, "PRINTER_IP", "192.168.0.184")
monkeypatch.setattr(pb, "PRINTER_MAC", "")
monkeypatch.setattr(pb, "_port_open", lambda ip, port, timeout=3: False)
try:
pb.discover_printer()
assert False, "expected RuntimeError"
except RuntimeError as e:
assert "no printer_mac" in str(e)