BrowseClearViewModelTest.kt
composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/ui/screens/settings/browse/BrowseClearViewModelTest.kt
1package eu.friedcutlet.yokan.ui.screens.settings.browse23import eu.friedcutlet.yokan.ui.ViewModelTestHost4import eu.friedcutlet.yokan.database.InMemoryYokanDatabase5import eu.friedcutlet.yokan.database.YokanDatabase6import eu.friedcutlet.yokan.domain.ItemType7import eu.friedcutlet.yokan.services.download.DownloadProgress8import eu.friedcutlet.yokan.services.download.DownloadRequest9import eu.friedcutlet.yokan.services.download.DownloadService10import eu.friedcutlet.yokan.services.library.SelectiveDataClearService11import kotlin.test.AfterTest12import kotlin.test.Test13import kotlin.test.assertEquals14import kotlin.test.assertFalse15import kotlinx.coroutines.ExperimentalCoroutinesApi16import kotlinx.coroutines.test.UnconfinedTestDispatcher17import kotlinx.coroutines.test.runTest1819@OptIn(ExperimentalCoroutinesApi::class)20class BrowseClearViewModelTest {21 private val host = ViewModelTestHost()2223 @AfterTest24 fun tearDown() = host.close()2526 @Test27 fun reportsSuccessOnlyAfterCommittedClearCompletes() = runTest {28 val vm = viewModel(InMemoryYokanDatabase())2930 vm.clearLibrary(setOf(ItemType.Manga))3132 assertFalse(vm.state.value.busy)33 assertEquals("Library records cleared locally; no hub is configured.", vm.state.value.success)34 assertEquals(null, vm.state.value.error)35 }3637 @Test38 fun keepsFailureAsActionableErrorWithoutSuccess() = runTest {39 val vm = viewModel(object : YokanDatabase by InMemoryYokanDatabase() {40 override fun supportsAtomicSnapshotPersistence() = false41 })4243 vm.clearSources(setOf(ItemType.Manga))4445 assertFalse(vm.state.value.busy)46 assertEquals("This database backend cannot atomically clear data; nothing was changed.", vm.state.value.error)47 assertEquals(null, vm.state.value.success)48 }4950 private fun viewModel(database: YokanDatabase): BrowseClearViewModel {51 val service = SelectiveDataClearService(database, NoActiveDownloads)52 return host.create { BrowseClearViewModel(service, hasConfiguredHub = { false }, workDispatcher = UnconfinedTestDispatcher()) }53 }54}5556private object NoActiveDownloads : DownloadService {57 override suspend fun initialize(poolSize: Int) = Unit58 override fun enqueue(request: DownloadRequest, onProgress: (DownloadProgress) -> Unit) = Unit59 override fun cancel(taskId: String) = Unit60 override fun pause(taskId: String) = Unit61 override fun resume(taskId: String) = Unit62 override fun retry(taskId: String) = false63 override fun storageLocation(taskId: String): String? = null64 override fun deleteArtifacts(taskId: String) = Unit65 override fun activeDownloads(): List<DownloadProgress> = emptyList()66}67