package v1 import ( "bytes" "fmt" "net/http" "net/http/httptest" "testing" "github.com/gin-gonic/gin" "github.com/gochat/gochat/internal/automation" "github.com/gochat/gochat/internal/model" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" "gorm.io/driver/sqlite" "gorm.io/gorm" "gorm.io/gorm/logger" ) type CsatSurveyHandlerTestSuite struct { suite.Suite db *gorm.DB handler *CsatSurveyHandler account *model.Account } type csatSurveyTestDBProvider struct { db *gorm.DB } func (p *csatSurveyTestDBProvider) DB() *gorm.DB { return p.db } func (s *CsatSurveyHandlerTestSuite) SetupSuite() { gin.SetMode(gin.TestMode) db, err := gorm.Open(sqlite.Open("file::memory:"), &gorm.Config{ Logger: logger.Default.LogMode(logger.Silent), }) s.Require().NoError(err) s.Require().NoError(db.AutoMigrate(&model.Account{}, &model.Conversation{}, &automation.CsatSurveyResponse{}, &model.ReportingEventsRollup{})) s.db = db svc := automation.NewCsatSurveyService(&csatSurveyTestDBProvider{db: db}) s.handler = NewCsatSurveyHandler(svc) s.account = &model.Account{Name: "test-csat-survey-account"} s.Require().NoError(db.Create(s.account).Error) } func (s *CsatSurveyHandlerTestSuite) SetupTest() { s.db.Exec("DELETE FROM csat_survey_responses") } func (s *CsatSurveyHandlerTestSuite) TearDownSuite() { if s.db != nil { sqlDB, _ := s.db.DB() sqlDB.Close() } } func TestCsatSurveyHandlerSuite(t *testing.T) { suite.Run(t, new(CsatSurveyHandlerTestSuite)) } func (s *CsatSurveyHandlerTestSuite) TestList_Success() { r := gin.New() r.GET("/api/v1/accounts/:account_id/csats", s.handler.List) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/csats", s.account.ID), nil) r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusOK, w.Code) } func (s *CsatSurveyHandlerTestSuite) TestMetrics_Success() { r := gin.New() r.GET("/api/v1/accounts/:account_id/csat_metrics", s.handler.Metrics) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/csat_metrics", s.account.ID), nil) r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusOK, w.Code) } func (s *CsatSurveyHandlerTestSuite) TestUpdateReviewNotes_Success() { survey := &automation.CsatSurveyResponse{AccountID: s.account.ID, ConversationID: 1, Rating: 5} s.Require().NoError(s.db.Create(survey).Error) r := gin.New() r.PUT("/api/v1/accounts/:account_id/csats/:id/review_notes", func(c *gin.Context) { c.Set("user_id", float64(1)) s.handler.UpdateReviewNotes(c) }) w := httptest.NewRecorder() body := `{"review_notes":"good conversation"}` req, _ := http.NewRequest("PUT", fmt.Sprintf("/api/v1/accounts/%d/csats/%d/review_notes", s.account.ID, survey.ID), bytes.NewBufferString(body)) req.Header.Set("Content-Type", "application/json") r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusOK, w.Code) } func (s *CsatSurveyHandlerTestSuite) TestUpdate_Success() { survey := &automation.CsatSurveyResponse{AccountID: s.account.ID, ConversationID: 1, Rating: 5} s.Require().NoError(s.db.Create(survey).Error) r := gin.New() r.PUT("/api/v1/accounts/:account_id/csats/:id", func(c *gin.Context) { c.Set("user_id", float64(1)) s.handler.Update(c) }) w := httptest.NewRecorder() body := `{"review_notes":"updated notes"}` req, _ := http.NewRequest("PUT", fmt.Sprintf("/api/v1/accounts/%d/csats/%d", s.account.ID, survey.ID), bytes.NewBufferString(body)) req.Header.Set("Content-Type", "application/json") r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusOK, w.Code) } func (s *CsatSurveyHandlerTestSuite) TestList_BadRequest_InvalidAccountID() { r := gin.New() r.GET("/api/v1/accounts/:account_id/csats", s.handler.List) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/api/v1/accounts/abc/csats", nil) r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusBadRequest, w.Code) }