65 lines
2.1 KiB
Go
65 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestInvalidChat(t *testing.T) {
|
|
for _, body := range []string{
|
|
`{`,
|
|
`{"model":"m","messages":[]}`,
|
|
`{"model":"m","messages":[{"role":"assistant","content":"text"}]}`,
|
|
`{"model":"m","messages":[{"role":"user","content":[{"type":"image_url","image_url":{"url":"https://example.com/image"}}]}]}`,
|
|
`{"model":"m","messages":[{"role":"user","content":null}]}`,
|
|
`{"model":"m","messages":[{"role":"user","content":"text"}],"tools":[{"type":"function"}]}`,
|
|
} {
|
|
t.Run(body, func(t *testing.T) {
|
|
w := httptest.NewRecorder()
|
|
(&bridge{binary: "/nonexistent"}).handler().ServeHTTP(w, httptest.NewRequest(http.MethodPost, "/v1/chat/completions", strings.NewReader(body)))
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Fatalf("status %d: %s", w.Code, w.Body)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestHistoryRoles(t *testing.T) {
|
|
var req chatRequest
|
|
err := json.Unmarshal([]byte(`{"model":"m","messages":[{"role":"system","content":"Instruction"},{"role":"user","content":"Question"},{"role":"assistant","content":"Answer"},{"role":"user","content":[{"type":"text","text":"Follow"},{"type":"text","text":"up"}]}]}`), &req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, prompt, err := inputs(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if prompt != "Followup" || len(items) != 3 {
|
|
t.Fatalf("%v %q", items, prompt)
|
|
}
|
|
for i, role := range []string{"developer", "user", "assistant"} {
|
|
if items[i]["role"] != role {
|
|
t.Fatalf("history %v", items)
|
|
}
|
|
}
|
|
if got := items[2]["content"].([]object)[0]; got["type"] != "output_text" || got["text"] != "Answer" {
|
|
t.Fatalf("assistant content %v", got)
|
|
}
|
|
}
|
|
|
|
func TestUnavailableCodex(t *testing.T) {
|
|
w := httptest.NewRecorder()
|
|
(&bridge{binary: "/nonexistent"}).handler().ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/v1/models", nil))
|
|
if w.Code != http.StatusBadGateway {
|
|
t.Fatalf("status %d", w.Code)
|
|
}
|
|
var result struct {
|
|
Error *rpcError `json:"error"`
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil || result.Error == nil {
|
|
t.Fatalf("invalid error: %s", w.Body)
|
|
}
|
|
}
|