Skip to content

Commit 8815645

Browse files
committed
supply more tests and heading boilerplate
Signed-off-by: Hang Yin <[email protected]>
1 parent 3631757 commit 8815645

File tree

3 files changed

+94
-28
lines changed

3 files changed

+94
-28
lines changed

pkg/epp/requestcontrol/director_test.go

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func TestHandleRequest(t *testing.T) {
8585
wantRespBody map[string]interface{}
8686
}{
8787
{
88-
name: "successful request",
88+
name: "successful completions request",
8989
reqBodyMap: map[string]interface{}{
9090
"model": tsModel,
9191
"prompt": "test prompt",
@@ -129,7 +129,42 @@ func TestHandleRequest(t *testing.T) {
129129
},
130130
},
131131
{
132-
name: "successful request with target model",
132+
name: "successful chat completions request with multiple messages",
133+
reqBodyMap: map[string]interface{}{
134+
"model": tsModel,
135+
"messages": []interface{}{
136+
map[string]interface{}{
137+
"role": "developer",
138+
"content": "You are a helpful assistant.",
139+
},
140+
map[string]interface{}{
141+
"role": "user",
142+
"content": "Hello!",
143+
},
144+
},
145+
},
146+
wantReqCtx: &handlers.RequestContext{
147+
Model: tsModel,
148+
ResolvedTargetModel: tsModel,
149+
TargetPod: "/pod1",
150+
TargetEndpoint: "address-1:8000",
151+
},
152+
wantRespBody: map[string]interface{}{
153+
"model": tsModel,
154+
"messages": []interface{}{
155+
map[string]interface{}{
156+
"role": "developer",
157+
"content": "You are a helpful assistant.",
158+
},
159+
map[string]interface{}{
160+
"role": "user",
161+
"content": "Hello!",
162+
},
163+
},
164+
},
165+
},
166+
{
167+
name: "successful completions request with target model",
133168
reqBodyMap: map[string]interface{}{
134169
"model": modelWithTarget,
135170
"prompt": "test prompt",
@@ -149,6 +184,21 @@ func TestHandleRequest(t *testing.T) {
149184
name: "no model defined, expect err",
150185
wantErrCode: errutil.BadRequest,
151186
},
187+
{
188+
name: "prompt or messages not found, expect err",
189+
reqBodyMap: map[string]interface{}{
190+
"model": tsModel,
191+
},
192+
wantErrCode: errutil.BadRequest,
193+
},
194+
{
195+
name: "empty messages, expect err",
196+
reqBodyMap: map[string]interface{}{
197+
"model": tsModel,
198+
"messages": []interface{}{},
199+
},
200+
wantErrCode: errutil.BadRequest,
201+
},
152202
{
153203
name: "invalid model defined, expect err",
154204
reqBodyMap: map[string]interface{}{

pkg/epp/util/request/body.go

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package request
218

319
import (
@@ -7,8 +23,7 @@ import (
723
)
824

925
func ExtractPromptFromRequestBody(body map[string]interface{}) (string, error) {
10-
_, ok := body["messages"]
11-
if ok {
26+
if _, ok := body["messages"]; ok {
1227
return extractPromptFromMessagesField(body)
1328
}
1429
return extractPromptField(body)
@@ -35,32 +50,33 @@ func extractPromptFromMessagesField(body map[string]interface{}) (string, error)
3550
if !ok {
3651
return "", errutil.Error{Code: errutil.BadRequest, Msg: "messages is not a list"}
3752
}
53+
if len(messageList) == 0 {
54+
return "", errutil.Error{Code: errutil.BadRequest, Msg: "messages is empty"}
55+
}
3856

3957
prompt := ""
40-
if len(messageList) > 0 {
41-
for _, msg := range messageList {
42-
msgMap, ok := msg.(map[string]interface{})
43-
if !ok {
44-
continue
45-
}
46-
content, ok := msgMap["content"]
47-
if !ok {
48-
continue
49-
}
50-
contentStr, ok := content.(string)
51-
if !ok {
52-
continue
53-
}
54-
role, ok := msgMap["role"]
55-
if !ok {
56-
continue
57-
}
58-
roleStr, ok := role.(string)
59-
if !ok {
60-
continue
61-
}
62-
prompt += constructChatMessage(roleStr, contentStr)
58+
for _, msg := range messageList {
59+
msgMap, ok := msg.(map[string]interface{})
60+
if !ok {
61+
continue
62+
}
63+
content, ok := msgMap["content"]
64+
if !ok {
65+
continue
66+
}
67+
contentStr, ok := content.(string)
68+
if !ok {
69+
continue
70+
}
71+
role, ok := msgMap["role"]
72+
if !ok {
73+
continue
74+
}
75+
roleStr, ok := role.(string)
76+
if !ok {
77+
continue
6378
}
79+
prompt += constructChatMessage(roleStr, contentStr)
6480
}
6581
return prompt, nil
6682
}

pkg/epp/util/request/body_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func TestExtractPromptFromRequestBody(t *testing.T) {
9191
}
9292
}
9393

94-
func TestExtractPromptForCompletions(t *testing.T) {
94+
func TestExtractPromptField(t *testing.T) {
9595
tests := []struct {
9696
name string
9797
body map[string]interface{}

0 commit comments

Comments
 (0)