diff --git a/tools/test-update-check.sh b/tools/test-update-check.sh new file mode 100755 index 0000000..7beb895 --- /dev/null +++ b/tools/test-update-check.sh @@ -0,0 +1,320 @@ +#!/usr/bin/env bash +# Test for hooks/flight-deck-update-check.sh. +# +# Hermetic and side-effect free: every case runs with HOME pointed at a +# throwaway fixture under $TMPDIR and a stub curl ahead of it on PATH, so no +# case reads the real config directory, writes the real daily cache, touches +# the network, or spends GitHub rate limit. The fixture is what makes the +# hook testable at all: it reads exactly two things from its environment, +# $HOME (for VERSION and the cache file) and curl on PATH, and both are +# substitutable. +# +# This covers the part tools/doctor.sh deliberately cannot. doctor's +# hook-execution check (section 3c) runs every hook with curl stripped from +# PATH on purpose, precisely so this hook exits on its documented fail-open +# guard before reaching its cache write; that keeps doctor read-only, and it +# also means doctor only ever executes this hook's first few lines. The +# version comparison, the cache logic, and the JSON emission below are never +# reached there. +# +# Requires jq (the hook itself needs jq and fails open without it, so every +# case would read "quiet" and hide real regressions; skipping beats reporting +# misleading passes). +# +# Usage: bash tools/test-update-check.sh + +set -uo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +HOOK="$(cd "$SCRIPT_DIR/.." && pwd)/hooks/flight-deck-update-check.sh" + +if ! command -v jq >/dev/null 2>&1; then + echo "SKIP jq not found on PATH: hooks/flight-deck-update-check.sh fails open without it, so this test cannot exercise it. Install jq to run it." + exit 0 +fi + +if [ ! -x "$HOOK" ]; then + echo "FAIL hook not found or not executable: $HOOK" + exit 1 +fi + +pass=0 +fail=0 +skipped=0 +xfail=0 + +WORK="$(mktemp -d "${TMPDIR:-/tmp}/update-check-test.XXXXXX")" || { + echo "FAIL could not create a scratch directory under \${TMPDIR:-/tmp}" + exit 1 +} +trap 'rm -rf "$WORK"' EXIT + +# Side-effect witness: the hook's only write is its daily cache under $HOME. +# Record the real one's state now and re-check it at the end, so a fixture +# that silently fails to take effect (an unexported HOME, a hardcoded path +# introduced later) is caught rather than quietly corrupting the developer's +# own install. +REAL_CACHE="${HOME}/.claude/.flight-deck-update-check" +real_cache_before="absent" +[ -e "$REAL_CACHE" ] && real_cache_before="$(cat "$REAL_CACHE" 2>/dev/null)" + +# Absolute path to bash, for every scratch script this test writes. The +# reduced-PATH cases below hand the hook a PATH holding only the tools it +# needs, and bash is not one of them, so a "#!/usr/bin/env bash" shebang on a +# scratch script would fail with "env: 'bash': No such file or directory" the +# moment the hook invoked it. Resolving once, absolutely, keeps those scripts +# runnable whatever PATH they are called under. +BASH_BIN="$(bash -c 'command -v bash' 2>/dev/null || echo /bin/bash)" + +# --------------------------------------------------------------------------- +# Stub curl. Ahead of the real one on PATH, it ignores every argument and +# answers according to $STUB_CURL_MODE, so each case picks the GitHub reply it +# needs without a network round trip. Modes mirror what the API can actually +# do to this hook: a normal release payload, a transport failure, a body that +# is not JSON, valid JSON with no tag_name, and an empty body. +# --------------------------------------------------------------------------- +STUB_BIN="$WORK/stub-bin" +mkdir -p "$STUB_BIN" +printf '#!%s\n' "$BASH_BIN" > "$STUB_BIN/curl" +cat >> "$STUB_BIN/curl" <<'STUB' +case "${STUB_CURL_MODE:-ok}" in + ok) printf '{"tag_name":"v%s"}' "${STUB_CURL_TAG:-1.14.0}" ;; + no_v) printf '{"tag_name":"%s"}' "${STUB_CURL_TAG:-1.14.0}" ;; + malformed) printf 'not json at all' ;; + empty_tag) printf '{"name":"release with no tag_name"}' ;; + empty) : ;; + fail) exit 22 ;; +esac +STUB +chmod +x "$STUB_BIN/curl" + +# A PATH with everything the hook needs plus the stub curl in front. The hook +# shells out to jq, curl, tr, date, cat, sort and tail, so the inherited PATH +# is kept rather than rebuilt: only curl needs controlling here. +STUB_PATH="$STUB_BIN:$PATH" + +# A PATH built from a scratch bin that deliberately omits one tool, for the +# fail-open cases. Everything the hook needs except the named omission. +# +# Each entry is a wrapper script exec'ing the real binary by absolute path, +# not a symlink to it. On Cygwin and MSYS a symlinked binary outside its own +# directory cannot find its DLLs and dies with "error while loading shared +# libraries", which would surface as a hook failure that has nothing to do +# with the hook. A shell wrapper is interpreted by bash and calls the real +# path, so it behaves identically on every platform. +# +# make_reduced_path OMITTED_TOOL -> prints the directory +make_reduced_path() { + local omit="$1" dir tool tool_path + dir="$(mktemp -d "$WORK/reduced.XXXXXX")" || return 1 + for tool in jq curl tr date cat sort tail; do + [ "$tool" = "$omit" ] && continue + # Resolve in a child shell: an interactive setup that shadows one of these + # names with a shell function would make `command -v` here print the + # function name, producing a broken wrapper and a false failure unrelated + # to the hook. + tool_path="$(bash -c "command -v $tool" 2>/dev/null || true)" + [ -z "$tool_path" ] && continue + printf '#!%s\nexec "%s" "$@"\n' "$BASH_BIN" "$tool_path" > "$dir/$tool" + chmod +x "$dir/$tool" + done + # The stub curl still belongs there unless curl is the omitted tool. + if [ "$omit" != "curl" ]; then + printf '#!%s\nexec "%s" "$@"\n' "$BASH_BIN" "$STUB_BIN/curl" > "$dir/curl" + chmod +x "$dir/curl" + fi + printf '%s' "$dir" +} + +# make_home INSTALLED_VERSION [CACHE_CONTENT] -> prints a fixture HOME +# INSTALLED_VERSION of "-" writes no VERSION file at all. +make_home() { + local version="$1" cache="${2:-}" home + home="$(mktemp -d "$WORK/home.XXXXXX")" || return 1 + mkdir -p "$home/.claude/skills/mission-control" + [ "$version" != "-" ] && printf '%s\n' "$version" > "$home/.claude/skills/mission-control/VERSION" + [ -n "$cache" ] && printf '%s\n' "$cache" > "$home/.claude/.flight-deck-update-check" + printf '%s' "$home" +} + +report() { + local ok="$1" name="$2" detail="$3" + if [ "$ok" -eq 1 ]; then + pass=$((pass + 1)) + printf 'PASS %-64s %s\n' "$name" "$detail" + else + fail=$((fail + 1)) + printf 'FAIL %-64s %s\n' "$name" "$detail" + fi +} + +# run_case NAME EXPECT(notify|silent) INSTALLED CACHE MODE TAG [MUST_CONTAIN...] +# +# Runs the hook once against a fresh fixture and asserts, on every case and +# not only the notifying ones: +# - it notified exactly when EXPECT says; +# - it exited 0, always: the fail-open contract does not bend; +# - stderr stayed empty: a hook that prints shell noise on a session start +# is a defect even when it exits 0; +# - when it notified, the payload is valid JSON carrying +# hookSpecificOutput.hookEventName == "SessionStart". +# MUST_CONTAIN arguments are substrings asserted against additionalContext, +# to prove the right notice fired and not merely "a" notice. +run_case() { + local name="$1" expect="$2" installed="$3" cache="$4" mode="$5" tag="$6" + shift 6 + + local home err_file raw_out exit_code notified ctx event ok reason needle + home="$(make_home "$installed" "$cache")" + err_file="$WORK/stderr.case" + + raw_out=$(HOME="$home" PATH="$STUB_PATH" STUB_CURL_MODE="$mode" STUB_CURL_TAG="$tag" \ + "$HOOK" 2>"$err_file") + exit_code=$? + + notified="silent" + [ -n "$raw_out" ] && notified="notify" + + ok=1 + reason="" + + [ "$notified" != "$expect" ] && { ok=0; reason="got=$notified expected=$expect"; } + [ "$exit_code" -ne 0 ] && { ok=0; reason="$reason exit=$exit_code (expected 0)"; } + [ -s "$err_file" ] && { ok=0; reason="$reason stderr not empty: [$(tr '\n' ' ' < "$err_file")]"; } + + if [ "$notified" = "notify" ]; then + event=$(printf '%s' "$raw_out" | jq -r '.hookSpecificOutput.hookEventName // empty' 2>/dev/null) + ctx=$(printf '%s' "$raw_out" | jq -r '.hookSpecificOutput.additionalContext // empty' 2>/dev/null) + [ "$event" != "SessionStart" ] && { ok=0; reason="$reason hookEventName=[$event] expected SessionStart"; } + [ -z "$ctx" ] && { ok=0; reason="$reason additionalContext missing or empty"; } + for needle in "$@"; do + printf '%s' "$ctx" | grep -qF -- "$needle" || { ok=0; reason="$reason missing substring: $needle"; } + done + fi + + rm -f "$err_file" + report "$ok" "$name" "${reason:-got=$notified exit=$exit_code}" +} + +echo "== Notification logic (stub GitHub returns v1.14.0 unless noted) ==" +run_case "installed 1.13.0, no cache -> notifies, names both versions" \ + notify "1.13.0" "" ok "1.14.0" "1.14.0" "1.13.0" +run_case "installed 1.14.0 (up to date) -> silent" \ + silent "1.14.0" "" ok "1.14.0" +run_case "installed 1.15.0 (ahead of the latest release) -> silent" \ + silent "1.15.0" "" ok "1.14.0" +run_case "installed 1.9.0 vs 1.14.0 -> notifies (version sort, not lexical)" \ + notify "1.9.0" "" ok "1.14.0" "1.14.0" +run_case "installed 1.14.0 vs 1.9.0 latest -> silent (version sort, not lexical)" \ + silent "1.14.0" "" ok "1.9.0" +run_case "tag without the v prefix -> still compared correctly" \ + notify "1.13.0" "" no_v "1.14.0" "1.14.0" +run_case "VERSION with surrounding whitespace -> trimmed, silent when equal" \ + silent " 1.14.0 " "" ok "1.14.0" + +echo +echo "== Fail-open contract: exit 0, no stdout, no stderr ==" +run_case "no VERSION file -> silent" \ + silent "-" "" ok "1.14.0" +run_case "empty VERSION file -> silent" \ + silent "" "" ok "1.14.0" +run_case "curl transport failure -> silent" \ + silent "1.13.0" "" fail "1.14.0" +run_case "malformed JSON body -> silent" \ + silent "1.13.0" "" malformed "1.14.0" +run_case "valid JSON with no tag_name -> silent" \ + silent "1.13.0" "" empty_tag "1.14.0" +run_case "empty response body -> silent" \ + silent "1.13.0" "" empty "1.14.0" +run_case "corrupt cache (non-numeric) -> treated as stale, notifies" \ + notify "1.13.0" "not-a-timestamp" ok "1.14.0" "1.14.0" + +echo +echo "== Missing dependencies: fails open rather than erroring the session ==" +missing_dep_case() { + local name="$1" omit="$2" home reduced raw_out exit_code err_file ok reason + home="$(make_home "1.13.0")" + reduced="$(make_reduced_path "$omit")" + err_file="$WORK/stderr.dep" + + raw_out=$(HOME="$home" PATH="$reduced" STUB_CURL_MODE=ok STUB_CURL_TAG=1.14.0 \ + "$HOOK" 2>"$err_file") + exit_code=$? + + ok=1 + reason="" + [ -n "$raw_out" ] && { ok=0; reason="expected silence, got output: $raw_out"; } + [ "$exit_code" -ne 0 ] && { ok=0; reason="$reason exit=$exit_code (expected 0)"; } + [ -s "$err_file" ] && { ok=0; reason="$reason stderr not empty: [$(tr '\n' ' ' < "$err_file")]"; } + [ -e "$home/.claude/.flight-deck-update-check" ] && { ok=0; reason="$reason wrote a cache file despite bailing early"; } + + rm -f "$err_file" + report "$ok" "$name" "${reason:-exit=$exit_code, silent, no cache written}" +} + +missing_dep_case "no curl on PATH -> silent, exit 0, no cache written" curl +if [ -x /opt/homebrew/bin/jq ]; then + skipped=$((skipped + 1)) + printf 'SKIP %-64s %s\n' "no jq on PATH -> silent, exit 0" \ + "/opt/homebrew/bin/jq exists; the hook's documented fallback finds it, so PATH alone cannot hide jq here" +else + missing_dep_case "no jq on PATH -> silent, exit 0, no cache written" jq +fi + +echo +echo "== Cache behaviour ==" +# Today: a fresh cache short-circuits before the version comparison, so an +# install that is behind stays silent for up to 24h, and whichever session +# start happens to win the daily race is the only one that ever hears about +# it. That is issue #2. This case asserts today's behaviour and says so +# loudly if it changes, so the fix flips it deliberately rather than +# silently. +cache_home="$(make_home "1.13.0" "$(date +%s)")" +cache_out=$(HOME="$cache_home" PATH="$STUB_PATH" STUB_CURL_MODE=ok STUB_CURL_TAG=1.14.0 "$HOOK" 2>/dev/null) +if [ -z "$cache_out" ]; then + xfail=$((xfail + 1)) + printf 'XFAIL %-64s %s\n' "fresh cache, install behind -> silent" \ + "known bug #2: the daily network throttle also gates the notice" +else + printf 'XPASS %-64s %s\n' "fresh cache, install behind -> notifies" \ + "#2 appears fixed: turn this case into a plain run_case expecting notify" +fi + +# The cache write itself: after a completed check the timestamp must land in +# the fixture, and nowhere else. +write_home="$(make_home "1.13.0")" +HOME="$write_home" PATH="$STUB_PATH" STUB_CURL_MODE=ok STUB_CURL_TAG=1.14.0 "$HOOK" >/dev/null 2>&1 +written="$write_home/.claude/.flight-deck-update-check" +if [ -s "$written" ] && [ -z "$(tr -d '0-9 \n' < "$written")" ]; then + report 1 "completed check writes a numeric timestamp into the fixture cache" "content=[$(cat "$written")]" +else + report 0 "completed check writes a numeric timestamp into the fixture cache" "content=[$(cat "$written" 2>/dev/null)]" +fi + +echo +echo "== Malformed stdin: SessionStart hooks receive JSON they must not choke on ==" +stdin_home="$(make_home "1.14.0")" +stdin_out=$(printf 'not json at all' | HOME="$stdin_home" PATH="$STUB_PATH" STUB_CURL_MODE=ok "$HOOK" 2>&1) +stdin_ec=$? +if [ -z "$stdin_out" ] && [ "$stdin_ec" -eq 0 ]; then + report 1 "garbage on stdin -> silent, exit 0" "exit=$stdin_ec" +else + report 0 "garbage on stdin -> silent, exit 0" "out=[$stdin_out] exit=$stdin_ec" +fi + +echo +echo "== Side effects: the real install must be untouched ==" +real_cache_after="absent" +[ -e "$REAL_CACHE" ] && real_cache_after="$(cat "$REAL_CACHE" 2>/dev/null)" +if [ "$real_cache_before" = "$real_cache_after" ]; then + report 1 "real ~/.claude cache unchanged by this run" "state=$real_cache_after" +else + report 0 "real ~/.claude cache unchanged by this run" "before=[$real_cache_before] after=[$real_cache_after]" +fi + +echo +summary="Results: $pass passed, $fail failed" +[ "$xfail" -gt 0 ] && summary="$summary, $xfail expected-fail (issue #2)" +[ "$skipped" -gt 0 ] && summary="$summary, $skipped skipped" +echo "$summary" +[ "$fail" -eq 0 ]