Skip to content
New issue

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

fix: second message edit will fail [WPB-10773] #2976

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ internal class MessageTextEditHandlerImpl internal constructor(
&& editStatus is Message.EditStatus.Edited
) {
// if the locally stored message is also already edited, we check which one is newer
if (editStatus.lastEditInstant < message.date) {
if (editStatus.lastEditInstant > message.date) {
// our local pending or failed edit is newer than one we got from the backend so we update locally only message id and date
messageRepository.updateTextMessage(
conversationId = message.conversationId,
Expand Down Expand Up @@ -100,5 +100,4 @@ internal class MessageTextEditHandlerImpl internal constructor(
)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class MessageTextEditHandlerTest {
@Test
fun givenEditIsOlderThanLocalPendingStoredEdit_whenHandling_thenShouldUpdateOnlyMessageIdAndDate() = runTest {
val originalContent = TestMessage.TEXT_CONTENT
val originalEditStatus = Message.EditStatus.Edited(Instant.UNIX_FIRST_DATE)
val originalEditStatus = Message.EditStatus.Edited(Instant.DISTANT_FUTURE) // original message date is newer than edit date
val originalMessage = ORIGINAL_MESSAGE.copy(
editStatus = originalEditStatus,
content = originalContent,
Expand All @@ -127,7 +127,7 @@ class MessageTextEditHandlerTest {
)
val editContent = EDIT_CONTENT
val editMessage = EDIT_MESSAGE.copy(
date = EDIT_MESSAGE.date - 10.minutes,
date = originalEditStatus.lastEditInstant - 10.minutes, // edit date is older than original message date
content = editContent
)
val expectedContent = MessageContent.TextEdited(
Expand All @@ -151,6 +151,41 @@ class MessageTextEditHandlerTest {
}
}

@Test
fun givenAnAlreadyEditedMessage_whenNewEditIsInTheFuture_thenMessageContentIsUpdatd() = runTest {
val originalContent = TestMessage.TEXT_CONTENT
val originalEditStatus = Message.EditStatus.Edited(Instant.UNIX_FIRST_DATE)
val originalMessage = ORIGINAL_MESSAGE.copy(
editStatus = originalEditStatus,
content = originalContent,
status = Message.Status.Sent
)
val editContent = EDIT_CONTENT
val editMessage = EDIT_MESSAGE.copy(
date = Instant.UNIX_FIRST_DATE + 10.minutes,
content = editContent
)
val expectedContent = MessageContent.TextEdited(
editMessageId = editContent.editMessageId,
newContent = editContent.newContent,
newMentions = editContent.newMentions
)
val (arrangement, messageTextEditHandler) = arrange {
withGetMessageById(Either.Right(originalMessage))
}

messageTextEditHandler.handle(editMessage, editContent)

with(arrangement) {
coVerify {
messageRepository.updateTextMessage(any(), eq(expectedContent), eq(editMessage.id), eq(editMessage.date))
}.wasInvoked(exactly = once)
coVerify {
messageRepository.updateMessageStatus(any(), any(), any())
}.wasInvoked(exactly = once)
}
}

private suspend fun arrange(block: suspend Arrangement.() -> Unit) = Arrangement(block).arrange()

private class Arrangement(
Expand Down
Loading