Sub topic 11
Testing a hook with sample events
This chapter assumes you can read basic shell and Python, know what JSON is, and have a hook script like the guard.py from the earlier chapters. It shows how to test that script without running an agent.
Why test without an agent
A hook is a small program. It reads one JSON event from stdin and answers with an exit code and a message on stderr. Because that contract is simple, you can feed it sample events yourself. This is faster than asking an agent to run a risky command, and it is safer. A failing hook might let a real rm -rf / through.
The basic test
Pipe a sample event into the script, then print the exit code with $?.
echo '{"tool_name":"shell","tool_input":{"command":"rm -rf /"}}' | python3 guard.py
echo "exit code: $?"
echo '{"tool_name":"shell","tool_input":{"command":"ls -la"}}' | python3 guard.py
echo "exit code: $?"The first command should print a "Blocked" message and exit code: 2. The second should print nothing and exit code: 0. Check both results. A hook that blocks everything passes the first test and fails the second.
What to cover
Write at least one event for each of these cases:
| Case | Example command | Expected exit code |
|---|---|---|
| Clearly dangerous | `rm -rf /` | 2 |
| Harmless | `ls -la` | 0 |
| Variant spelling | `RM -rf /` or `git push --force origin main` | 2 |
| Look-alike that is safe | `git push origin main` | 0 |
| Malformed JSON | `not json` | 2 |
| Missing field | `{"tool_input":{}}` | 2 |
The look-alike row catches patterns that are too broad. The last two rows check that the hook fails closed, as described in the earlier chapter.
Automating the cases
A small Python script runs every case and reports mismatches. It uses only the standard library and assumes guard.py is in the same folder.
import json
import subprocess
import sys
CASES = [
("rm -rf /", 2),
("ls -la", 0),
("git push --force origin main", 2),
("git push origin main", 0),
]
def run_hook(stdin_text):
result = subprocess.run(
[sys.executable, "guard.py"],
input=stdin_text,
capture_output=True,
text=True,
timeout=5,
)
return result.returncode
failures = 0
for command, expected in CASES:
event = {"tool_name": "shell", "tool_input": {"command": command}}
actual = run_hook(json.dumps(event))
status = "ok" if actual == expected else "FAIL"
if actual != expected:
failures += 1
print(f"{status}: {command!r} expected {expected}, got {actual}")
for raw in ["not json", "{}"]:
actual = run_hook(raw)
status = "ok" if actual == 2 else "FAIL"
if actual != 2:
failures += 1
print(f"{status}: input {raw!r} expected 2, got {actual}")
sys.exit(1 if failures else 0)The timeout=5 also catches a hook that hangs, which an agent would treat as a failure.
Common errors
- Testing only the block case. Always include safe commands, or an over-broad pattern goes unnoticed.
- Reading the wrong exit code. In a pipeline,
$?reports the last command. Runpython3 guard.pylast, as shown above. - Wrong field names. Your agent may name fields differently from these samples. Capture a real event from your agent's documentation or logs and copy its shape.
- Hook passes tests but agent still gets through. A unit test proves the script's logic only. Run one real session as a final check.
Exercise
A hook blocks rm -rf / correctly. Your test suite has only that one case and it passes. Name two more cases to add and the exit code each should produce.
Show answer
Add a harmless command such as ls -la (exit 0) to prove the hook does not block everything. Add malformed input such as not json (exit 2) to prove it fails closed. A look-alike safe command such as git push origin main (exit 0) is also a good choice.