-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_test.go
More file actions
180 lines (156 loc) · 5.01 KB
/
Copy pathintegration_test.go
File metadata and controls
180 lines (156 loc) · 5.01 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
//go:build integration
// Integration tests run against the live https://test.servicestack.net Services:
//
// go test -tags integration ./...
//
// Use SERVICESTACK_TEST_URL to run them against a different ServiceStack instance.
package servicestack_test
import (
"os"
"strings"
"testing"
ss "github.com/ServiceStack/servicestack-go"
"github.com/ServiceStack/servicestack-go/dtos"
)
// Model the ChatCompletion integration test uses, available on test.servicestack.net
const chatModel = "openai/gpt-oss-120b"
func testUrl() string {
if baseUrl := os.Getenv("SERVICESTACK_TEST_URL"); baseUrl != "" {
return baseUrl
}
return "https://test.servicestack.net"
}
func TestIntegrationSend(t *testing.T) {
client := ss.NewClient(testUrl())
res, err := ss.Send(client, dtos.Hello{Name: "World", Title: "Mr"})
if err != nil {
t.Fatalf("Send failed: %v", err)
}
if res.Result != "Hello, Mr. World!" {
t.Errorf("got %q, want Hello, Mr. World!", res.Result)
}
}
func TestIntegrationValidationErrors(t *testing.T) {
client := ss.NewClient(testUrl())
_, err := ss.Send(client, dtos.ThrowValidation{})
webEx, ok := ss.AsWebServiceException(err)
if !ok {
t.Fatalf("got %T, want *WebServiceException", err)
}
if webEx.StatusCode != 400 {
t.Errorf("got StatusCode %d, want 400", webEx.StatusCode)
}
if !webEx.IsValidationError() {
t.Fatalf("expected field errors, got %+v", webEx.ResponseStatus)
}
if !strings.Contains(webEx.FieldError("Age"), "must be between 1 and 120") {
t.Errorf("got Age FieldError %q", webEx.FieldError("Age"))
}
if webEx.FieldError("Email") == "" {
t.Error("expected an Email field error")
}
}
func TestIntegrationErrorStatusCodes(t *testing.T) {
client := ss.NewClient(testUrl())
_, err := ss.Send(client, dtos.ThrowType{Type: "NotFound", Message: "Not Here"})
webEx, ok := ss.AsWebServiceException(err)
if !ok {
t.Fatalf("got %T, want *WebServiceException", err)
}
if !webEx.IsNotFound() {
t.Errorf("got StatusCode %d, want 404", webEx.StatusCode)
}
if webEx.ErrorCode() != "NotFound" || webEx.ErrorMessage() != "Not Here" {
t.Errorf("got %q: %q", webEx.ErrorCode(), webEx.ErrorMessage())
}
}
func TestIntegrationUnauthorized(t *testing.T) {
client := ss.NewClient(testUrl())
api := ss.Api(client, dtos.HelloSecure{Name: "World"})
if api.Succeeded() {
t.Fatal("expected HelloSecure to require authentication")
}
if api.ErrorCode() != "Unauthorized" {
t.Errorf("got ErrorCode %q, want Unauthorized", api.ErrorCode())
}
}
func TestIntegrationAuthenticateThenCallSecureService(t *testing.T) {
client := ss.NewClient(testUrl())
authRes, err := client.Authenticate("test", "test")
if err != nil {
t.Fatalf("Authenticate failed: %v", err)
}
if authRes.UserName != "test" {
t.Errorf("got UserName %q, want test", authRes.UserName)
}
// The authenticated Session is maintained in the Client's cookie jar
res, err := ss.Send(client, dtos.HelloSecure{Name: "World"})
if err != nil {
t.Fatalf("HelloSecure failed: %v", err)
}
if res.Result != "Hello, World!" {
t.Errorf("got %q, want Hello, World!", res.Result)
}
}
func TestIntegrationSendAll(t *testing.T) {
client := ss.NewClient(testUrl())
responses, err := ss.SendAll(client, []dtos.Hello{{Name: "A", Title: "Mr"}, {Name: "B", Title: "Ms"}})
if err != nil {
t.Fatalf("SendAll failed: %v", err)
}
if len(responses) != 2 {
t.Fatalf("got %d responses, want 2", len(responses))
}
if responses[0].Result != "Hello, Mr. A!" || responses[1].Result != "Hello, Ms. B!" {
t.Errorf("got %+v", responses)
}
}
func TestIntegrationCustomRoute(t *testing.T) {
client := ss.NewClient(testUrl())
res, err := ss.GetUrl[dtos.HelloResponse](client, "/hello/World")
if err != nil {
t.Fatalf("GetUrl failed: %v", err)
}
if res.Result != "Hello, World!" {
t.Errorf("got %q, want Hello, World!", res.Result)
}
}
// Sends a Request to ServiceStack AI Chat's OpenAI-compatible ChatCompletion API
func TestIntegrationChatCompletion(t *testing.T) {
client := ss.NewClient(testUrl())
// The ChatCompletion API requires an authenticated User
if _, err := client.Authenticate("test", "test"); err != nil {
t.Fatalf("Authenticate failed: %v", err)
}
res, err := ss.Send(client, dtos.ChatCompletion{
Model: chatModel,
Messages: []dtos.AiMessage{
{
Role: "user",
// Content parts are polymorphic, e.g. text, image_url or input_audio
Content: []any{
dtos.AiTextContent{
AiContent: dtos.AiContent{Type: "text"},
Text: "Capital of France? Answer in 3 words",
},
},
},
},
})
if err != nil {
// A shared LLM can be rate limited or temporarily unavailable
if webEx, ok := ss.AsWebServiceException(err); ok && webEx.IsAny(429, 502, 503, 504) {
t.Skipf("ChatCompletion unavailable: %v", err)
}
t.Fatalf("ChatCompletion failed: %v", err)
}
if len(res.Choices) == 0 {
t.Fatalf("got no choices, response: %+v", res)
}
if res.Choices[0].Message.Content == "" {
t.Errorf("got an empty message, response: %+v", res)
}
if res.Model != chatModel {
t.Errorf("got model %q, want %q", res.Model, chatModel)
}
}