-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_test.go
More file actions
377 lines (294 loc) · 10.5 KB
/
command_test.go
File metadata and controls
377 lines (294 loc) · 10.5 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package cli
import (
"os"
"slices"
"strings"
"testing"
"github.com/ethpandaops/claude-agent-sdk-go/internal/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestBuildArgs_WithSystemPromptFile tests that --system-prompt-file flag is
// present and --system-prompt is absent when SystemPromptFile is set.
func TestBuildArgs_WithSystemPromptFile(t *testing.T) {
t.Parallel()
options := &config.Options{
SystemPromptFile: "/path/to/prompt.md",
}
args := BuildArgs("test", options, false)
idx := slices.Index(args, "--system-prompt-file")
require.NotEqual(t, -1, idx, "Expected --system-prompt-file flag to be present")
require.Less(t, idx+1, len(args), "Expected value after --system-prompt-file flag")
require.Equal(t, "/path/to/prompt.md", args[idx+1])
// --system-prompt should NOT be present when --system-prompt-file is used
require.NotContains(t, args, "--system-prompt")
}
// TestBuildArgs_SystemPromptFilePrecedence tests that Preset > File > String.
func TestBuildArgs_SystemPromptFilePrecedence(t *testing.T) {
t.Parallel()
t.Run("preset takes precedence over file", func(t *testing.T) {
t.Parallel()
appendText := "extra context"
options := &config.Options{
SystemPromptPreset: &config.SystemPromptPreset{
Type: "preset",
Preset: "claude_code",
Append: &appendText,
},
SystemPromptFile: "/path/to/prompt.md",
SystemPrompt: "plain string prompt",
}
args := BuildArgs("test", options, false)
// Preset should win: --append-system-prompt present
require.Contains(t, args, "--append-system-prompt")
// File and string should not appear
require.NotContains(t, args, "--system-prompt-file")
require.NotContains(t, args, "--system-prompt")
})
t.Run("file takes precedence over string", func(t *testing.T) {
t.Parallel()
options := &config.Options{
SystemPromptFile: "/path/to/prompt.md",
SystemPrompt: "plain string prompt",
}
args := BuildArgs("test", options, false)
idx := slices.Index(args, "--system-prompt-file")
require.NotEqual(t, -1, idx, "Expected --system-prompt-file flag")
require.Equal(t, "/path/to/prompt.md", args[idx+1])
require.NotContains(t, args, "--system-prompt")
})
t.Run("string used when no preset or file", func(t *testing.T) {
t.Parallel()
options := &config.Options{
SystemPrompt: "plain string prompt",
}
args := BuildArgs("test", options, false)
idx := slices.Index(args, "--system-prompt")
require.NotEqual(t, -1, idx, "Expected --system-prompt flag")
require.Equal(t, "plain string prompt", args[idx+1])
require.NotContains(t, args, "--system-prompt-file")
})
}
// TestBuildArgs_WithTaskBudget tests that --task-budget flag is present.
func TestBuildArgs_WithTaskBudget(t *testing.T) {
t.Parallel()
budget := 50000
options := &config.Options{
TaskBudget: &budget,
}
args := BuildArgs("test", options, false)
idx := slices.Index(args, "--task-budget")
require.NotEqual(t, -1, idx, "Expected --task-budget flag to be present")
require.Less(t, idx+1, len(args), "Expected value after --task-budget flag")
require.Equal(t, "50000", args[idx+1])
}
// TestBuildEnvironment_FiltersCLAUDECODE tests that the CLAUDECODE env var is filtered.
func TestBuildEnvironment_FiltersCLAUDECODE(t *testing.T) {
t.Setenv("CLAUDECODE", "test-value")
options := &config.Options{}
env := BuildEnvironment(options)
for _, e := range env {
key, _, ok := strings.Cut(e, "=")
if ok && key == "CLAUDECODE" {
t.Fatal("Expected CLAUDECODE to be filtered from environment")
}
}
}
// TestBuildEnvironment_UserEnvOverridesEntrypoint tests that user-provided env
// vars override SDK defaults via last-wins semantics.
func TestBuildEnvironment_UserEnvOverridesEntrypoint(t *testing.T) {
options := &config.Options{
Env: map[string]string{
"CLAUDE_CODE_ENTRYPOINT": "custom-entrypoint",
},
}
env := BuildEnvironment(options)
// Find all CLAUDE_CODE_ENTRYPOINT entries; last one should be the user's
var lastValue string
for _, e := range env {
if key, val, ok := strings.Cut(e, "="); ok && key == "CLAUDE_CODE_ENTRYPOINT" {
lastValue = val
}
}
require.Equal(t, "custom-entrypoint", lastValue,
"User-provided CLAUDE_CODE_ENTRYPOINT should be last (wins in exec.Cmd.Env)")
}
func TestBuildEnvironment_WithUnattendedRetry(t *testing.T) {
enabled := true
env := BuildEnvironment(&config.Options{UnattendedRetry: &enabled})
require.Contains(t, env, "CLAUDE_CODE_UNATTENDED_RETRY=1")
}
func TestBuildEnvironment_WithRuntimeOverrides(t *testing.T) {
maxToolConcurrency := 7
autoCompactPct := 82
blockingLimit := 4096
disableCompact := true
disableAutoCompact := true
env := BuildEnvironment(&config.Options{
MaxToolUseConcurrency: &maxToolConcurrency,
AutoCompactPercentageOverride: &autoCompactPct,
BlockingLimitOverride: &blockingLimit,
DisableCompact: &disableCompact,
DisableAutoCompact: &disableAutoCompact,
})
require.Contains(t, env, "CLAUDE_CODE_MAX_TOOL_USE_CONCURRENCY=7")
require.Contains(t, env, "CLAUDE_AUTOCOMPACT_PCT_OVERRIDE=82")
require.Contains(t, env, "CLAUDE_CODE_BLOCKING_LIMIT_OVERRIDE=4096")
require.Contains(t, env, "DISABLE_COMPACT=1")
require.Contains(t, env, "DISABLE_AUTO_COMPACT=1")
}
func TestBuildArgs_OneShotDoesNotIncludePrompt(t *testing.T) {
t.Parallel()
prompt := "This is a large prompt that should not appear in CLI args"
args := BuildArgs(prompt, &config.Options{}, false)
// Should contain --print but not the prompt text or "--"
assert.True(t, slices.Contains(args, "--print"),
"one-shot mode should include --print flag")
for _, arg := range args {
assert.False(t, strings.Contains(arg, prompt),
"prompt should not appear in CLI args (avoids ARG_MAX)")
}
assert.False(t, slices.Contains(args, "--"),
"one-shot mode should not include -- separator")
}
func TestBuildArgs_StreamingModeUnchanged(t *testing.T) {
t.Parallel()
args := BuildArgs("test prompt", &config.Options{}, true)
assert.True(t, slices.Contains(args, "--input-format"),
"streaming mode should include --input-format flag")
assert.False(t, slices.Contains(args, "--print"),
"streaming mode should not include --print flag")
}
func TestBuildArgs_LargePromptNotInArgs(t *testing.T) {
t.Parallel()
// Simulate a large prompt (>128KB)
largePrompt := strings.Repeat("x", 200*1024)
args := BuildArgs(largePrompt, &config.Options{}, false)
// Calculate total args size
totalSize := 0
for _, arg := range args {
totalSize += len(arg)
}
require.Less(t, totalSize, 100*1024,
"total args size should be small (prompt sent via stdin, not args)")
}
func TestBuildArgs_StreamingAllowsDangerouslySkipPermissions(t *testing.T) {
t.Parallel()
args := BuildArgs("test", &config.Options{
PermissionMode: "default",
}, true)
require.Contains(t, args, "--allow-dangerously-skip-permissions")
}
func TestBuildArgs_OneShotDoesNotAddDangerouslySkipPermissionsAllowFlag(t *testing.T) {
t.Parallel()
args := BuildArgs("test", &config.Options{
PermissionMode: "default",
}, false)
require.NotContains(t, args, "--allow-dangerously-skip-permissions")
}
const settingsFlag = "--settings"
func TestBuildArgs_WithFastModeMergedIntoSettingsJSON(t *testing.T) {
enabled := true
options := &config.Options{
Settings: `{"test":"value"}`,
FastMode: &enabled,
}
args := BuildArgs("test", options, false)
require.Contains(t, args, settingsFlag)
for i, arg := range args {
if arg == settingsFlag && i+1 < len(args) {
require.Contains(t, args[i+1], `"fastMode":true`)
require.Contains(t, args[i+1], `"test":"value"`)
return
}
}
t.Fatal("expected merged --settings payload")
}
func TestBuildArgs_WithFastModeMergedIntoSettingsFile(t *testing.T) {
tmpDir := t.TempDir()
settingsPath := tmpDir + "/settings.json"
require.NoError(t, os.WriteFile(settingsPath, []byte(`{"theme":"dark"}`), 0o600))
enabled := true
options := &config.Options{
Settings: settingsPath,
FastMode: &enabled,
}
args := BuildArgs("test", options, false)
require.Contains(t, args, settingsFlag)
for i, arg := range args {
if arg == settingsFlag && i+1 < len(args) {
require.Contains(t, args[i+1], `"fastMode":true`)
require.Contains(t, args[i+1], `"theme":"dark"`)
return
}
}
t.Fatal("expected merged --settings payload")
}
func TestBuildArgs_WithSessionID(t *testing.T) {
t.Parallel()
args := BuildArgs("test", &config.Options{SessionID: "sess-abc-123"}, false)
idx := slices.Index(args, "--session-id")
require.NotEqual(t, -1, idx, "Expected --session-id flag")
require.Equal(t, "sess-abc-123", args[idx+1])
}
func TestBuildArgs_SessionIDOmittedWhenEmpty(t *testing.T) {
t.Parallel()
args := BuildArgs("test", &config.Options{}, false)
require.NotContains(t, args, "--session-id")
}
func TestBuildArgs_SettingSourcesOmittedWhenEmpty(t *testing.T) {
t.Parallel()
args := BuildArgs("test", &config.Options{}, false)
require.NotContains(t, args, "--setting-sources")
}
func TestBuildArgs_SettingSourcesIncludedWhenSet(t *testing.T) {
t.Parallel()
args := BuildArgs("test", &config.Options{
SettingSources: []config.SettingSource{config.SettingSourceUser, config.SettingSourceProject},
}, false)
idx := slices.Index(args, "--setting-sources")
require.NotEqual(t, -1, idx, "Expected --setting-sources flag")
require.Equal(t, "user,project", args[idx+1])
}
func TestBuildArgs_ThinkingConfig(t *testing.T) {
t.Parallel()
tests := []struct {
name string
thinking config.ThinkingConfig
wantFlag string
wantValue string
}{
{
name: "adaptive uses --thinking adaptive",
thinking: config.ThinkingConfigAdaptive{},
wantFlag: "--thinking",
wantValue: "adaptive",
},
{
name: "enabled uses --max-thinking-tokens with budget",
thinking: config.ThinkingConfigEnabled{BudgetTokens: 16000},
wantFlag: "--max-thinking-tokens",
wantValue: "16000",
},
{
name: "disabled uses --thinking disabled",
thinking: config.ThinkingConfigDisabled{},
wantFlag: "--thinking",
wantValue: "disabled",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
args := BuildArgs("test", &config.Options{Thinking: tt.thinking}, false)
idx := slices.Index(args, tt.wantFlag)
require.NotEqual(t, -1, idx, "Expected %s flag", tt.wantFlag)
require.Equal(t, tt.wantValue, args[idx+1])
})
}
}
func TestBuildArgs_ThinkingAdaptiveDoesNotUseMaxThinkingTokens(t *testing.T) {
t.Parallel()
args := BuildArgs("test", &config.Options{Thinking: config.ThinkingConfigAdaptive{}}, false)
require.NotContains(t, args, "--max-thinking-tokens")
}