import os import pty import select import pytest from tamq.terminal import ( TerminalOutputError, format_delivery, terminal_frame, write_terminal_output, ) def test_delivery_format_escapes_controls_and_marks_operator_origin(): assert format_delivery("repo-a", "first\nsecond\x1b[31m", "operator_input") == ( "From:repo-a/o: first\nFrom:repo-a/o: second\\x1b[31m" ) assert format_delivery("repo-a", "worker", "worker_output") == ( "From:repo-a: worker" ) def test_terminal_frame_scrolls_only_rows_above_the_cursor(): assert terminal_frame( "repo-a", "hello", "operator_input", cursor_y=8, pane_height=24 ) == ( "\x1b7\x1b[1;8r\x1b[8;1H" "\n\rFrom:repo-a/o: hello" "\x1b[r\x1b8" ) def test_terminal_frame_falls_back_when_stable_region_is_not_safe(): expected = "\r\nFrom:repo-a: hello\r\n" assert terminal_frame("repo-a", "hello", "worker_output", cursor_y=0, pane_height=24) == expected assert terminal_frame( "repo-a", "hello", "worker_output", cursor_y=8, pane_height=24, alternate_on=True ) == expected def test_terminal_output_reaches_pty_output_but_not_input(): master, slave = pty.openpty() try: tty_path = os.ttyname(slave) write_terminal_output(tty_path, terminal_frame("repo-a", "hello", "worker_output")) readable, _, _ = select.select([master], [], [], 1) assert readable == [master] assert b"From:repo-a: hello" in os.read(master, 4096) readable_input, _, _ = select.select([slave], [], [], 0) assert readable_input == [] finally: os.close(master) os.close(slave) def test_terminal_output_rejects_non_pty_paths(tmp_path): target = tmp_path / "ordinary-file" target.write_text("untouched", encoding="utf-8") with pytest.raises(TerminalOutputError, match="non-PTY"): write_terminal_output(target, "message") assert target.read_text(encoding="utf-8") == "untouched"