Repository navigation
Expand file tree
/
Copy pathe2e_test.go
More file actions
498 lines (470 loc) · 16.1 KB
/
Copy pathe2e_test.go
File metadata and controls
498 lines (470 loc) · 16.1 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//go:build e2e
package llm
// End-to-end tests against live LLM providers. Tag-gated so they never
// build into CI or a plain `go test ./...`:
//
// go test -tags e2e -run 'TestE2E' -timeout 15m -v .
//
// Credentials come from the process environment or a repo-root .env file
// (KEY=VALUE lines, optional `export ` prefix and double quotes), parsed by
// loadDotEnv. Neither the file nor any key is ever logged — the SDK
// guarantees API keys stay out of all error text. Each provider's tests
// skip cleanly when its key is absent, so the suite covers exactly the
// providers you have credentials for.
//
// Adding a provider = one e2eTarget entry below. Model defaults can be
// overridden per provider via <ID>_E2E_MODEL (e.g. OPENROUTER_E2E_MODEL).
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"strings"
"testing"
"time"
)
// loadDotEnv parses a .env file and sets any variable not already present
// in the process environment (environment wins over file). Values may be
// wrapped in single or double quotes; `KEY=VALUE` and `export KEY=VALUE`
// forms are accepted; `#` starts a comment.
func loadDotEnv(t *testing.T, path string) {
t.Helper()
b, err := os.ReadFile(path)
if err != nil {
return // no .env: environment only
}
for i, line := range strings.Split(string(b), "\n") {
line = strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(line), "export "))
if line == "" || strings.HasPrefix(line, "#") {
continue
}
k, v, ok := strings.Cut(line, "=")
if !ok {
t.Logf(".env line %d: not KEY=VALUE, ignored", i+1)
continue
}
k = strings.TrimSpace(k)
v = strings.Trim(strings.TrimSpace(v), `"'`)
if k == "" {
continue
}
if _, exists := os.LookupEnv(k); !exists {
if err := os.Setenv(k, v); err != nil {
t.Fatalf("setenv %s: %v", k, err)
}
}
}
}
// e2eEnvKey resolves one credential, skipping the caller when absent.
func e2eEnvKey(t *testing.T, env string) string {
t.Helper()
loadDotEnv(t, ".env")
key := strings.TrimSpace(os.Getenv(env))
if key == "" {
t.Skipf("%s not set (env or .env); skipping", env)
}
return key
}
// e2eTarget is one live provider under test.
type e2eTarget struct {
id string // registry id (built-in) or custom provider id
baseURL string // "" = built-in registry entry via FromEnv
format Format // wire format for custom providers
keyEnv string // env var holding the API key
model string // default chat model (override: <ID>_E2E_MODEL)
tools bool // provider reliably supports tool calling
streamUsage bool // provider reliably returns usage on streams
}
var e2eTargets = []e2eTarget{
{id: "deepseek", baseURL: "", keyEnv: "DEEPSEEK_API_KEY", model: "deepseek-chat", tools: true, streamUsage: true},
{id: "openrouter", baseURL: "https://openrouter.ai/api/v1", format: FormatOpenAI, keyEnv: "OPENROUTER_API_KEY", model: "openai/gpt-4o-mini", tools: true},
{id: "zai", baseURL: "", keyEnv: "ZAI_API_KEY", model: "glm-5-turbo", tools: true},
}
// chatModel resolves the target's model: <ID>_E2E_MODEL beats the default.
func (tg e2eTarget) chatModel() string {
if v := strings.TrimSpace(os.Getenv(strings.ToUpper(tg.id) + "_E2E_MODEL")); v != "" {
return v
}
return tg.model
}
// chat builds a chat client against the live endpoint.
func (tg e2eTarget) chat(t *testing.T) *ChatClient {
t.Helper()
e2eEnvKey(t, tg.keyEnv)
var sdk *SDK
if tg.baseURL == "" {
sdk = New(FromEnv()) // built-in registry: production discovery path
} else {
sdk = New(WithProvider(tg.id,
WithFormat(tg.format),
WithBaseURL(tg.baseURL),
WithAPIKey(e2eEnvKey(t, tg.keyEnv)),
))
}
cc, err := sdk.Chat(tg.id, tg.chatModel())
if err != nil {
t.Fatalf("Chat(%s, %s): %v", tg.id, tg.chatModel(), err)
}
return cc
}
// badKeyClient builds a client identical to the target's but with a
// deliberately invalid key (error-taxonomy probe).
func (tg e2eTarget) badKeyClient(t *testing.T) *ChatClient {
t.Helper()
e2eEnvKey(t, tg.keyEnv)
var sdk *SDK
if tg.baseURL == "" {
sdk = New(WithProvider(tg.id, WithAPIKey("sk-e2e-invalid-probe")))
} else {
sdk = New(WithProvider(tg.id,
WithFormat(tg.format),
WithBaseURL(tg.baseURL),
WithAPIKey("sk-e2e-invalid-probe"),
))
}
cc, err := sdk.Chat(tg.id, tg.chatModel())
if err != nil {
t.Fatalf("Chat(%s): %v", tg.id, err)
}
return cc
}
func e2eCtx(t *testing.T, d time.Duration) context.Context {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), d)
t.Cleanup(cancel)
return ctx
}
// TestE2EProviders runs the generic matrix (discovery, buffered, streaming,
// tools, bad-key) for every provider whose key is present.
func TestE2EProviders(t *testing.T) {
for _, tg := range e2eTargets {
tg := tg
t.Run(tg.id, func(t *testing.T) {
t.Run("discovery", func(t *testing.T) { tg.testDiscovery(t) })
t.Run("buffered", func(t *testing.T) { tg.testBuffered(t) })
t.Run("streaming", func(t *testing.T) { tg.testStreaming(t) })
if tg.tools {
t.Run("tools", func(t *testing.T) { tg.testTools(t) })
}
t.Run("badkey", func(t *testing.T) { tg.testBadKey(t) })
})
}
}
func (tg e2eTarget) testDiscovery(t *testing.T) {
t.Helper()
e2eEnvKey(t, tg.keyEnv)
sdk := New(FromEnv())
if tg.baseURL != "" {
sdk = New(WithProvider(tg.id,
WithFormat(tg.format),
WithBaseURL(tg.baseURL),
WithAPIKey(strings.TrimSpace(os.Getenv(tg.keyEnv))),
))
}
p, err := sdk.Provider(tg.id)
if err != nil {
t.Fatal(err)
}
if !p.Authenticated() {
t.Fatal("provider resolved but not authenticated")
}
models, err := p.ListModels(e2eCtx(t, 60*time.Second))
if err != nil {
t.Fatalf("ListModels: %v", err)
}
if len(models) == 0 {
t.Fatal("expected at least one accessible model")
}
ids := make([]string, 0, len(models))
for _, m := range models {
if m.ID == "" {
t.Errorf("model with empty ID: %+v", m)
}
ids = append(ids, m.ID)
}
if len(ids) > 15 {
ids = ids[:15]
}
t.Logf("accessible models (%d total, first 15): %s", len(models), strings.Join(ids, ", "))
}
func (tg e2eTarget) testBuffered(t *testing.T) {
t.Helper()
cc := tg.chat(t)
res, err := cc.Call(e2eCtx(t, 120*time.Second), &ChatRequest{
Messages: []Message{{Role: RoleUser, Content: "Reply with exactly: OK"}},
MaxTokens: 20,
})
if err != nil {
t.Fatalf("Call: %v", err)
}
if !strings.Contains(strings.ToUpper(res.Content), "OK") {
t.Errorf("content = %q, want it to contain OK", res.Content)
}
if res.FinishReason != FinishStop {
t.Errorf("finish = %q, want stop", res.FinishReason)
}
if res.Usage.PromptTokens == 0 || res.Usage.CompletionTokens == 0 {
t.Errorf("usage = %+v, want live token counts", res.Usage)
}
}
func (tg e2eTarget) testStreaming(t *testing.T) {
t.Helper()
cc := tg.chat(t)
var content strings.Builder
sawContent := false
res, err := cc.CallStream(e2eCtx(t, 120*time.Second), &ChatRequest{
Messages: []Message{{Role: RoleUser, Content: "Count from 1 to 5, digits only."}},
MaxTokens: 60,
}, func(d Delta) error {
if d.Kind == DeltaContent {
sawContent = true
content.WriteString(d.Text)
}
return nil
})
if err != nil {
t.Fatalf("CallStream: %v", err)
}
if !sawContent {
t.Error("no content deltas received")
}
if res.Content == "" {
t.Fatal("empty result content")
}
if res.Content != content.String() {
t.Errorf("folded deltas %q != final result %q", content.String(), res.Content)
}
if res.FinishReason != FinishStop {
t.Errorf("finish = %q, want stop", res.FinishReason)
}
if tg.streamUsage && res.Usage.CompletionTokens == 0 {
t.Errorf("streaming usage missing (stream_options.include_usage): %+v", res.Usage)
}
}
func (tg e2eTarget) testTools(t *testing.T) {
t.Helper()
cc := tg.chat(t)
weather := ToolDef{
Name: "get_weather",
Description: "Current weather for a city",
Parameters: json.RawMessage(`{"type":"object","properties":{"city":{"type":"string"}},"required":["city"]}`),
}
city := "Lisbon"
question := fmt.Sprintf("What's the weather in %s? Use the tool.", city)
ctx := e2eCtx(t, 120*time.Second)
// Round trip: tool call -> tool result -> final answer.
res, err := cc.Call(ctx, &ChatRequest{
Messages: []Message{{Role: RoleUser, Content: question}},
Tools: []ToolDef{weather},
MaxTokens: 200,
})
if err != nil {
t.Fatalf("tool call: %v", err)
}
if len(res.ToolCalls) != 1 {
t.Fatalf("want exactly 1 tool call, got %d (finish %q, content %q)", len(res.ToolCalls), res.FinishReason, res.Content)
}
tc := res.ToolCalls[0]
if tc.Name != "get_weather" {
t.Errorf("tool name = %q, want get_weather", tc.Name)
}
if !json.Valid([]byte(tc.Arguments)) {
t.Errorf("arguments not valid JSON: %q", tc.Arguments)
}
follow := &ChatRequest{
Messages: []Message{
{Role: RoleUser, Content: question},
{Role: RoleAssistant, Content: res.Content, ToolCalls: res.ToolCalls},
{Role: RoleTool, ToolCallID: tc.ID, ToolName: tc.Name, Content: `{"temp_c":24,"condition":"sunny"}`},
},
Tools: []ToolDef{weather},
MaxTokens: 200,
}
res2, err := cc.Call(ctx, follow)
if err != nil {
t.Fatalf("follow-up: %v", err)
}
if res2.Content == "" {
t.Fatal("model did not answer using the tool result")
}
// Streamed: argument fragments must assemble into a valid call.
streamed, err := cc.CallStream(ctx, &ChatRequest{
Messages: []Message{{Role: RoleUser, Content: "What's the weather in Tokyo? Use the tool."}},
Tools: []ToolDef{weather},
MaxTokens: 200,
}, func(Delta) error { return nil })
if err != nil {
t.Fatalf("CallStream: %v", err)
}
if len(streamed.ToolCalls) != 1 {
t.Fatalf("want exactly 1 streamed tool call, got %d", len(streamed.ToolCalls))
}
if !json.Valid([]byte(streamed.ToolCalls[0].Arguments)) {
t.Errorf("streamed arguments not valid JSON: %q", streamed.ToolCalls[0].Arguments)
}
}
func (tg e2eTarget) testBadKey(t *testing.T) {
t.Helper()
cc := tg.badKeyClient(t)
_, err := cc.Call(e2eCtx(t, 30*time.Second), &ChatRequest{Messages: []Message{{Role: RoleUser, Content: "hi"}}})
var ae *APIError
if !errors.As(err, &ae) {
t.Fatalf("err = %v (%T), want *APIError", err, err)
}
if ae.Status != http.StatusUnauthorized {
t.Errorf("status = %d, want 401", ae.Status)
}
if ae.Retryable {
t.Error("401 must not be retryable")
}
if strings.Contains(err.Error(), "sk-e2e-invalid-probe") {
t.Error("error text leaked the API key")
}
}
// ── deepseek-specific arms ───────────────────────────────────────────────
// deepseek-reasoner: reasoning content is captured and the answer is right.
func TestE2EReasonerThinking(t *testing.T) {
tg := e2eTargets[0]
cc := tg.chat(t)
res, err := cc.Call(e2eCtx(t, 180*time.Second), &ChatRequest{
Messages: []Message{{Role: RoleUser, Content: "A clock shows 3:15. What is the angle in degrees between the hour and minute hands? Work it out, then answer with the number only."}},
MaxTokens: 1000,
})
if err != nil {
t.Fatalf("Call: %v", err)
}
if res.ReasoningContent == "" {
// Provider-side behavior: DeepSeek elides reasoning_content for some
// prompts/runs. The SDK capture path is unit-covered; here we only
// probe, not assert.
t.Log("provider returned no reasoning content (server-side elision)")
}
if !strings.Contains(res.Content, "7.5") {
// Model IQ is not the SDK's contract; when the provider elides
// reasoning, arithmetic riddles can come back wrong. The live
// invariants are: call succeeds, content parses, finish is canonical.
t.Logf("answer = %q (provider may be wrong without reasoning)", res.Content)
}
if res.FinishReason != FinishStop && res.FinishReason != FinishLength {
t.Errorf("finish = %q, want stop or length", res.FinishReason)
}
}
// Reasoner streaming: reasoning deltas flow through DeltaReasoning.
func TestE2EReasonerStreaming(t *testing.T) {
tg := e2eTargets[0]
cc := tg.chat(t)
var sawReasoning bool
res, err := cc.CallStream(e2eCtx(t, 180*time.Second), &ChatRequest{
Messages: []Message{{Role: RoleUser, Content: "A water lily patch doubles in size every day. It covers the whole lake on day 48. On which day was it half covered? Answer with the day number only."}},
MaxTokens: 1000,
}, func(d Delta) error {
if d.Kind == DeltaReasoning && d.Text != "" {
sawReasoning = true
}
return nil
})
if err != nil {
t.Fatalf("CallStream: %v", err)
}
if !sawReasoning {
t.Log("provider returned no reasoning deltas (server-side elision); capture path is unit-covered")
}
if !strings.Contains(res.Content, "24") {
// Model IQ is not the SDK's contract (see the buffered reasoner note).
t.Logf("answer = %q (provider may be wrong without reasoning)", res.Content)
}
if res.FinishReason != FinishStop && res.FinishReason != FinishLength {
t.Errorf("finish = %q, want stop or length", res.FinishReason)
}
}
// ── TTS arms ─────────────────────────────────────────────────────────────
// e2eTTSModel resolves the TTS model for a provider id: <ID>_TTS_E2E_MODEL
// beats the default (kept separate from the chat-model override).
func e2eTTSModel(t *testing.T, id, def string) string {
t.Helper()
if v := strings.TrimSpace(os.Getenv(strings.ToUpper(id) + "_TTS_E2E_MODEL")); v != "" {
return v
}
return def
}
// TestE2ETTSOpenAI probes the OpenAI /audio/speech path against the live
// OpenAI endpoint (built-in registry, key via OPENAI_API_KEY). Asserts only
// SDK guarantees: the call succeeds and non-empty audio with a non-empty
// MIME type comes back. The audio's decodability is never asserted.
func TestE2ETTSOpenAI(t *testing.T) {
const keyEnv = "OPENAI_API_KEY"
key := e2eEnvKey(t, keyEnv)
sdk := New(WithProvider("openai", WithAPIKey(key)))
res, err := sdk.Speak(t.Context(), "openai", e2eTTSModel(t, "openai", "tts-1"), SpeakRequest{
Text: "go-llm-sdk text to speech probe.",
Voice: "alloy",
})
if err != nil {
t.Fatalf("Speak: %v", err)
}
if len(res.Audio) == 0 {
t.Errorf("Speak returned empty audio")
}
if res.MIMEType == "" {
t.Errorf("Speak returned empty MIMEType")
}
t.Logf("audio=%d bytes mime=%q model=%q", len(res.Audio), res.MIMEType, res.Model)
}
// TestE2ETTSGemini probes the Gemini AUDIO-modality TTS path (built-in
// registry, key via GEMINI_API_KEY). Same soft contract: non-empty audio
// bytes + non-empty MIME type; no format assumptions beyond what the SDK
// already guarantees.
func TestE2ETTSGemini(t *testing.T) {
const keyEnv = "GEMINI_API_KEY"
key := e2eEnvKey(t, keyEnv)
sdk := New(WithProvider("gemini", WithAPIKey(key)))
res, err := sdk.Speak(t.Context(), "gemini", e2eTTSModel(t, "gemini", "gemini-2.5-flash-preview-tts"), SpeakRequest{
Text: "go-llm-sdk text to speech probe.",
Voice: "Kore",
})
if err != nil {
t.Fatalf("Speak: %v", err)
}
if len(res.Audio) == 0 {
t.Errorf("Speak returned empty audio")
}
if res.MIMEType == "" {
t.Errorf("Speak returned empty MIMEType")
}
t.Logf("audio=%d bytes mime=%q model=%q", len(res.Audio), res.MIMEType, res.Model)
}
// TestE2ETranscribeOpenAI probes the OpenAI /audio/transcriptions path
// against the live endpoint (built-in registry, key via OPENAI_API_KEY).
// Self-contained: audio is synthesized first via Speak, then transcribed
// (model override via OPENAI_STT_E2E_MODEL, default whisper-1). Asserts
// only SDK guarantees: the call succeeds and non-empty text comes back.
// Transcript accuracy is never asserted.
func TestE2ETranscribeOpenAI(t *testing.T) {
const keyEnv = "OPENAI_API_KEY"
key := e2eEnvKey(t, keyEnv)
sdk := New(WithProvider("openai", WithAPIKey(key)))
spoken, err := sdk.Speak(t.Context(), "openai", e2eTTSModel(t, "openai", "tts-1"), SpeakRequest{
Text: "go-llm-sdk speech to text probe.",
Voice: "alloy",
})
if err != nil {
t.Fatalf("Speak: %v", err)
}
sttModel := "whisper-1"
if v := strings.TrimSpace(os.Getenv("OPENAI_STT_E2E_MODEL")); v != "" {
sttModel = v
}
res, err := sdk.Transcribe(t.Context(), "openai", sttModel, TranscribeRequest{
Audio: spoken.Audio,
Filename: "probe.mp3",
})
if err != nil {
t.Fatalf("Transcribe: %v", err)
}
if res.Text == "" {
t.Errorf("Transcribe returned empty text")
}
t.Logf("text=%q duration=%.1fs model=%q", res.Text, res.DurationSec, res.Model)
}