|
| 1 | +package stream_chat |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestMessageHistory(t *testing.T) { |
| 12 | + client := initClient(t) |
| 13 | + users := randomUsers(t, client, 2) |
| 14 | + user1 := users[0] |
| 15 | + user2 := users[1] |
| 16 | + |
| 17 | + ch := initChannel(t, client, user1.ID) |
| 18 | + |
| 19 | + ctx := context.Background() |
| 20 | + initialText := "initial text" |
| 21 | + customField := "custom_field" |
| 22 | + initialCustomFieldValue := "custom value" |
| 23 | + // send a message with initial text |
| 24 | + response, err := ch.SendMessage(ctx, &Message{Text: initialText, ExtraData: map[string]interface{}{customField: initialCustomFieldValue}}, user1.ID) |
| 25 | + require.NoError(t, err) |
| 26 | + message := response.Message |
| 27 | + |
| 28 | + updatedText1 := "updated text" |
| 29 | + updatedCustomFieldValue := "updated custom value" |
| 30 | + // update the message by user1 |
| 31 | + _, err = client.UpdateMessage(ctx, &Message{Text: updatedText1, ExtraData: map[string]interface{}{customField: updatedCustomFieldValue}, UserID: user1.ID}, message.ID) |
| 32 | + assert.NoError(t, err) |
| 33 | + |
| 34 | + updatedText2 := "updated text 2" |
| 35 | + // update the message by user2 |
| 36 | + _, err = client.UpdateMessage(ctx, &Message{Text: updatedText2, UserID: user2.ID}, message.ID) |
| 37 | + assert.NoError(t, err) |
| 38 | + |
| 39 | + t.Run("test query", func(t *testing.T) { |
| 40 | + req := QueryMessageHistoryRequest{ |
| 41 | + Filter: map[string]interface{}{ |
| 42 | + "message_id": message.ID, |
| 43 | + }, |
| 44 | + } |
| 45 | + messageHistoryResponse, err := client.QueryMessageHistory(ctx, req) |
| 46 | + assert.NoError(t, err) |
| 47 | + assert.NotNil(t, messageHistoryResponse) |
| 48 | + |
| 49 | + history := messageHistoryResponse.MessageHistory |
| 50 | + assert.Equal(t, 2, len(history)) |
| 51 | + |
| 52 | + firstUpdate := history[1] |
| 53 | + assert.Equal(t, initialText, firstUpdate.Text) |
| 54 | + assert.Equal(t, user1.ID, firstUpdate.MessageUpdatedByID) |
| 55 | + assert.Equal(t, initialCustomFieldValue, firstUpdate.ExtraData[customField].(string)) |
| 56 | + |
| 57 | + secondUpdate := history[0] |
| 58 | + assert.Equal(t, updatedText1, secondUpdate.Text) |
| 59 | + assert.Equal(t, user2.ID, secondUpdate.MessageUpdatedByID) |
| 60 | + assert.Equal(t, updatedCustomFieldValue, secondUpdate.ExtraData[customField].(string)) |
| 61 | + }) |
| 62 | + |
| 63 | + t.Run("test sorting", func(t *testing.T) { |
| 64 | + sortedHistoryQueryRequest := QueryMessageHistoryRequest{ |
| 65 | + Filter: map[string]interface{}{ |
| 66 | + "message_id": message.ID, |
| 67 | + }, |
| 68 | + Sort: []*SortOption{ |
| 69 | + { |
| 70 | + Field: "message_updated_at", |
| 71 | + Direction: 1, |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | + sortedHistoryResponse, err := client.QueryMessageHistory(ctx, sortedHistoryQueryRequest) |
| 76 | + assert.NoError(t, err) |
| 77 | + assert.NotNil(t, sortedHistoryResponse) |
| 78 | + |
| 79 | + sortedHistory := sortedHistoryResponse.MessageHistory |
| 80 | + assert.Equal(t, 2, len(sortedHistory)) |
| 81 | + |
| 82 | + firstUpdate := sortedHistory[0] |
| 83 | + assert.Equal(t, initialText, firstUpdate.Text) |
| 84 | + assert.Equal(t, user1.ID, firstUpdate.MessageUpdatedByID) |
| 85 | + |
| 86 | + secondUpdate := sortedHistory[1] |
| 87 | + assert.Equal(t, updatedText1, secondUpdate.Text) |
| 88 | + assert.Equal(t, user2.ID, secondUpdate.MessageUpdatedByID) |
| 89 | + }) |
| 90 | +} |
0 commit comments