|
| 1 | +package com.flipcash.shared.chat |
| 2 | + |
| 3 | +import com.flipcash.app.persistence.sources.ChatMessageDataSource |
| 4 | +import com.flipcash.app.persistence.sources.ChatMetadataDataSource |
| 5 | +import com.flipcash.services.controllers.ChatController |
| 6 | +import com.flipcash.services.models.chat.ChatFeedPage |
| 7 | +import com.flipcash.services.models.chat.ChatId |
| 8 | +import com.flipcash.services.models.chat.ChatMetadata |
| 9 | +import com.flipcash.services.models.chat.ChatType |
| 10 | +import com.flipcash.shared.chat.internal.delegates.FeedSyncDelegate |
| 11 | +import io.mockk.coEvery |
| 12 | +import io.mockk.mockk |
| 13 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 14 | +import kotlinx.coroutines.launch |
| 15 | +import kotlinx.coroutines.test.TestScope |
| 16 | +import kotlinx.coroutines.test.UnconfinedTestDispatcher |
| 17 | +import kotlinx.coroutines.test.runCurrent |
| 18 | +import kotlinx.coroutines.test.runTest |
| 19 | +import kotlin.time.Instant |
| 20 | +import org.junit.Assert.assertEquals |
| 21 | +import org.junit.Test |
| 22 | + |
| 23 | +/** |
| 24 | + * Covers which chats a feed sync decides to backfill. The decision is keyed on the locally |
| 25 | + * applied event cursor — never on whether the chat holds any message rows, because the sync |
| 26 | + * itself persists every chat's last-message preview as a row. |
| 27 | + */ |
| 28 | +@OptIn(ExperimentalCoroutinesApi::class) |
| 29 | +class FeedSyncBackfillTest { |
| 30 | + |
| 31 | + private fun metadata(hex: String, latestEventSequence: Long) = ChatMetadata( |
| 32 | + chatId = ChatId(hex), |
| 33 | + type = ChatType.CONTACT_DM, |
| 34 | + members = emptyList(), |
| 35 | + lastMessage = null, |
| 36 | + lastActivity = Instant.fromEpochSeconds(1000), |
| 37 | + latestEventSequence = latestEventSequence, |
| 38 | + ) |
| 39 | + |
| 40 | + /** |
| 41 | + * Runs one feed sync over a single chat whose server head is [serverHead] and whose locally |
| 42 | + * applied cursor is [localCursor], and returns the events it emitted. Every successful sync |
| 43 | + * ends with [FeedSyncDelegate.Event.CatchUpComplete], so that marker closes each expectation |
| 44 | + * below; what varies is the backfill decision in front of it. |
| 45 | + */ |
| 46 | + private suspend fun TestScope.backfillEvents( |
| 47 | + serverHead: Long, |
| 48 | + localCursor: Long, |
| 49 | + ): List<FeedSyncDelegate.Event> { |
| 50 | + val chat = metadata(CHAT_HEX, serverHead) |
| 51 | + |
| 52 | + val chatController = mockk<ChatController>(relaxed = true) |
| 53 | + coEvery { chatController.getDmChatFeed(ChatType.CONTACT_DM, any()) } returns |
| 54 | + Result.success(ChatFeedPage(listOf(chat), null, false)) |
| 55 | + coEvery { chatController.getDmChatFeed(ChatType.TIP_DM, any()) } returns |
| 56 | + Result.success(ChatFeedPage(emptyList(), null, false)) |
| 57 | + |
| 58 | + val metadataDataSource = mockk<ChatMetadataDataSource>(relaxed = true) |
| 59 | + coEvery { metadataDataSource.getLatestEventSequence(chat.chatId) } returns localCursor |
| 60 | + |
| 61 | + // Nothing here stubs `hasMessages`: the decision must not consult it. The sync persists |
| 62 | + // every chat's last-message preview, so it would answer true even for a chat whose |
| 63 | + // transcript was never pulled. |
| 64 | + val messageDataSource = mockk<ChatMessageDataSource>(relaxed = true) |
| 65 | + |
| 66 | + val delegate = FeedSyncDelegate( |
| 67 | + chatController = chatController, |
| 68 | + metadataDataSource = metadataDataSource, |
| 69 | + messageDataSource = messageDataSource, |
| 70 | + memberDataSource = mockk(relaxed = true), |
| 71 | + stateHolder = mockk(relaxed = true), |
| 72 | + userManager = mockk(relaxed = true), |
| 73 | + ) |
| 74 | + |
| 75 | + val received = mutableListOf<FeedSyncDelegate.Event>() |
| 76 | + val collector = launch(UnconfinedTestDispatcher(testScheduler)) { |
| 77 | + delegate.events.collect { received += it } |
| 78 | + } |
| 79 | + delegate.performFeedSync() |
| 80 | + runCurrent() |
| 81 | + collector.cancel() |
| 82 | + return received |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + fun `chat that was never fetched is backfilled with its newest page`() = runTest { |
| 87 | + // The sync has just written this chat's last-message preview, so it holds a message row — |
| 88 | + // only the unseated cursor reveals that its transcript was never pulled. |
| 89 | + val events = backfillEvents(serverHead = 12, localCursor = 0) |
| 90 | + |
| 91 | + assertEquals( |
| 92 | + listOf( |
| 93 | + FeedSyncDelegate.Event.LoadMessages(ChatId(CHAT_HEX)), |
| 94 | + FeedSyncDelegate.Event.CatchUpComplete, |
| 95 | + ), |
| 96 | + events, |
| 97 | + ) |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + fun `chat that was never fetched is never delta synced from zero`() = runTest { |
| 102 | + // A delta after sequence 0 re-pulls the entire history instead of the newest page. |
| 103 | + val events = backfillEvents(serverHead = 0, localCursor = 0) |
| 104 | + |
| 105 | + assertEquals( |
| 106 | + listOf( |
| 107 | + FeedSyncDelegate.Event.LoadMessages(ChatId(CHAT_HEX)), |
| 108 | + FeedSyncDelegate.Event.CatchUpComplete, |
| 109 | + ), |
| 110 | + events, |
| 111 | + ) |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + fun `chat behind the server head catches up with a delta sync`() = runTest { |
| 116 | + val events = backfillEvents(serverHead = 12, localCursor = 5) |
| 117 | + |
| 118 | + assertEquals( |
| 119 | + listOf( |
| 120 | + FeedSyncDelegate.Event.DeltaSyncNeeded(ChatId(CHAT_HEX)), |
| 121 | + FeedSyncDelegate.Event.CatchUpComplete, |
| 122 | + ), |
| 123 | + events, |
| 124 | + ) |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + fun `chat already at the server head needs no backfill`() = runTest { |
| 129 | + val events = backfillEvents(serverHead = 12, localCursor = 12) |
| 130 | + |
| 131 | + assertEquals(listOf(FeedSyncDelegate.Event.CatchUpComplete), events) |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + fun `chat ahead of a stale feed head needs no backfill`() = runTest { |
| 136 | + // The stream can apply events the feed page predates; that is not a gap. |
| 137 | + val events = backfillEvents(serverHead = 12, localCursor = 20) |
| 138 | + |
| 139 | + assertEquals(listOf(FeedSyncDelegate.Event.CatchUpComplete), events) |
| 140 | + } |
| 141 | + |
| 142 | + private companion object { |
| 143 | + const val CHAT_HEX = "aabbccdd" |
| 144 | + } |
| 145 | +} |
0 commit comments