Some checks failed
tamq-ci / test (push) Failing after 7s
Assistant: codex Assistant-Model: gpt-5.6-sol Assistant-Session: 01a03397-4d51-7fd1-8ff2-946eb22ea2bc
98 lines
2.9 KiB
Python
98 lines
2.9 KiB
Python
import fcntl
|
|
import os
|
|
import pty
|
|
import struct
|
|
import termios
|
|
|
|
from tamq.ptytap import PtyTap, copy_winsize, observed_line, write_all
|
|
|
|
|
|
class RecordingBroker:
|
|
def __init__(self):
|
|
self.lines = []
|
|
|
|
def inspect_line(self, line):
|
|
self.lines.append(line)
|
|
return line.startswith(("@", "#"))
|
|
|
|
|
|
def test_input_observer_accepts_raw_terminal_carriage_returns():
|
|
broker = RecordingBroker()
|
|
observed = []
|
|
tap = PtyTap(["true"], broker, on_line=observed.append)
|
|
buffer = bytearray()
|
|
|
|
tap._observe_input(buffer, b"#activity-core: hel")
|
|
tap._observe_input(buffer, b"lo\rplain line\n")
|
|
|
|
assert broker.lines == ["#activity-core: hello", "plain line"]
|
|
assert observed == ["#activity-core: hello"]
|
|
|
|
|
|
def test_input_observer_accepts_enhanced_terminal_enter_sequences():
|
|
broker = RecordingBroker()
|
|
observed = []
|
|
tap = PtyTap(["true"], broker, on_line=observed.append)
|
|
buffer = bytearray()
|
|
|
|
tap._observe_input(buffer, b"#activity-core: CSI-u\x1b[13;1")
|
|
assert broker.lines == []
|
|
tap._observe_input(buffer, b":1u#other: modify\x1b[27;1;13~")
|
|
tap._observe_input(buffer, b"plain\x1bOM")
|
|
|
|
assert broker.lines == [
|
|
"#activity-core: CSI-u",
|
|
"#other: modify",
|
|
"plain",
|
|
]
|
|
assert observed == ["#activity-core: CSI-u", "#other: modify"]
|
|
|
|
|
|
def test_input_observer_ignores_terminal_replies_before_typed_line():
|
|
broker = RecordingBroker()
|
|
observed = []
|
|
tap = PtyTap(["true"], broker, on_line=observed.append)
|
|
buffer = bytearray()
|
|
|
|
tap._observe_input(buffer, b"\x1b[9;1R\x1b[?1;2;4c")
|
|
tap._observe_input(buffer, b"#activity-core: Helol\x7f\x7flo\r")
|
|
|
|
assert broker.lines == ["#activity-core: Hello"]
|
|
assert observed == ["#activity-core: Hello"]
|
|
|
|
|
|
def test_observed_line_removes_common_terminal_protocol_wrappers():
|
|
raw = (
|
|
b"\x1b[200~#target: pasted\x1b[201~"
|
|
b"\x1b]10;rgb:ffff/ffff/ffff\x07"
|
|
b"\x1bP1$r0m\x1b\\"
|
|
)
|
|
|
|
assert observed_line(raw) == "#target: pasted"
|
|
|
|
|
|
def test_copy_winsize_preserves_rows_columns_and_pixels():
|
|
source_master, source_slave = pty.openpty()
|
|
target_master, target_slave = pty.openpty()
|
|
expected = struct.pack("HHHH", 41, 103, 900, 1600)
|
|
try:
|
|
fcntl.ioctl(source_slave, termios.TIOCSWINSZ, expected)
|
|
assert copy_winsize(source_slave, target_master) is True
|
|
actual = fcntl.ioctl(target_slave, termios.TIOCGWINSZ, b"\0" * 8)
|
|
assert actual == expected
|
|
finally:
|
|
for fd in (source_master, source_slave, target_master, target_slave):
|
|
os.close(fd)
|
|
|
|
|
|
def test_write_all_retries_partial_writes(monkeypatch):
|
|
writes = []
|
|
|
|
def partial_write(fd, data):
|
|
chunk = bytes(data[:2])
|
|
writes.append((fd, chunk))
|
|
return len(chunk)
|
|
|
|
monkeypatch.setattr(os, "write", partial_write)
|
|
write_all(9, b"abcde")
|
|
assert writes == [(9, b"ab"), (9, b"cd"), (9, b"e")]
|