-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathunstruct_test.go
More file actions
219 lines (183 loc) · 5.2 KB
/
Copy pathunstruct_test.go
File metadata and controls
219 lines (183 loc) · 5.2 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
package unstruct
import (
"context"
"strings"
"testing"
"time"
)
// Test structs
type TestProject struct {
Name string `json:"name" unstruct:"basic"`
Code string `json:"code" unstruct:"basic"`
Latitude float64 `json:"lat" unstruct:"coords"`
Longitude float64 `json:"lon" unstruct:"coords"`
}
// Test structure with missing prompts
type ProjectWithMissingPrompts struct {
Name string `json:"name" unstruct:"basic"`
Code string `json:"code"` // Missing prompt - should error
Latitude float64 `json:"lat"` // Missing prompt - should error
Longitude float64 `json:"lon" unstruct:"coords"`
}
// Mock prompt provider
type mockPrompts struct{}
func (m mockPrompts) GetPrompt(tag string, version int) (string, error) {
prompts := map[string]string{
"basic": "Extract {{.Keys}} from the text as JSON.",
"coords": "Find {{.Keys}} coordinates in the text as JSON.",
}
if prompt, ok := prompts[tag]; ok {
return prompt, nil
}
return "", nil
}
func TestUnstructor_SchemaReflection(t *testing.T) {
sch, err := schemaOf[TestProject]()
if err != nil {
t.Fatalf("schemaOf failed: %v", err)
}
// Check that we have the expected prompt groups
if len(sch.group2keys) != 2 {
t.Errorf("Expected 2 prompt groups, got %d", len(sch.group2keys))
}
// Check field mapping
if len(sch.json2field) != 4 {
t.Errorf("Expected 4 fields, got %d", len(sch.json2field))
}
// Find basic group
var basicKeys []string
var coordKeys []string
for pk, keys := range sch.group2keys {
switch pk.prompt {
case "basic":
basicKeys = keys
case "coords":
coordKeys = keys
}
}
// Check basic keys
if len(basicKeys) != 2 {
t.Errorf("Expected 2 basic keys, got %d: %v", len(basicKeys), basicKeys)
}
// Check coords keys
if len(coordKeys) != 2 {
t.Errorf("Expected 2 coord keys, got %d: %v", len(coordKeys), coordKeys)
}
}
func TestUnstructor_CallPrompt(t *testing.T) {
ext := newTestingUnstructor[TestProject](mockPrompts{})
keys := []string{"name", "code"}
doc := "Project Alpha with code ABC-123"
assets := []Asset{&TextAsset{Content: doc}}
raw, err := ext.callPrompt(
context.Background(),
"basic",
keys,
assets,
"test-model",
nil, // no parameters
Options{},
)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if len(raw) == 0 {
t.Error("Expected non-empty response")
}
}
func TestUnstructor_UnstructAll(t *testing.T) {
ext := newTestingUnstructor[TestProject](mockPrompts{})
doc := "Project Alpha with code ABC-123. Located at coordinates 40.7128, -74.0060."
assets := []Asset{&TextAsset{Content: doc}}
result, err := ext.Unstruct(
context.Background(),
assets,
WithModel("test-model"),
WithTimeout(10*time.Second),
)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if result == nil {
t.Error("Expected non-nil result")
return
}
// With our test invoker, we should get the mock data
if result.Name != "Test Project" {
t.Errorf("Expected name 'Test Project', got %q", result.Name)
}
if result.Code != "TEST-123" {
t.Errorf("Expected code 'TEST-123', got %q", result.Code)
}
}
func TestSanitizeJSONResponse(t *testing.T) {
tests := []struct {
input string
expected string
}{
{
input: "```json\n{\"key\": \"value\"}\n```",
expected: "{\"key\": \"value\"}",
},
{
input: "```\n{\"key\": \"value\"}\n```",
expected: "{\"key\": \"value\"}",
},
{
input: " {\"key\": \"value\"} ",
expected: "{\"key\": \"value\"}",
},
{
input: "{\"key\": \"value\"}",
expected: "{\"key\": \"value\"}",
},
}
for _, test := range tests {
result := string(SanitizeJSONResponse([]byte(test.input)))
if result != test.expected {
t.Errorf("For input %q, expected %q, got %q", test.input, test.expected, result)
}
}
}
func TestUnstructor_RequiredPrompts(t *testing.T) {
ext := newTestingUnstructor[ProjectWithMissingPrompts](mockPrompts{})
doc := "Project Alpha with code ABC-123. Located at coordinates 40.7128, -74.0060."
assets := []Asset{&TextAsset{Content: doc}}
// Should error because fields are missing prompts and no fallback is provided
result, err := ext.Unstruct(
context.Background(),
assets,
WithModel("test-model"),
WithTimeout(10*time.Second),
)
if err == nil {
t.Error("Expected error for missing prompts, got nil")
}
if result != nil {
t.Error("Expected nil result when error occurs")
}
// Check that the error message mentions missing prompts
expectedError := "no prompt specified"
if !strings.Contains(err.Error(), expectedError) {
t.Errorf("Expected error to contain %q, got: %v", expectedError, err)
}
}
func TestUnstructor_WithFallbackPrompt(t *testing.T) {
ext := newTestingUnstructor[ProjectWithMissingPrompts](mockPrompts{})
doc := "Project Alpha with code ABC-123. Located at coordinates 40.7128, -74.0060."
assets := []Asset{&TextAsset{Content: doc}}
// Should succeed when fallback prompt is provided
result, err := ext.Unstruct(
context.Background(),
assets,
WithModel("test-model"),
WithTimeout(10*time.Second),
WithFallbackPrompt("basic"), // Explicit fallback
)
if err != nil {
t.Errorf("Expected no error with fallback prompt, got: %v", err)
}
if result == nil {
t.Error("Expected non-nil result with fallback prompt")
}
}