diff --git a/tests/test_docker.sh b/tests/test_docker.sh index 6262d7910..38817bca6 100644 --- a/tests/test_docker.sh +++ b/tests/test_docker.sh @@ -41,7 +41,7 @@ echo "=== Validating output ===" PASS=true check() { - if echo "$STATUS_OUTPUT" | grep -q "$1"; then + if [[ "$STATUS_OUTPUT" == *"$1"* ]]; then echo " PASS: found '$1'" else echo " FAIL: missing '$1'" diff --git a/tests/test_docker_smoke.py b/tests/test_docker_smoke.py index 945626a3d..225d444f8 100644 --- a/tests/test_docker_smoke.py +++ b/tests/test_docker_smoke.py @@ -15,6 +15,10 @@ pytestmark = pytest.mark.skipif( reason="the Docker smoke script runs under Unix Bash", ) SCRIPT = Path(__file__).with_name("test_docker.sh") +STATUS_OUTPUT = ( + "nanobot Status\nConfig: configured\nWorkspace: ready\nModel: test\n" + "OpenRouter API: unset\nAnthropic API: unset\nOpenAI API: unset" +) @pytest.fixture @@ -30,14 +34,13 @@ def docker_boundary(tmp_path): "stage = args[0]\n" "if stage == 'run': stage = args[-1]\n" "if stage == 'status':\n" - " print('nanobot Status\\nConfig: configured\\nWorkspace: ready\\nModel: test\\n'\n" - " 'OpenRouter API: unset\\nAnthropic API: unset\\nOpenAI API: unset')\n" + " print(os.environ['DOCKER_STATUS_OUTPUT'])\n" "if stage == os.environ.get('DOCKER_FAIL_STAGE'): sys.exit(23)\n", encoding="utf-8", ) executable.chmod(0o755) - def run(name, *, fail_stage=""): + def run(name, *, fail_stage="", status_output=STATUS_OUTPUT): log = tmp_path / f"{name}.jsonl" result = subprocess.run( ["bash", str(SCRIPT)], @@ -46,6 +49,7 @@ def docker_boundary(tmp_path): "PATH": f"{executable.parent}{os.pathsep}{os.environ.get('PATH', '')}", "DOCKER_CALLS": str(log), "DOCKER_FAIL_STAGE": fail_stage, + "DOCKER_STATUS_OUTPUT": status_output, }, capture_output=True, text=True, @@ -94,3 +98,19 @@ def test_concurrent_smoke_runs_use_distinct_resources_and_cleanup(docker_boundar assert "All checks passed" in result.stdout resources.append(_assert_owned_cleanup(calls)) assert resources[0].isdisjoint(resources[1]) + + +def test_smoke_accepts_markers_before_large_diagnostic_output(docker_boundary): + # Status captures stderr too. Early grep exit must not turn valid output into SIGPIPE failure. + result, calls = docker_boundary("large", status_output=STATUS_OUTPUT + "\ndiagnostic" * 8192) + assert result.returncode == 0, result.stdout[-2000:] + result.stderr + assert "All checks passed" in result.stdout + _assert_owned_cleanup(calls) + + +def test_smoke_rejects_successful_status_with_missing_marker(docker_boundary): + result, calls = docker_boundary("missing", status_output=STATUS_OUTPUT.replace("Model:", "")) + assert result.returncode == 1, result.stdout + result.stderr + assert "FAIL: missing 'Model:'" in result.stdout + assert "All checks passed" not in result.stdout + _assert_owned_cleanup(calls) diff --git a/tests/tools/test_exec_session_tools.py b/tests/tools/test_exec_session_tools.py index ce27b96b4..ef02d9f8f 100644 --- a/tests/tools/test_exec_session_tools.py +++ b/tests/tools/test_exec_session_tools.py @@ -445,7 +445,7 @@ def test_exec_rejects_unsupported_shell(tmp_path): def test_exec_can_continue_with_stdin(tmp_path): async def run() -> tuple[str, str, str]: manager = ExecSessionManager() - exec_tool = ExecTool(working_dir=str(tmp_path), timeout=5, session_manager=manager) + exec_tool = ExecTool(working_dir=str(tmp_path), timeout=30, session_manager=manager) stdin_tool = ExecSessionTool(manager=manager) command = _python_command( "import sys; print('ready', flush=True); " @@ -453,16 +453,18 @@ def test_exec_can_continue_with_stdin(tmp_path): ) try: - initial = await exec_tool.execute(command=command, yield_time_ms=500) + initial = await exec_tool.execute(command=command, yield_time_ms=0) sid = _session_id(initial) - result = await stdin_tool.execute(session_id=sid, input="ping\n", timeout_ms=1000) - observed, final = await _poll_if_running(result, stdin_tool) + if "ready" not in initial: + initial += await stdin_tool.execute(session_id=sid, wait_for="ready", timeout_ms=10000) + assert "ready" in initial, initial + result = await stdin_tool.execute(session_id=sid, input="ping\n", timeout_ms=0) + observed, final = await _poll_if_running(result, stdin_tool, timeout_ms=10000) return initial, observed, final finally: await manager.close_all() initial, result, final = asyncio.run(run()) - assert "ready" in initial + result assert "Process running" in initial assert "Elapsed:" in initial assert "got:ping" in result @@ -471,30 +473,37 @@ def test_exec_can_continue_with_stdin(tmp_path): def test_exec_session_can_close_stdin(tmp_path): - async def run() -> tuple[str, str]: + async def run() -> tuple[str, str, str]: manager = ExecSessionManager() - exec_tool = ExecTool(working_dir=str(tmp_path), timeout=5, session_manager=manager) + exec_tool = ExecTool(working_dir=str(tmp_path), timeout=30, session_manager=manager) stdin_tool = ExecSessionTool(manager=manager) command = _python_command( "import sys; print('ready', flush=True); " "data=sys.stdin.read(); print('got:' + data, flush=True)" ) - initial = await exec_tool.execute(command=command, yield_time_ms=1500) - sid = _session_id(initial) - result = await stdin_tool.execute( - session_id=sid, - input="payload", - close_stdin=True, - timeout_ms=1500, - ) - return initial, result + try: + initial = await exec_tool.execute(command=command, yield_time_ms=0) + sid = _session_id(initial) + if "ready" not in initial: + initial += await stdin_tool.execute(session_id=sid, wait_for="ready", timeout_ms=10000) + assert "ready" in initial, initial + # Check the acknowledgement separately from the child's EOF-driven exit. + result = await stdin_tool.execute( + session_id=sid, + input="payload", + close_stdin=True, + timeout_ms=0, + ) + observed, final = await _poll_if_running(result, stdin_tool, timeout_ms=10000) + return result, observed, final + finally: + await manager.close_all() - initial, result = asyncio.run(run()) - assert "ready" in initial + result - assert "got:payload" in result + result, observed, final = asyncio.run(run()) assert "Stdin closed." in result - assert "Exit code: 0" in result + assert "got:payload" in observed + assert "Exit code: 0" in final def test_exec_session_can_terminate_session(tmp_path):