-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmetaai_coverage_test.go
More file actions
229 lines (203 loc) Β· 6.13 KB
/
Copy pathmetaai_coverage_test.go
File metadata and controls
229 lines (203 loc) Β· 6.13 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package metaai
import (
"context"
"net/http"
"strings"
"testing"
)
// Root package coverage tests.
// Mock only at HTTP boundary, test behavior from caller perspective.
func TestTruncateLongStringAddsEllipsis(t *testing.T) {
long := "this is a very long string that exceeds the limit"
got := truncate(long, 10)
if !strings.HasSuffix(got, "β¦") {
t.Errorf("truncated should end with ellipsis, got %q", got)
}
}
func TestTruncateShortStringUnchanged(t *testing.T) {
short := "hi"
got := truncate(short, 10)
if got != short {
t.Errorf("got %q, want %q", got, short)
}
}
func TestResolveConversationIDTopicOverridden(t *testing.T) {
c, _ := NewClient(WithCookies(map[string]string{"datr": "d"}))
c.topics["mytopic"] = "topic-conv-id"
got := c.resolveConversationID(&ChatOptions{Topic: "mytopic"})
if got != "topic-conv-id" {
t.Errorf("got %q, want 'topic-conv-id'", got)
}
}
func TestResolveConversationIDNewConversationReturnsEmpty(t *testing.T) {
c, _ := NewClient(WithCookies(map[string]string{"datr": "d"}))
c.externalConversationID = "existing-conv"
got := c.resolveConversationID(&ChatOptions{NewConversation: true})
if got != "" {
t.Errorf("got %q, want empty for new conversation", got)
}
}
func TestResolveConversationIDUsesExternalWhenNoOpts(t *testing.T) {
c, _ := NewClient(WithCookies(map[string]string{"datr": "d"}))
c.externalConversationID = "stored-conv"
got := c.resolveConversationID(nil)
if got != "stored-conv" {
t.Errorf("got %q, want 'stored-conv'", got)
}
}
func TestResolveChatConfigPriorityCallOverridesTopic(t *testing.T) {
c, _ := NewClient(WithCookies(map[string]string{"datr": "d"}))
c.SetTopicConfig("t1", boolPtrCov(true), nil, strPtrCov("learn"))
thinking, _, _ := c.resolveChatConfig(&ChatOptions{
Topic: "t1",
Thinking: boolPtrCov(false),
})
if thinking {
t.Error("call override should win over topic: thinking should be false")
}
}
func TestResolveChatConfigDefaultsFromConstructor(t *testing.T) {
c, _ := NewClient(
WithCookies(map[string]string{"datr": "d"}),
WithDefaultThinking(true),
WithDefaultMode("analyze"),
)
thinking, _, mode := c.resolveChatConfig(nil)
if !thinking {
t.Error("default thinking should be true")
}
if mode != "analyze" {
t.Errorf("mode = %q, want 'analyze'", mode)
}
}
func TestIsValidModeAcceptsKnownModes(t *testing.T) {
for _, m := range []string{ModeLearn, ModeAnalyze, ModeCreateImage, ModeCreateVideo} {
if !isValidMode(m) {
t.Errorf("mode %q should be valid", m)
}
}
}
func TestIsValidModeRejectsUnknown(t *testing.T) {
if isValidMode("invalid") {
t.Error("invalid mode should be rejected")
}
}
func TestToIntConvertsNumbers(t *testing.T) {
cases := []struct {
input any
want int
ok bool
}{
{200, 200, true},
{int64(200), 200, true},
{float64(200), 200, true},
{"200", 0, false},
}
for _, tc := range cases {
got, ok := toInt(tc.input)
if ok != tc.ok || (ok && got != tc.want) {
t.Errorf("toInt(%v) = %d, %v; want %d, %v", tc.input, got, ok, tc.want, tc.ok)
}
}
}
func TestAttachCookiesSetsHeader(t *testing.T) {
req, _ := http.NewRequest("GET", "http://example.com", nil)
attachCookies(req, map[string]string{"datr": "d", "ecto_1_sess": "s"})
if req.Header.Get("Cookie") == "" {
t.Error("cookie header not set")
}
}
func TestAttachCookiesSkipsWhenEmpty(t *testing.T) {
req, _ := http.NewRequest("GET", "http://example.com", nil)
attachCookies(req, nil)
if req.Header.Get("Cookie") != "" {
t.Error("cookie header should be empty for nil cookies")
}
}
func TestNewClientWithProxySetsTransport(t *testing.T) {
c, err := NewClient(
WithCookies(map[string]string{"datr": "d"}),
WithProxy("http://proxy.example.com:8080"),
)
if err != nil {
t.Fatal(err)
}
if c.http == nil {
t.Error("HTTP client not set")
}
}
func TestNewClientWithCustomTimeout(t *testing.T) {
c, _ := NewClient(
WithCookies(map[string]string{"datr": "d"}),
WithHTTPTimeout(5000000000),
)
if c.cfg.HTTPTimeout != 5000000000 {
t.Error("timeout not set")
}
}
func TestNewClientWithDotEnvDisabled(t *testing.T) {
c, _ := NewClient(
WithCookies(map[string]string{"datr": "d"}),
WithDotEnv(false, ""),
)
if c.cfg.LoadDotEnv {
t.Error("dotenv should be disabled")
}
}
func TestCloseClippyWhenNoConnection(t *testing.T) {
c, _ := NewClient(WithCookies(map[string]string{"datr": "d"}))
c.closeClippy() // should not panic
}
func TestGenerateImageHandlesAuthError(t *testing.T) {
c, _ := NewClient(WithCookies(map[string]string{"datr": "d"}))
_, err := c.GenerateImage(context.Background(), "test", "SQUARE", 1)
if err == nil {
t.Error("expected error without access token")
}
}
func TestGenerateVideoHandlesAuthError(t *testing.T) {
c, _ := NewClient(WithCookies(map[string]string{"datr": "d"}))
_, err := c.GenerateVideo(context.Background(), "test")
if err == nil {
t.Error("expected error without access token")
}
}
func TestGetConversationHistoryHandlesAuthError(t *testing.T) {
c, _ := NewClient(WithCookies(map[string]string{"datr": "d"}))
_, err := c.GetConversationHistory(context.Background(), 5, 0)
if err == nil {
t.Error("expected error without access token")
}
}
func TestAnalyzeImageRejectsMissingMediaID(t *testing.T) {
c, _ := NewClient(WithCookies(map[string]string{"datr": "d"}))
_, err := c.AnalyzeImage(context.Background(), "", "", "what?", nil)
if err == nil {
t.Error("expected error for missing media ID")
}
}
func TestVibesHandlesAuthError(t *testing.T) {
c, _ := NewClient(WithCookies(map[string]string{"datr": "d"}))
_, err := c.Vibes(context.Background(), VibesList, "")
if err == nil {
t.Error("expected error without access token")
}
}
func TestGraphQLErrorCodeExtraction(t *testing.T) {
e := graphQLError{
Message: "test",
Extensions: map[string]any{"code": "GRAPHQL_VALIDATION_FAILED"},
}
if e.code() != "GRAPHQL_VALIDATION_FAILED" {
t.Errorf("code = %q", e.code())
}
}
func TestGraphQLErrorCodeEmptyWhenNoExtensions(t *testing.T) {
e := graphQLError{Message: "test"}
if e.code() != "" {
t.Errorf("code = %q, want empty", e.code())
}
}
// helpers (prefixed to avoid collision with session_test.go)
func boolPtrCov(b bool) *bool { return &b }
func strPtrCov(s string) *string { return &s }