"""Tests for the RunEvent/EventSink seam (afk_developer/events.py). Session 3 of the refactor-handoffs plan (docs/refactor-handoffs/03-tui-watch.md Part 1). ``ConsoleSink`` must render exactly what a bare ``console.print(text)`` call rendered before this seam existed -- these tests are the "zero behavior change" proof for the seam itself; the migrated call sites in scheduler/queue.py, scheduler/worker.py, and scheduler/best_of_n.py are covered by the existing (unchanged) test suites for those modules. """ import threading from io import StringIO from rich.console import Console from afk_developer.events import ConsoleSink, RunEvent, emit, get_sink, governance_sink, set_sink def test_console_sink_renders_event_text_verbatim() -> None: buffer = StringIO() sink = ConsoleSink(Console(file=buffer, force_terminal=False, width=200)) sink.emit(RunEvent(kind="info", text="[green]Task PASSED[/green] -- widget-1")) # force_terminal=False strips markup down to plain text but must not # otherwise alter the message. assert "Task PASSED -- widget-1" in buffer.getvalue() def test_console_sink_ascii_sanitizes_model_authored_unicode() -> None: buffer = StringIO() sink = ConsoleSink(Console(file=buffer, force_terminal=False, width=200)) sink.emit(RunEvent(kind="info", text="cube -> ship \u2192 complete")) assert buffer.getvalue().strip() == "cube -> ship ? complete" def test_set_sink_swap_lets_a_caller_capture_events_instead_of_printing() -> None: captured: list[RunEvent] = [] class RecordingSink: def emit(self, event: RunEvent) -> None: captured.append(event) previous = get_sink() try: set_sink(RecordingSink()) emit("task_passed", "Task PASSED -- widget-1", task_id="widget-1") finally: set_sink(previous) assert len(captured) == 1 assert captured[0].kind == "task_passed" assert captured[0].text == "Task PASSED -- widget-1" assert captured[0].data == {"task_id": "widget-1"} def test_get_sink_returns_a_console_sink_by_default() -> None: assert isinstance(get_sink(), ConsoleSink) def test_console_sink_serializes_concurrent_emit_so_lines_never_interleave() -> None: buffer = StringIO() sink = ConsoleSink(Console(file=buffer, force_terminal=False, width=200)) barrier = threading.Barrier(2) def _emit(label: str) -> None: barrier.wait() sink.emit(RunEvent(kind="info", text=f"line-{label}-AAAAAAAAAA-{label}")) threads = [threading.Thread(target=_emit, args=(label,)) for label in ("x", "y")] for thread in threads: thread.start() for thread in threads: thread.join() lines = [line for line in buffer.getvalue().splitlines() if line] assert len(lines) == 2 # Each full label must appear intact on exactly one line -- a broken lock # could otherwise interleave the two threads' output mid-line. assert any("line-x-AAAAAAAAAA-x" in line for line in lines) assert any("line-y-AAAAAAAAAA-y" in line for line in lines) def test_governance_sink_mirrors_existing_run_events_to_the_authoritative_log(tmp_path) -> None: from afk_developer.event_store import DurableEventStore, EventProjector events_path = tmp_path / "governance-events.jsonl" snapshot_path = tmp_path / "governance-projection.json" with governance_sink(events_path, snapshot_path): emit("task_passed", "Task passed", task_id="task-one", model=object()) projection = EventProjector().rebuild(DurableEventStore(events_path), snapshot_path) assert projection.task_statuses == {"task-one": "passed"} assert projection.latest_run_events["task-one"]["data"]["model"].startswith("