We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stakwork Run
File: /tmp/stakwork/sphinx-tribes/handlers/chat.go
package chat import ( "encoding/json" "errors" "fmt" "net/http" "net/http/httptest" "testing" "github.com/go-chi/chi/v5" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" ) // Mocking the database interface type MockDB struct { mock.Mock } func (mdb *MockDB) GetChatMessagesForChatID(chatID string) ([]string, error) { args := mdb.Called(chatID) return args.Get(0).([]string), args.Error(1) } // ChatHandler struct with a mock database type ChatHandler struct { db *MockDB } // ChatResponse and HistoryChatResponse structs type ChatResponse struct { Success bool `json:"success"` Message string `json:"message"` } type HistoryChatResponse struct { Success bool `json:"success"` Data []string `json:"data"` } func TestGetChatHistory(t *testing.T) { mockDB := new(MockDB) handler := &ChatHandler{db: mockDB} tests := []struct { name string chatID string mockMessages []string mockError error expectedStatus int expectedBody interface{} }{ { name: "Valid Chat ID with Messages", chatID: "valid-chat-id", mockMessages: []string{"message1", "message2"}, mockError: nil, expectedStatus: http.StatusOK, expectedBody: HistoryChatResponse{ Success: true, Data: []string{"message1", "message2"}, }, }, { name: "Valid Chat ID with No Messages", chatID: "valid-chat-id-no-messages", mockMessages: []string{}, mockError: nil, expectedStatus: http.StatusOK, expectedBody: HistoryChatResponse{ Success: true, Data: []string{}, }, }, { name: "Empty Chat ID", chatID: "", expectedStatus: http.StatusBadRequest, expectedBody: ChatResponse{ Success: false, Message: "Chat ID is required", }, }, { name: "Non-Existent Chat ID", chatID: "non-existent-chat-id", mockMessages: []string{}, mockError: nil, expectedStatus: http.StatusOK, expectedBody: HistoryChatResponse{ Success: true, Data: []string{}, }, }, { name: "Database Error", chatID: "valid-chat-id", mockMessages: nil, mockError: errors.New("database error"), expectedStatus: http.StatusInternalServerError, expectedBody: ChatResponse{ Success: false, Message: "Failed to fetch chat history: database error", }, }, { name: "Large Number of Messages", chatID: "large-message-chat-id", mockMessages: make([]string, 1000), // Simulating large data set mockError: nil, expectedStatus: http.StatusOK, expectedBody: HistoryChatResponse{ Success: true, Data: make([]string, 1000), }, }, { name: "Chat ID with Special Characters", chatID: "special-!@#-chat-id", mockMessages: []string{"specialMessage"}, mockError: nil, expectedStatus: http.StatusOK, expectedBody: HistoryChatResponse{ Success: true, Data: []string{"specialMessage"}, }, }, { name: "Chat ID with Maximum Length", chatID: string(make([]byte, 255)), mockMessages: []string{"maxLengthMessage"}, mockError: nil, expectedStatus: http.StatusOK, expectedBody: HistoryChatResponse{ Success: true, Data: []string{"maxLengthMessage"}, }, }, { name: "Malformed Chat ID", chatID: "malformed-chat-id-###", mockMessages: []string{}, mockError: nil, expectedStatus: http.StatusOK, expectedBody: HistoryChatResponse{ Success: true, Data: []string{}, }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if tt.chatID != "" { mockDB.On("GetChatMessagesForChatID", tt.chatID).Return(tt.mockMessages, tt.mockError) } req := httptest.NewRequest("GET", fmt.Sprintf("/history/%s", tt.chatID), nil) rr := httptest.NewRecorder() r := chi.NewRouter() r.Get("/history/{uuid}", handler.GetChatHistory) r.ServeHTTP(rr, req) assert.Equal(t, tt.expectedStatus, rr.Code) var actualBody interface{} if tt.expectedStatus == http.StatusOK { actualBody = &HistoryChatResponse{} } else { actualBody = &ChatResponse{} } json.NewDecoder(rr.Body).Decode(actualBody) assert.Equal(t, tt.expectedBody, actualBody) }) } }
The text was updated successfully, but these errors were encountered:
@tomsmith8 assign me?
Sorry, something went wrong.
@tomsmith8 I can help?
@tomsmith8 Please assign me?
@tomsmith8 please assign
MirzaHanan
Successfully merging a pull request may close this issue.
Unit Test Coverage for "GetChatHistory"
Stakwork Run
Unit Test Code
File: /tmp/stakwork/sphinx-tribes/handlers/chat.go
The text was updated successfully, but these errors were encountered: