92 lines
3.5 KiB
Python
92 lines
3.5 KiB
Python
"""Drive the interactive TUI in a pty against the fake API. Prints screen text at checkpoints."""
|
|
import os, pty, sys, time, subprocess, select, re, json, tempfile, signal
|
|
|
|
root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
port = 20000 + os.getpid() % 20000
|
|
srv = subprocess.Popen([sys.executable, f"{root}/tests/fake_anthropic.py", str(port)])
|
|
tmp = tempfile.mkdtemp()
|
|
os.makedirs(f"{tmp}/home/.hx"); os.makedirs(f"{tmp}/repo")
|
|
open(f"{tmp}/home/.hx/auth.json", "w").write(json.dumps({"claude": {"access": "fake-token", "refresh": "r", "expires": 99999999999999}}))
|
|
open(f"{tmp}/repo/notes.md", "w").write("NOTES\n")
|
|
os.makedirs(f"{tmp}/home/.agents/skills/deploy")
|
|
slug = "".join(c if c.isascii() and c.isalnum() else "-" for c in f"{tmp}/repo").strip("-")
|
|
os.makedirs(f"{tmp}/home/.hx/sessions/{slug}")
|
|
open(f"{tmp}/home/.hx/sessions/{slug}/1.jsonl", "w").write(json.dumps({"id": 0, "parent": None, "message": {"role": "user", "content": [{"type": "text", "text": "OLDPROMPT about parsers"}]}}) + "\n")
|
|
os.makedirs(f"{tmp}/repo/.agents/skills/lint")
|
|
open(f"{tmp}/repo/.agents/skills/lint/SKILL.md", "w").write("---\nname: lint\ndescription: project lint\n---\nBODY\n")
|
|
open(f"{tmp}/home/.agents/skills/deploy/SKILL.md", "w").write("---\nname: deploy\ndescription: ship it\n---\nBODY\n")
|
|
time.sleep(0.5)
|
|
|
|
pid, fd = pty.fork()
|
|
if pid == 0:
|
|
os.chdir(f"{tmp}/repo")
|
|
os.environ.update(HOME=f"{tmp}/home", HX_BASE_URL=f"http://127.0.0.1:{port}", TERM="xterm-256color", COLUMNS="100", LINES="30")
|
|
os.execv(f"{root}/target/debug/hx", ["hx"])
|
|
|
|
import fcntl, termios, struct
|
|
fcntl.ioctl(fd, termios.TIOCSWINSZ, struct.pack("HHHH", 30, 100, 0, 0))
|
|
buf = b""
|
|
def pump(t):
|
|
global buf
|
|
end = time.time() + t
|
|
while time.time() < end:
|
|
r, _, _ = select.select([fd], [], [], 0.05)
|
|
if r:
|
|
try:
|
|
buf += os.read(fd, 65536)
|
|
except OSError:
|
|
return
|
|
def plain():
|
|
s = buf.decode(errors="replace")
|
|
s = re.sub(r"\x1b\[[0-9;?]*[A-Za-z]", " ", s)
|
|
return re.sub(r"\x1b[()][A-Z0-9]|\x1b[=>]", "", s)
|
|
def send(b, t=0.6):
|
|
os.write(fd, b); pump(t)
|
|
|
|
ok = True
|
|
def check(label, needle):
|
|
global ok
|
|
hit = needle in plain()
|
|
ok &= hit
|
|
print(f"{'PASS' if hit else 'FAIL'} {label}: {needle!r}")
|
|
|
|
pump(1.5)
|
|
check("startup footer", "Claude")
|
|
send(b"/skills\r")
|
|
check("skills from ~/.agents", "/deploy — ship it")
|
|
check("project .agents skills", "/lint — project lint")
|
|
send(b"@not"); send(b"\t")
|
|
check("tab completion", "es.md")
|
|
send(b"\x15") # ctrl-u
|
|
send(b"make hello.txt"); send(b"\r", 3)
|
|
check("tool call shown", "hello.txt")
|
|
check("tool outcome", "1 lines")
|
|
check("final answer", "Done: file says hi from hx.")
|
|
check("file written", "") if os.path.exists(f"{tmp}/repo/hello.txt") else (print("FAIL file"), None)
|
|
send(b"/model claude-opus-4-1\r")
|
|
check("model switch", "model → claude-opus-4-1")
|
|
send(b"/bogus\r")
|
|
check("unknown cmd", "unknown command /bogus")
|
|
send(b"\x10") # ctrl-p
|
|
send(b"OLDPROMPT")
|
|
check("picker lists past session", "session just now")
|
|
send(b"\r", 1)
|
|
check("picker resumes session", "resumed ")
|
|
send(b"/new\r")
|
|
check("new session", "new session")
|
|
send(b"/sessions\r")
|
|
check("sessions picker", "enter resume · tab scope")
|
|
send(b"make", 0.8)
|
|
check("sessions filter", "make hello.txt")
|
|
send(b"\r", 1)
|
|
check("resume from /sessions", "resumed ")
|
|
send(b"\x03", 1) # ctrl-c on empty -> quit
|
|
try:
|
|
_, st = os.waitpid(pid, 0)
|
|
print("exit status", st)
|
|
except ChildProcessError:
|
|
pass
|
|
check("exit summary", "session")
|
|
srv.kill()
|
|
sys.exit(0 if ok else 1)
|