package v1 import ( "encoding/json" "net/http" "net/http/httptest" "testing" "github.com/gin-gonic/gin" "github.com/stretchr/testify/suite" "gorm.io/driver/sqlite" "gorm.io/gorm" "github.com/gochat/gochat/internal/model" "github.com/gochat/gochat/internal/repository" "github.com/gochat/gochat/internal/service" ) type LiveReportHandlerTestSuite struct { suite.Suite db *gorm.DB handler *LiveReportHandler router *gin.Engine } func (s *LiveReportHandlerTestSuite) SetupSuite() { s.db, _ = gorm.Open(sqlite.Open(":memory:"), &gorm.Config{}) s.db.AutoMigrate(&model.Account{}, &model.Conversation{}, &model.ReportingEventsRollup{}) anSvc := service.NewAnalyticsService( repository.NewReportingEventRepo(s.db), repository.NewReportingEventsRollupRepo(s.db), ) s.handler = NewLiveReportHandler(anSvc) gin.SetMode(gin.TestMode) r := gin.New() r.GET("/api/v1/accounts/:account_id/live_reports/conversation_metrics", s.handler.ConversationMetrics) s.router = r } func TestLiveReportHandlerTestSuite(t *testing.T) { suite.Run(t, new(LiveReportHandlerTestSuite)) } func (s *LiveReportHandlerTestSuite) TestConversationMetrics_InvalidAccountID() { w := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/v1/accounts/abc/live_reports/conversation_metrics", nil) s.router.ServeHTTP(w, req) s.Equal(http.StatusBadRequest, w.Code) } func (s *LiveReportHandlerTestSuite) TestConversationMetrics_Success() { w := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/v1/accounts/1/live_reports/conversation_metrics", nil) s.router.ServeHTTP(w, req) s.Equal(http.StatusOK, w.Code) var body map[string]interface{} s.NoError(json.Unmarshal(w.Body.Bytes(), &body)) s.NotContains(body, "success") s.Equal(float64(0), body["open"]) s.Equal(float64(0), body["unattended"]) s.Equal(float64(0), body["unassigned"]) s.Equal(float64(0), body["pending"]) }