This repository has been archived on 2026-04-03. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
core/server/handlers/apiconfigs_test.go
2026-02-21 19:03:19 +00:00

127 lines
2.9 KiB
Go

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 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 channel_id", `{"content":"hello"}`},
{"missing content", `{"channel_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)
}
}
}