-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
249 lines (211 loc) · 6.69 KB
/
Copy pathmain_test.go
File metadata and controls
249 lines (211 loc) · 6.69 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
package main
import (
"context"
"testing"
"time"
"github.com/maximhq/bifrost/core/schemas"
"github.com/tidwall/gjson"
)
func newTestContext() *schemas.BifrostContext {
return schemas.NewBifrostContext(context.Background(), time.Now().Add(time.Minute))
}
func TestGetName(t *testing.T) {
name := GetName()
if name != "encoding-format" {
t.Errorf("expected plugin name 'encoding-format', got '%s'", name)
}
}
func TestInitDefault(t *testing.T) {
activeConfig = pluginConfig{}
if err := Init(nil); err != nil {
t.Fatalf("Init(nil) returned error: %v", err)
}
if activeConfig.EncodingFormat != "float" {
t.Errorf("expected default encoding_format 'float', got '%s'", activeConfig.EncodingFormat)
}
}
func TestInitWithFloatConfig(t *testing.T) {
activeConfig = pluginConfig{}
cfg := map[string]interface{}{"encoding_format": "float"}
if err := Init(cfg); err != nil {
t.Fatalf("Init with float config returned error: %v", err)
}
if activeConfig.EncodingFormat != "float" {
t.Errorf("expected encoding_format 'float', got '%s'", activeConfig.EncodingFormat)
}
}
func TestInitWithBase64Config(t *testing.T) {
activeConfig = pluginConfig{}
cfg := map[string]interface{}{"encoding_format": "base64"}
if err := Init(cfg); err != nil {
t.Fatalf("Init with base64 config returned error: %v", err)
}
if activeConfig.EncodingFormat != "base64" {
t.Errorf("expected encoding_format 'base64', got '%s'", activeConfig.EncodingFormat)
}
}
func TestInitWithUnsupportedFormat(t *testing.T) {
activeConfig = pluginConfig{}
cfg := map[string]interface{}{"encoding_format": "unsupported"}
err := Init(cfg)
if err == nil {
t.Fatal("expected error for unsupported encoding_format, got nil")
}
}
func TestInitWithEmptyConfig(t *testing.T) {
activeConfig = pluginConfig{}
cfg := map[string]interface{}{}
if err := Init(cfg); err != nil {
t.Fatalf("Init with empty config returned error: %v", err)
}
if activeConfig.EncodingFormat != "float" {
t.Errorf("expected default encoding_format 'float', got '%s'", activeConfig.EncodingFormat)
}
}
func TestIsEmbeddingRequest(t *testing.T) {
tests := []struct {
name string
path string
expected bool
}{
{"standard embeddings path", "/v1/embeddings", true},
{"embedding singular", "/v1/embedding", true},
{"uppercase path", "/V1/EMBEDDINGS", true},
{"mixed case", "/v1/Embeddings", true},
{"nested path", "/api/v1/embeddings/create", true},
{"chat path", "/v1/chat/completions", false},
{"completions path", "/v1/completions", false},
{"empty path", "", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := &schemas.HTTPRequest{Path: tt.path}
result := isEmbeddingRequest(req)
if result != tt.expected {
t.Errorf("isEmbeddingRequest(%q) = %v, want %v", tt.path, result, tt.expected)
}
})
}
}
func TestHTTPTransportPreHookInjectsEncodingFormat(t *testing.T) {
activeConfig = pluginConfig{EncodingFormat: "float"}
ctx := newTestContext()
req := &schemas.HTTPRequest{
Path: "/v1/embeddings",
Body: []byte(`{"model":"text-embedding-ada-002","input":"hello"}`),
}
resp, err := HTTPTransportPreHook(ctx, req)
if err != nil {
t.Fatalf("HTTPTransportPreHook returned error: %v", err)
}
if resp != nil {
t.Fatal("expected nil response (continue processing)")
}
result := gjson.GetBytes(req.Body, "encoding_format")
if !result.Exists() {
t.Fatal("encoding_format was not injected into request body")
}
if result.String() != "float" {
t.Errorf("expected encoding_format 'float', got '%s'", result.String())
}
}
func TestHTTPTransportPreHookInjectsBase64(t *testing.T) {
activeConfig = pluginConfig{EncodingFormat: "base64"}
ctx := newTestContext()
req := &schemas.HTTPRequest{
Path: "/v1/embeddings",
Body: []byte(`{"model":"text-embedding-ada-002","input":"hello"}`),
}
resp, err := HTTPTransportPreHook(ctx, req)
if err != nil {
t.Fatalf("HTTPTransportPreHook returned error: %v", err)
}
if resp != nil {
t.Fatal("expected nil response")
}
result := gjson.GetBytes(req.Body, "encoding_format")
if result.String() != "base64" {
t.Errorf("expected encoding_format 'base64', got '%s'", result.String())
}
}
func TestHTTPTransportPreHookSkipsExistingFormat(t *testing.T) {
activeConfig = pluginConfig{EncodingFormat: "float"}
ctx := newTestContext()
req := &schemas.HTTPRequest{
Path: "/v1/embeddings",
Body: []byte(`{"model":"text-embedding-ada-002","input":"hello","encoding_format":"base64"}`),
}
originalBody := string(req.Body)
resp, err := HTTPTransportPreHook(ctx, req)
if err != nil {
t.Fatalf("HTTPTransportPreHook returned error: %v", err)
}
if resp != nil {
t.Fatal("expected nil response")
}
if string(req.Body) != originalBody {
t.Error("request body was modified when encoding_format was already set")
}
result := gjson.GetBytes(req.Body, "encoding_format")
if result.String() != "base64" {
t.Errorf("expected existing encoding_format 'base64' to be preserved, got '%s'", result.String())
}
}
func TestHTTPTransportPreHookInjectsWhenNull(t *testing.T) {
activeConfig = pluginConfig{EncodingFormat: "float"}
ctx := newTestContext()
req := &schemas.HTTPRequest{
Path: "/v1/embeddings",
Body: []byte(`{"model":"text-embedding-ada-002","input":"hello","encoding_format":null}`),
}
resp, err := HTTPTransportPreHook(ctx, req)
if err != nil {
t.Fatalf("HTTPTransportPreHook returned error: %v", err)
}
if resp != nil {
t.Fatal("expected nil response")
}
result := gjson.GetBytes(req.Body, "encoding_format")
if result.String() != "float" {
t.Errorf("expected encoding_format 'float' after null replacement, got '%s'", result.String())
}
}
func TestHTTPTransportPreHookSkipsNonEmbeddingRequests(t *testing.T) {
activeConfig = pluginConfig{EncodingFormat: "float"}
ctx := newTestContext()
req := &schemas.HTTPRequest{
Path: "/v1/chat/completions",
Body: []byte(`{"model":"gpt-4","messages":[{"role":"user","content":"hello"}]}`),
}
originalBody := string(req.Body)
resp, err := HTTPTransportPreHook(ctx, req)
if err != nil {
t.Fatalf("HTTPTransportPreHook returned error: %v", err)
}
if resp != nil {
t.Fatal("expected nil response")
}
if string(req.Body) != originalBody {
t.Error("non-embedding request body was modified")
}
}
func TestHTTPTransportPreHookSkipsEmptyBody(t *testing.T) {
activeConfig = pluginConfig{EncodingFormat: "float"}
ctx := newTestContext()
req := &schemas.HTTPRequest{
Path: "/v1/embeddings",
Body: []byte{},
}
resp, err := HTTPTransportPreHook(ctx, req)
if err != nil {
t.Fatalf("HTTPTransportPreHook returned error: %v", err)
}
if resp != nil {
t.Fatal("expected nil response")
}
}
func TestCleanup(t *testing.T) {
if err := Cleanup(); err != nil {
t.Fatalf("Cleanup returned error: %v", err)
}
}