import os import pty import select import pytest from tamq.terminal import ( TerminalOutputError, format_comment, format_pushy_input, terminal_frame, write_terminal_output, ) def test_comment_format_escapes_controls_and_prefixes_every_line(): assert format_comment("repo-a", "first\nsecond\x1b[31m", "m-1") == ( "#repo-a: first\n# second\\x1b[31m [m-1]" ) def test_pushy_input_is_one_sanitized_shell_comment(): assert format_pushy_input("repo-a", "first\nsecond\x1b[31m", "m-1") == ( "# from repo-a: first\\x0asecond\\x1b[31m [m-1]" ) def test_terminal_frame_scrolls_only_rows_above_the_cursor(): assert terminal_frame( "repo-a", "first\nsecond", "m-1", cursor_y=8, pane_height=24 ) == ( "\x1b7\x1b[1;8r\x1b[8;1H" "\n\r#repo-a: first\n\r# second [m-1]" "\x1b[r\x1b8" ) def test_terminal_frame_falls_back_when_stable_region_is_not_safe(): expected = "\r\n#repo-a: hello [m-1]\r\n" assert terminal_frame("repo-a", "hello", "m-1", cursor_y=0, pane_height=24) == expected assert terminal_frame( "repo-a", "hello", "m-1", cursor_y=8, pane_height=24, alternate_on=True ) == expected assert terminal_frame("repo-a", "hello", "m-1") == ( "\r\n#repo-a: hello [m-1]\r\n" ) 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", "m-1")) readable, _, _ = select.select([master], [], [], 1) assert readable == [master] assert b"#repo-a: hello [m-1]" 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"