package v1 import ( "bytes" "encoding/json" "fmt" "net/http" "net/http/httptest" "testing" "github.com/gin-gonic/gin" "github.com/gochat/gochat/internal/model" "github.com/gochat/gochat/internal/repository" "github.com/gochat/gochat/internal/service" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" "gorm.io/driver/sqlite" "gorm.io/gorm" "gorm.io/gorm/logger" ) type AgentBotHandlerTestSuite struct { suite.Suite db *gorm.DB handler *AgentBotHandler account *model.Account user *model.User } func (s *AgentBotHandlerTestSuite) 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.User{}, &model.AgentBot{}, &model.AgentBotInbox{})) s.db = db repo := repository.NewAgentBotRepo(db) svc := service.NewAgentBotService(repo) s.handler = NewAgentBotHandler(svc) s.account = &model.Account{Name: "test-agent-bot-account"} s.db.Create(s.account) s.user = &model.User{Name: "test-agent-bot-user", Email: "bot@example.com"} s.db.Create(s.user) } func (s *AgentBotHandlerTestSuite) TearDownSuite() { if s.db != nil { sqlDB, _ := s.db.DB() sqlDB.Close() } } func TestAgentBotHandlerSuite(t *testing.T) { suite.Run(t, new(AgentBotHandlerTestSuite)) } func (s *AgentBotHandlerTestSuite) TestPlatformList() { r := gin.New() r.GET("/platform/api/v1/agent_bots", s.handler.PlatformList) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/platform/api/v1/agent_bots", nil) r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusOK, w.Code) } func (s *AgentBotHandlerTestSuite) TestPlatformCreate_Success() { r := gin.New() r.POST("/platform/api/v1/agent_bots", s.handler.PlatformCreate) body := map[string]interface{}{ "name": "test-global-bot", "bot_type": "webhook", "outgoing_url": "https://example.com/webhook", } b, _ := json.Marshal(body) w := httptest.NewRecorder() req, _ := http.NewRequest("POST", "/platform/api/v1/agent_bots", bytes.NewBuffer(b)) req.Header.Set("Content-Type", "application/json") r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusCreated, w.Code) } func (s *AgentBotHandlerTestSuite) TestPlatformCreate_BadRequest() { r := gin.New() r.POST("/platform/api/v1/agent_bots", s.handler.PlatformCreate) w := httptest.NewRecorder() req, _ := http.NewRequest("POST", "/platform/api/v1/agent_bots", nil) req.Header.Set("Content-Type", "application/json") r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusBadRequest, w.Code) } func (s *AgentBotHandlerTestSuite) TestAccountList() { r := gin.New() r.GET("/api/v1/accounts/:account_id/agent_bots", s.handler.List) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/agent_bots", s.account.ID), nil) r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusOK, w.Code) } func (s *AgentBotHandlerTestSuite) TestAccountCreate_Success() { r := gin.New() r.POST("/api/v1/accounts/:account_id/agent_bots", s.handler.Create) body := map[string]interface{}{ "name": "test-account-bot", "bot_type": "webhook", "outgoing_url": "https://example.com/webhook", } b, _ := json.Marshal(body) w := httptest.NewRecorder() req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/agent_bots", s.account.ID), bytes.NewBuffer(b)) req.Header.Set("Content-Type", "application/json") r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusCreated, w.Code) } func (s *AgentBotHandlerTestSuite) TestAccountCreate_BadRequest() { r := gin.New() r.POST("/api/v1/accounts/:account_id/agent_bots", s.handler.Create) w := httptest.NewRecorder() req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/agent_bots", s.account.ID), nil) req.Header.Set("Content-Type", "application/json") r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusBadRequest, w.Code) } func (s *AgentBotHandlerTestSuite) TestAccountGet_NotFound() { r := gin.New() r.GET("/api/v1/accounts/:account_id/agent_bots/:agent_bot_id", s.handler.Get) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/agent_bots/99999", s.account.ID), nil) r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusNotFound, w.Code) } func (s *AgentBotHandlerTestSuite) TestPlatformGet_NotFound() { r := gin.New() r.GET("/platform/api/v1/agent_bots/:agent_bot_id", s.handler.PlatformGet) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/platform/api/v1/agent_bots/99999", nil) r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusNotFound, w.Code) } func (s *AgentBotHandlerTestSuite) TestUpdate_BadRequest() { r := gin.New() r.PUT("/api/v1/accounts/:account_id/agent_bots/:agent_bot_id", s.handler.Update) w := httptest.NewRecorder() req, _ := http.NewRequest("PUT", fmt.Sprintf("/api/v1/accounts/%d/agent_bots/abc", s.account.ID), nil) req.Header.Set("Content-Type", "application/json") r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusBadRequest, w.Code) } func (s *AgentBotHandlerTestSuite) TestDelete_BadRequest() { r := gin.New() r.DELETE("/api/v1/accounts/:account_id/agent_bots/:agent_bot_id", s.handler.Delete) w := httptest.NewRecorder() req, _ := http.NewRequest("DELETE", fmt.Sprintf("/api/v1/accounts/%d/agent_bots/abc", s.account.ID), nil) r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusBadRequest, w.Code) } func (s *AgentBotHandlerTestSuite) TestResetToken_BadRequest() { r := gin.New() r.POST("/api/v1/accounts/:account_id/agent_bots/:agent_bot_id/reset_token", s.handler.ResetToken) w := httptest.NewRecorder() req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/agent_bots/abc/reset_token", s.account.ID), nil) r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusBadRequest, w.Code) } func (s *AgentBotHandlerTestSuite) TestResetSecret_BadRequest() { r := gin.New() r.POST("/api/v1/accounts/:account_id/agent_bots/:agent_bot_id/reset_secret", s.handler.ResetSecret) w := httptest.NewRecorder() req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/agent_bots/abc/reset_secret", s.account.ID), nil) r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusBadRequest, w.Code) } func (s *AgentBotHandlerTestSuite) TestDeleteAvatar_BadRequest() { r := gin.New() r.DELETE("/api/v1/accounts/:account_id/agent_bots/:agent_bot_id/avatar", s.handler.DeleteAvatar) w := httptest.NewRecorder() req, _ := http.NewRequest("DELETE", fmt.Sprintf("/api/v1/accounts/%d/agent_bots/abc/avatar", s.account.ID), nil) r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusBadRequest, w.Code) }