"""Developer-only PTY test for the compiled model picker. Skipped by default. Set PIPELINES_HARNESS_PTY_DIR to build the CLI, drive the real picker under a PTY and retain raw ANSI bytes for inspection. This performs no inference, network call or server mutation. """ import json import os from pathlib import Path import pty import select import shutil import subprocess import tempfile import time import unittest class HarnessPickerPTYTests(unittest.TestCase): directory = None binary = None # Longer than at least one cursor-blink interval so a blink cannot silently # reset the active row before the confirming key is sent. blink_wait = 1.5 @classmethod def setUpClass(cls): cls.directory = os.environ.get("PIPELINES_HARNESS_PTY_DIR") if not cls.directory: raise unittest.SkipTest("set PIPELINES_HARNESS_PTY_DIR to run compiled picker PTY tests") if not shutil.which("go"): raise unittest.SkipTest("host Go is required to build the picker") root = Path(__file__).resolve().parents[1] build = Path(tempfile.mkdtemp(prefix="pipelines-pty-")) cls.binary = build / "pipelines" subprocess.run(["go", "build", "-o", str(cls.binary), "./cmd/pipelines"], cwd=root, check=True) Path(cls.directory).mkdir(parents=True, exist_ok=True) def drive(self, name, models, nav=b"", current="", expect=None, confirm=True): with tempfile.TemporaryDirectory() as tmp: catalog, result = Path(tmp) / "models.json", Path(tmp) / "selected.txt" catalog.write_text(json.dumps(models)) command = [str(self.binary), "choose-model", "--options-file", str(catalog), "--output", str(result), "--title", "PTY picker", "--purpose", "Interactive model purpose", "--step", "Step 1 of 2"] if current: command += ["--current", current] master, slave = pty.openpty() process = subprocess.Popen(command, stdin=slave, stdout=slave, stderr=slave, close_fds=True) os.close(slave) output = b"" sent = False confirmed = False sent_at = 0.0 deadline = time.monotonic() + 25 try: while time.monotonic() < deadline: ready, _, _ = select.select([master], [], [], 0.2) if ready: try: chunk = os.read(master, 4096) except OSError: break if not chunk: break output += chunk if not sent and b"Filter" in output: os.write(master, nav) sent = True sent_at = time.monotonic() # Wait through at least one full blink interval before # confirming so a blink-resets-index regression is caught. if sent and confirm and not confirmed and time.monotonic() - sent_at >= self.blink_wait: os.write(master, b"\r") confirmed = True if process.poll() is not None: break finally: if process.poll() is None: process.terminate() try: process.wait(timeout=5) except subprocess.TimeoutExpired: process.kill() os.close(master) (Path(self.directory) / (name + ".ansi")).write_bytes(output) self.assertIn(b"PTY picker", output, name) if confirm: self.assertTrue(result.exists(), name) written = result.read_text().strip() self.assertIn(written, models) self.assertEqual(written, expect, name) else: self.assertFalse(result.exists(), name) def test_arrow_scrolled_model_survives_blinks(self): models = [f"provider/model-{i:02d}" for i in range(20)] # Move down past the visible window, then confirm the exact arrow row. self.drive("selection", models, nav=b"\x1b[B" * 15, current=models[0], expect=models[15]) def test_saved_model_stays_selected_across_blinks(self): models = [f"provider/model-{i:02d}" for i in range(5)] self.drive("saved", models, nav=b"", current=models[3], expect=models[3]) def test_fuzzy_filter_and_confirm(self): models = ["opencode-go/deepseek-v4.1-flash", "other/model"] self.drive("fuzzy", models, nav=b"dskfl", expect="opencode-go/deepseek-v4.1-flash") def test_escape_cancels_without_writing_selection(self): models = ["opencode-go/deepseek-v4.1-flash"] self.drive("cancel", models, nav=b"\x1b", confirm=False) if __name__ == "__main__": unittest.main()