Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / test / java / com / vayunmathur / communicate / rcs / RcsSlice3DatabaseTest.kt
RcsSlice3DatabaseTest.kt203 lines · 7.9 KiBRaw
| 1 | package com.vayunmathur.communicate.rcs |
| 2 | |
| 3 | // FIXTURE RELATIVE-TIME EXCEPTION: timestamps are seeded relative to the wall clock the code under test reads internally, so the fresh/expired/relative-time branch under test keeps its meaning; a fixed past instant would silently change that branch. |
| 4 | |
| 5 | import androidx.room3.Room |
| 6 | import androidx.sqlite.driver.bundled.BundledSQLiteDriver |
| 7 | import androidx.test.core.app.ApplicationProvider |
| 8 | import com.vayunmathur.communicate.data.rcs.RcsCachedMessage |
| 9 | import com.vayunmathur.communicate.data.rcs.RcsConversation |
| 10 | import com.vayunmathur.communicate.data.rcs.RcsDatabase |
| 11 | import com.vayunmathur.communicate.data.rcs.RcsMlsGroup |
| 12 | import com.vayunmathur.communicate.data.rcs.RcsMlsIdentity |
| 13 | import com.vayunmathur.communicate.data.rcs.RcsOutboxMessage |
| 14 | import com.vayunmathur.communicate.data.rcs.RcsTypeConverters |
| 15 | import kotlin.test.AfterTest |
| 16 | import kotlin.test.BeforeTest |
| 17 | import kotlin.test.Test |
| 18 | import kotlin.test.assertEquals |
| 19 | import kotlin.test.assertNull |
| 20 | import kotlin.test.assertTrue |
| 21 | import kotlinx.coroutines.test.runTest |
| 22 | import org.junit.runner.RunWith |
| 23 | import org.robolectric.RobolectricTestRunner |
| 24 | import org.robolectric.annotation.Config |
| 25 | |
| 26 | /** |
| 27 | * Slice-3 tests for the RCS Room database ([RcsDatabase]): DAO round trips |
| 28 | * for conversations, cached messages (incl. delivery-status advancement), |
| 29 | * MLS identity/group rows, outbox queueing, and the type converters. |
| 30 | * |
| 31 | * Runs against a real in-memory Room database (bundled sqlite driver) under |
| 32 | * Robolectric, mirroring the brief's |
| 33 | * `Room.inMemoryDatabaseBuilder(...).allowMainThreadQueries().build()` |
| 34 | * recipe for the Room 3 API. |
| 35 | */ |
| 36 | @RunWith(RobolectricTestRunner::class) |
| 37 | @Config(sdk = [34]) |
| 38 | class RcsSlice3DatabaseTest { |
| 39 | |
| 40 | private lateinit var db: RcsDatabase |
| 41 | |
| 42 | @BeforeTest |
| 43 | fun setUp() { |
| 44 | val ctx = ApplicationProvider.getApplicationContext<android.content.Context>() |
| 45 | db = Room.inMemoryDatabaseBuilder(ctx, RcsDatabase::class.java) |
| 46 | .setDriver(BundledSQLiteDriver()) |
| 47 | .allowMainThreadQueries() |
| 48 | .build() |
| 49 | } |
| 50 | |
| 51 | @AfterTest |
| 52 | fun tearDown() { |
| 53 | db.close() |
| 54 | } |
| 55 | |
| 56 | @Test |
| 57 | fun conversationRoundTrip() = runTest { |
| 58 | assertNull(db.conversationDao().getConversation("+15550001111")) |
| 59 | db.conversationDao().upsert(RcsConversation(remoteId = "+15550001111", unreadCount = 2)) |
| 60 | val got = db.conversationDao().getConversation("+15550001111") ?: error("missing") |
| 61 | assertEquals(2, got.unreadCount) |
| 62 | assertEquals("+15550001111", got.remoteId) |
| 63 | // Replace semantics. |
| 64 | db.conversationDao().upsert(RcsConversation(remoteId = "+15550001111", unreadCount = 0, name = "Bob")) |
| 65 | assertEquals("Bob", db.conversationDao().getConversation("+15550001111")?.name) |
| 66 | // Recent ordering + delete. |
| 67 | db.conversationDao().upsert( |
| 68 | RcsConversation(remoteId = "+15550002222", lastMessageTimestamp = 999L), |
| 69 | ) |
| 70 | val recent = db.conversationDao().getRecent(10) |
| 71 | assertEquals("+15550002222", recent.first().remoteId) |
| 72 | db.conversationDao().delete("+15550001111") |
| 73 | assertNull(db.conversationDao().getConversation("+15550001111")) |
| 74 | } |
| 75 | |
| 76 | @Test |
| 77 | fun cachedMessageCrudAndStatusLadder() = runTest { |
| 78 | val dao = db.cachedMessageDao() |
| 79 | dao.upsert( |
| 80 | RcsCachedMessage( |
| 81 | messageId = "slice3-m1", |
| 82 | conversationId = "slice3-conv", |
| 83 | body = "hello", |
| 84 | timestamp = 100L, |
| 85 | outgoing = true, |
| 86 | imdnId = "imdn-1", |
| 87 | ), |
| 88 | ) |
| 89 | assertEquals("hello", dao.get("slice3-m1")?.body) |
| 90 | assertEquals("imdn-1", dao.get("slice3-m1")?.imdnId) |
| 91 | // Status only advances upward. |
| 92 | dao.upsert(dao.get("slice3-m1")!!.copy(status = 1)) |
| 93 | dao.markDelivered("slice3-m1") |
| 94 | assertEquals(2, dao.get("slice3-m1")?.status) |
| 95 | dao.markDelivered("slice3-m1") |
| 96 | assertEquals(2, dao.get("slice3-m1")?.status) |
| 97 | dao.markReadStatus("slice3-m1") |
| 98 | assertEquals(3, dao.get("slice3-m1")?.status) |
| 99 | // A fresh row at status 0 jumps straight to read. |
| 100 | dao.upsert( |
| 101 | RcsCachedMessage( |
| 102 | messageId = "slice3-m2", |
| 103 | conversationId = "slice3-conv", |
| 104 | body = "second", |
| 105 | timestamp = 200L, |
| 106 | outgoing = true, |
| 107 | ), |
| 108 | ) |
| 109 | dao.markReadStatus("slice3-m2") |
| 110 | assertEquals(3, dao.get("slice3-m2")?.status) |
| 111 | // Per-conversation listing is timestamp-ordered. |
| 112 | val listed = dao.getForConversation("slice3-conv") |
| 113 | assertEquals(listOf("slice3-m1", "slice3-m2"), listed.map { it.messageId }) |
| 114 | // Latest-per-conversation query. |
| 115 | val latest = dao.getLatestPerConversation() |
| 116 | assertEquals("slice3-m2", latest.first().messageId) |
| 117 | // Delete by conversation. |
| 118 | dao.deleteConversation("slice3-conv") |
| 119 | assertTrue(dao.getForConversation("slice3-conv").isEmpty()) |
| 120 | } |
| 121 | |
| 122 | @Test |
| 123 | fun mlsIdentityAndGroupRoundTrip() = runTest { |
| 124 | val idDao = db.mlsIdentityDao() |
| 125 | assertNull(idDao.get("+15550003333")) |
| 126 | idDao.upsert(RcsMlsIdentity(e164 = "+15550003333", identity = byteArrayOf(1, 2, 3))) |
| 127 | val got = idDao.get("+15550003333") ?: error("missing identity") |
| 128 | assertTrue(got.identity.contentEquals(byteArrayOf(1, 2, 3))) |
| 129 | assertEquals(false, got.verified) |
| 130 | idDao.upsert(got.copy(verified = true)) |
| 131 | assertEquals(true, idDao.get("+15550003333")?.verified) |
| 132 | idDao.delete("+15550003333") |
| 133 | assertNull(idDao.get("+15550003333")) |
| 134 | |
| 135 | val groupDao = db.mlsGroupDao() |
| 136 | assertNull(groupDao.getByConversation("slice3-gconv")) |
| 137 | groupDao.upsert( |
| 138 | RcsMlsGroup( |
| 139 | groupIdHex = "aabbcc", |
| 140 | conversationId = "slice3-gconv", |
| 141 | storage = byteArrayOf(9, 9), |
| 142 | members = "+1=1,+2=2", |
| 143 | ), |
| 144 | ) |
| 145 | assertEquals("aabbcc", groupDao.get("aabbcc")?.groupIdHex) |
| 146 | assertEquals("+1=1,+2=2", groupDao.getByConversation("slice3-gconv")?.members) |
| 147 | groupDao.deleteByConversation("slice3-gconv") |
| 148 | assertNull(groupDao.get("aabbcc")) |
| 149 | } |
| 150 | |
| 151 | @Test |
| 152 | fun outboxQueueLifecycle() = runTest { |
| 153 | val dao = db.outboxDao() |
| 154 | assertEquals(0, dao.count()) |
| 155 | val now = System.currentTimeMillis() |
| 156 | dao.upsert( |
| 157 | RcsOutboxMessage( |
| 158 | messageId = "slice3-ob-1", |
| 159 | conversationId = "slice3-conv", |
| 160 | kind = "text", |
| 161 | body = "retry me", |
| 162 | nextRetryMs = now - 1000, |
| 163 | expiryMs = now + 86_400_000L, |
| 164 | ), |
| 165 | ) |
| 166 | dao.upsert( |
| 167 | RcsOutboxMessage( |
| 168 | messageId = "slice3-ob-2", |
| 169 | conversationId = "slice3-conv", |
| 170 | kind = "file", |
| 171 | body = "", |
| 172 | attachments = "content://x/1", |
| 173 | nextRetryMs = now + 86_400_000L, |
| 174 | expiryMs = now + 2 * 86_400_000L, |
| 175 | ), |
| 176 | ) |
| 177 | assertEquals(2, dao.count()) |
| 178 | // Only the due row is returned. |
| 179 | val due = dao.dueForRetry(now) |
| 180 | assertEquals(listOf("slice3-ob-1"), due.map { it.messageId }) |
| 181 | assertEquals("slice3-ob-1", dao.get("slice3-ob-1")?.body?.let { "slice3-ob-1" }) |
| 182 | dao.delete("slice3-ob-1") |
| 183 | assertEquals(1, dao.count()) |
| 184 | dao.deleteConversation("slice3-conv") |
| 185 | assertEquals(0, dao.count()) |
| 186 | } |
| 187 | |
| 188 | @Test |
| 189 | fun typeConvertersRoundTrip() { |
| 190 | val conv = RcsTypeConverters() |
| 191 | assertEquals("", conv.fromStringList(emptyList())) |
| 192 | assertEquals("a,b,c", conv.fromStringList(listOf("a", "b", "c"))) |
| 193 | assertEquals(emptyList(), conv.toStringList("")) |
| 194 | assertEquals(listOf("a", "b"), conv.toStringList("a,b")) |
| 195 | } |
| 196 | |
| 197 | @Test |
| 198 | fun migrationsListCoversV1ToV3() { |
| 199 | val versions = RcsDatabase.migrations.map { it.startVersion to it.endVersion } |
| 200 | assertTrue(versions.contains(1 to 2)) |
| 201 | assertTrue(versions.contains(2 to 3)) |
| 202 | } |
| 203 | } |