-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhook_input_test.go
More file actions
180 lines (163 loc) · 5.09 KB
/
hook_input_test.go
File metadata and controls
180 lines (163 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package agentx
import (
"strings"
"testing"
)
func TestReadHookInput(t *testing.T) {
tests := []struct {
name string
input string
wantNil bool
check func(t *testing.T, hi *HookInput)
}{
{
name: "empty input",
input: "",
wantNil: true,
},
{
name: "invalid JSON",
input: "not json",
wantNil: true,
},
{
name: "session start event",
input: `{"session_id":"abc123","hook_event_name":"SessionStart","source":"startup"}`,
check: func(t *testing.T, hi *HookInput) {
if hi.SessionID != "abc123" {
t.Errorf("session_id = %q, want %q", hi.SessionID, "abc123")
}
if hi.HookEventName != "SessionStart" {
t.Errorf("hook_event_name = %q, want %q", hi.HookEventName, "SessionStart")
}
if hi.Source != "startup" {
t.Errorf("source = %q, want %q", hi.Source, "startup")
}
},
},
{
name: "tool event with payloads",
input: `{"session_id":"xyz","hook_event_name":"PostToolUse","tool_name":"Bash","tool_input":{"command":"ls"},"tool_response":"file.txt"}`,
check: func(t *testing.T, hi *HookInput) {
if hi.ToolName != "Bash" {
t.Errorf("tool_name = %q, want %q", hi.ToolName, "Bash")
}
if string(hi.ToolInput) != `{"command":"ls"}` {
t.Errorf("tool_input = %q, want %q", string(hi.ToolInput), `{"command":"ls"}`)
}
},
},
{
name: "compact event with trigger",
input: `{"session_id":"s1","hook_event_name":"PreCompact","trigger":"auto"}`,
check: func(t *testing.T, hi *HookInput) {
if hi.Trigger != "auto" {
t.Errorf("trigger = %q, want %q", hi.Trigger, "auto")
}
},
},
{
name: "minimal valid JSON object",
input: `{}`,
check: func(t *testing.T, hi *HookInput) {
if hi.SessionID != "" {
t.Errorf("session_id = %q, want empty", hi.SessionID)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := strings.NewReader(tt.input)
got := ReadHookInput(r)
if tt.wantNil {
if got != nil {
t.Errorf("ReadHookInput() = %+v, want nil", got)
}
return
}
if got == nil {
t.Fatal("ReadHookInput() = nil, want non-nil")
}
if tt.check != nil {
tt.check(t, got)
}
})
}
}
func TestReadHookInput_RawBytesPreserved(t *testing.T) {
// unknown fields should be preserved in RawBytes for subprocess passthrough
input := `{"session_id":"s1","hook_event_name":"SessionStart","custom_agent_field":"secret","nested":{"deep":true}}`
got := ReadHookInput(strings.NewReader(input))
if got == nil {
t.Fatal("ReadHookInput() = nil, want non-nil")
}
if got.SessionID != "s1" {
t.Errorf("session_id = %q, want %q", got.SessionID, "s1")
}
// RawBytes must contain the original payload including unknown fields
raw := string(got.RawBytes)
if !strings.Contains(raw, "custom_agent_field") {
t.Errorf("RawBytes should preserve unknown fields, got: %s", raw)
}
if !strings.Contains(raw, `"deep":true`) {
t.Errorf("RawBytes should preserve nested unknown fields, got: %s", raw)
}
}
func TestReadHookInput_ExactlyAtLimit(t *testing.T) {
// Create a payload that's exactly at the 256KB limit
prefix := `{"session_id":"s1","hook_event_name":"PostToolUse","tool_response":"`
suffix := `"}`
overhead := len(prefix) + len(suffix)
filler := strings.Repeat("x", 256*1024-overhead)
payload := prefix + filler + suffix
if len(payload) != 256*1024 {
t.Fatalf("payload length = %d, want %d", len(payload), 256*1024)
}
input := ReadHookInput(strings.NewReader(payload))
if input == nil {
t.Fatal("ReadHookInput() = nil, want non-nil for payload exactly at limit")
}
if input.SessionID != "s1" {
t.Errorf("session_id = %q, want %q", input.SessionID, "s1")
}
}
func TestReadHookInput_ExceedsLimit(t *testing.T) {
// Create a payload that exceeds the 256KB limit
prefix := `{"session_id":"s1","hook_event_name":"PostToolUse","tool_response":"`
suffix := `"}`
overhead := len(prefix) + len(suffix)
filler := strings.Repeat("x", 256*1024-overhead+100)
payload := prefix + filler + suffix
if len(payload) <= 256*1024 {
t.Fatalf("payload length = %d, want > %d", len(payload), 256*1024)
}
// Truncated JSON can't parse
input := ReadHookInput(strings.NewReader(payload))
if input != nil {
t.Error("ReadHookInput() should return nil for truncated JSON")
}
}
func TestReadHookInput_TruncatedJSON(t *testing.T) {
// Valid JSON start but cut off mid-field
payload := `{"session_id":"s1","hook_event_name":"PostToo`
input := ReadHookInput(strings.NewReader(payload))
if input != nil {
t.Error("ReadHookInput() should return nil for truncated JSON")
}
}
func TestReadHookInput_LargePayload(t *testing.T) {
// simulate a large tool response that would span multiple pipe reads
largeOutput := strings.Repeat("x", 100000) // 100KB
input := `{"session_id":"s1","tool_response":"` + largeOutput + `"}`
got := ReadHookInput(strings.NewReader(input))
if got == nil {
t.Fatal("ReadHookInput() = nil for large payload, want non-nil")
}
if got.SessionID != "s1" {
t.Errorf("session_id = %q, want %q", got.SessionID, "s1")
}
if len(got.RawBytes) < 100000 {
t.Errorf("RawBytes length = %d, want >= 100000", len(got.RawBytes))
}
}