-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcmd_test.go
More file actions
183 lines (177 loc) · 5.44 KB
/
cmd_test.go
File metadata and controls
183 lines (177 loc) · 5.44 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
package main
import (
"flag"
"os"
"path/filepath"
"testing"
"time"
"google.golang.org/genai"
)
func TestArgsInvalid(t *testing.T) {
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
testCases := []struct {
name string
args []string
interactive bool
expected bool
}{
{
name: "piped content not referenced",
args: []string{"-f", "example.xml", "what does this code do?"},
interactive: false,
expected: true,
},
{
name: "pipe content into gen",
args: []string{"-f", "-", "what does this code do?"},
interactive: false,
expected: false,
},
{
name: "system prompt from stdin and prompt as argument",
args: []string{"-s", "-f", "-", "ten names for flowers"},
interactive: false,
expected: false,
},
{
name: "system prompt as file and argument from pipe",
args: []string{"-f", "editor.sprompt", "-"},
interactive: false,
expected: false,
},
{
name: "chat with input from pipe",
args: []string{"-c", "-"},
interactive: false,
expected: false,
},
{
name: "two files and prompt",
args: []string{"-t", "-f", "../twitter/img/123497680.jpg", "-f", "../twitter/img/123406895.jpg", "what are the differences between these photos?"},
interactive: true,
expected: false,
},
{
name: "no flag no prompt",
args: []string{},
interactive: true,
expected: true,
},
{
name: "-tool with prompt",
args: []string{"-tool", "list available models"},
interactive: true,
expected: false,
},
{
name: "system flag no prompt, no stdin",
args: []string{"-s"},
interactive: true,
expected: true,
},
{
name: "chat mode no prompt",
args: []string{"-c"},
interactive: true,
expected: true,
},
{
name: "chat mode with prompt",
args: []string{"-c", "ten names for money"},
interactive: true,
expected: false,
},
{
name: "-c and -s with prompt",
args: []string{"-s", "-c", "ten names for money"},
interactive: true,
expected: false,
},
{
name: "path to system prompt and prompt argument",
args: []string{"-f", "french.sprompt", "ten names for flowers"},
interactive: true,
expected: false,
},
{
name: "-code with prompt",
args: []string{"-code", "Levenshtein distance between Paris and Praha"},
interactive: true,
expected: false,
},
{
name: "embed and digest path but nothing to embed",
args: []string{"-e", "-d", "/path/to/digest"},
interactive: true,
expected: true,
},
{
name: "path to regular prompt with parameter",
args: []string{"-f", "/path/to/some.prompt", "-p", "key=value"},
interactive: true,
expected: false,
},
{
name: "parameterized prompt",
args: []string{"-p", "a=1", "-p", "b=2", "complete this sentence: replace {a} apple with {a} banana and {b} oranges for a good ..."},
interactive: true,
expected: false,
},
}
var params *Parameters
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Reset params for each test case.
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
params = &Parameters{} // Re-initialize the params struct
keyVals := ParamMap{} // Reset the keyVals map
params.FilePaths = ParamArray{}
params.DigestPaths = ParamArray{}
flag.BoolVar(¶ms.Verbose, "V", false, "")
flag.BoolVar(¶ms.SegmentBackground, "b", false, "")
flag.BoolVar(¶ms.ChatMode, "c", false, "")
flag.BoolVar(¶ms.CodeGen, "code", false, "")
flag.Var(¶ms.DigestPaths, "d", "")
flag.BoolVar(¶ms.Embed, "e", false, "")
flag.Var(¶ms.FilePaths, "f", "")
flag.BoolVar(¶ms.GoogleSearch, "g", false, "")
flag.BoolVar(¶ms.Help, "h", false, "")
flag.BoolVar(¶ms.ImgModality, "img", false, "")
flag.BoolVar(¶ms.JSON, "json", false, "")
flag.IntVar(¶ms.K, "k", 3, "")
flag.Float64Var(¶ms.Lambda, "l", 0.5, "")
flag.Func("think", "", func(v string) error {
params.ThinkingLevel = genai.ThinkingLevelUnspecified
return nil
})
flag.StringVar(¶ms.GenModel, "m", "gemini-2.0-flash", "")
flag.Var(¶ms.MCPServers, "mcp", "")
flag.BoolVar(¶ms.OnlyKvs, "o", false, "")
flag.Var(&keyVals, "p", "")
flag.BoolVar(¶ms.SystemInstruction, "s", false, "")
flag.BoolVar(¶ms.Segment, "seg", false, "")
flag.BoolVar(¶ms.TokenCount, "t", false, "")
flag.Float64Var(¶ms.Temp, "temp", 1.0, "")
flag.DurationVar(¶ms.Timeout, "timeout", 90*time.Second, "")
flag.BoolVar(¶ms.Tool, "tool", false, "")
flag.Float64Var(¶ms.TopP, "top_p", 0.95, "")
flag.BoolVar(¶ms.Unsafe, "unsafe", false, "")
flag.BoolVar(¶ms.Version, "v", false, "")
flag.BoolVar(¶ms.Walk, "w", false, "")
progName := filepath.Base(t.Name())
os.Args = []string{progName}
os.Args = append(os.Args, tc.args...)
err := flag.CommandLine.Parse(os.Args[1:])
if err != nil {
t.Fatalf("Error parsing params: %v", err)
}
params.Args = flag.CommandLine.Args()
params.Interactive = tc.interactive
actual := isArgsInvalid(params, keyVals)
if (actual != nil) != tc.expected {
t.Errorf("For test case '%s', expected %t, but got %t, Args: %v", tc.name, tc.expected, actual, tc.args)
}
})
}
}