summaryrefslogtreecommitdiff
path: root/internal/executor/gemini_test.go
blob: 073525c0e4ba828855e5d5b154eef44f541111d3 (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
package executor

import (
	"context"
	"io"
	"log/slog"
	"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())
	}
}