#!/usr/bin/env bash set -euo pipefail SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) HELPER_PATH=$(cd "$SCRIPT_DIR/.." && pwd)/run-review.sh fail() { echo "FAIL: $*" >&2 exit 1 } assert_file_contains() { local file=$1 local pattern=$2 if ! rg -q --fixed-strings "$pattern" "$file"; then echo "Expected pattern not found: $pattern" >&2 echo "--- $file ---" >&2 sed -n '1,200p' "$file" >&2 || true fail "missing pattern in $file" fi } assert_exit_code() { local actual=$1 local expected=$2 if [[ "$actual" -ne "$expected" ]]; then fail "expected exit code $expected, got $actual" fi } assert_nonzero_exit() { local actual=$1 if [[ "$actual" -eq 0 ]]; then fail "expected non-zero exit code" fi } make_command() { local file=$1 local body=$2 cat >"$file" <&2 exit 7 ' if run_helper "$command_file" "$stdout_file" "$stderr_file" "$status_file" \ --poll-seconds 1 \ --soft-timeout-seconds 5 \ --stall-warning-seconds 3 \ --hard-timeout-seconds 10; then local exit_code=0 else local exit_code=$? fi assert_nonzero_exit "$exit_code" assert_file_contains "$stderr_file" "boom" assert_file_contains "$status_file" "state=failed" } test_empty_output_is_terminal() { local dir=$1 local command_file=$dir/empty-output.sh local stdout_file=$dir/empty-output.stdout local stderr_file=$dir/empty-output.stderr local status_file=$dir/empty-output.status make_command "$command_file" ' sleep 1 exit 0 ' if run_helper "$command_file" "$stdout_file" "$stderr_file" "$status_file" \ --poll-seconds 1 \ --soft-timeout-seconds 5 \ --stall-warning-seconds 3 \ --hard-timeout-seconds 10; then local exit_code=0 else local exit_code=$? fi assert_nonzero_exit "$exit_code" assert_file_contains "$status_file" "state=completed-empty-output" } test_signal_cleanup() { local dir=$1 local command_file=$dir/signal-child.sh local stdout_file=$dir/signal-child.stdout local stderr_file=$dir/signal-child.stderr local status_file=$dir/signal-child.status local child_pid_file=$dir/child.pid make_command "$command_file" " printf '%s\n' \"\$\$\" > \"$child_pid_file\" sleep 30 " set +e "$HELPER_PATH" \ --command-file "$command_file" \ --stdout-file "$stdout_file" \ --stderr-file "$stderr_file" \ --status-file "$status_file" \ --poll-seconds 1 \ --soft-timeout-seconds 5 \ --stall-warning-seconds 2 \ --hard-timeout-seconds 10 & local helper_pid=$! set -e sleep 2 kill -TERM "$helper_pid" set +e wait "$helper_pid" local exit_code=$? set -e assert_nonzero_exit "$exit_code" [[ -f "$child_pid_file" ]] || fail "child pid file was not written" local child_pid child_pid=$(cat "$child_pid_file") sleep 1 if kill -0 "$child_pid" 2>/dev/null; then fail "child process is still alive after helper termination" fi } main() { [[ -x "$HELPER_PATH" ]] || fail "helper is not executable: $HELPER_PATH" local tmp_dir tmp_dir=$(mktemp -d) trap "rm -rf '$tmp_dir'" EXIT test_delayed_success "$tmp_dir" test_soft_timeout_continues "$tmp_dir" test_nonzero_failure "$tmp_dir" test_empty_output_is_terminal "$tmp_dir" test_signal_cleanup "$tmp_dir" echo "PASS: reviewer runtime smoke tests" } main "$@"