-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserve_test.go
More file actions
234 lines (222 loc) · 8.59 KB
/
Copy pathserve_test.go
File metadata and controls
234 lines (222 loc) · 8.59 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package agenthooks
import (
"bytes"
"context"
"encoding/json"
"fmt"
"path/filepath"
"strings"
"testing"
)
func openCodeToolFrame(seq int, tool string) string {
return fmt.Sprintf(`{"seq":%d,"hook":"tool.execute.before","input":{"sessionID":"s","callID":"c%d","tool":%q},"output":{"args":{}}}`, seq, seq, tool)
}
func TestServeLoop(t *testing.T) {
r := quietRunner()
r.OnToolPre(func(ctx context.Context, e *ToolPreEvent) (ToolPreDecision, error) {
if e.Tool.Name == "bash" {
return Deny("no bash in this session"), nil
}
return NoDecision(), nil
})
lines := []string{
`{"seq":1,"hook":"initialize","input":{"serverUrl":"http://127.0.0.1:1","directory":"/work","worktree":""}}`,
strings.TrimSpace(string(fixture(t, "opencode/tool_execute_before.json"))), // seq 3, bash
strings.TrimSpace(string(fixture(t, "opencode/session_idle.json"))), // seq 9
}
var out, errb bytes.Buffer
code := r.Run(context.Background(), []string{"agenthooks", "serve", "--provider=opencode"},
strings.NewReader(strings.Join(lines, "\n")+"\n"), &out, &errb)
if code != 0 {
t.Fatalf("serve exit %d, stderr: %s", code, errb.String())
}
replies := parseReplies(t, out.String())
if len(replies) != 3 {
t.Fatalf("expected 3 replies, got %d: %s", len(replies), out.String())
}
if replies[0].Seq != 1 || replies[0].Error != "" {
t.Errorf("initialize reply wrong: %+v", replies[0])
}
if replies[1].Seq != 3 || replies[1].Error != "no bash in this session" {
t.Errorf("deny must ride the error field (shim re-throws): %+v", replies[1])
}
if replies[2].Seq != 9 || replies[2].Error != "" || len(replies[2].Output) != 0 {
t.Errorf("session.idle no-op wrong: %+v", replies[2])
}
}
func TestServeUpdateInput(t *testing.T) {
r := quietRunner()
r.OnToolPre(func(ctx context.Context, e *ToolPreEvent) (ToolPreDecision, error) {
return NoDecision().WithUpdatedInput(map[string]any{"command": "make test -j4", "timeout": 120}), nil
})
var out, errb bytes.Buffer
line := strings.TrimSpace(string(fixture(t, "opencode/tool_execute_before.json")))
code := r.Run(context.Background(), []string{"agenthooks", "serve", "--provider=opencode"},
strings.NewReader(line+"\n"), &out, &errb)
if code != 0 {
t.Fatalf("serve exit %d", code)
}
replies := parseReplies(t, out.String())
if len(replies) != 1 {
t.Fatalf("expected 1 reply, got %d", len(replies))
}
args, ok := replies[0].Output["args"].(map[string]any)
if !ok || args["command"] != "make test -j4" {
t.Errorf("updated input must ride output.args: %+v", replies[0].Output)
}
}
func TestServeOpenCodeActiveMCPInventory(t *testing.T) {
isolateHome(t)
cwd := t.TempDir()
writeConfig(t, filepath.Join(cwd, "opencode.json"), `{"mcp":{"disk_only":{"type":"remote","url":"https://disk.example.com/mcp"}}}`)
r := quietRunner()
seen := map[string]ToolCall{}
r.OnToolPre(func(_ context.Context, e *ToolPreEvent) (ToolPreDecision, error) {
seen[e.Tool.Name] = e.Tool
if strings.Contains(string(e.Raw), "active.example.com") {
t.Error("active inventory leaked into Event.Raw")
}
return NoDecision(), nil
})
lines := []string{
fmt.Sprintf(`{"seq":1,"hook":"initialize","input":{"directory":%q,"mcp":{"active":{"type":"remote","url":"https://active.example.com/mcp"},"local":{"type":"local","command":["node","server.js"]},"disabled":{"type":"remote","url":"https://off.example.com/mcp","enabled":false}}}}`, cwd),
openCodeToolFrame(2, "active_run"),
openCodeToolFrame(3, "local_do"),
openCodeToolFrame(4, "disabled_run"),
openCodeToolFrame(5, "disk_only_run"),
}
var out, errb bytes.Buffer
if code := r.Run(context.Background(), []string{"agenthooks", "serve", "--provider=opencode"},
strings.NewReader(strings.Join(lines, "\n")+"\n"), &out, &errb); code != 0 {
t.Fatalf("serve exit %d: %s", code, errb.String())
}
if got := seen["active_run"].MCP; got == nil || got.URL != "https://active.example.com/mcp" || !got.FromConfig {
t.Fatalf("active remote MCP = %+v", got)
}
if got := seen["local_do"].MCP; got == nil || got.Command != "node server.js" || !got.FromConfig {
t.Fatalf("active local MCP = %+v", got)
}
for _, name := range []string{"disabled_run", "disk_only_run"} {
if seen[name].MCP != nil {
t.Fatalf("%s resolved outside active inventory: %+v", name, seen[name].MCP)
}
}
}
func TestServeOpenCodeReportsInventoryBeforeFirstMCPTool(t *testing.T) {
r := quietRunner(WithDedupDir(t.TempDir()))
var order []string
r.OnMCPInventory(func(_ context.Context, event *MCPInventoryEvent) error {
order = append(order, "inventory")
if !event.Complete || len(event.Servers) != 1 || event.Servers[0].Name != "active" {
t.Fatalf("serve inventory = %+v", event)
}
return nil
})
r.OnToolPre(func(_ context.Context, _ *ToolPreEvent) (ToolPreDecision, error) {
order = append(order, "tool")
return NoDecision(), nil
})
lines := []string{
`{"seq":1,"hook":"initialize","input":{"directory":"/work","mcp":{"active":{"type":"remote","url":"https://active.example.com/mcp"}}}}`,
openCodeToolFrame(2, "active_run"),
}
var out, errb bytes.Buffer
if code := r.Run(t.Context(), []string{"agenthooks", "serve", "--provider=opencode"},
strings.NewReader(strings.Join(lines, "\n")+"\n"), &out, &errb); code != 0 {
t.Fatalf("serve exit %d: %s", code, errb.String())
}
if got := strings.Join(order, ","); got != "inventory,tool" {
t.Fatalf("dispatch order = %q, want inventory,tool", got)
}
}
func TestServeOpenCodeReportsInventoryBeforeSessionCreated(t *testing.T) {
r := quietRunner(WithDedupDir(t.TempDir()))
var order []string
r.OnMCPInventory(func(_ context.Context, event *MCPInventoryEvent) error {
order = append(order, "inventory:"+event.Session.ID)
return nil
})
r.OnSessionStart(func(_ context.Context, event *SessionStartEvent) (SessionStartDecision, error) {
order = append(order, "session:"+event.Session.ID)
if event.Session.CWD != "/session-work" {
t.Fatalf("session CWD = %q", event.Session.CWD)
}
return ContinueSession(), nil
})
lines := []string{
`{"seq":1,"hook":"initialize","input":{"directory":"/work","mcp":{"active":{"type":"remote","url":"https://active.example.com/mcp"}}}}`,
`{"seq":2,"hook":"session.created","input":{"info":{"id":"session-created","directory":"/session-work"}},"output":null}`,
}
var out, errb bytes.Buffer
if code := r.Run(t.Context(), []string{"agenthooks", "serve", "--provider=opencode"},
strings.NewReader(strings.Join(lines, "\n")+"\n"), &out, &errb); code != 0 {
t.Fatalf("serve exit %d: %s", code, errb.String())
}
if got := strings.Join(order, ","); got != "inventory:session-created,session:session-created" {
t.Fatalf("dispatch order = %q", got)
}
}
func TestServeOpenCodeMCPInventoryFallbackAndEmpty(t *testing.T) {
for _, tc := range []struct {
name string
mcpField string
disabled bool
wantMatch bool
}{
{name: "omitted falls back", wantMatch: true},
{name: "empty is authoritative", mcpField: `,"mcp":{}`},
{name: "resolution disabled", mcpField: `,"mcp":{"disk":{"type":"remote","url":"https://active.example.com/mcp"}}`, disabled: true},
} {
t.Run(tc.name, func(t *testing.T) {
isolateHome(t)
cwd := t.TempDir()
writeConfig(t, filepath.Join(cwd, "opencode.json"), `{"mcp":{"disk":{"type":"remote","url":"https://disk.example.com/mcp"}}}`)
var opts []Option
if tc.disabled {
opts = append(opts, WithoutMCPResolution())
}
r := quietRunner(opts...)
var call ToolCall
r.OnToolPre(func(_ context.Context, e *ToolPreEvent) (ToolPreDecision, error) {
call = e.Tool
return NoDecision(), nil
})
lines := []string{
fmt.Sprintf(`{"seq":1,"hook":"initialize","input":{"directory":%q%s}}`, cwd, tc.mcpField),
openCodeToolFrame(2, "disk_run"),
}
var out, errb bytes.Buffer
if code := r.Run(context.Background(), []string{"agenthooks", "serve", "--provider=opencode"},
strings.NewReader(strings.Join(lines, "\n")+"\n"), &out, &errb); code != 0 {
t.Fatalf("serve exit %d: %s", code, errb.String())
}
if tc.wantMatch {
if call.MCP == nil || call.MCP.URL != "https://disk.example.com/mcp" {
t.Fatalf("disk fallback = %+v", call.MCP)
}
} else if call.MCP != nil {
t.Fatalf("unexpected MCP resolution = %+v", call.MCP)
}
})
}
}
type testReply struct {
Seq int64 `json:"seq"`
Output map[string]any `json:"output"`
Error string `json:"error"`
}
func parseReplies(t *testing.T, s string) []testReply {
t.Helper()
var out []testReply
for _, line := range strings.Split(strings.TrimSpace(s), "\n") {
if line == "" {
continue
}
var r testReply
if err := json.Unmarshal([]byte(line), &r); err != nil {
t.Fatalf("bad reply line %q: %v", line, err)
}
out = append(out, r)
}
return out
}