import socket import sys import textwrap import pytest import yaml from rtut import legacy, pack as P def free_port(): s = socket.socket(); s.bind(("127.0.0.1", 0)); p = s.getsockname()[1]; s.close(); return p def make_repo(tmp_path, command, url, cwd="."): d = tmp_path / "runbooks" / "old" d.mkdir(parents=True) (d / "pack.yaml").write_text(yaml.safe_dump({ "spec": "runbook-pack/v0.1", "id": "t.old", "title": "Old console", "owner": "t", "engine": "legacy-command", "legacy": {"command": command, "cwd": cwd, "url": url}})) return P.load(d / "pack.yaml") def test_launch_and_stop(tmp_path): port = free_port() pk = make_repo(tmp_path, [sys.executable, "-m", "http.server", str(port), "--bind", "127.0.0.1"], f"http://127.0.0.1:{port}") proc = legacy.launch(pk, timeout=10) try: assert proc.poll() is None finally: proc.terminate(); proc.wait(5) def test_timeout_when_console_never_answers(tmp_path): pk = make_repo(tmp_path, [sys.executable, "-c", "import time; time.sleep(30)"], f"http://127.0.0.1:{free_port()}") with pytest.raises(TimeoutError): legacy.launch(pk, timeout=1) def test_cwd_escape_refused(tmp_path): pk = make_repo(tmp_path, ["true"], "", cwd="../..") with pytest.raises(ValueError): legacy.launch(pk)