git.iko.soy/opencode-android
on 1a92af4 help sign in
59 lines 1.9 kB history raw
1package soy.iko.opencode.data.repo
3import androidx.test.core.app.ApplicationProvider
4import androidx.test.ext.junit.runners.AndroidJUnit4
5import kotlinx.coroutines.CoroutineScope
6import kotlinx.coroutines.Dispatchers
7import kotlinx.coroutines.SupervisorJob
8import kotlinx.coroutines.cancel
9import kotlinx.coroutines.runBlocking
10import org.junit.Assert.assertEquals
11import org.junit.Test
12import org.junit.runner.RunWith
14@RunWith(AndroidJUnit4::class)
15class DraftStoreTest {
17 private val context = ApplicationProvider.getApplicationContext<android.content.Context>()
19 @Test
20 fun setGetRemoveRoundTrip() = runBlocking {
21 val scope = CoroutineScope(SupervisorJob() + Dispatchers.Unconfined)
22 val store = DraftStore(context, scope)
24 // Use a unique session ID so it doesn't collide with other tests.
25 val sid = "test-${System.nanoTime()}"
27 store.set(sid, "hello world")
28 assertEquals("hello world", store.get(sid))
30 // Overwrite
31 store.set(sid, "updated")
32 assertEquals("updated", store.get(sid))
34 // Blank clears the entry
35 store.set(sid, " ")
36 assertEquals("", store.get(sid))
38 // Explicit remove
39 store.set(sid, "text")
40 store.remove(sid)
41 assertEquals("", store.get(sid))
43 scope.cancel()
44 }
46 @Test
47 fun getReturnsEmptyForUnknownSession() {
48 // Create the scope in a local val so it can be cancelled — DraftStore launches a
49 // debounced-persistence collector into the supplied scope that keeps running until
50 // cancelled, and an uncanceled scope leaks across test invocations on device.
51 val scope = CoroutineScope(SupervisorJob() + Dispatchers.Unconfined)
52 try {
53 val store = DraftStore(context, scope)
54 assertEquals("", store.get("never-set-${System.nanoTime()}"))
55 } finally {
56 scope.cancel()
57 }
58 }