|
| 1 | +"""End-to-end tests for hook event types with real Claude API calls.""" |
| 2 | + |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from claude_agent_sdk import ( |
| 8 | + ClaudeAgentOptions, |
| 9 | + ClaudeSDKClient, |
| 10 | + HookContext, |
| 11 | + HookInput, |
| 12 | + HookJSONOutput, |
| 13 | + HookMatcher, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.e2e |
| 18 | +@pytest.mark.asyncio |
| 19 | +async def test_pre_tool_use_hook_with_additional_context(): |
| 20 | + """Test PreToolUse hook returning additionalContext field end-to-end.""" |
| 21 | + hook_invocations: list[dict[str, Any]] = [] |
| 22 | + |
| 23 | + async def pre_tool_hook( |
| 24 | + input_data: HookInput, tool_use_id: str | None, context: HookContext |
| 25 | + ) -> HookJSONOutput: |
| 26 | + """PreToolUse hook that provides additionalContext.""" |
| 27 | + tool_name = input_data.get("tool_name", "") |
| 28 | + hook_invocations.append( |
| 29 | + {"tool_name": tool_name, "tool_use_id": input_data.get("tool_use_id")} |
| 30 | + ) |
| 31 | + |
| 32 | + return { |
| 33 | + "hookSpecificOutput": { |
| 34 | + "hookEventName": "PreToolUse", |
| 35 | + "permissionDecision": "allow", |
| 36 | + "permissionDecisionReason": "Approved with context", |
| 37 | + "additionalContext": "This command is running in a test environment", |
| 38 | + }, |
| 39 | + } |
| 40 | + |
| 41 | + options = ClaudeAgentOptions( |
| 42 | + allowed_tools=["Bash"], |
| 43 | + hooks={ |
| 44 | + "PreToolUse": [ |
| 45 | + HookMatcher(matcher="Bash", hooks=[pre_tool_hook]), |
| 46 | + ], |
| 47 | + }, |
| 48 | + ) |
| 49 | + |
| 50 | + async with ClaudeSDKClient(options=options) as client: |
| 51 | + await client.query("Run: echo 'test additional context'") |
| 52 | + |
| 53 | + async for message in client.receive_response(): |
| 54 | + print(f"Got message: {message}") |
| 55 | + |
| 56 | + print(f"Hook invocations: {hook_invocations}") |
| 57 | + assert len(hook_invocations) > 0, "PreToolUse hook should have been invoked" |
| 58 | + # Verify tool_use_id is present in the input (new field) |
| 59 | + assert hook_invocations[0]["tool_use_id"] is not None, ( |
| 60 | + "tool_use_id should be present in PreToolUse input" |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.e2e |
| 65 | +@pytest.mark.asyncio |
| 66 | +async def test_post_tool_use_hook_with_tool_use_id(): |
| 67 | + """Test PostToolUse hook receives tool_use_id field end-to-end.""" |
| 68 | + hook_invocations: list[dict[str, Any]] = [] |
| 69 | + |
| 70 | + async def post_tool_hook( |
| 71 | + input_data: HookInput, tool_use_id: str | None, context: HookContext |
| 72 | + ) -> HookJSONOutput: |
| 73 | + """PostToolUse hook that verifies tool_use_id is present.""" |
| 74 | + tool_name = input_data.get("tool_name", "") |
| 75 | + hook_invocations.append( |
| 76 | + { |
| 77 | + "tool_name": tool_name, |
| 78 | + "tool_use_id": input_data.get("tool_use_id"), |
| 79 | + } |
| 80 | + ) |
| 81 | + |
| 82 | + return { |
| 83 | + "hookSpecificOutput": { |
| 84 | + "hookEventName": "PostToolUse", |
| 85 | + "additionalContext": "Post-tool monitoring active", |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + options = ClaudeAgentOptions( |
| 90 | + allowed_tools=["Bash"], |
| 91 | + hooks={ |
| 92 | + "PostToolUse": [ |
| 93 | + HookMatcher(matcher="Bash", hooks=[post_tool_hook]), |
| 94 | + ], |
| 95 | + }, |
| 96 | + ) |
| 97 | + |
| 98 | + async with ClaudeSDKClient(options=options) as client: |
| 99 | + await client.query("Run: echo 'test tool_use_id'") |
| 100 | + |
| 101 | + async for message in client.receive_response(): |
| 102 | + print(f"Got message: {message}") |
| 103 | + |
| 104 | + print(f"Hook invocations: {hook_invocations}") |
| 105 | + assert len(hook_invocations) > 0, "PostToolUse hook should have been invoked" |
| 106 | + # Verify tool_use_id is present in the input (new field) |
| 107 | + assert hook_invocations[0]["tool_use_id"] is not None, ( |
| 108 | + "tool_use_id should be present in PostToolUse input" |
| 109 | + ) |
| 110 | + |
| 111 | + |
| 112 | +@pytest.mark.e2e |
| 113 | +@pytest.mark.asyncio |
| 114 | +async def test_notification_hook(): |
| 115 | + """Test Notification hook fires end-to-end.""" |
| 116 | + hook_invocations: list[dict[str, Any]] = [] |
| 117 | + |
| 118 | + async def notification_hook( |
| 119 | + input_data: HookInput, tool_use_id: str | None, context: HookContext |
| 120 | + ) -> HookJSONOutput: |
| 121 | + """Notification hook that tracks invocations.""" |
| 122 | + hook_invocations.append( |
| 123 | + { |
| 124 | + "hook_event_name": input_data.get("hook_event_name"), |
| 125 | + "message": input_data.get("message"), |
| 126 | + "notification_type": input_data.get("notification_type"), |
| 127 | + } |
| 128 | + ) |
| 129 | + return { |
| 130 | + "hookSpecificOutput": { |
| 131 | + "hookEventName": "Notification", |
| 132 | + "additionalContext": "Notification received", |
| 133 | + }, |
| 134 | + } |
| 135 | + |
| 136 | + options = ClaudeAgentOptions( |
| 137 | + hooks={ |
| 138 | + "Notification": [ |
| 139 | + HookMatcher(hooks=[notification_hook]), |
| 140 | + ], |
| 141 | + }, |
| 142 | + ) |
| 143 | + |
| 144 | + async with ClaudeSDKClient(options=options) as client: |
| 145 | + await client.query("Say hello in one word.") |
| 146 | + |
| 147 | + async for message in client.receive_response(): |
| 148 | + print(f"Got message: {message}") |
| 149 | + |
| 150 | + print(f"Notification hook invocations: {hook_invocations}") |
| 151 | + # Notification hooks may or may not fire depending on CLI behavior. |
| 152 | + # This test verifies the hook registration doesn't cause errors. |
| 153 | + # If it fires, verify the shape is correct. |
| 154 | + for invocation in hook_invocations: |
| 155 | + assert invocation["hook_event_name"] == "Notification" |
| 156 | + assert invocation["notification_type"] is not None |
| 157 | + |
| 158 | + |
| 159 | +@pytest.mark.e2e |
| 160 | +@pytest.mark.asyncio |
| 161 | +async def test_multiple_hooks_together(): |
| 162 | + """Test registering multiple hook event types together end-to-end.""" |
| 163 | + all_invocations: list[dict[str, Any]] = [] |
| 164 | + |
| 165 | + async def track_hook( |
| 166 | + input_data: HookInput, tool_use_id: str | None, context: HookContext |
| 167 | + ) -> HookJSONOutput: |
| 168 | + """Generic hook that tracks all invocations.""" |
| 169 | + all_invocations.append( |
| 170 | + { |
| 171 | + "hook_event_name": input_data.get("hook_event_name"), |
| 172 | + } |
| 173 | + ) |
| 174 | + return {} |
| 175 | + |
| 176 | + options = ClaudeAgentOptions( |
| 177 | + allowed_tools=["Bash"], |
| 178 | + hooks={ |
| 179 | + "Notification": [HookMatcher(hooks=[track_hook])], |
| 180 | + "PreToolUse": [HookMatcher(matcher="Bash", hooks=[track_hook])], |
| 181 | + "PostToolUse": [HookMatcher(matcher="Bash", hooks=[track_hook])], |
| 182 | + }, |
| 183 | + ) |
| 184 | + |
| 185 | + async with ClaudeSDKClient(options=options) as client: |
| 186 | + await client.query("Run: echo 'multi-hook test'") |
| 187 | + |
| 188 | + async for message in client.receive_response(): |
| 189 | + print(f"Got message: {message}") |
| 190 | + |
| 191 | + print(f"All hook invocations: {all_invocations}") |
| 192 | + event_names = [inv["hook_event_name"] for inv in all_invocations] |
| 193 | + |
| 194 | + # At minimum, PreToolUse and PostToolUse should fire for the Bash command |
| 195 | + assert "PreToolUse" in event_names, "PreToolUse hook should have fired" |
| 196 | + assert "PostToolUse" in event_names, "PostToolUse hook should have fired" |
0 commit comments