(t *testing.T)
| 1028 | } |
| 1029 | |
| 1030 | func TestSession_PostToolUseFailureHook(t *testing.T) { |
| 1031 | t.Run("dispatches with parsed input and returns additional context", func(t *testing.T) { |
| 1032 | session, cleanup := newTestSession() |
| 1033 | defer cleanup() |
| 1034 | |
| 1035 | var captured PostToolUseFailureHookInput |
| 1036 | session.registerHooks(&SessionHooks{ |
| 1037 | OnPostToolUseFailure: func(input PostToolUseFailureHookInput, _ HookInvocation) (*PostToolUseFailureHookOutput, error) { |
| 1038 | captured = input |
| 1039 | return &PostToolUseFailureHookOutput{ |
| 1040 | AdditionalContext: "extra-context: " + input.Error, |
| 1041 | }, nil |
| 1042 | }, |
| 1043 | }) |
| 1044 | |
| 1045 | raw := json.RawMessage(`{ |
| 1046 | "sessionId": "sess-1", |
| 1047 | "timestamp": 1700000000, |
| 1048 | "cwd": "/work", |
| 1049 | "toolName": "tool-x", |
| 1050 | "toolArgs": {"foo": "bar"}, |
| 1051 | "error": "boom" |
| 1052 | }`) |
| 1053 | output, err := session.handleHooksInvoke("postToolUseFailure", raw) |
| 1054 | if err != nil { |
| 1055 | t.Fatalf("unexpected error: %v", err) |
| 1056 | } |
| 1057 | if captured.SessionID != "sess-1" { |
| 1058 | t.Errorf("expected sessionId 'sess-1', got %q", captured.SessionID) |
| 1059 | } |
| 1060 | if captured.ToolName != "tool-x" { |
| 1061 | t.Errorf("expected toolName 'tool-x', got %q", captured.ToolName) |
| 1062 | } |
| 1063 | if captured.Error != "boom" { |
| 1064 | t.Errorf("expected error 'boom', got %q", captured.Error) |
| 1065 | } |
| 1066 | if !captured.Timestamp.Equal(time.UnixMilli(1700000000)) { |
| 1067 | t.Errorf("expected timestamp %v, got %v", time.UnixMilli(1700000000), captured.Timestamp) |
| 1068 | } |
| 1069 | if captured.WorkingDirectory != "/work" { |
| 1070 | t.Errorf("expected WorkingDirectory '/work', got %q", captured.WorkingDirectory) |
| 1071 | } |
| 1072 | out, ok := output.(*PostToolUseFailureHookOutput) |
| 1073 | if !ok { |
| 1074 | t.Fatalf("expected *PostToolUseFailureHookOutput, got %T", output) |
| 1075 | } |
| 1076 | if out.AdditionalContext != "extra-context: boom" { |
| 1077 | t.Errorf("unexpected AdditionalContext: %q", out.AdditionalContext) |
| 1078 | } |
| 1079 | }) |
| 1080 | |
| 1081 | t.Run("no handler registered returns nil without error", func(t *testing.T) { |
| 1082 | session, cleanup := newTestSession() |
| 1083 | defer cleanup() |
| 1084 | session.registerHooks(&SessionHooks{}) |
| 1085 | |
| 1086 | output, err := session.handleHooksInvoke("postToolUseFailure", json.RawMessage(`{"sessionId":"sess-1","timestamp":0,"cwd":"","toolName":"t","toolArgs":null,"error":"e"}`)) |
| 1087 | if err != nil { |
nothing calls this directly
no test coverage detected
searching dependent graphs…