Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions bin/doctor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@ bb_init_paths

BAUDBOT_ROOT="${BAUDBOT_ROOT:-$(cd "$SCRIPT_DIR/.." && pwd)}"

for arg in "$@"; do
case "$arg" in
-h|--help)
echo "Usage: baudbot doctor"
exit 0
;;
esac
done
if bb_has_arg "--help" "$@" || bb_has_arg "-h" "$@"; then
echo "Usage: baudbot doctor"
exit 0
fi

doctor_init_counters
IS_ROOT=0
Expand Down
29 changes: 29 additions & 0 deletions bin/lib/check-report-common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash
# Shared counter helpers for check/report style shell scripts.

bb_counter_reset_many() {
local counter_name
for counter_name in "$@"; do
local -n counter_ref="$counter_name"
counter_ref=0
done
}

bb_counter_inc() {
local counter_name="$1"
local -n counter_ref="$counter_name"
counter_ref=$((counter_ref + 1))
}

bb_summary_print_header() {
echo "Summary"
echo "───────"
}

bb_summary_print_item() {
local icon="$1"
local label="$2"
local value="$3"

printf " %s %-9s %s\n" "$icon" "$label:" "$value"
}
86 changes: 86 additions & 0 deletions bin/lib/check-report-common.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/bin/bash
# Tests for bin/lib/check-report-common.sh

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# shellcheck source=bin/lib/check-report-common.sh
source "$SCRIPT_DIR/check-report-common.sh"

TOTAL=0
PASSED=0
FAILED=0

run_test() {
local name="$1"
shift
local out

TOTAL=$((TOTAL + 1))
printf " %-45s " "$name"

out="$(mktemp /tmp/baudbot-check-report-common-test-output.XXXXXX)"
if "$@" >"$out" 2>&1; then
echo "✓"
PASSED=$((PASSED + 1))
else
echo "✗ FAILED"
tail -40 "$out" | sed 's/^/ /'
FAILED=$((FAILED + 1))
fi
rm -f "$out"
}

test_reset_many_sets_zero() {
(
set -euo pipefail
a=7
b=3
c=9
bb_counter_reset_many a b c
[ "$a" -eq 0 ]
[ "$b" -eq 0 ]
[ "$c" -eq 0 ]
)
}

test_inc_increments_named_counter() {
(
set -euo pipefail
value=0
bb_counter_inc value
bb_counter_inc value
[ "$value" -eq 2 ]
)
}

test_summary_helpers_render_rows() {
(
set -euo pipefail
local out
out="$(mktemp /tmp/check-report-summary.XXXXXX)"
trap 'rm -f "$out"' EXIT

{
bb_summary_print_header
bb_summary_print_item "✅" "Pass" "3"
} >"$out"

grep -q '^Summary$' "$out"
grep -q '✅ Pass:' "$out"
)
}

echo "=== check-report-common tests ==="
echo ""

run_test "reset many sets counters to zero" test_reset_many_sets_zero
run_test "increment updates named counter" test_inc_increments_named_counter
run_test "summary helpers render rows" test_summary_helpers_render_rows

echo ""
echo "=== $PASSED/$TOTAL passed, $FAILED failed ==="

if [ "$FAILED" -gt 0 ]; then
exit 1
fi
14 changes: 8 additions & 6 deletions bin/lib/doctor-common.sh
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
#!/bin/bash
# Shared helpers for bin/doctor.sh

BB_DOCTOR_COMMON_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=bin/lib/check-report-common.sh
source "$BB_DOCTOR_COMMON_DIR/check-report-common.sh"

doctor_init_counters() {
PASS=0
FAIL=0
WARN=0
bb_counter_reset_many PASS FAIL WARN
}

doctor_pass() {
echo " ✓ $1"
PASS=$((PASS + 1))
bb_counter_inc PASS
}

doctor_fail() {
echo " ✗ $1"
FAIL=$((FAIL + 1))
bb_counter_inc FAIL
}

doctor_warn() {
echo " ⚠ $1"
WARN=$((WARN + 1))
bb_counter_inc WARN
}

doctor_summary_and_exit() {
Expand Down
76 changes: 76 additions & 0 deletions bin/lib/release-runtime-common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/bin/bash
# Shared runtime helpers for release update/rollback scripts.
#
# Expects caller to provide:
# - log() and die() functions
# - restart_baudbot_service_if_active()
# - json_get_string_stdin()
# - BAUDBOT_AGENT_USER and BAUDBOT_AGENT_HOME

bb_run_release_override_cmd() {
local description="$1"
local command="$2"
local env_array_name="$3"

[ -n "$command" ] || return 1

local -n env_ref="$env_array_name"
log "running $description override"
env "${env_ref[@]}" bash -lc "$command"
}

bb_run_release_restart_and_health() {
local restart_cmd="$1"
local skip_restart="$2"
local health_cmd="$3"
local env_array_name="$4"

if [ -n "$restart_cmd" ]; then
bb_run_release_override_cmd "restart" "$restart_cmd" "$env_array_name"
elif [ "$skip_restart" = "1" ]; then
log "skipping restart"
else
restart_baudbot_service_if_active
fi

if [ -n "$health_cmd" ]; then
bb_run_release_override_cmd "health" "$health_cmd" "$env_array_name"
fi
Comment thread
sentry[bot] marked this conversation as resolved.
}

bb_verify_deployed_release_sha() {
local expected_sha="$1"
local skip_version_check="$2"
local verified_label="${3:-}"

if [ "$skip_version_check" = "1" ]; then
return 0
fi

if [ "$(id -u)" -ne 0 ]; then
log "non-root run: skipping deployed version verification"
return 0
fi

if ! id "$BAUDBOT_AGENT_USER" >/dev/null 2>&1; then
log "agent user '$BAUDBOT_AGENT_USER' missing; skipping deployed version verification"
return 0
fi

local version_file="$BAUDBOT_AGENT_HOME/.pi/agent/baudbot-version.json"
local deployed_sha

deployed_sha="$(sudo -u "$BAUDBOT_AGENT_USER" sh -c "cat '$version_file' 2>/dev/null" | json_get_string_stdin "sha" 2>/dev/null || true)"

if [ -z "$deployed_sha" ]; then
die "deployed version file missing or unreadable: $version_file"
fi

if [ "$deployed_sha" != "$expected_sha" ]; then
die "deployed sha mismatch (expected $expected_sha, got $deployed_sha)"
fi

if [ -n "$verified_label" ]; then
log "deployed version verified: $verified_label"
fi
}
Comment on lines +1 to +76
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding unit tests for release-runtime-common.sh. These functions contain critical release logic (restart, health checks, version verification) that would benefit from test coverage.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Prompt To Fix With AI
This is a comment left during a code review.
Path: bin/lib/release-runtime-common.sh
Line: 1-74

Comment:
Consider adding unit tests for `release-runtime-common.sh`. These functions contain critical release logic (restart, health checks, version verification) that would benefit from test coverage.

<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>

How can I resolve this? If you propose a fix, please make it concise.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added coverage in a new bin/lib/release-runtime-common.test.sh and wired it into test/shell-scripts.test.mjs.

The tests cover restart/health override behavior, including failure propagation and default restart fallback behavior when no override is provided.

Responded by dev-agent using openai/gpt-5.

128 changes: 128 additions & 0 deletions bin/lib/release-runtime-common.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#!/bin/bash
# Tests for bin/lib/release-runtime-common.sh

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# shellcheck source=bin/lib/release-runtime-common.sh
source "$SCRIPT_DIR/release-runtime-common.sh"

TOTAL=0
PASSED=0
FAILED=0

run_test() {
local name="$1"
shift
local out

TOTAL=$((TOTAL + 1))
printf " %-45s " "$name"

out="$(mktemp /tmp/baudbot-release-runtime-common-test-output.XXXXXX)"
if "$@" >"$out" 2>&1; then
echo "✓"
PASSED=$((PASSED + 1))
else
echo "✗ FAILED"
tail -40 "$out" | sed 's/^/ /'
FAILED=$((FAILED + 1))
fi
rm -f "$out"
}

TEST_LOGS=()
RESTART_CALLS=0

log() {
TEST_LOGS+=("$*")
}

die() {
echo "die:$*" >&2
exit 1
}

restart_baudbot_service_if_active() {
RESTART_CALLS=$((RESTART_CALLS + 1))
}

json_get_string_stdin() {
local _key="$1"
cat
}

reset_state() {
TEST_LOGS=()
RESTART_CALLS=0
}

test_restart_override_failure_does_not_fallback() {
(
set -euo pipefail
reset_state
# shellcheck disable=SC2034 # consumed via nameref in bb_run_release_restart_and_health
local hook_env=("X_TEST=1")

set +e
bb_run_release_restart_and_health "false" "0" "" hook_env
rc=$?
set -e

[ "$rc" -ne 0 ]
[ "$RESTART_CALLS" -eq 0 ]
)
}

test_no_restart_override_uses_default_restart() {
(
set -euo pipefail
reset_state
local hook_env=()

bb_run_release_restart_and_health "" "0" "" hook_env
[ "$RESTART_CALLS" -eq 1 ]
)
}

test_skip_restart_logs_and_does_not_restart() {
(
set -euo pipefail
reset_state
local hook_env=()

bb_run_release_restart_and_health "" "1" "" hook_env
[ "$RESTART_CALLS" -eq 0 ]
)
}

test_health_override_failure_propagates() {
(
set -euo pipefail
reset_state
# shellcheck disable=SC2034 # consumed via nameref in bb_run_release_restart_and_health
local hook_env=("X_TEST=1")

set +e
bb_run_release_restart_and_health "" "1" "false" hook_env
rc=$?
set -e

[ "$rc" -ne 0 ]
)
}

echo "=== release-runtime-common tests ==="
echo ""

run_test "restart override failure does not fallback" test_restart_override_failure_does_not_fallback
run_test "default restart runs without override" test_no_restart_override_uses_default_restart
run_test "skip restart avoids default restart" test_skip_restart_logs_and_does_not_restart
run_test "health override failure propagates" test_health_override_failure_propagates

echo ""
echo "=== $PASSED/$TOTAL passed, $FAILED failed ==="

if [ "$FAILED" -gt 0 ]; then
exit 1
fi
Loading