From 3d0398deb73234899acbf6e26a314ffc9d930239 Mon Sep 17 00:00:00 2001 From: aliraza556 Date: Thu, 19 Dec 2024 23:58:03 +0500 Subject: [PATCH] Unit Test GetChat --- handlers/chat_test.go | 166 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) diff --git a/handlers/chat_test.go b/handlers/chat_test.go index cebbb9172..71f17e78f 100644 --- a/handlers/chat_test.go +++ b/handlers/chat_test.go @@ -4,8 +4,10 @@ import ( "bytes" "context" "encoding/json" + "fmt" "net/http" "net/http/httptest" + "net/url" "testing" "github.com/go-chi/chi" @@ -149,3 +151,167 @@ func TestUpdateChat(t *testing.T) { assert.Equal(t, "Chat not found", response.Message) }) } + +func TestGetChat(t *testing.T) { + teardownSuite := SetupSuite(t) + defer teardownSuite(t) + + chatHandler := NewChatHandler(&http.Client{}, db.TestDB) + + t.Run("should successfully get chats when valid workspace_id is provided", func(t *testing.T) { + + db.DeleteAllChats() + + chats := []*db.Chat{ + { + ID: uuid.New().String(), + WorkspaceID: "workspace1", + Title: "Chat 1", + }, + { + ID: uuid.New().String(), + WorkspaceID: "workspace1", + Title: "Chat 2", + }, + } + for _, chat := range chats { + db.TestDB.AddChat(chat) + } + + rr := httptest.NewRecorder() + req, err := http.NewRequest(http.MethodGet, "/hivechat?workspace_id=workspace1", nil) + assert.NoError(t, err) + + handler := http.HandlerFunc(chatHandler.GetChat) + handler.ServeHTTP(rr, req) + + var response ChatResponse + err = json.NewDecoder(rr.Body).Decode(&response) + assert.NoError(t, err) + + assert.Equal(t, http.StatusOK, rr.Code) + assert.True(t, response.Success) + + responseChats, ok := response.Data.([]interface{}) + assert.True(t, ok) + assert.Equal(t, 2, len(responseChats)) + + firstChat := responseChats[0].(map[string]interface{}) + assert.NotEmpty(t, firstChat["id"]) + assert.Equal(t, "workspace1", firstChat["workspaceId"]) + assert.Contains(t, []string{"Chat 1", "Chat 2"}, firstChat["title"]) + }) + + t.Run("should return empty array when no chats exist for workspace", func(t *testing.T) { + rr := httptest.NewRecorder() + req, err := http.NewRequest(http.MethodGet, "/hivechat?workspace_id=nonexistent", nil) + assert.NoError(t, err) + + handler := http.HandlerFunc(chatHandler.GetChat) + handler.ServeHTTP(rr, req) + + var response ChatResponse + err = json.NewDecoder(rr.Body).Decode(&response) + assert.NoError(t, err) + + assert.Equal(t, http.StatusOK, rr.Code) + assert.True(t, response.Success) + + responseChats, ok := response.Data.([]interface{}) + assert.True(t, ok) + assert.Empty(t, responseChats) + }) + + t.Run("should return bad request when workspace_id is missing", func(t *testing.T) { + rr := httptest.NewRecorder() + req, err := http.NewRequest(http.MethodGet, "/hivechat", nil) + assert.NoError(t, err) + + handler := http.HandlerFunc(chatHandler.GetChat) + handler.ServeHTTP(rr, req) + + var response ChatResponse + err = json.NewDecoder(rr.Body).Decode(&response) + assert.NoError(t, err) + + assert.Equal(t, http.StatusBadRequest, rr.Code) + assert.False(t, response.Success) + assert.Equal(t, "workspace_id query parameter is required", response.Message) + }) + + t.Run("should return bad request when workspace_id is empty", func(t *testing.T) { + rr := httptest.NewRecorder() + req, err := http.NewRequest(http.MethodGet, "/hivechat?workspace_id=", nil) + assert.NoError(t, err) + + handler := http.HandlerFunc(chatHandler.GetChat) + handler.ServeHTTP(rr, req) + + var response ChatResponse + err = json.NewDecoder(rr.Body).Decode(&response) + assert.NoError(t, err) + + assert.Equal(t, http.StatusBadRequest, rr.Code) + assert.False(t, response.Success) + assert.Equal(t, "workspace_id query parameter is required", response.Message) + }) + + t.Run("should handle special characters in workspace_id", func(t *testing.T) { + workspaceID := "workspace-123-special" + chat := &db.Chat{ + ID: uuid.New().String(), + WorkspaceID: workspaceID, + Title: "Special Chat", + } + db.TestDB.AddChat(chat) + + rr := httptest.NewRecorder() + req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/hivechat?workspace_id=%s", url.QueryEscape(workspaceID)), nil) + assert.NoError(t, err) + + handler := http.HandlerFunc(chatHandler.GetChat) + handler.ServeHTTP(rr, req) + + var response ChatResponse + err = json.NewDecoder(rr.Body).Decode(&response) + assert.NoError(t, err) + + assert.Equal(t, http.StatusOK, rr.Code) + assert.True(t, response.Success) + + responseChats, ok := response.Data.([]interface{}) + assert.True(t, ok) + assert.Equal(t, 1, len(responseChats)) + }) + + t.Run("should handle large number of chats", func(t *testing.T) { + workspaceID := "workspace-large" + + for i := 0; i < 100; i++ { + chat := &db.Chat{ + ID: uuid.New().String(), + WorkspaceID: workspaceID, + Title: fmt.Sprintf("Chat %d", i), + } + db.TestDB.AddChat(chat) + } + + rr := httptest.NewRecorder() + req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/hivechat?workspace_id=%s", workspaceID), nil) + assert.NoError(t, err) + + handler := http.HandlerFunc(chatHandler.GetChat) + handler.ServeHTTP(rr, req) + + var response ChatResponse + err = json.NewDecoder(rr.Body).Decode(&response) + assert.NoError(t, err) + + assert.Equal(t, http.StatusOK, rr.Code) + assert.True(t, response.Success) + + responseChats, ok := response.Data.([]interface{}) + assert.True(t, ok) + assert.Equal(t, 100, len(responseChats)) + }) +}