git.iko.soy/opencode-android
on fac92bb help sign in
70 lines 2.5 kB history raw
1@file:OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)
3package soy.iko.opencode.ui.file
5import kotlinx.coroutines.Dispatchers
6import kotlinx.coroutines.ExperimentalCoroutinesApi
7import kotlinx.coroutines.test.StandardTestDispatcher
8import kotlinx.coroutines.test.TestScope
9import kotlinx.coroutines.test.resetMain
10import kotlinx.coroutines.test.runTest
11import kotlinx.coroutines.test.setMain
12import org.junit.After
13import org.junit.Assert.assertEquals
14import org.junit.Assert.assertFalse
15import org.junit.Assert.assertNotNull
16import org.junit.Assert.assertNull
17import org.junit.Before
18import org.junit.Test
19import soy.iko.opencode.data.model.FileContent
20import soy.iko.opencode.data.model.ServerProfile
21import soy.iko.opencode.ui.testing.FakeAppContainer
22import soy.iko.opencode.ui.testing.FakeEventStreamClient
23import soy.iko.opencode.ui.testing.FakeOpencodeApiClient
24import soy.iko.opencode.ui.testing.FakeOpencodeConnection
25import soy.iko.opencode.ui.testing.FakeSessionRepository
27class FileViewModelTest {
28 private val testDispatcher = StandardTestDispatcher()
29 private val testScope = TestScope(testDispatcher)
31 @Before
32 fun setUp() { Dispatchers.setMain(testDispatcher) }
34 @After
35 fun tearDown() { Dispatchers.resetMain() }
37 private fun makeContainer(api: FakeOpencodeApiClient): FakeAppContainer {
38 val events = FakeEventStreamClient()
39 val repo = FakeSessionRepository(api, events)
40 val container = FakeAppContainer()
41 container.setActiveConnection(
42 FakeOpencodeConnection(api, events, repo, ServerProfile(id = "s1", label = "T", baseUrl = "http://localhost")),
43 )
44 return container
45 }
47 @Test
48 fun load_success_populatesContent() = testScope.runTest {
49 val api = FakeOpencodeApiClient().apply { fileContent = FileContent(content = "hello source") }
50 val vm = FileViewModel(makeContainer(api), "src/Main.kt")
51 testScheduler.advanceUntilIdle()
53 val state = vm.state.value
54 assertFalse(state.loading)
55 assertNotNull(state.content)
56 assertEquals("hello source", state.content?.content)
57 assertNull(state.error)
58 }
60 @Test
61 fun load_withoutConnection_showsNotConnected() = testScope.runTest {
62 val vm = FileViewModel(FakeAppContainer(), "src/Main.kt")
63 testScheduler.advanceUntilIdle()
65 val state = vm.state.value
66 assertFalse(state.loading)
67 assertNotNull(state.error)
68 assertNull(state.content)
69 }