Skip to content

Commit 522a362

Browse files
committed
refine unit test
Signed-off-by: JaredforReal <w13431838023@gmail.com>
1 parent 049cff4 commit 522a362

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

src/semantic-router/pkg/extproc/mapping_responses_test.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,118 @@ func TestTranslateSSEChunkToResponses(t *testing.T) {
5252
t.Fatalf("expected events")
5353
}
5454
}
55+
56+
func TestMapResponsesRequestToChatCompletions_ToolsPassThrough(t *testing.T) {
57+
in := []byte(`{
58+
"model":"gpt-test",
59+
"input":"call a tool",
60+
"tools":[{"type":"function","function":{"name":"get_time","parameters":{"type":"object","properties":{}}}}],
61+
"tool_choice":"auto"
62+
}`)
63+
out, err := mapResponsesRequestToChatCompletions(in)
64+
if err != nil {
65+
t.Fatalf("unexpected error: %v", err)
66+
}
67+
var m map[string]interface{}
68+
if err := json.Unmarshal(out, &m); err != nil {
69+
t.Fatalf("unmarshal mapped: %v", err)
70+
}
71+
if _, ok := m["tools"]; !ok {
72+
t.Fatalf("tools not passed through")
73+
}
74+
if v, ok := m["tool_choice"]; !ok || v == nil {
75+
t.Fatalf("tool_choice not passed through")
76+
}
77+
}
78+
79+
func TestMapChatCompletionToResponses_ToolCallsModern(t *testing.T) {
80+
in := []byte(`{
81+
"id":"x","object":"chat.completion","created":2,"model":"m",
82+
"choices":[{"index":0,"finish_reason":"stop","message":{
83+
"role":"assistant",
84+
"content":"",
85+
"tool_calls":[{"type":"function","function":{"name":"get_time","arguments":"{\\"tz\\":\\"UTC\\"}"}}]
86+
}}],
87+
"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}
88+
}`)
89+
out, err := mapChatCompletionToResponses(in)
90+
if err != nil {
91+
t.Fatalf("unexpected error: %v", err)
92+
}
93+
var m map[string]interface{}
94+
if err := json.Unmarshal(out, &m); err != nil {
95+
t.Fatalf("unmarshal: %v", err)
96+
}
97+
outs, _ := m["output"].([]interface{})
98+
if len(outs) == 0 {
99+
t.Fatalf("expected output entries")
100+
}
101+
var hasTool bool
102+
for _, o := range outs {
103+
om := o.(map[string]interface{})
104+
if om["type"] == "tool_call" {
105+
hasTool = true
106+
}
107+
}
108+
if !hasTool {
109+
t.Fatalf("expected tool_call in output")
110+
}
111+
}
112+
113+
func TestMapChatCompletionToResponses_FunctionCallLegacy(t *testing.T) {
114+
in := []byte(`{
115+
"id":"x","object":"chat.completion","created":2,"model":"m",
116+
"choices":[{"index":0,"finish_reason":"stop","message":{
117+
"role":"assistant",
118+
"content":"",
119+
"function_call":{"name":"get_time","arguments":"{\\"tz\\":\\"UTC\\"}"}
120+
}}],
121+
"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}
122+
}`)
123+
out, err := mapChatCompletionToResponses(in)
124+
if err != nil {
125+
t.Fatalf("unexpected error: %v", err)
126+
}
127+
var m map[string]interface{}
128+
if err := json.Unmarshal(out, &m); err != nil {
129+
t.Fatalf("unmarshal: %v", err)
130+
}
131+
outs, _ := m["output"].([]interface{})
132+
var hasTool bool
133+
for _, o := range outs {
134+
if om, ok := o.(map[string]interface{}); ok && om["type"] == "tool_call" {
135+
hasTool = true
136+
}
137+
}
138+
if !hasTool {
139+
t.Fatalf("expected legacy function tool_call in output")
140+
}
141+
}
142+
143+
func TestTranslateSSEChunkToResponses_ToolCallsDelta(t *testing.T) {
144+
chunk := []byte(`{
145+
"id":"c1","object":"chat.completion.chunk","created":1,
146+
"model":"m",
147+
"choices":[{"index":0,
148+
"delta":{
149+
"tool_calls":[{"index":0,"function":{"name":"get_time","arguments":"{\\"tz\\":\\"UTC\\"}"}}]
150+
},
151+
"finish_reason":null
152+
}]
153+
}`)
154+
evs, ok := translateSSEChunkToResponses(chunk)
155+
if !ok || len(evs) == 0 {
156+
t.Fatalf("expected events for tool_calls delta")
157+
}
158+
var hasToolDelta bool
159+
for _, ev := range evs {
160+
var m map[string]interface{}
161+
_ = json.Unmarshal(ev, &m)
162+
if m["type"] == "response.tool_calls.delta" {
163+
hasToolDelta = true
164+
}
165+
}
166+
if !hasToolDelta {
167+
t.Fatalf("expected response.tool_calls.delta event")
168+
}
169+
}

0 commit comments

Comments
 (0)