summaryrefslogtreecommitdiff
path: root/internal/executor/gemini_test.go
blob: cd11ebc90fb3f390f0112277dadf44fd70203d65 (plain)
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
package executor

import (
	"bytes"
	"context"
	"errors"
	"io"
	"log/slog"
	"os"
	"path/filepath"
	"strings"
	"testing"

	"github.com/thepeterstone/claudomator/internal/storage"
	"github.com/thepeterstone/claudomator/internal/task"
)

func TestGeminiRunner_BuildArgs_BasicTask(t *testing.T) {
	r := &GeminiRunner{}
	tk := &task.Task{
		Agent: task.AgentConfig{
			Type:         "gemini",
			Instructions: "fix the bug",
			Model:        "gemini-2.5-flash-lite",
			SkipPlanning: true,
		},
	}

	args := r.buildArgs(tk, &storage.Execution{ID: "test-exec"}, "/tmp/q.json")

	// Gemini CLI: instructions passed via -p for non-interactive mode
	if len(args) < 2 || args[0] != "-p" || args[1] != "fix the bug" {
		t.Errorf("expected -p <instructions> as first args, got: %v", args)
	}

	argMap := make(map[string]bool)
	for _, a := range args {
		argMap[a] = true
	}
	for _, want := range []string{"--output-format", "stream-json", "--model", "gemini-2.5-flash-lite"} {
		if !argMap[want] {
			t.Errorf("missing arg %q in %v", want, args)
		}
	}
}

func TestGeminiRunner_BuildArgs_PreamblePrepended(t *testing.T) {
	r := &GeminiRunner{}
	tk := &task.Task{
		Agent: task.AgentConfig{
			Type:         "gemini",
			Instructions: "fix the bug",
			SkipPlanning: false,
		},
	}

	args := r.buildArgs(tk, &storage.Execution{ID: "test-exec"}, "/tmp/q.json")

	if len(args) < 2 || args[0] != "-p" {
		t.Fatalf("expected -p <instructions> as first args, got: %v", args)
	}
	if !strings.HasPrefix(args[1], planningPreamble) {
		t.Errorf("instructions should start with planning preamble")
	}
	if !strings.HasSuffix(args[1], "fix the bug") {
		t.Errorf("instructions should end with original instructions")
	}
}

func TestGeminiRunner_BuildArgs_IncludesYolo(t *testing.T) {
	r := &GeminiRunner{}
	tk := &task.Task{
		Agent: task.AgentConfig{
			Type:         "gemini",
			Instructions: "write a doc",
			SkipPlanning: true,
		},
	}
	args := r.buildArgs(tk, &storage.Execution{ID: "test-exec"}, "/tmp/q.json")
	argMap := make(map[string]bool)
	for _, a := range args {
		argMap[a] = true
	}
	if !argMap["--yolo"] {
		t.Errorf("expected --yolo in gemini args (enables all tools); got: %v", args)
	}
}

func TestGeminiRunner_BuildArgs_IncludesPromptFlag(t *testing.T) {
	r := &GeminiRunner{}
	tk := &task.Task{
		Agent: task.AgentConfig{
			Type:         "gemini",
			Instructions: "do the thing",
			SkipPlanning: true,
		},
	}
	args := r.buildArgs(tk, &storage.Execution{ID: "test-exec"}, "/tmp/q.json")
	// Instructions must be passed via -p/--prompt for non-interactive headless mode,
	// not as a bare positional (which starts interactive mode).
	found := false
	for i, a := range args {
		if (a == "-p" || a == "--prompt") && i+1 < len(args) && args[i+1] == "do the thing" {
			found = true
			break
		}
	}
	if !found {
		t.Errorf("expected instructions passed via -p/--prompt flag; got: %v", args)
	}
}

func TestGeminiRunner_Run_InaccessibleProjectDir_ReturnsError(t *testing.T) {
	r := &GeminiRunner{
		BinaryPath: "true", // would succeed if it ran
		Logger:     slog.New(slog.NewTextHandler(io.Discard, nil)),
		LogDir:     t.TempDir(),
	}
	tk := &task.Task{
		Agent: task.AgentConfig{
			Type:         "gemini",
			ProjectDir:   "/nonexistent/path/does/not/exist",
			SkipPlanning: true,
		},
	}
	exec := &storage.Execution{ID: "test-exec"}

	err := r.Run(context.Background(), tk, exec)

	if err == nil {
		t.Fatal("expected error for inaccessible project_dir, got nil")
	}
	if !strings.Contains(err.Error(), "project_dir") {
		t.Errorf("expected 'project_dir' in error, got: %v", err)
	}
}

func TestGeminiRunner_BinaryPath_Default(t *testing.T) {
	r := &GeminiRunner{}
	if r.binaryPath() != "gemini" {
		t.Errorf("want 'gemini', got %q", r.binaryPath())
	}
}

func TestGeminiRunner_BinaryPath_Custom(t *testing.T) {
	r := &GeminiRunner{BinaryPath: "/usr/local/bin/gemini"}
	if r.binaryPath() != "/usr/local/bin/gemini" {
		t.Errorf("want custom path, got %q", r.binaryPath())
	}
}


func TestParseGeminiStream_ParsesStructuredOutput(t *testing.T) {
	// Simulate a stream-json input with various message types, including a result with error and cost.
	input := streamLine(`{"type":"content_block_start","content_block":{"text":"Hello,"}}`) +
		streamLine(`{"type":"content_block_delta","content_block":{"text":" World!"}}`) +
		streamLine(`{"type":"content_block_end"}`) +
		streamLine(`{"type":"result","subtype":"error_during_execution","is_error":true,"result":"something went wrong","total_cost_usd":0.123}`)

	reader := strings.NewReader(input)
	var writer bytes.Buffer // To capture what's written to the output log
	logger := slog.New(slog.NewTextHandler(io.Discard, nil))

	cost, err := parseGeminiStream(reader, &writer, logger)

	if err == nil {
		t.Errorf("expected an error, got nil")
	}
	if !strings.Contains(err.Error(), "something went wrong") {
		t.Errorf("expected error message to contain 'something went wrong', got: %v", err)
	}

	if cost != 0.123 {
		t.Errorf("expected cost 0.123, got %f", cost)
	}

	// Verify that the writer received the content (even if parseGeminiStream isn't fully parsing it yet)
	expectedWriterContent := input
	if writer.String() != expectedWriterContent {
		t.Errorf("writer content mismatch:\nwant:\n%s\ngot:\n%s", expectedWriterContent, writer.String())
	}
}

// TestGeminiRunner_Run_ProjectDir_RunsInSandbox verifies that when project_dir
// is set, the gemini subprocess runs inside a sandbox clone — not in
// project_dir itself.
func TestGeminiRunner_Run_ProjectDir_RunsInSandbox(t *testing.T) {
	projectDir := t.TempDir()
	initGitRepo(t, projectDir)

	logDir := t.TempDir()
	cwdFile := filepath.Join(logDir, "gemini-cwd.txt")

	// Fake gemini binary that records its $PWD then exits 0.
	scriptPath := filepath.Join(t.TempDir(), "fake-gemini.sh")
	script := "#!/bin/sh\nprintf '%s' \"$PWD\" > " + cwdFile + "\n"
	if err := os.WriteFile(scriptPath, []byte(script), 0755); err != nil {
		t.Fatalf("write script: %v", err)
	}

	r := &GeminiRunner{
		BinaryPath: scriptPath,
		Logger:     slog.New(slog.NewTextHandler(io.Discard, nil)),
		LogDir:     logDir,
	}
	tk := &task.Task{
		Agent: task.AgentConfig{
			Type:         "gemini",
			Instructions: "do work",
			ProjectDir:   projectDir,
			SkipPlanning: true,
		},
	}
	e := &storage.Execution{ID: "sandbox-exec", TaskID: "task-1"}

	if err := r.Run(context.Background(), tk, e); err != nil {
		t.Fatalf("Run: %v", err)
	}

	got, err := os.ReadFile(cwdFile)
	if err != nil {
		t.Fatalf("cwd file not written: %v", err)
	}
	cwd := string(got)
	if cwd == projectDir {
		t.Errorf("ran directly in project_dir; expected sandbox clone (cwd=%q)", cwd)
	}
	// Sandbox should be removed after successful teardown (no edits → nothing to push).
	// We can't assert the exact dir, but it should not be projectDir.
}

// TestGeminiRunner_Run_BlockedError_IncludesSandboxDir verifies that when the
// agent writes a question file before exiting, the BlockedError carries the
// sandbox path so resume runs in the same dir.
func TestGeminiRunner_Run_BlockedError_IncludesSandboxDir(t *testing.T) {
	src := t.TempDir()
	initGitRepo(t, src)
	logDir := t.TempDir()

	scriptPath := filepath.Join(t.TempDir(), "fake-gemini.sh")
	if err := os.WriteFile(scriptPath, []byte(`#!/bin/sh
if [ -n "$CLAUDOMATOR_QUESTION_FILE" ]; then
  printf '{"text":"Should I continue?"}' > "$CLAUDOMATOR_QUESTION_FILE"
fi
`), 0755); err != nil {
		t.Fatalf("write script: %v", err)
	}

	r := &GeminiRunner{
		BinaryPath: scriptPath,
		Logger:     slog.New(slog.NewTextHandler(io.Discard, nil)),
		LogDir:     logDir,
	}
	tk := &task.Task{
		Agent: task.AgentConfig{
			Type:         "gemini",
			Instructions: "do something",
			ProjectDir:   src,
			SkipPlanning: true,
		},
	}
	e := &storage.Execution{ID: "blocked-gemini-exec", TaskID: "task-1"}

	err := r.Run(context.Background(), tk, e)

	var blocked *BlockedError
	if !errors.As(err, &blocked) {
		t.Fatalf("expected BlockedError, got: %v", err)
	}
	if blocked.SandboxDir == "" {
		t.Error("BlockedError.SandboxDir should be set when gemini task runs in a sandbox")
	}
	if _, statErr := os.Stat(blocked.SandboxDir); os.IsNotExist(statErr) {
		t.Error("sandbox directory should be preserved when blocked")
	} else {
		os.RemoveAll(blocked.SandboxDir)
	}
}

// TestGeminiRunner_Run_ExecError_PreservesSandbox verifies that when gemini
// exits non-zero, the sandbox path is included in the wrapped error so the
// user can inspect partial work.
func TestGeminiRunner_Run_ExecError_PreservesSandbox(t *testing.T) {
	src := t.TempDir()
	initGitRepo(t, src)
	logDir := t.TempDir()

	// "false" exits 1, no output.
	r := &GeminiRunner{
		BinaryPath: "false",
		Logger:     slog.New(slog.NewTextHandler(io.Discard, nil)),
		LogDir:     logDir,
	}
	tk := &task.Task{
		Agent: task.AgentConfig{
			Type:         "gemini",
			Instructions: "do something",
			ProjectDir:   src,
			SkipPlanning: true,
		},
	}
	e := &storage.Execution{ID: "err-gemini-exec", TaskID: "task-1"}

	err := r.Run(context.Background(), tk, e)
	if err == nil {
		t.Fatal("expected error from failing gemini exit")
	}
	if !strings.Contains(err.Error(), "sandbox preserved at ") {
		t.Errorf("expected error to include sandbox path; got: %v", err)
	}
	// Extract path and verify it exists.
	idx := strings.Index(err.Error(), "sandbox preserved at ")
	rest := err.Error()[idx+len("sandbox preserved at "):]
	rest = strings.TrimSuffix(rest, ")")
	rest = strings.TrimSpace(rest)
	if _, statErr := os.Stat(rest); os.IsNotExist(statErr) {
		t.Errorf("sandbox path from error should exist on disk: %q", rest)
	} else {
		os.RemoveAll(rest)
	}
}

// TestGeminiRunner_Run_ResumeUsesStoredSandboxDir verifies that a resume
// execution runs in the preserved SandboxDir rather than cloning fresh.
func TestGeminiRunner_Run_ResumeUsesStoredSandboxDir(t *testing.T) {
	logDir := t.TempDir()
	sandboxDir := t.TempDir()
	initGitRepo(t, sandboxDir)
	cwdFile := filepath.Join(logDir, "cwd.txt")

	scriptPath := filepath.Join(t.TempDir(), "fake-gemini.sh")
	script := "#!/bin/sh\nprintf '%s' \"$PWD\" > " + cwdFile + "\n"
	if err := os.WriteFile(scriptPath, []byte(script), 0755); err != nil {
		t.Fatalf("write script: %v", err)
	}

	r := &GeminiRunner{
		BinaryPath: scriptPath,
		Logger:     slog.New(slog.NewTextHandler(io.Discard, nil)),
		LogDir:     logDir,
	}
	tk := &task.Task{
		Agent: task.AgentConfig{
			Type:         "gemini",
			SkipPlanning: true,
		},
	}
	e := &storage.Execution{
		ID:              "resume-gemini-1",
		TaskID:          "task-resume",
		ResumeSessionID: "session-abc",
		SandboxDir:      sandboxDir,
	}

	if err := r.Run(context.Background(), tk, e); err != nil {
		t.Fatalf("Run with preserved sandbox: %v", err)
	}

	got, err := os.ReadFile(cwdFile)
	if err != nil {
		t.Fatalf("cwd file not written: %v", err)
	}
	if string(got) != sandboxDir {
		t.Errorf("resume should run in preserved sandbox; got cwd=%q want %q", got, sandboxDir)
	}
}

// TestGeminiRunner_Run_StaleSandboxDir_ClonesAfresh verifies that a resume
// pointing at a missing sandbox falls back to cloning a fresh sandbox from
// project_dir rather than failing outright.
func TestGeminiRunner_Run_StaleSandboxDir_ClonesAfresh(t *testing.T) {
	logDir := t.TempDir()
	projectDir := t.TempDir()
	initGitRepo(t, projectDir)

	cwdFile := filepath.Join(logDir, "cwd.txt")
	scriptPath := filepath.Join(t.TempDir(), "fake-gemini.sh")
	script := "#!/bin/sh\nprintf '%s' \"$PWD\" > " + cwdFile + "\n"
	if err := os.WriteFile(scriptPath, []byte(script), 0755); err != nil {
		t.Fatalf("write script: %v", err)
	}

	r := &GeminiRunner{
		BinaryPath: scriptPath,
		Logger:     slog.New(slog.NewTextHandler(io.Discard, nil)),
		LogDir:     logDir,
	}
	tk := &task.Task{
		Agent: task.AgentConfig{
			Type:         "gemini",
			ProjectDir:   projectDir,
			SkipPlanning: true,
		},
	}
	staleSandbox := filepath.Join(t.TempDir(), "gone")
	e := &storage.Execution{
		ID:              "resume-gemini-2",
		TaskID:          "task-stale",
		ResumeSessionID: "session-xyz",
		SandboxDir:      staleSandbox,
	}

	if err := r.Run(context.Background(), tk, e); err != nil {
		t.Fatalf("Run with stale sandbox: %v", err)
	}

	got, err := os.ReadFile(cwdFile)
	if err != nil {
		t.Fatalf("cwd file not written: %v", err)
	}
	cwd := string(got)
	if cwd == staleSandbox {
		t.Error("ran in stale (nonexistent) sandbox dir")
	}
	if cwd == projectDir {
		t.Error("ran directly in project_dir; expected a fresh sandbox clone")
	}
}

// TestGeminiRunner_Run_NoProjectDir_SkipsSandbox verifies that a task with no
// project_dir doesn't trigger sandbox setup (matches LocalRunner/non-coding
// task semantics).
func TestGeminiRunner_Run_NoProjectDir_SkipsSandbox(t *testing.T) {
	logDir := t.TempDir()

	r := &GeminiRunner{
		BinaryPath: "true", // exits 0, no output
		Logger:     slog.New(slog.NewTextHandler(io.Discard, nil)),
		LogDir:     logDir,
	}
	tk := &task.Task{
		Agent: task.AgentConfig{
			Type:         "gemini",
			Instructions: "summarize: 2+2",
			SkipPlanning: true,
			// No ProjectDir
		},
	}
	e := &storage.Execution{ID: "no-pd-gemini", TaskID: "task-nopd"}

	if err := r.Run(context.Background(), tk, e); err != nil {
		t.Fatalf("Run without project_dir: %v", err)
	}
	if e.SandboxDir != "" {
		t.Errorf("SandboxDir should be empty for tasks without project_dir, got %q", e.SandboxDir)
	}
}