Repository navigation
Expand file tree
/
Copy pathmain_validation_test.go
More file actions
191 lines (182 loc) · 6.72 KB
/
Copy pathmain_validation_test.go
File metadata and controls
191 lines (182 loc) · 6.72 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
package main
import (
"testing"
"github.com/Quality-Max/qmax-code/internal/agent"
)
// TestIsValidModelName is the regression test for QUA-579: pre-fix, the
// -model flag accepted any string and forwarded it to the API, where it
// produced a confusing 400/401 instead of a clear local error.
func TestIsValidModelName(t *testing.T) {
cases := []struct {
in string
want bool
}{
// Shorthands resolved by resolveModel.
{"auto", true},
{"sonnet", true},
{"opus", true},
{"haiku", true},
{"AUTO", true}, // case-insensitive
// Real model IDs from api.Model* constants.
{"claude-opus-5-5", true},
{"claude-opus-4-8", true},
{"claude-opus-4-7", true},
{"claude-sonnet-5", true},
{"claude-fable-5", true},
{"claude-fable-5-1", true},
{"fable", true},
{"claude-sonnet-4-6", true},
{"claude-haiku-4-5-20251001", true},
// Rejected.
{"nonsense", false},
{"claude-future-model-9-0", false},
{"gpt-4", false},
{"", false},
{"opus4", false},
}
for _, c := range cases {
if got := isValidModelName(c.in); got != c.want {
t.Errorf("isValidModelName(%q) = %v, want %v", c.in, got, c.want)
}
}
}
func TestResolveLocalOnly(t *testing.T) {
tests := []struct {
name string
flagEnabled, persisted, env bool
want bool
}{
{name: "all disabled", want: false},
{name: "flag", flagEnabled: true, want: true},
{name: "persisted config", persisted: true, want: true},
{name: "environment", env: true, want: true},
{name: "multiple sources", flagEnabled: true, persisted: true, env: true, want: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := resolveLocalOnly(tt.flagEnabled, tt.persisted, tt.env); got != tt.want {
t.Fatalf("resolveLocalOnly(%v, %v, %v) = %v, want %v",
tt.flagEnabled, tt.persisted, tt.env, got, tt.want)
}
})
}
}
func TestShouldRunInteractiveSetup(t *testing.T) {
tests := []struct {
name string
localOnly bool
hasAPIClient bool
want bool
}{
{name: "standalone skips setup", localOnly: true, want: false},
{name: "unconfigured connected mode runs setup", want: true},
{name: "direct API available", hasAPIClient: true, want: false},
{name: "standalone with credentials still skips", localOnly: true, hasAPIClient: true, want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := shouldRunInteractiveSetup(tt.localOnly, tt.hasAPIClient); got != tt.want {
t.Fatalf("shouldRunInteractiveSetup(%v, %v) = %v, want %v",
tt.localOnly, tt.hasAPIClient, got, tt.want)
}
})
}
}
func TestHeadlessSetupFallback(t *testing.T) {
tests := []struct {
name string
stdinTTY bool
want bool
}{
{name: "piped stdin falls back to standalone", stdinTTY: false, want: true},
{name: "terminal stdin keeps interactive onboarding", stdinTTY: true, want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := headlessSetupFallback(tt.stdinTTY); got != tt.want {
t.Fatalf("headlessSetupFallback(%v) = %v, want %v", tt.stdinTTY, got, tt.want)
}
})
}
}
// TestShouldOfferAnthropicKey is the regression test for the "no ability to
// skip the Anthropic key" bug: the offer must stay skippable, must not
// re-ask after onboarding already asked, and must stay out of CLI backend
// modes, headless runs, and local-only runs with Ollama configured.
func TestShouldOfferAnthropicKey(t *testing.T) {
tests := []struct {
name string
cliBackend string
anthropicKey string
localOnly bool
ollamaConfigured bool
stdinTTY bool
alreadyOffered bool
want bool
}{
{name: "interactive api backend without key offers", stdinTTY: true, want: true},
{name: "key present never offers", anthropicKey: "sk-ant-x", stdinTTY: true, want: false},
{name: "cli backend never offers", cliBackend: "cc", stdinTTY: true, want: false},
{name: "headless never offers", want: false},
{name: "local ollama never offers", localOnly: true, ollamaConfigured: true, stdinTTY: true, want: false},
{name: "local without ollama still offers", localOnly: true, stdinTTY: true, want: true},
{name: "onboarding already offered skips re-ask", stdinTTY: true, alreadyOffered: true, want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := shouldOfferAnthropicKey(tt.cliBackend, tt.anthropicKey, tt.localOnly, tt.ollamaConfigured, tt.stdinTTY, tt.alreadyOffered); got != tt.want {
t.Fatalf("shouldOfferAnthropicKey(%q, %q, %v, %v, %v, %v) = %v, want %v",
tt.cliBackend, tt.anthropicKey, tt.localOnly, tt.ollamaConfigured, tt.stdinTTY, tt.alreadyOffered, got, tt.want)
}
})
}
}
// TestShouldExitWithoutInference pins the post-fix contract: an interactive
// run that skipped the Anthropic key enters the REPL instead of exiting;
// only headless runs without any inference still fail fast.
func TestShouldExitWithoutInference(t *testing.T) {
tests := []struct {
name string
cliBackend string
anthropicKey string
localOnly bool
ollamaConfigured bool
stdinTTY bool
want bool
}{
{name: "interactive skip no longer exits", stdinTTY: true, want: false},
{name: "headless one-shot still exits", want: true},
{name: "headless with key does not exit", anthropicKey: "sk-ant-x", want: false},
{name: "cli backend never exits", cliBackend: "codex", want: false},
{name: "local ollama never exits", localOnly: true, ollamaConfigured: true, want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := shouldExitWithoutInference(tt.cliBackend, tt.anthropicKey, tt.localOnly, tt.ollamaConfigured, tt.stdinTTY); got != tt.want {
t.Fatalf("shouldExitWithoutInference(%q, %q, %v, %v, %v) = %v, want %v",
tt.cliBackend, tt.anthropicKey, tt.localOnly, tt.ollamaConfigured, tt.stdinTTY, got, tt.want)
}
})
}
}
func TestShouldUseStreamingBuiltIn(t *testing.T) {
tests := []struct {
name string
ag *agent.Agent
want bool
}{
{name: "nil agent", want: false},
{name: "direct API", ag: &agent.Agent{}, want: false},
{name: "Cerebras", ag: &agent.Agent{Cerebras: &agent.CerebrasClient{}}, want: true},
{name: "Ollama full", ag: &agent.Agent{Ollama: &agent.OllamaClient{}, Mode: agent.OllamaModeFull}, want: true},
{name: "Ollama chat", ag: &agent.Agent{Ollama: &agent.OllamaClient{}, Mode: agent.OllamaModeChat}, want: true},
{name: "Ollama off", ag: &agent.Agent{Ollama: &agent.OllamaClient{}, Mode: agent.OllamaModeOff}, want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := shouldUseStreamingBuiltIn(tt.ag); got != tt.want {
t.Fatalf("shouldUseStreamingBuiltIn() = %v, want %v", got, tt.want)
}
})
}
}