|
| 1 | +#!/bin/bash |
| 2 | +# Tests for the Warp Claude Code plugin hook scripts. |
| 3 | +# |
| 4 | +# Validates that each hook script produces correctly structured JSON payloads |
| 5 | +# by piping mock Claude Code hook input into the scripts and checking the output. |
| 6 | +# |
| 7 | +# Usage: ./tests/test-hooks.sh |
| 8 | +# |
| 9 | +# Since the hook scripts write OSC sequences to /dev/tty (not stdout), |
| 10 | +# we test build-payload.sh directly — it's the shared JSON construction logic |
| 11 | +# that all hook scripts use. |
| 12 | + |
| 13 | +set -uo pipefail |
| 14 | + |
| 15 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../scripts" && pwd)" |
| 16 | +source "$SCRIPT_DIR/build-payload.sh" |
| 17 | + |
| 18 | +PASSED=0 |
| 19 | +FAILED=0 |
| 20 | + |
| 21 | +# --- Test helpers --- |
| 22 | + |
| 23 | +assert_eq() { |
| 24 | + local test_name="$1" |
| 25 | + local expected="$2" |
| 26 | + local actual="$3" |
| 27 | + if [ "$expected" = "$actual" ]; then |
| 28 | + echo " ✓ $test_name" |
| 29 | + PASSED=$((PASSED + 1)) |
| 30 | + else |
| 31 | + echo " ✗ $test_name" |
| 32 | + echo " expected: $expected" |
| 33 | + echo " actual: $actual" |
| 34 | + FAILED=$((FAILED + 1)) |
| 35 | + fi |
| 36 | +} |
| 37 | + |
| 38 | +assert_json_field() { |
| 39 | + local test_name="$1" |
| 40 | + local json="$2" |
| 41 | + local field="$3" |
| 42 | + local expected="$4" |
| 43 | + local actual |
| 44 | + actual=$(echo "$json" | jq -r "$field" 2>/dev/null) |
| 45 | + assert_eq "$test_name" "$expected" "$actual" |
| 46 | +} |
| 47 | + |
| 48 | +# --- Tests --- |
| 49 | + |
| 50 | +echo "=== build-payload.sh ===" |
| 51 | + |
| 52 | +echo "" |
| 53 | +echo "--- Common fields ---" |
| 54 | +PAYLOAD=$(build_payload '{"session_id":"sess-123","cwd":"/Users/alice/my-project"}' "stop") |
| 55 | +assert_json_field "v is 1" "$PAYLOAD" ".v" "1" |
| 56 | +assert_json_field "agent is claude" "$PAYLOAD" ".agent" "claude" |
| 57 | +assert_json_field "event is stop" "$PAYLOAD" ".event" "stop" |
| 58 | +assert_json_field "session_id extracted" "$PAYLOAD" ".session_id" "sess-123" |
| 59 | +assert_json_field "cwd extracted" "$PAYLOAD" ".cwd" "/Users/alice/my-project" |
| 60 | +assert_json_field "project is basename of cwd" "$PAYLOAD" ".project" "my-project" |
| 61 | + |
| 62 | +echo "" |
| 63 | +echo "--- Common fields with missing data ---" |
| 64 | +PAYLOAD=$(build_payload '{}' "stop") |
| 65 | +assert_json_field "empty session_id" "$PAYLOAD" ".session_id" "" |
| 66 | +assert_json_field "empty cwd" "$PAYLOAD" ".cwd" "" |
| 67 | +assert_json_field "empty project" "$PAYLOAD" ".project" "" |
| 68 | + |
| 69 | +echo "" |
| 70 | +echo "--- Extra args are merged ---" |
| 71 | +PAYLOAD=$(build_payload '{"session_id":"s1","cwd":"/tmp/proj"}' "stop" \ |
| 72 | + --arg query "hello" \ |
| 73 | + --arg response "world") |
| 74 | +assert_json_field "query merged" "$PAYLOAD" ".query" "hello" |
| 75 | +assert_json_field "response merged" "$PAYLOAD" ".response" "world" |
| 76 | +assert_json_field "common fields still present" "$PAYLOAD" ".session_id" "s1" |
| 77 | + |
| 78 | +echo "" |
| 79 | +echo "--- Stop event ---" |
| 80 | +PAYLOAD=$(build_payload '{"session_id":"s1","cwd":"/tmp/proj"}' "stop" \ |
| 81 | + --arg query "write a haiku" \ |
| 82 | + --arg response "Memory is safe, the borrow checker stands guard" \ |
| 83 | + --arg transcript_path "/tmp/transcript.jsonl") |
| 84 | +assert_json_field "event is stop" "$PAYLOAD" ".event" "stop" |
| 85 | +assert_json_field "query present" "$PAYLOAD" ".query" "write a haiku" |
| 86 | +assert_json_field "response present" "$PAYLOAD" ".response" "Memory is safe, the borrow checker stands guard" |
| 87 | +assert_json_field "transcript_path present" "$PAYLOAD" ".transcript_path" "/tmp/transcript.jsonl" |
| 88 | + |
| 89 | +echo "" |
| 90 | +echo "--- Permission request event ---" |
| 91 | +PAYLOAD=$(build_payload '{"session_id":"s1","cwd":"/tmp/proj"}' "permission_request" \ |
| 92 | + --arg summary "Wants to run Bash: rm -rf /tmp" \ |
| 93 | + --arg tool_name "Bash" \ |
| 94 | + --argjson tool_input '{"command":"rm -rf /tmp"}') |
| 95 | +assert_json_field "event is permission_request" "$PAYLOAD" ".event" "permission_request" |
| 96 | +assert_json_field "summary present" "$PAYLOAD" ".summary" "Wants to run Bash: rm -rf /tmp" |
| 97 | +assert_json_field "tool_name present" "$PAYLOAD" ".tool_name" "Bash" |
| 98 | +assert_json_field "tool_input.command present" "$PAYLOAD" ".tool_input.command" "rm -rf /tmp" |
| 99 | + |
| 100 | +echo "" |
| 101 | +echo "--- Idle prompt event ---" |
| 102 | +PAYLOAD=$(build_payload '{"session_id":"s1","cwd":"/tmp/proj","notification_type":"idle_prompt"}' "idle_prompt" \ |
| 103 | + --arg summary "Claude is waiting for your input") |
| 104 | +assert_json_field "event is idle_prompt" "$PAYLOAD" ".event" "idle_prompt" |
| 105 | +assert_json_field "summary present" "$PAYLOAD" ".summary" "Claude is waiting for your input" |
| 106 | + |
| 107 | +echo "" |
| 108 | +echo "--- JSON special characters in values ---" |
| 109 | +PAYLOAD=$(build_payload '{"session_id":"s1","cwd":"/tmp/proj"}' "stop" \ |
| 110 | + --arg query 'what does "hello world" mean?' \ |
| 111 | + --arg response 'It means greeting. Use: printf("hello")') |
| 112 | +assert_json_field "quotes in query preserved" "$PAYLOAD" ".query" 'what does "hello world" mean?' |
| 113 | +assert_json_field "parens in response preserved" "$PAYLOAD" ".response" 'It means greeting. Use: printf("hello")' |
| 114 | + |
| 115 | +# --- Summary --- |
| 116 | + |
| 117 | +echo "" |
| 118 | +echo "=== Results: $PASSED passed, $FAILED failed ===" |
| 119 | + |
| 120 | +if [ "$FAILED" -gt 0 ]; then |
| 121 | + exit 1 |
| 122 | +fi |
0 commit comments