Files
gochat/internal/handler/api/v1/rag_handler_test.go
T
2026-06-04 15:44:48 +08:00

59 lines
1.6 KiB
Go

package v1
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/suite"
"github.com/gochat/gochat/internal/service"
)
type RAGHandlerTestSuite struct {
suite.Suite
handler *RAGHandler
router *gin.Engine
}
func (s *RAGHandlerTestSuite) SetupSuite() {
// Nil/zero service for param-validation only
svc := &service.RAGService{}
s.handler = NewRAGHandler(svc)
gin.SetMode(gin.TestMode)
r := gin.New()
group := r.Group("/api/v1/accounts/:id")
group.POST("/captain/rag/query", s.handler.Query)
group.POST("/captain/rag/index/:response_id", s.handler.IndexResponse)
s.router = r
}
func TestRAGHandlerTestSuite(t *testing.T) {
suite.Run(t, new(RAGHandlerTestSuite))
}
func (s *RAGHandlerTestSuite) TestQuery_InvalidAccountID() {
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/api/v1/accounts/abc/captain/rag/query", bytes.NewBufferString(`{"query":"test"}`))
req.Header.Set("Content-Type", "application/json")
s.router.ServeHTTP(w, req)
s.Equal(http.StatusBadRequest, w.Code)
}
func (s *RAGHandlerTestSuite) TestQuery_InvalidJSON() {
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/api/v1/accounts/1/captain/rag/query", bytes.NewBufferString(`{invalid`))
req.Header.Set("Content-Type", "application/json")
s.router.ServeHTTP(w, req)
s.Equal(http.StatusBadRequest, w.Code)
}
func (s *RAGHandlerTestSuite) TestIndexResponse_InvalidResponseID() {
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/api/v1/accounts/1/captain/rag/index/abc", nil)
s.router.ServeHTTP(w, req)
s.Equal(http.StatusBadRequest, w.Code)
}