Repository navigation
Expand file tree
/
Copy pathcli_test.go
More file actions
207 lines (179 loc) · 5.18 KB
/
Copy pathcli_test.go
File metadata and controls
207 lines (179 loc) · 5.18 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
package echo
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
)
// writeFakeCLI writes a tiny shell script to dir that echoes the args it
// received followed by stdin, so tests can assert how the CLIProvider wired
// up its command line.
func writeFakeCLI(t *testing.T, dir, name string) string {
t.Helper()
path := filepath.Join(dir, name)
script := `#!/bin/sh
echo "args: $@"
echo "--- stdin ---"
cat
`
if err := os.WriteFile(path, []byte(script), 0o755); err != nil {
t.Fatalf("write fake cli: %v", err)
}
return path
}
func TestCLIProvider_StdinPrompt(t *testing.T) {
dir := t.TempDir()
bin := writeFakeCLI(t, dir, "fake")
p := &CLIProvider{
Binary: bin,
ModelFlag: "--model",
ExtraArgs: []string{"-p"},
}
ctx := context.Background()
resp, err := p.call(ctx, QuickMessage("hello world"), CallConfig{Model: "opus"})
if err != nil {
t.Fatalf("call: %v", err)
}
if !strings.Contains(resp.Text, "args: -p --model opus") {
t.Errorf("expected args in output, got: %q", resp.Text)
}
if !strings.Contains(resp.Text, "[user]: hello world") {
t.Errorf("expected prompt delivered via stdin, got: %q", resp.Text)
}
if resp.Metadata["binary"] != bin {
t.Errorf("expected binary metadata %q, got %v", bin, resp.Metadata["binary"])
}
}
func TestCLIProvider_PromptAsArg(t *testing.T) {
dir := t.TempDir()
bin := writeFakeCLI(t, dir, "fake")
p := &CLIProvider{
Binary: bin,
ModelFlag: "-m",
ExtraArgs: []string{"exec"},
PromptAsArg: true,
}
ctx := context.Background()
resp, err := p.call(ctx, QuickMessage("hi"), CallConfig{Model: "gpt-5"})
if err != nil {
t.Fatalf("call: %v", err)
}
// Prompt should arrive as a positional arg, not via stdin.
if !strings.Contains(resp.Text, "args: exec -m gpt-5 [user]: hi") {
t.Errorf("expected prompt as arg, got: %q", resp.Text)
}
}
func TestCLIProvider_EnvVarOverride(t *testing.T) {
dir := t.TempDir()
bin := writeFakeCLI(t, dir, "override")
t.Setenv("ECHO_TEST_CLI_PATH", bin)
p := &CLIProvider{
Binary: "/nonexistent/claude",
EnvVar: "ECHO_TEST_CLI_PATH",
ModelFlag: "--model",
}
ctx := context.Background()
resp, err := p.call(ctx, QuickMessage("ping"), CallConfig{Model: "haiku"})
if err != nil {
t.Fatalf("call: %v", err)
}
if !strings.Contains(resp.Text, "--model haiku") {
t.Errorf("expected --model in output, got: %q", resp.Text)
}
}
func TestCLIProvider_SystemMessage(t *testing.T) {
dir := t.TempDir()
bin := writeFakeCLI(t, dir, "fake")
p := &CLIProvider{Binary: bin}
ctx := context.Background()
msgs := []Message{
{Role: System, Content: "be terse"},
{Role: User, Content: "hi"},
}
resp, err := p.call(ctx, msgs, CallConfig{Model: "opus"})
if err != nil {
t.Fatalf("call: %v", err)
}
if !strings.Contains(resp.Text, "[system]: be terse") {
t.Errorf("expected system message in prompt, got: %q", resp.Text)
}
if !strings.Contains(resp.Text, "[user]: hi") {
t.Errorf("expected user message in prompt, got: %q", resp.Text)
}
}
func TestCLIProvider_CallConfigSystemOverride(t *testing.T) {
dir := t.TempDir()
bin := writeFakeCLI(t, dir, "fake")
p := &CLIProvider{Binary: bin}
ctx := context.Background()
resp, err := p.call(ctx, QuickMessage("hi"), CallConfig{SystemMsg: "override"})
if err != nil {
t.Fatalf("call: %v", err)
}
if !strings.Contains(resp.Text, "[system]: override") {
t.Errorf("expected system override in prompt, got: %q", resp.Text)
}
}
func TestCLIProvider_FailurePropagatesStderr(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "broken")
script := `#!/bin/sh
echo "boom" >&2
exit 2
`
if err := os.WriteFile(path, []byte(script), 0o755); err != nil {
t.Fatalf("write broken cli: %v", err)
}
p := &CLIProvider{Binary: path}
_, err := p.call(context.Background(), QuickMessage("hi"), CallConfig{})
if err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), "boom") {
t.Errorf("expected stderr in error, got: %v", err)
}
}
func TestEnableLocalCLI(t *testing.T) {
client, err := NewClient()
if err != nil {
t.Fatalf("NewClient: %v", err)
}
EnableLocalCLI(client)
cc := client.(*CommonClient)
for _, name := range []string{"claude-cli", "codex-cli", "gemini-cli"} {
if _, ok := cc.providerMap[name]; !ok {
t.Errorf("expected %s to be registered", name)
}
}
// And must NOT be registered by default in NewCommonClient.
base, err := NewCommonClient(map[string]string{})
if err != nil {
t.Fatalf("NewCommonClient: %v", err)
}
bc := base.(*CommonClient)
for _, name := range []string{"claude-cli", "codex-cli", "gemini-cli"} {
if _, ok := bc.providerMap[name]; ok {
t.Errorf("%s should not be auto-registered", name)
}
}
}
func TestCLIProvider_RoutingThroughClient(t *testing.T) {
dir := t.TempDir()
bin := writeFakeCLI(t, dir, "fake")
client, err := NewClient(WithModel("claude-cli/opus"))
if err != nil {
t.Fatalf("NewClient: %v", err)
}
client.SetProvider("claude-cli", &CLIProvider{
Binary: bin,
ModelFlag: "--model",
})
resp, err := client.Complete(context.Background(), QuickMessage("hi"))
if err != nil {
t.Fatalf("Complete: %v", err)
}
if !strings.Contains(resp.Text, "--model opus") {
t.Errorf("expected model routed into CLI flag, got: %q", resp.Text)
}
}