epoch_forge/tui_efo/tests/terminal_smoke.py

50 lines
2.4 KiB
Python

"""Unix PTY release smoke check. Run from the repository root after cargo build.
Uses only Python's standard library and a temporary copy of the example world.
"""
import fcntl, hashlib, os, pathlib, pty, select, shutil, struct, subprocess, tempfile, termios, time
binary = str(pathlib.Path('target/debug/efo-tui').resolve())
def snapshot(root):
return {str(p.relative_to(root)): hashlib.sha256(p.read_bytes()).hexdigest() for p in root.rglob('*') if p.is_file()}
with tempfile.TemporaryDirectory() as directory:
root = pathlib.Path(directory) / 'world'
shutil.copytree('examples/ravenous-draan', root)
before = snapshot(root)
for exit_keys in [b'q', b'\x03']:
master, slave = pty.openpty()
fcntl.ioctl(slave, termios.TIOCSWINSZ, struct.pack('HHHH', 28, 120, 0, 0))
original = termios.tcgetattr(slave)
process = subprocess.Popen([binary, '--repository', str(root), '--no-color'], stdin=slave, stdout=slave, stderr=slave)
output = bytearray()
def drain(duration):
deadline = time.monotonic() + duration
while time.monotonic() < deadline:
if select.select([master], [], [], 0.02)[0]:
chunk = os.read(master, 65536)
output.extend(chunk)
if b'\x1b[6n' in chunk:
os.write(master, b'\x1b[1;1R')
try:
drain(0.3)
for keys in [b'\t', b'/dra', b'\r', b'2', b'3', b'\t', b'\x1b[F', b'?', b'\x1b[F', b'?', b'r']:
os.write(master, keys)
drain(0.06)
fcntl.ioctl(slave, termios.TIOCSWINSZ, struct.pack('HHHH', 16, 60, 0, 0))
os.write(master, b'\t')
drain(0.1)
os.write(master, exit_keys)
drain(0.2)
assert process.wait(timeout=3) == 0, bytes(output[-2000:])
assert termios.tcgetattr(slave) == original, 'terminal attributes were not restored'
assert b'\x1b[?1049h' in output and b'\x1b[?1049l' in output, 'alternate screen lifecycle missing'
assert snapshot(root) == before, 'repository was modified'
print(f'PTY smoke passed: exit={exit_keys!r}, navigation/search/layers/help/resize, terminal restored, repository unchanged')
finally:
if process.poll() is None:
process.kill()
process.wait()
os.close(master)
os.close(slave)