"""Reviewer findings #2 / #4 — bullpen_worker must distinguish a FAILED room_read from an empty room, and check post success. (#4: `except: since=0` replayed all history when the offset read failed; the root cause was room_read swallowing failure as [].)""" import importlib.util from pathlib import Path REPO = Path(__file__).resolve().parents[1] _spec = importlib.util.spec_from_file_location("bw", REPO / "lib" / "bullpen_worker.py") bw = importlib.util.module_from_spec(_spec) _spec.loader.exec_module(bw) class FakeCP: def __init__(self, rc=0, stdout=""): self.returncode = rc self.stdout = stdout def test_ok_checks_returncode(): assert bw._ok(FakeCP(rc=0)) is True assert bw._ok(FakeCP(rc=1)) is False assert bw._ok(None) is False def test_read_failure_is_none_not_empty(monkeypatch): # #4: a failed read must NOT look like an empty room (which seeded since=0 -> replay) monkeypatch.setattr(bw, "lmcp", lambda *a, **k: FakeCP(rc=1, stdout="")) assert bw._read_since(0) is None def test_read_success_empty_is_empty_list(monkeypatch): monkeypatch.setattr(bw, "lmcp", lambda *a, **k: FakeCP(rc=0, stdout="")) assert bw._read_since(0) == [] def test_read_parses_jsonl(monkeypatch): monkeypatch.setattr(bw, "lmcp", lambda *a, **k: FakeCP(rc=0, stdout='{"id":1}\n \n{"id":2}\n')) assert [m["id"] for m in bw._read_since(0)] == [1, 2] def test_newest_id_from_room(monkeypatch): monkeypatch.setattr(bw, "lmcp", lambda *a, **k: FakeCP(rc=0, stdout='{"id":5}\n{"id":9}\n{"id":3}\n')) assert bw._newest_id() == 9 def test_newest_id_is_zero_only_on_empty_not_on_failure(monkeypatch): # empty room -> 0 is fine; a FAILED read -> 0 too, but never a replay because room_read=None monkeypatch.setattr(bw, "lmcp", lambda *a, **k: FakeCP(rc=1)) assert bw._newest_id() == 0 monkeypatch.setattr(bw, "lmcp", lambda *a, **k: FakeCP(rc=0, stdout="")) assert bw._newest_id() == 0