Issue 9 chat api (#25)

This commit is contained in:
2026-02-16 01:01:33 +00:00
parent 8f10352e7d
commit 676710a005
5 changed files with 836 additions and 78 deletions

View File

@@ -27,16 +27,14 @@ func TestHealthEndpoint(t *testing.T) {
r.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
t.Errorf("Expected %d, got %d", http.StatusOK, w.Code)
}
}
func TestCORSHeaders(t *testing.T) {
r := gin.New()
r.Use(middleware.CORS())
r.GET("/test", func(c *gin.Context) {
c.Status(200)
})
r.GET("/test", func(c *gin.Context) { c.Status(200) })
w := httptest.NewRecorder()
req, _ := http.NewRequest("OPTIONS", "/test", nil)
@@ -50,12 +48,15 @@ func TestCORSHeaders(t *testing.T) {
}
}
func TestAuthRoutesRegistered(t *testing.T) {
cfg := &config.Config{JWTSecret: "test-secret"}
func TestAllRoutesRegistered(t *testing.T) {
cfg := &config.Config{JWTSecret: "test"}
auth := handlers.NewAuthHandler(cfg)
chats := handlers.NewChatHandler()
msgs := handlers.NewMessageHandler()
r := gin.New()
api := r.Group("/api/v1")
authGroup := api.Group("/auth")
{
authGroup.POST("/register", auth.Register)
@@ -64,6 +65,19 @@ func TestAuthRoutesRegistered(t *testing.T) {
authGroup.POST("/logout", auth.Logout)
}
protected := api.Group("")
{
protected.GET("/chats", chats.ListChats)
protected.POST("/chats", chats.CreateChat)
protected.GET("/chats/:id", chats.GetChat)
protected.PUT("/chats/:id", chats.UpdateChat)
protected.DELETE("/chats/:id", chats.DeleteChat)
protected.GET("/chats/:id/messages", msgs.ListMessages)
protected.POST("/chats/:id/messages", msgs.CreateMessage)
protected.POST("/chats/:id/regenerate", msgs.Regenerate)
}
routes := r.Routes()
routePaths := make(map[string]bool)
for _, route := range routes {
@@ -75,65 +89,47 @@ func TestAuthRoutesRegistered(t *testing.T) {
"POST /api/v1/auth/login",
"POST /api/v1/auth/refresh",
"POST /api/v1/auth/logout",
"GET /api/v1/chats",
"POST /api/v1/chats",
"GET /api/v1/chats/:id",
"PUT /api/v1/chats/:id",
"DELETE /api/v1/chats/:id",
"GET /api/v1/chats/:id/messages",
"POST /api/v1/chats/:id/messages",
"POST /api/v1/chats/:id/regenerate",
}
for _, e := range expected {
if !routePaths[e] {
t.Errorf("Expected route %s to be registered", e)
t.Errorf("Missing route: %s", e)
}
}
}
func TestProtectedRoutesRegistered(t *testing.T) {
func TestStubHandler(t *testing.T) {
r := gin.New()
api := r.Group("/api/v1")
protected := api.Group("")
{
protected.GET("/chats", handleListChats)
protected.POST("/chats", handleCreateChat)
protected.GET("/chats/:id", handleGetChat)
protected.DELETE("/chats/:id", handleDeleteChat)
protected.GET("/chats/:id/messages", handleGetMessages)
protected.POST("/chats/:id/messages", handleCreateMessage)
protected.GET("/settings", handleGetSettings)
protected.GET("/api-configs", handleListAPIConfigs)
protected.GET("/models", handleListModels)
}
r.GET("/test", stubHandler("settings"))
routes := r.Routes()
if len(routes) != 9 {
t.Errorf("Expected 9 routes, got %d", len(routes))
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/test", nil)
r.ServeHTTP(w, req)
if w.Code != 501 {
t.Errorf("Expected 501, got %d", w.Code)
}
}
func TestAuthMiddlewareNoDatabase(t *testing.T) {
cfg := &config.Config{JWTSecret: "test-secret"}
cfg := &config.Config{JWTSecret: "test"}
r := gin.New()
r.Use(middleware.Auth(cfg))
r.GET("/protected", func(c *gin.Context) {
c.JSON(200, gin.H{"ok": true})
})
r.GET("/protected", func(c *gin.Context) { c.JSON(200, gin.H{"ok": true}) })
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/protected", nil)
r.ServeHTTP(w, req)
// No database connected → middleware should pass through
if w.Code != http.StatusOK {
t.Errorf("Expected pass-through with no DB, got status %d", w.Code)
}
}
func TestPlaceholderHandlersReturn501(t *testing.T) {
r := gin.New()
r.GET("/chats", handleListChats)
r.POST("/chats", handleCreateChat)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/chats", nil)
r.ServeHTTP(w, req)
if w.Code != 501 {
t.Errorf("Expected 501 for unimplemented handler, got %d", w.Code)
t.Errorf("Expected pass-through with no DB, got %d", w.Code)
}
}