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

87 lines
3.2 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 ConversationInsightHandlerTestSuite struct {
suite.Suite
handler *ConversationInsightHandler
router *gin.Engine
}
func (s *ConversationInsightHandlerTestSuite) SetupSuite() {
// Use nil/zero service for routing/param-validation tests
svc := &service.ConversationInsightService{}
s.handler = NewConversationInsightHandler(svc)
gin.SetMode(gin.TestMode)
r := gin.New()
group := r.Group("/api/v1/accounts/:id")
group.POST("/captain/insights/participants", s.handler.AnalyzeParticipants)
group.POST("/captain/insights/action_items", s.handler.ExtractActionItems)
group.POST("/captain/insights/label_suggestions", s.handler.SuggestLabels)
s.router = r
}
func TestConversationInsightHandlerTestSuite(t *testing.T) {
suite.Run(t, new(ConversationInsightHandlerTestSuite))
}
func (s *ConversationInsightHandlerTestSuite) TestAnalyzeParticipants_InvalidAccountID() {
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/api/v1/accounts/abc/captain/insights/participants", bytes.NewBufferString(`{"conversation_id":1}`))
req.Header.Set("Content-Type", "application/json")
s.router.ServeHTTP(w, req)
s.Equal(http.StatusBadRequest, w.Code)
}
func (s *ConversationInsightHandlerTestSuite) TestAnalyzeParticipants_InvalidJSON() {
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/api/v1/accounts/1/captain/insights/participants", bytes.NewBufferString(`{invalid`))
req.Header.Set("Content-Type", "application/json")
s.router.ServeHTTP(w, req)
s.Equal(http.StatusBadRequest, w.Code)
}
func (s *ConversationInsightHandlerTestSuite) TestExtractActionItems_InvalidAccountID() {
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/api/v1/accounts/abc/captain/insights/action_items", bytes.NewBufferString(`{"conversation_id":1}`))
req.Header.Set("Content-Type", "application/json")
s.router.ServeHTTP(w, req)
s.Equal(http.StatusBadRequest, w.Code)
}
func (s *ConversationInsightHandlerTestSuite) TestExtractActionItems_InvalidJSON() {
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/api/v1/accounts/1/captain/insights/action_items", bytes.NewBufferString(`{invalid`))
req.Header.Set("Content-Type", "application/json")
s.router.ServeHTTP(w, req)
s.Equal(http.StatusBadRequest, w.Code)
}
func (s *ConversationInsightHandlerTestSuite) TestSuggestLabels_InvalidAccountID() {
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/api/v1/accounts/abc/captain/insights/label_suggestions", bytes.NewBufferString(`{"conversation_id":1}`))
req.Header.Set("Content-Type", "application/json")
s.router.ServeHTTP(w, req)
s.Equal(http.StatusBadRequest, w.Code)
}
func (s *ConversationInsightHandlerTestSuite) TestSuggestLabels_InvalidJSON() {
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/api/v1/accounts/1/captain/insights/label_suggestions", bytes.NewBufferString(`{invalid`))
req.Header.Set("Content-Type", "application/json")
s.router.ServeHTTP(w, req)
s.Equal(http.StatusBadRequest, w.Code)
}