feat(search): align chatwoot search payloads
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/gochat/gochat/internal/model"
|
||||
"github.com/gochat/gochat/internal/search"
|
||||
@@ -20,9 +21,10 @@ type mockSearchRepo struct {
|
||||
convTotal int64
|
||||
convErr error
|
||||
|
||||
messages []model.Message
|
||||
msgTotal int64
|
||||
msgErr error
|
||||
messages []model.Message
|
||||
msgTotal int64
|
||||
msgErr error
|
||||
msgFilter *search.SearchFilter
|
||||
|
||||
contacts []model.Contact
|
||||
contactTotal int64
|
||||
@@ -42,6 +44,7 @@ func (m *mockSearchRepo) SearchConversations(ctx context.Context, accountID uint
|
||||
}
|
||||
|
||||
func (m *mockSearchRepo) SearchMessages(ctx context.Context, accountID uint, query string, filter *search.SearchFilter) ([]model.Message, int64, error) {
|
||||
m.msgFilter = filter
|
||||
return m.messages, m.msgTotal, m.msgErr
|
||||
}
|
||||
|
||||
@@ -117,7 +120,13 @@ func TestSearchHandler_GlobalSearch_Success(t *testing.T) {
|
||||
|
||||
var body map[string]interface{}
|
||||
assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &body))
|
||||
assert.Equal(t, true, body["success"])
|
||||
require.NotContains(t, body, "success")
|
||||
payload, ok := body["payload"].(map[string]interface{})
|
||||
require.True(t, ok)
|
||||
assert.Len(t, payload["conversations"], 1)
|
||||
assert.Len(t, payload["contacts"], 1)
|
||||
assert.Len(t, payload["messages"], 1)
|
||||
assert.Len(t, payload["articles"], 1)
|
||||
}
|
||||
|
||||
func TestSearchHandler_GlobalSearch_InvalidAccountID(t *testing.T) {
|
||||
@@ -156,11 +165,10 @@ func TestSearchHandler_GlobalSearch_WithFilterParams(t *testing.T) {
|
||||
|
||||
var body map[string]interface{}
|
||||
assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &body))
|
||||
assert.Equal(t, true, body["success"])
|
||||
|
||||
meta := body["meta"].(map[string]interface{})
|
||||
assert.Equal(t, float64(2), meta["page"])
|
||||
assert.Equal(t, float64(5), meta["per_page"])
|
||||
require.NotContains(t, body, "success")
|
||||
payload, ok := body["payload"].(map[string]interface{})
|
||||
require.True(t, ok)
|
||||
assert.Len(t, payload["conversations"], 1)
|
||||
}
|
||||
|
||||
// ========== SearchConversations handler tests ==========
|
||||
@@ -182,11 +190,12 @@ func TestSearchHandler_SearchConversations_Success(t *testing.T) {
|
||||
|
||||
var body map[string]interface{}
|
||||
assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &body))
|
||||
assert.Equal(t, true, body["success"])
|
||||
|
||||
data := body["data"].(map[string]interface{})
|
||||
results := data["results"]
|
||||
assert.NotNil(t, results)
|
||||
require.NotContains(t, body, "success")
|
||||
payload, ok := body["payload"].(map[string]interface{})
|
||||
require.True(t, ok)
|
||||
results, ok := payload["conversations"].([]interface{})
|
||||
require.True(t, ok)
|
||||
require.Len(t, results, 1)
|
||||
}
|
||||
|
||||
func TestSearchHandler_SearchConversations_InvalidAccountID(t *testing.T) {
|
||||
@@ -227,18 +236,62 @@ func TestSearchHandler_SearchMessages_Success(t *testing.T) {
|
||||
router := setupSearchHandlerRouter(handler)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest("GET", "/api/v1/accounts/1/search/messages?q=hello&page=1&per_page=10", nil)
|
||||
req := httptest.NewRequest("GET", "/api/v1/accounts/1/search/messages?q=hello&page=1", nil)
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
|
||||
var body map[string]interface{}
|
||||
assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &body))
|
||||
assert.Equal(t, true, body["success"])
|
||||
require.NotContains(t, body, "success")
|
||||
payload, ok := body["payload"].(map[string]interface{})
|
||||
require.True(t, ok)
|
||||
results, ok := payload["messages"].([]interface{})
|
||||
require.True(t, ok)
|
||||
require.Len(t, results, 1)
|
||||
require.NotNil(t, repo.msgFilter)
|
||||
assert.Equal(t, 15, repo.msgFilter.PerPage)
|
||||
}
|
||||
|
||||
data := body["data"].(map[string]interface{})
|
||||
results := data["results"]
|
||||
assert.NotNil(t, results)
|
||||
func TestSearchHandler_SearchMessages_MeiliHitPayloadShape(t *testing.T) {
|
||||
svc := search.NewSearchServiceWithEngine(&stubSearchEngine{
|
||||
resp: &search.SearchResponse{
|
||||
Results: []search.SearchResult{{
|
||||
Type: search.ResultTypeMessage,
|
||||
ID: 9,
|
||||
AccountID: 1,
|
||||
Data: map[string]any{
|
||||
"data": map[string]any{
|
||||
"message": map[string]any{
|
||||
"id": float64(9),
|
||||
"content": "hello from meili",
|
||||
"account_id": float64(1),
|
||||
"conversation_id": float64(3),
|
||||
"message_type": "incoming",
|
||||
"created_at_ts": float64(1700000000),
|
||||
},
|
||||
},
|
||||
},
|
||||
}},
|
||||
ByType: map[string]int64{"message": 1},
|
||||
},
|
||||
}, nil)
|
||||
handler := NewSearchHandler(svc)
|
||||
router := setupSearchHandlerRouter(handler)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest("GET", "/api/v1/accounts/1/search/messages?q=hello", 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)
|
||||
messages := payload["messages"].([]any)
|
||||
require.Len(t, messages, 1)
|
||||
message := messages[0].(map[string]any)
|
||||
assert.Equal(t, float64(0), message["message_type"])
|
||||
assert.Equal(t, float64(1700000000), message["created_at"])
|
||||
}
|
||||
|
||||
func TestSearchHandler_SearchMessages_InvalidAccountID(t *testing.T) {
|
||||
@@ -286,11 +339,14 @@ func TestSearchHandler_SearchContacts_Success(t *testing.T) {
|
||||
|
||||
var body map[string]interface{}
|
||||
assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &body))
|
||||
assert.Equal(t, true, body["success"])
|
||||
|
||||
data := body["data"].(map[string]interface{})
|
||||
results := data["results"]
|
||||
assert.NotNil(t, results)
|
||||
require.NotContains(t, body, "success")
|
||||
payload, ok := body["payload"].(map[string]interface{})
|
||||
require.True(t, ok)
|
||||
results, ok := payload["contacts"].([]interface{})
|
||||
require.True(t, ok)
|
||||
require.Len(t, results, 1)
|
||||
contact := results[0].(map[string]interface{})
|
||||
assert.Equal(t, "Alice", contact["name"])
|
||||
}
|
||||
|
||||
func TestSearchHandler_SearchContacts_InvalidAccountID(t *testing.T) {
|
||||
@@ -338,11 +394,12 @@ func TestSearchHandler_SearchArticles_Success(t *testing.T) {
|
||||
|
||||
var body map[string]interface{}
|
||||
assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &body))
|
||||
assert.Equal(t, true, body["success"])
|
||||
|
||||
data := body["data"].(map[string]interface{})
|
||||
results := data["results"]
|
||||
assert.NotNil(t, results)
|
||||
require.NotContains(t, body, "success")
|
||||
payload, ok := body["payload"].(map[string]interface{})
|
||||
require.True(t, ok)
|
||||
results, ok := payload["articles"].([]interface{})
|
||||
require.True(t, ok)
|
||||
require.Len(t, results, 1)
|
||||
}
|
||||
|
||||
func TestSearchHandler_SearchArticles_InvalidAccountID(t *testing.T) {
|
||||
@@ -370,3 +427,24 @@ func TestSearchHandler_SearchArticles_ServiceError(t *testing.T) {
|
||||
|
||||
assert.Equal(t, http.StatusUnprocessableEntity, w.Code)
|
||||
}
|
||||
|
||||
type stubSearchEngine struct {
|
||||
resp *search.SearchResponse
|
||||
err error
|
||||
}
|
||||
|
||||
func (s *stubSearchEngine) Search(ctx context.Context, accountID uint, query string, filter *search.SearchFilter) (*search.SearchResponse, error) {
|
||||
return s.resp, s.err
|
||||
}
|
||||
|
||||
func (s *stubSearchEngine) IndexDocument(ctx context.Context, doc search.SearchDocument) error {
|
||||
return nil
|
||||
}
|
||||
func (s *stubSearchEngine) IndexBatch(ctx context.Context, docs []search.SearchDocument) error {
|
||||
return nil
|
||||
}
|
||||
func (s *stubSearchEngine) DeleteDocument(ctx context.Context, docType search.SearchResultType, accountID uint, id uint) error {
|
||||
return nil
|
||||
}
|
||||
func (s *stubSearchEngine) Bootstrap(ctx context.Context) error { return nil }
|
||||
func (s *stubSearchEngine) Close() error { return nil }
|
||||
|
||||
Reference in New Issue
Block a user