from __future__ import annotations from pathlib import Path from relay.dispatch import DispatchManager def test_writes_input_when_session_dir_empty(tmp_path: Path) -> None: mgr = DispatchManager(tmp_path / "dispatch") delivered = mgr.queue_or_write("session-1", "hello") assert delivered is True assert (tmp_path / "dispatch" / "session-1" / "input.txt").read_text() == "hello" def test_does_not_overwrite_pending_input(tmp_path: Path) -> None: mgr = DispatchManager(tmp_path / "dispatch") mgr.queue_or_write("session-1", "first") delivered = mgr.queue_or_write("session-1", "second") assert delivered is False # First file unchanged assert (tmp_path / "dispatch" / "session-1" / "input.txt").read_text() == "first" assert mgr.has_pending("session-1") def test_flush_all_delivers_after_consumer_deletes(tmp_path: Path) -> None: mgr = DispatchManager(tmp_path / "dispatch") mgr.queue_or_write("session-1", "first") mgr.queue_or_write("session-1", "second") # Consumer "consumes" first (tmp_path / "dispatch" / "session-1" / "input.txt").unlink() delivered = mgr.flush_all() assert delivered == 1 assert (tmp_path / "dispatch" / "session-1" / "input.txt").read_text() == "second" assert not mgr.has_pending("session-1") def test_independent_sessions_do_not_block_each_other(tmp_path: Path) -> None: mgr = DispatchManager(tmp_path / "dispatch") mgr.queue_or_write("session-1", "a") # blocks on session-1 delivered = mgr.queue_or_write("session-2", "b") # session-2 unaffected assert delivered is True assert (tmp_path / "dispatch" / "session-2" / "input.txt").read_text() == "b" def test_input_path_resolution(tmp_path: Path) -> None: mgr = DispatchManager(tmp_path / "dispatch") p = mgr.input_path("session-1") assert p == tmp_path / "dispatch" / "session-1" / "input.txt"