epoch_forge/tui_efo/tests/terminal_smoke.py

76 lines
4.2 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, json, 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'v', b'\x1b[F', b'v', 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)
if exit_keys == b'\x03':
def send(*keys):
for key in keys:
os.write(master, key)
drain(0.08)
def world():
return json.loads((root / '.efo/worktree.json').read_text())
original_world = world()
send(b'a', b'\x1b[200~TUI smoke created\x1b[201~', b'\t', b'\x1b[200~Multiline\nDra\xc3\xa4n\x1b[201~', b'\x13')
assert world() == original_world, 'preview wrote state before confirmation'
send(b'y')
assert any(e['name'] == 'TUI smoke created' and e['description'] == 'Multiline\nDraän' for e in world()['world']['characters']), 'create/paste failed'
send(b'u', b'y')
assert world() == original_world, 'undo creation failed'
send(b'\x1b[Z', b'\x1b[H', b'e', b'\x15', b'TUI smoke renamed', b'\x13', b'y')
assert any(e['name'] == 'TUI smoke renamed' for e in world()['world']['characters']), 'edit failed'
send(b'u', b'n')
assert any(e['name'] == 'TUI smoke renamed' for e in world()['world']['characters']), 'cancel undo failed'
send(b'u', b'y')
assert world() == original_world, 'undo edit failed'
send(b'a', b'Discard draft', b'\x1b', b'y')
assert world() == original_world, 'discard changed state'
current = snapshot(root)
assert {k: v for k, v in current.items() if k != '.efo/worktree.json'} == {k: v for k, v in before.items() if k != '.efo/worktree.json'}, 'editor changed files outside worktree'
print('PTY authoring passed: create, multiline paste, preview, edit, discard, confirmed undo; HEAD/index unchanged')
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'
if exit_keys == b'q':
assert snapshot(root) == before, 'browsing modified repository'
print(f'PTY smoke passed: exit={exit_keys!r}, navigation/search/layers/help/resize, terminal restored')
finally:
if process.poll() is None:
process.kill()
process.wait()
os.close(master)
os.close(slave)