feat(search): align message result payloads
This commit is contained in:
@@ -12,6 +12,9 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
|
||||
"github.com/gochat/gochat/internal/model"
|
||||
"github.com/gochat/gochat/internal/search"
|
||||
@@ -342,9 +345,11 @@ func TestSearchHandler_SearchMessages_MeiliHitPayloadShape(t *testing.T) {
|
||||
"id": float64(9),
|
||||
"content": "hello from meili",
|
||||
"account_id": float64(1),
|
||||
"conversation_id": float64(3),
|
||||
"conversation_id": float64(42),
|
||||
"message_type": "incoming",
|
||||
"created_at_ts": float64(1700000000),
|
||||
"sender": map[string]any{"id": float64(7), "name": "Ada Contact"},
|
||||
"attachments": []any{map[string]any{"id": float64(11), "file_type": "file", "data_url": "https://files.example/doc.pdf"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -368,6 +373,68 @@ func TestSearchHandler_SearchMessages_MeiliHitPayloadShape(t *testing.T) {
|
||||
message := messages[0].(map[string]any)
|
||||
assert.Equal(t, float64(0), message["message_type"])
|
||||
assert.Equal(t, float64(1700000000), message["created_at"])
|
||||
assert.Equal(t, float64(42), message["conversation_id"])
|
||||
assert.Equal(t, "Ada Contact", message["sender"].(map[string]any)["name"])
|
||||
attachments := message["attachments"].([]any)
|
||||
require.Len(t, attachments, 1)
|
||||
assert.Equal(t, "https://files.example/doc.pdf", attachments[0].(map[string]any)["data_url"])
|
||||
}
|
||||
|
||||
func TestSearchHandler_SearchMessages_HydratesChatwootMessagePayload(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open("file:search-message-payload?mode=memory&cache=private"), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, db.AutoMigrate(&model.Account{}, &model.User{}, &model.Inbox{}, &model.Contact{}, &model.Conversation{}, &model.Message{}, &model.Attachment{}))
|
||||
|
||||
account := model.Account{Name: "Acme"}
|
||||
require.NoError(t, db.Create(&account).Error)
|
||||
inbox := model.Inbox{AccountID: account.ID, Name: "Website", ChannelType: "web_widget"}
|
||||
require.NoError(t, db.Create(&inbox).Error)
|
||||
contact := model.Contact{AccountID: account.ID, Name: "Ada Contact", Email: "ada@example.com"}
|
||||
require.NoError(t, db.Create(&contact).Error)
|
||||
agent := model.User{Name: "Agent One", Email: "agent@example.com"}
|
||||
require.NoError(t, db.Create(&agent).Error)
|
||||
displayID := uint(42)
|
||||
conversation := model.Conversation{AccountID: account.ID, InboxID: inbox.ID, ContactID: contact.ID, DisplayID: &displayID, Status: "open"}
|
||||
require.NoError(t, db.Create(&conversation).Error)
|
||||
message := model.Message{
|
||||
AccountID: account.ID,
|
||||
InboxID: inbox.ID,
|
||||
ConversationID: conversation.ID,
|
||||
SenderID: &agent.ID,
|
||||
SenderType: "user",
|
||||
MessageType: "outgoing",
|
||||
ContentType: "text",
|
||||
Status: "sent",
|
||||
Content: "hello with attachment",
|
||||
ContentAttributes: datatypes.JSON([]byte(`{"source":"search"}`)),
|
||||
}
|
||||
require.NoError(t, db.Create(&message).Error)
|
||||
require.NoError(t, db.Create(&model.Attachment{AccountID: account.ID, MessageID: message.ID, FileType: "file", FileName: "quote.pdf", FileURL: "https://files.example/quote.pdf", FileSize: 128}).Error)
|
||||
|
||||
repo := &mockSearchRepo{messages: []model.Message{{Base: model.Base{ID: message.ID}, AccountID: account.ID}}, msgTotal: 1}
|
||||
svc := search.NewSearchService(repo)
|
||||
handler := NewSearchHandler(svc, db)
|
||||
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)
|
||||
got := messages[0].(map[string]any)
|
||||
assert.Equal(t, float64(displayID), got["conversation_id"])
|
||||
sender := got["sender"].(map[string]any)
|
||||
assert.Equal(t, "Agent One", sender["name"])
|
||||
attachments := got["attachments"].([]any)
|
||||
require.Len(t, attachments, 1)
|
||||
attachment := attachments[0].(map[string]any)
|
||||
assert.Equal(t, "file", attachment["file_type"])
|
||||
assert.Equal(t, "https://files.example/quote.pdf", attachment["data_url"])
|
||||
}
|
||||
|
||||
func TestSearchHandler_SearchMessages_InvalidAccountID(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user