import os import subprocess import textwrap import time from pathlib import Path def test_start_main_script_manages_pid_status_stop_and_logs(tmp_path): fake_bin = tmp_path / "llama-server" fake_bin.write_text( textwrap.dedent( """\ #!/usr/bin/env bash echo "fake llama-server $*" >&2 trap 'exit 0' TERM INT while true; do sleep 1; done """ ) ) fake_bin.chmod(0o755) pid_file = tmp_path / "llama.pid" log_file = tmp_path / "llama.log" env = { **os.environ, "DUCK_LLAMA_SERVER_BIN": str(fake_bin), "DUCK_MAIN_MODEL_PATH": str(tmp_path / "model.gguf"), "DUCK_LLAMA_PID_FILE": str(pid_file), "DUCK_LLAMA_LOG_FILE": str(log_file), "DUCK_MAIN_PORT": "18081", } Path(env["DUCK_MAIN_MODEL_PATH"]).write_text("fake") script = "scripts/llama/start_main.sh" stopped = subprocess.run([script, "status"], env=env, text=True, capture_output=True) assert stopped.returncode == 3 assert "not running" in stopped.stdout started = subprocess.run([script, "start"], env=env, text=True, capture_output=True) assert started.returncode == 0 assert pid_file.exists() try: running = subprocess.run([script, "status"], env=env, text=True, capture_output=True) assert running.returncode == 0 assert "running" in running.stdout time.sleep(0.2) logs = subprocess.run( [script, "logs", "--lines", "20"], env=env, text=True, capture_output=True ) assert logs.returncode == 0 assert "--alias local-main" in logs.stdout finally: stopped = subprocess.run([script, "stop"], env=env, text=True, capture_output=True) assert stopped.returncode == 0 assert not pid_file.exists()