Repository navigation
Expand file tree
/
Copy pathagentconfig_test.go
More file actions
304 lines (292 loc) · 9.17 KB
/
Copy pathagentconfig_test.go
File metadata and controls
304 lines (292 loc) · 9.17 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package acpruntime
import (
"bytes"
"context"
"encoding/json"
"os"
"path/filepath"
"sync"
"testing"
"time"
)
// TestApplyClaudeAgentConfig verifies the Claude profile translates AgentConfig
// into _meta.claudeCode.options with disallowedTools + settings.permissions.
func TestApplyClaudeAgentConfig(t *testing.T) {
profile := ResolveAgentProfile(Agent{Type: ClaudeCodeACPRegistryID})
if profile.ApplyAgentConfig == nil {
t.Fatalf("Claude profile has no ApplyAgentConfig")
}
agent, meta := profile.ApplyAgentConfig(Agent{Type: ClaudeCodeACPRegistryID}, AgentConfig{
DisallowedTools: []string{"WebFetch"},
AllowedTools: []string{"Bash(echo:*)"},
Permissions: PermissionConfig{
Deny: []string{"Read(./.env*)"},
},
})
_ = agent // agent is unchanged for Claude
if meta == nil {
t.Fatalf("meta is nil")
}
cc, ok := meta["claudeCode"].(map[string]any)
if !ok {
t.Fatalf("missing claudeCode wrapper: %#v", meta)
}
options, ok := cc["options"].(map[string]any)
if !ok {
t.Fatalf("missing options: %#v", cc)
}
// disallowedTools
dt, ok := options["disallowedTools"].([]string)
if !ok {
t.Fatalf("disallowedTools not []string: %#v", options["disallowedTools"])
}
if len(dt) != 1 || dt[0] != "WebFetch" {
t.Fatalf("disallowedTools = %v", dt)
}
// allowedTools
at, ok := options["allowedTools"].([]string)
if !ok {
t.Fatalf("allowedTools not []string: %#v", options["allowedTools"])
}
if len(at) != 1 || at[0] != "Bash(echo:*)" {
t.Fatalf("allowedTools = %v", at)
}
settings, ok := options["settings"].(map[string]any)
if !ok {
t.Fatalf("missing settings")
}
perm, ok := settings["permissions"].(map[string]any)
if !ok {
t.Fatalf("missing permissions")
}
deny, ok := perm["deny"].([]string)
if !ok {
t.Fatalf("deny not []string: %#v", perm["deny"])
}
if len(deny) != 1 || deny[0] != "Read(./.env*)" {
t.Fatalf("deny = %v", deny)
}
}
// TestApplyClaudeAgentConfigEmpty verifies empty AgentConfig produces empty meta.
func TestApplyClaudeAgentConfigEmpty(t *testing.T) {
profile := ResolveAgentProfile(Agent{Type: ClaudeCodeACPRegistryID})
_, meta := profile.ApplyAgentConfig(Agent{}, AgentConfig{})
// CreateClaudeCodeOptions(empty) still produces {"claudeCode":{"options":{}}}
cc, ok := meta["claudeCode"].(map[string]any)
if !ok {
t.Fatalf("expected claudeCode wrapper even when empty")
}
options := cc["options"].(map[string]any)
if len(options) != 0 {
t.Fatalf("expected empty options, got %#v", options)
}
}
// TestApplyCodexAgentConfig verifies the Codex profile translates AgentConfig
// into CODEX_CONFIG env JSON with sandbox_mode + model.
func TestApplyCodexAgentConfig(t *testing.T) {
profile := ResolveAgentProfile(Agent{Type: CodexACPRegistryID})
if profile.ApplyAgentConfig == nil {
t.Fatalf("Codex profile has no ApplyAgentConfig")
}
agent, meta := profile.ApplyAgentConfig(Agent{Type: CodexACPRegistryID}, AgentConfig{
Model: "deepseek-chat",
Sandbox: "read-only",
})
if meta != nil {
t.Fatalf("codex should return nil meta, got %#v", meta)
}
if agent.Env == nil {
t.Fatalf("agent.Env is nil")
}
codexConfig := agent.Env["CODEX_CONFIG"]
if codexConfig == "" {
t.Fatalf("CODEX_CONFIG env is empty")
}
var config map[string]any
if err := json.Unmarshal([]byte(codexConfig), &config); err != nil {
t.Fatalf("CODEX_CONFIG not valid JSON: %v", err)
}
if config["model"] != "deepseek-chat" {
t.Fatalf("model = %v, want deepseek-chat", config["model"])
}
if config["sandbox_mode"] != "read-only" {
t.Fatalf("sandbox_mode = %v, want read-only", config["sandbox_mode"])
}
}
// TestApplyCodexAgentConfigFullAccess verifies sandbox mapping.
func TestApplyCodexAgentConfigFullAccess(t *testing.T) {
profile := ResolveAgentProfile(Agent{Type: CodexACPRegistryID})
agent, _ := profile.ApplyAgentConfig(Agent{}, AgentConfig{Sandbox: "full-access"})
var config map[string]any
json.Unmarshal([]byte(agent.Env["CODEX_CONFIG"]), &config)
if config["sandbox_mode"] != "danger-full-access" {
t.Fatalf("sandbox_mode = %v, want danger-full-access", config["sandbox_mode"])
}
}
// TestApplyOpenCodeAgentConfig verifies model goes to _meta.
func TestApplyOpenCodeAgentConfig(t *testing.T) {
profile := ResolveAgentProfile(Agent{Type: OpenCodeACPRegistryID})
if profile.ApplyAgentConfig == nil {
t.Fatalf("OpenCode profile has no ApplyAgentConfig")
}
agent, meta := profile.ApplyAgentConfig(Agent{}, AgentConfig{
Model: "glm-5.2",
Extra: map[string]any{"provider": "zai"},
})
_ = agent
if meta["model"] != "glm-5.2" {
t.Fatalf("model = %v", meta["model"])
}
if meta["provider"] != "zai" {
t.Fatalf("provider = %v", meta["provider"])
}
}
// TestApplyAgentConfigUnknownAgent verifies agents without ApplyAgentConfig
// return nil meta (best-effort skipped).
func TestApplyAgentConfigUnknownAgent(t *testing.T) {
profile := ResolveAgentProfile(Agent{Type: "unknown-agent"})
if profile.ApplyAgentConfig != nil {
t.Fatalf("unknown agent should have nil ApplyAgentConfig")
}
}
// TestCreateCodexConfig verifies the convenience function produces valid JSON.
func TestCreateCodexConfig(t *testing.T) {
env, err := CreateCodexConfig(CodexConfig{
Model: "gpt-5.5",
SandboxMode: "workspace-write",
ApprovalPolicy: "on-request",
})
if err != nil {
t.Fatalf("CreateCodexConfig error: %v", err)
}
var config map[string]any
if err := json.Unmarshal([]byte(env["CODEX_CONFIG"]), &config); err != nil {
t.Fatalf("invalid JSON: %v", err)
}
if config["model"] != "gpt-5.5" {
t.Fatalf("model = %v", config["model"])
}
if config["sandbox_mode"] != "workspace-write" {
t.Fatalf("sandbox_mode = %v", config["sandbox_mode"])
}
if config["approval_policy"] != "on-request" {
t.Fatalf("approval_policy = %v", config["approval_policy"])
}
}
// TestCreateCodexConfigMerge verifies existing CODEX_CONFIG is preserved.
func TestCreateCodexConfigMerge(t *testing.T) {
env, _ := CreateCodexConfig(CodexConfig{Model: "gpt-5.5"})
// Second call should merge, not clobber
env2, _ := buildCodexEnv(CodexConfig{SandboxMode: "read-only"}, env)
var config map[string]any
json.Unmarshal([]byte(env2["CODEX_CONFIG"]), &config)
if config["model"] != "gpt-5.5" {
t.Fatalf("model lost in merge: %v", config["model"])
}
if config["sandbox_mode"] != "read-only" {
t.Fatalf("sandbox lost in merge: %v", config["sandbox_mode"])
}
}
// TestWriteOpenCodeConfig verifies file creation and content.
func TestWriteOpenCodeConfig(t *testing.T) {
cwd := t.TempDir()
err := WriteOpenCodeConfig(cwd, OpenCodeConfig{
Model: "glm-5.2",
Provider: "zai",
Permission: OpenCodePermission{
Deny: []string{"bash"},
Allow: []string{"read"},
},
})
if err != nil {
t.Fatalf("WriteOpenCodeConfig error: %v", err)
}
data, err := os.ReadFile(filepath.Join(cwd, "opencode.json"))
if err != nil {
t.Fatalf("ReadFile error: %v", err)
}
var config map[string]any
if err := json.Unmarshal(data, &config); err != nil {
t.Fatalf("invalid JSON: %v", err)
}
if config["model"] != "glm-5.2" {
t.Fatalf("model = %v", config["model"])
}
if config["provider"] != "zai" {
t.Fatalf("provider = %v", config["provider"])
}
perm, ok := config["permission"].(map[string]any)
if !ok {
t.Fatalf("missing permission")
}
if fmtSlice(perm["deny"]) != "[bash]" {
t.Fatalf("deny = %v", perm["deny"])
}
}
// TestAgentConfigViaStartSessionClaude verifies AgentConfig reaches the wire
// when used via StartSessionOptions for a Claude agent (simulator-based).
func TestAgentConfigViaStartSessionClaude(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
cwd := t.TempDir()
storage := t.TempDir()
simulatorBin := buildSimulatorBinary(t)
agent := Agent{
Type: LocalSimulatorAgentACPRegistryID,
Command: simulatorBin,
Args: []string{"--auth-mode", "none", "--storage-dir", storage},
}
var capturedNew []byte
var mu sync.Mutex
factory := NewStdioConnectionFactory(StdioFactoryOptions{
OnACPMessage: func(dir string, msg []byte) {
if dir == "outbound" && bytes.Contains(msg, []byte(`"session/new"`)) {
mu.Lock()
capturedNew = append(capturedNew[:0], msg...)
mu.Unlock()
}
},
})
runtime := NewRuntime(factory, RuntimeOptions{})
// Use ClaudeCodeACPRegistryID type so the Claude profile's ApplyAgentConfig
// fires (even though we spawn a simulator for determinism).
agent.Type = ClaudeCodeACPRegistryID
session, err := runtime.StartSession(ctx, StartSessionOptions{
Agent: agent,
CWD: cwd,
AgentConfig: &AgentConfig{
DisallowedTools: []string{"WebFetch"},
},
})
if err != nil {
t.Fatalf("StartSession error = %v", err)
}
defer session.Close(context.Background())
mu.Lock()
snap := append([]byte(nil), capturedNew...)
mu.Unlock()
if !bytes.Contains(snap, []byte(`"disallowedTools"`)) {
t.Fatalf("AgentConfig.DisallowedTools did not reach session/new wire: %s", snap)
}
}
func fmtSlice(v any) string {
if s, ok := v.([]string); ok {
b, _ := json.Marshal(s)
return string(b)
}
b, _ := json.Marshal(v)
// json.Marshal produces ["a","b"] but we compare with [a b] format in some tests
// normalize to [a b]
var arr []string
if json.Unmarshal(b, &arr) == nil {
result := "["
for i, s := range arr {
if i > 0 {
result += " "
}
result += s
}
return result + "]"
}
return string(b)
}