131
server/handlers/apiconfigs_test.go
Normal file
131
server/handlers/apiconfigs_test.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"git.gobha.me/xcaliber/chat-switchboard/providers"
|
||||
)
|
||||
|
||||
func init() {
|
||||
gin.SetMode(gin.TestMode)
|
||||
providers.Init()
|
||||
}
|
||||
|
||||
func TestCreateConfigMissingFields(t *testing.T) {
|
||||
h := NewAPIConfigHandler()
|
||||
r := gin.New()
|
||||
r.POST("/api-configs", func(c *gin.Context) {
|
||||
c.Set("user_id", "test-user")
|
||||
h.CreateConfig(c)
|
||||
})
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
body string
|
||||
}{
|
||||
{"missing name", `{"provider":"openai","endpoint":"http://x"}`},
|
||||
{"missing provider", `{"name":"Test","endpoint":"http://x"}`},
|
||||
{"missing endpoint", `{"name":"Test","provider":"openai"}`},
|
||||
{"empty body", `{}`},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("POST", "/api-configs",
|
||||
bytes.NewBufferString(tt.body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("Expected 400, got %d (body: %s)", w.Code, w.Body.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateConfigInvalidProvider(t *testing.T) {
|
||||
h := NewAPIConfigHandler()
|
||||
r := gin.New()
|
||||
r.POST("/api-configs", func(c *gin.Context) {
|
||||
c.Set("user_id", "test-user")
|
||||
h.CreateConfig(c)
|
||||
})
|
||||
|
||||
body := `{"name":"Test","provider":"nonexistent","endpoint":"http://x"}`
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("POST", "/api-configs",
|
||||
bytes.NewBufferString(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("Expected 400 for invalid provider, got %d", w.Code)
|
||||
}
|
||||
|
||||
var resp map[string]interface{}
|
||||
json.Unmarshal(w.Body.Bytes(), &resp)
|
||||
|
||||
if _, ok := resp["supported_providers"]; !ok {
|
||||
t.Error("Expected supported_providers in error response")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompletionHandlerMissingFields(t *testing.T) {
|
||||
h := NewCompletionHandler()
|
||||
r := gin.New()
|
||||
r.POST("/chat/completions", func(c *gin.Context) {
|
||||
c.Set("user_id", "test-user")
|
||||
h.Complete(c)
|
||||
})
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
body string
|
||||
}{
|
||||
{"missing chat_id", `{"content":"hello"}`},
|
||||
{"missing content", `{"chat_id":"abc"}`},
|
||||
{"empty body", `{}`},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("POST", "/chat/completions",
|
||||
bytes.NewBufferString(tt.body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("Expected 400, got %d (body: %s)", w.Code, w.Body.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSupportedProviders(t *testing.T) {
|
||||
ids := providers.List()
|
||||
|
||||
expected := map[string]bool{
|
||||
"openai": false,
|
||||
"anthropic": false,
|
||||
}
|
||||
|
||||
for _, id := range ids {
|
||||
if _, ok := expected[id]; ok {
|
||||
expected[id] = true
|
||||
}
|
||||
}
|
||||
|
||||
for name, found := range expected {
|
||||
if !found {
|
||||
t.Errorf("Expected provider %s to be registered", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user