-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
146 lines (135 loc) · 3.62 KB
/
Copy pathclient_test.go
File metadata and controls
146 lines (135 loc) · 3.62 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
package gosparkclient
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestNewSparkClient(t *testing.T) {
tests := []struct {
name string
opts []ConfigOption
wantErr bool
errContains string
}{
{
name: "valid configuration",
opts: []ConfigOption{
WithCredentials("appid", "apikey", "secret"),
WithURLs("http://example.com", "http://emb.example.com"),
WithDomain("domain"),
},
wantErr: false,
},
{
name: "missing credentials",
opts: []ConfigOption{
WithURLs("http://example.com", "http://emb.example.com"),
},
wantErr: true,
errContains: "AppID is required",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client, err := NewSparkClient(tt.opts...)
if tt.wantErr {
if err == nil {
t.Error("expected error, got nil")
} else if tt.errContains != "" && !strings.Contains(err.Error(), tt.errContains) {
t.Errorf("error = %v, want error containing %v", err, tt.errContains)
}
return
}
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if client == nil {
t.Error("expected client, got nil")
}
})
}
}
func TestSparkClient_ChatSimple(t *testing.T) {
// Create a mock server
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{
"header": {
"code": 0,
"message": "success",
"sid": "test-sid",
"status": 2
},
"payload": {
"choices": {
"status": 2,
"seq": 1,
"text": [
{
"content": "test response",
"role": "assistant",
"index": 0
}
]
},
"usage": {
"text": {
"question_tokens": 10,
"prompt_tokens": 10,
"completion_tokens": 20,
"total_tokens": 30
}
}
}
}`))
}))
defer mockServer.Close()
client, err := NewSparkClient(
WithCredentials("test-app-id", "test-api-key", "test-secret"),
WithURLs(mockServer.URL, mockServer.URL),
WithTimeout(time.Second),
)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
ctx := context.Background()
resp, err := client.ChatSimple(ctx, "Hello")
if err != nil {
t.Fatalf("ChatSimple failed: %v", err)
}
if resp == nil {
t.Fatal("expected response, got nil")
}
}
func TestSparkClient_WithNewConfig(t *testing.T) {
client, err := NewSparkClient(
WithCredentials("test-app-id", "test-api-key", "test-secret"),
WithURLs("http://example.com", "http://emb.example.com"),
)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
// Test creating new client with updated config
newClient, err := client.WithNewConfig(
WithTimeout(time.Second*60),
WithUID("new-uid"),
)
if err != nil {
t.Errorf("WithNewConfig failed: %v", err)
}
if newClient.config.Timeout != time.Second*60 {
t.Errorf("expected timeout 60s, got %v", newClient.config.Timeout)
}
if newClient.config.UID != "new-uid" {
t.Errorf("expected UID 'new-uid', got %v", newClient.config.UID)
}
// Verify original client remains unchanged
if client.config.Timeout == time.Second*60 {
t.Error("original client timeout should not have changed")
}
if client.config.UID == "new-uid" {
t.Error("original client UID should not have changed")
}
}