feat(search): align conversation result payloads
This commit is contained in:
@@ -6,10 +6,12 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gorm.io/datatypes"
|
||||
|
||||
"github.com/gochat/gochat/internal/model"
|
||||
"github.com/gochat/gochat/internal/search"
|
||||
@@ -198,6 +200,59 @@ func TestSearchHandler_SearchConversations_Success(t *testing.T) {
|
||||
require.Len(t, results, 1)
|
||||
}
|
||||
|
||||
func TestSearchHandler_SearchConversations_ChatwootPayloadShape(t *testing.T) {
|
||||
displayID := uint(42)
|
||||
createdAt := time.Date(2026, 6, 7, 8, 30, 0, 0, time.UTC)
|
||||
conversation := model.Conversation{
|
||||
Base: model.Base{ID: 7, CreatedAt: createdAt},
|
||||
DisplayID: &displayID,
|
||||
AccountID: 1,
|
||||
InboxID: 3,
|
||||
ContactID: 5,
|
||||
AssigneeID: uintPtr(9),
|
||||
AdditionalAttributes: datatypes.JSON(`{"mail_subject":"Need pricing"}`),
|
||||
Contact: &model.Contact{Base: model.Base{ID: 5}, AccountID: 1, Name: "Ada", Email: "ada@example.com"},
|
||||
Inbox: &model.Inbox{Base: model.Base{ID: 3}, AccountID: 1, Name: "Website", ChannelType: "web_widget", ChannelID: 11},
|
||||
Assignee: &model.User{Base: model.Base{ID: 9}, AccountID: 1, Name: "Agent One", Email: "agent@example.com", Role: "administrator"},
|
||||
Messages: []model.Message{{
|
||||
Base: model.Base{ID: 13, CreatedAt: createdAt.Add(-time.Minute)},
|
||||
AccountID: 1,
|
||||
InboxID: 3,
|
||||
ConversationID: 7,
|
||||
Content: "hello",
|
||||
MessageType: "incoming",
|
||||
ContentType: "text",
|
||||
Status: "sent",
|
||||
}},
|
||||
}
|
||||
repo := &mockSearchRepo{conversations: []model.Conversation{conversation}, convTotal: 1}
|
||||
svc := search.NewSearchService(repo)
|
||||
handler := NewSearchHandler(svc)
|
||||
router := setupSearchHandlerRouter(handler)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest("GET", "/api/v1/accounts/1/search/conversations?q=ada", nil)
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
require.Equal(t, http.StatusOK, w.Code)
|
||||
var body map[string]any
|
||||
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &body))
|
||||
payload := body["payload"].(map[string]any)
|
||||
results := payload["conversations"].([]any)
|
||||
require.Len(t, results, 1)
|
||||
item := results[0].(map[string]any)
|
||||
assert.Equal(t, float64(42), item["id"])
|
||||
assert.Equal(t, float64(createdAt.Unix()), item["created_at"])
|
||||
assert.Equal(t, "Need pricing", item["additional_attributes"].(map[string]any)["mail_subject"])
|
||||
assert.Equal(t, "Ada", item["contact"].(map[string]any)["name"])
|
||||
assert.Equal(t, "ada@example.com", item["contact"].(map[string]any)["email"])
|
||||
assert.Equal(t, "Website", item["inbox"].(map[string]any)["name"])
|
||||
assert.Equal(t, float64(11), item["inbox"].(map[string]any)["channel_id"])
|
||||
assert.Equal(t, "Agent One", item["agent"].(map[string]any)["available_name"])
|
||||
assert.Equal(t, float64(13), item["message"].(map[string]any)["id"])
|
||||
assert.Equal(t, float64(0), item["message"].(map[string]any)["message_type"])
|
||||
}
|
||||
|
||||
func TestSearchHandler_SearchConversations_InvalidAccountID(t *testing.T) {
|
||||
repo := &mockSearchRepo{}
|
||||
svc := search.NewSearchService(repo)
|
||||
|
||||
Reference in New Issue
Block a user