-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructured_test.go
More file actions
88 lines (78 loc) · 2.19 KB
/
Copy pathstructured_test.go
File metadata and controls
88 lines (78 loc) · 2.19 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
package llmkit
import (
"context"
"encoding/json"
"testing"
)
type typedMockClient struct {
resp *Response
err error
req Request
}
func (m *typedMockClient) Complete(_ context.Context, req Request) (*Response, error) {
m.req = req
return m.resp, m.err
}
func (m *typedMockClient) Stream(context.Context, Request) (<-chan StreamChunk, error) {
ch := make(chan StreamChunk)
close(ch)
return ch, nil
}
func (m *typedMockClient) Provider() string { return "mock" }
func (m *typedMockClient) Capabilities() Capabilities { return Capabilities{} }
func (m *typedMockClient) Close() error { return nil }
func TestCompleteTyped(t *testing.T) {
type payload struct {
Name string `json:"name"`
}
mock := &typedMockClient{
resp: &Response{Content: `{"name":"demo"}`},
}
out, err := CompleteTyped[payload](context.Background(), mock, Request{})
if err != nil {
t.Fatalf("CompleteTyped: %v", err)
}
if out.Value.Name != "demo" {
t.Fatalf("Value.Name = %q", out.Value.Name)
}
if len(mock.req.JSONSchema) == 0 {
t.Fatal("expected generated schema")
}
var schema map[string]any
if err := json.Unmarshal(mock.req.JSONSchema, &schema); err != nil {
t.Fatalf("schema unmarshal: %v", err)
}
}
func TestCompleteTypedRejectsEmptyContent(t *testing.T) {
_, err := CompleteTyped[map[string]any](context.Background(), &typedMockClient{
resp: &Response{Content: ""},
}, Request{})
if err == nil {
t.Fatal("expected error for empty content")
}
}
func TestCompleteTypedExtractsLastJSONValue(t *testing.T) {
type payload struct {
Name string `json:"name"`
}
out, err := CompleteTyped[payload](context.Background(), &typedMockClient{
resp: &Response{Content: "Thinking...\n{\"name\":\"demo\"}"},
}, Request{})
if err != nil {
t.Fatalf("CompleteTyped: %v", err)
}
if out.Value.Name != "demo" {
t.Fatalf("Value.Name = %q", out.Value.Name)
}
}
func TestCompleteTypedRejectsUnknownFields(t *testing.T) {
type payload struct {
Name string `json:"name"`
}
_, err := CompleteTyped[payload](context.Background(), &typedMockClient{
resp: &Response{Content: `{"name":"demo","extra":"nope"}`},
}, Request{})
if err == nil {
t.Fatal("expected error for unknown field")
}
}