package v1 import ( "bytes" "encoding/json" "net/http" "net/http/httptest" "testing" "github.com/gin-gonic/gin" "github.com/stretchr/testify/assert" ) func setupAgentBotInboxRouter() *gin.Engine { gin.SetMode(gin.TestMode) r := gin.New() r.RedirectTrailingSlash = false handler := NewAgentBotInboxHandler(nil) inboxes := r.Group("/api/v1/accounts/:account_id/agent_bot_inboxes") { inboxes.POST("/", handler.Bind) inboxes.DELETE("/:id", handler.Unbind) inboxes.PATCH("/:id/status", handler.UpdateStatus) inboxes.GET("/", handler.ListByInbox) inboxes.GET("/by_bot", handler.ListByBot) } return r } // ======================================== // AgentBotInbox — param validation tests // ======================================== func TestAgentBotInbox_Bind_BadAccountID(t *testing.T) { r := setupAgentBotInboxRouter() w := httptest.NewRecorder() req, _ := http.NewRequest("POST", "/api/v1/accounts/abc/agent_bot_inboxes/", nil) r.ServeHTTP(w, req) assert.Equal(t, http.StatusBadRequest, w.Code) var resp map[string]interface{} json.Unmarshal(w.Body.Bytes(), &resp) errBody := resp["error"].(map[string]interface{}) assert.Contains(t, errBody["message"], "invalid account_id") } func TestAgentBotInbox_Bind_InvalidJSON(t *testing.T) { r := setupAgentBotInboxRouter() w := httptest.NewRecorder() req, _ := http.NewRequest("POST", "/api/v1/accounts/1/agent_bot_inboxes/", bytes.NewReader([]byte("invalid json"))) req.Header.Set("Content-Type", "application/json") r.ServeHTTP(w, req) // account_id passes, ShouldBindJSON fails assert.Equal(t, http.StatusBadRequest, w.Code) var resp map[string]interface{} json.Unmarshal(w.Body.Bytes(), &resp) assert.False(t, resp["success"].(bool)) } func TestAgentBotInbox_Unbind_BadID(t *testing.T) { r := setupAgentBotInboxRouter() w := httptest.NewRecorder() req, _ := http.NewRequest("DELETE", "/api/v1/accounts/1/agent_bot_inboxes/abc", nil) r.ServeHTTP(w, req) assert.Equal(t, http.StatusBadRequest, w.Code) var resp map[string]interface{} json.Unmarshal(w.Body.Bytes(), &resp) errBody := resp["error"].(map[string]interface{}) assert.Contains(t, errBody["message"], "invalid binding ID") } func TestAgentBotInbox_UpdateStatus_BadID(t *testing.T) { r := setupAgentBotInboxRouter() w := httptest.NewRecorder() req, _ := http.NewRequest("PATCH", "/api/v1/accounts/1/agent_bot_inboxes/abc/status", nil) r.ServeHTTP(w, req) assert.Equal(t, http.StatusBadRequest, w.Code) var resp map[string]interface{} json.Unmarshal(w.Body.Bytes(), &resp) errBody := resp["error"].(map[string]interface{}) assert.Contains(t, errBody["message"], "invalid binding ID") } func TestAgentBotInbox_UpdateStatus_InvalidJSON(t *testing.T) { r := setupAgentBotInboxRouter() w := httptest.NewRecorder() req, _ := http.NewRequest("PATCH", "/api/v1/accounts/1/agent_bot_inboxes/1/status", bytes.NewReader([]byte("invalid json"))) req.Header.Set("Content-Type", "application/json") r.ServeHTTP(w, req) // id passes, ShouldBindJSON fails assert.Equal(t, http.StatusBadRequest, w.Code) var resp map[string]interface{} json.Unmarshal(w.Body.Bytes(), &resp) assert.False(t, resp["success"].(bool)) } func TestAgentBotInbox_ListByInbox_MissingInboxID(t *testing.T) { r := setupAgentBotInboxRouter() w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/api/v1/accounts/1/agent_bot_inboxes/", nil) r.ServeHTTP(w, req) // ListByInbox requires inbox_id query param assert.Equal(t, http.StatusBadRequest, w.Code) var resp map[string]interface{} json.Unmarshal(w.Body.Bytes(), &resp) errBody := resp["error"].(map[string]interface{}) assert.Contains(t, errBody["message"], "inbox_id") } func TestAgentBotInbox_ListByInbox_BadInboxID(t *testing.T) { r := setupAgentBotInboxRouter() w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/api/v1/accounts/1/agent_bot_inboxes/?inbox_id=abc", nil) r.ServeHTTP(w, req) assert.Equal(t, http.StatusBadRequest, w.Code) var resp map[string]interface{} json.Unmarshal(w.Body.Bytes(), &resp) errBody := resp["error"].(map[string]interface{}) assert.Contains(t, errBody["message"], "invalid inbox_id") } func TestAgentBotInbox_ListByBot_MissingBotID(t *testing.T) { r := setupAgentBotInboxRouter() w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/api/v1/accounts/1/agent_bot_inboxes/by_bot", nil) r.ServeHTTP(w, req) // ListByBot requires agent_bot_id query param assert.Equal(t, http.StatusBadRequest, w.Code) var resp map[string]interface{} json.Unmarshal(w.Body.Bytes(), &resp) errBody := resp["error"].(map[string]interface{}) assert.Contains(t, errBody["message"], "agent_bot_id") } func TestAgentBotInbox_ListByBot_BadBotID(t *testing.T) { r := setupAgentBotInboxRouter() w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/api/v1/accounts/1/agent_bot_inboxes/by_bot?agent_bot_id=abc", nil) r.ServeHTTP(w, req) assert.Equal(t, http.StatusBadRequest, w.Code) var resp map[string]interface{} json.Unmarshal(w.Body.Bytes(), &resp) errBody := resp["error"].(map[string]interface{}) assert.Contains(t, errBody["message"], "invalid agent_bot_id") }