git.iko.soy/opencode-android
on fac92bb help sign in
44 lines 1.7 kB history raw
1package soy.iko.opencode.data.repo
3import kotlinx.coroutines.runBlocking
4import org.junit.Assert.assertEquals
5import org.junit.Test
7class OutboxStoreTest {
9 /** The protected no-arg constructor runs with no Context, so disk ops are skipped and the
10 * in-memory queue can be exercised directly. */
11 private class TestOutbox : OutboxStore()
13 private fun msg(id: String, sessionId: String, text: String, createdAt: Long) =
14 OutboxMessage(id = id, profileId = "p", sessionId = sessionId, text = text, createdAt = createdAt)
16 @Test
17 fun enqueueOrdersByCreatedAtAndDedupesById() = runBlocking {
18 val store = TestOutbox()
19 store.enqueue(msg("b", "s1", "B", createdAt = 2))
20 store.enqueue(msg("a", "s1", "A", createdAt = 1))
21 // Same id replaces the prior entry rather than adding a duplicate.
22 store.enqueue(msg("a", "s1", "A2", createdAt = 1))
23 assertEquals(listOf("a", "b"), store.messages.value.map { it.id })
24 assertEquals("A2", store.messages.value.first { it.id == "a" }.text)
25 }
27 @Test
28 fun removeForSessionDropsOnlyThatSession() = runBlocking {
29 val store = TestOutbox()
30 store.enqueue(msg("1", "s1", "x", createdAt = 1))
31 store.enqueue(msg("2", "s2", "y", createdAt = 2))
32 store.removeForSession("s1")
33 assertEquals(listOf("2"), store.messages.value.map { it.id })
34 }
36 @Test
37 fun removeDropsSingleMessage() = runBlocking {
38 val store = TestOutbox()
39 store.enqueue(msg("1", "s1", "x", createdAt = 1))
40 store.enqueue(msg("2", "s1", "y", createdAt = 2))
41 store.remove("1")
42 assertEquals(listOf("2"), store.messages.value.map { it.id })
43 }