#!/usr/bin/env bash # Tests for plugins/rtk/.apm/hooks/route-through-rtk.sh, the PreToolUse hook # that routes a Bash tool call through `rtk hook claude` (#179). # # The hook acts only when run from a .claude/hooks/ path, so it is copied to the # path apm deploys it to and driven with real payloads, against a stub rtk, no # rtk at all, and the real rtk when one is installed. set -euo pipefail REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" HOOKS_DIR="$REPO_ROOT/plugins/rtk/.apm/hooks" SRC="$HOOKS_DIR/route-through-rtk.sh" PASS=0 FAIL=0 pass() { PASS=$((PASS + 1)); } fail() { echo " FAIL: $1"; FAIL=$((FAIL + 1)); } [[ -f "$SRC" ]] || { echo "FAIL: $SRC does not exist"; exit 1; } REAL_RTK="$(command -v rtk || true)" TIMEOUT="$(command -v timeout || true)" [[ -n "$TIMEOUT" ]] || { echo "FAIL: timeout(1) is not on PATH; install coreutils to run these tests"; exit 1; } WORK="$(mktemp -d)" trap 'rm -rf "$WORK"' EXIT for d in .claude/hooks/rtk/.apm/hooks elsewhere; do mkdir -p "$WORK/$d" cp "$SRC" "$WORK/$d/" done HOOK="$WORK/.claude/hooks/rtk/.apm/hooks/route-through-rtk.sh" # A PATH with only what the hook and the stub need: no rtk on it. mkdir -p "$WORK/base-bin" for tool in bash cat dirname; do ln -s "$(command -v "$tool")" "$WORK/base-bin/$tool" done # A stub rtk that records its arguments and stdin and prints a marker. mkdir -p "$WORK/stub-bin" cat > "$WORK/stub-bin/rtk" << STUB #!/usr/bin/env bash printf '%s\n' "\$*" > "$WORK/stub-args" cat > "$WORK/stub-stdin" echo stub-output STUB chmod +x "$WORK/stub-bin/rtk" payload() { printf '{"tool_name":"Bash","tool_input":{"command":"%s"}}' "$1" } # run : sets OUT and RC. run() { RC=0 OUT="$(payload "$3" | PATH="$2" "$TIMEOUT" 10 "$1" 2> "$WORK/stderr")" || RC=$? } echo "== Stub rtk receives 'hook claude' and the payload" rm -f "$WORK/stub-args" "$WORK/stub-stdin" run "$HOOK" "$WORK/stub-bin:$WORK/base-bin" "git status" if [[ $RC -eq 0 && "$OUT" == stub-output ]]; then pass; else fail "want rc 0 and stub output, got $RC: $OUT"; fi if [[ "$(cat "$WORK/stub-args" 2> /dev/null)" == "hook claude" ]]; then pass; else fail "stub args: $(cat "$WORK/stub-args" 2> /dev/null)"; fi if [[ "$(cat "$WORK/stub-stdin" 2> /dev/null)" == "$(payload "git status")" ]]; then pass; else fail "stub stdin: $(cat "$WORK/stub-stdin" 2> /dev/null)"; fi # stub_rtk : writes a stub rtk with to $WORK//rtk. stub_rtk() { mkdir -p "$WORK/$1" printf '#!/usr/bin/env bash\ncat > /dev/null\n%s\n' "$2" > "$WORK/$1/rtk" chmod +x "$WORK/$1/rtk" } stub_rtk fail2-bin "echo 'error: unrecognized subcommand' >&2; exit 2" stub_rtk fail1-bin "exit 1" stub_rtk silent-bin "exit 0" echo "== rtk exits 2 (usage error): exit 0, no stdout, never blocks" run "$HOOK" "$WORK/fail2-bin:$WORK/base-bin" "git status" if [[ $RC -eq 0 && -z "$OUT" ]]; then pass; else fail "rtk exit 2: want exit 0 and no stdout, got $RC: $OUT"; fi echo "== rtk exits 1: exit 0, no stdout" run "$HOOK" "$WORK/fail1-bin:$WORK/base-bin" "git status" if [[ $RC -eq 0 && -z "$OUT" ]]; then pass; else fail "rtk exit 1: want exit 0 and no stdout, got $RC: $OUT"; fi echo "== rtk prints nothing: exit 0, no stdout at all (not even a newline)" RC=0 payload "git status" | PATH="$WORK/silent-bin:$WORK/base-bin" "$TIMEOUT" 10 "$HOOK" > "$WORK/raw-out" 2> "$WORK/stderr" || RC=$? if [[ $RC -eq 0 && ! -s "$WORK/raw-out" ]]; then pass; else fail "silent rtk: want exit 0 and zero-byte stdout, got $RC: $(od -c "$WORK/raw-out")"; fi echo "== rtk absent: exit 0, no output" run "$HOOK" "$WORK/base-bin" "git status" if [[ $RC -eq 0 && -z "$OUT" && ! -s "$WORK/stderr" ]]; then pass; else fail "want silent exit 0, got $RC: $OUT $(cat "$WORK/stderr")"; fi echo "== Runtime: acts only from .claude/hooks/" rm -f "$WORK/stub-args" run "$WORK/elsewhere/route-through-rtk.sh" "$WORK/stub-bin:$WORK/base-bin" "git status" if [[ $RC -eq 0 && -z "$OUT" && ! -e "$WORK/stub-args" ]]; then pass; else fail "elsewhere: want silent exit 0, got $RC: $OUT"; fi echo "== Real rtk" if [[ -z "$REAL_RTK" ]]; then echo " SKIP: rtk is not installed; the real-rtk cases did not run" else REAL_PATH="$(dirname "$REAL_RTK"):$WORK/base-bin" run "$HOOK" "$REAL_PATH" "git status" if [[ $RC -eq 0 && "$OUT" == *'"command":"rtk git status"'* ]]; then pass; else fail "git status: want rewrite, got $RC: $OUT"; fi # Depends on rtk's built-in rules as of 0.42.4: `echo` has no rewrite rule, # so rtk passes it through with no output. A later rtk may add one. run "$HOOK" "$REAL_PATH" "echo hello" if [[ $RC -eq 0 && -z "$OUT" ]]; then pass; else fail "echo hello: want no output, got $RC: $OUT"; fi # Backs the README's "running both hooks is harmless": rtk leaves a command # that already starts with rtk alone, so a second hook adds nothing. run "$HOOK" "$REAL_PATH" "rtk git status" if [[ $RC -eq 0 && -z "$OUT" ]]; then pass; else fail "rtk git status: want no output, got $RC: $OUT"; fi fi echo "== hooks.json" if python3 - "$HOOKS_DIR/hooks.json" << 'PY' import json, sys (entry,) = json.load(open(sys.argv[1]))["hooks"]["PreToolUse"] (hook,) = entry["hooks"] assert entry["matcher"] == "Bash" assert hook["command"] == "${CLAUDE_PLUGIN_ROOT}/.apm/hooks/route-through-rtk.sh" PY then pass; else fail "hooks.json does not wire the script as expected"; fi echo echo "Passed: $PASS Failed: $FAIL" [[ $FAIL -eq 0 && $PASS -gt 0 ]]