Yokan / rebuild
Course 1 8624561

LibraryViewModelTest.kt

composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/ui/screens/library/LibraryViewModelTest.kt

test / fixture168 lines
1package eu.friedcutlet.yokan.ui.screens.library23import eu.friedcutlet.yokan.data.DefaultYokanRepository4import eu.friedcutlet.yokan.data.LibraryIndexStore5import eu.friedcutlet.yokan.data.SettingsStore6import eu.friedcutlet.yokan.database.DbChapter7import eu.friedcutlet.yokan.database.DbItemType8import eu.friedcutlet.yokan.database.DbManga9import eu.friedcutlet.yokan.database.InMemoryYokanDatabase10import eu.friedcutlet.yokan.database.emptySnapshot11import eu.friedcutlet.yokan.domain.ItemType12import eu.friedcutlet.yokan.services.download.ChapterDownloads13import eu.friedcutlet.yokan.services.download.DownloadProgress14import eu.friedcutlet.yokan.services.download.DownloadRequest15import eu.friedcutlet.yokan.services.download.DownloadService16import eu.friedcutlet.yokan.services.files.NoFiles17import eu.friedcutlet.yokan.services.library.DefaultLocalEntryManagementService18import eu.friedcutlet.yokan.services.library.LibraryUpdateController19import eu.friedcutlet.yokan.services.library.LibraryUpdateProgress20import eu.friedcutlet.yokan.services.library.LibraryUpdateResult21import eu.friedcutlet.yokan.services.library.LibraryUpdaterService22import eu.friedcutlet.yokan.services.library.LocalLibraryController23import eu.friedcutlet.yokan.services.library.LocalLibraryReconciliationResult24import eu.friedcutlet.yokan.services.library.LocalLibraryScanResult25import eu.friedcutlet.yokan.services.library.LocalLibraryService26import eu.friedcutlet.yokan.services.metadata.MetadataLibraryEntry27import eu.friedcutlet.yokan.services.metadata.MetadataMedia28import eu.friedcutlet.yokan.services.metadata.MetadataService29import eu.friedcutlet.yokan.services.notifications.NoNotifications30import eu.friedcutlet.yokan.ui.ViewModelTestHost31import eu.friedcutlet.yokan.ui.navigation.LibraryRequest32import kotlin.test.AfterTest33import kotlin.test.Test34import kotlin.test.assertEquals35import kotlin.test.assertTrue36import kotlinx.coroutines.ExperimentalCoroutinesApi37import kotlinx.coroutines.flow.MutableStateFlow38import kotlinx.coroutines.test.TestScope39import kotlinx.coroutines.test.advanceTimeBy40import kotlinx.coroutines.test.runCurrent41import kotlinx.coroutines.test.runTest4243@OptIn(ExperimentalCoroutinesApi::class)44class LibraryViewModelTest {45    private val host = ViewModelTestHost()46    private val dispatcher = host.dispatcher47    private val database = InMemoryYokanDatabase(48        emptySnapshot().copy(49            mangas = listOf(50                DbManga(id = 1, name = "Alpha", favorite = true),51                DbManga(id = 2, name = "Beta", favorite = true),52                DbManga(id = 3, name = "Anime", itemType = DbItemType.Anime, favorite = true),53                DbManga(id = 4, name = "Browsed"),54            ),55            chapters = listOf(DbChapter(id = 11, mangaId = 1, name = "One"), DbChapter(id = 12, mangaId = 1, name = "Two")),56        ),57    )58    private val repository = DefaultYokanRepository(database)5960    @AfterTest61    fun tearDown() = host.close()6263    private fun TestScope.viewModel(settings: SettingsStore = SettingsStore(repository, backgroundScope) {}): LibraryViewModel {64        val localLibrary = LocalLibraryController(65            localLibrary = NoScanLibrary,66            entryManagement = DefaultLocalEntryManagementService(database, NoScanLibrary),67            repository = repository,68            metadata = NoMetadata,69            trackers = emptyList(),70            files = NoFiles,71            settings = settings,72            importScope = backgroundScope,73        )74        return host.create {75            LibraryViewModel(76                itemType = ItemType.Manga,77                repository = repository,78                settings = settings,79                libraryIndex = LibraryIndexStore(repository, backgroundScope, dispatcher),80                libraryUpdates = LibraryUpdateController(NoUpdater, repository, NoNotifications, backgroundScope),81                localLibrary = localLibrary,82                chapterDownloads = ChapterDownloads(repository, database, NoDownloads, NoNotifications, settings, NoFiles, backgroundScope),83                libraryRequests = MutableStateFlow<LibraryRequest?>(null),84                workDispatcher = dispatcher,85            )86        }87    }8889    @Test90    fun listsOnlyFavouritesOfItsItemType() = runTest(dispatcher) {91        val vm = viewModel()92        runCurrent()9394        assertEquals(setOf(1L, 2L), vm.state.value.currentItems.map { it.id }.toSet())95    }9697    @Test98    fun markingTheSelectionReadClearsItAndWritesEveryChapter() = runTest(dispatcher) {99        val vm = viewModel()100        runCurrent()101102        vm.onEvent(LibraryEvent.BeginSelection(1))103        assertTrue(vm.state.value.selectionMode)104        vm.onEvent(LibraryEvent.MarkSelectedRead(isRead = true))105        runCurrent()106107        assertEquals(emptySet(), vm.state.value.selectedIds)108        assertTrue(repository.chapters(1).all { it.isRead })109    }110111    @Test112    fun searchShowsImmediatelyAndPersistsAfterTheDebounce() = runTest(dispatcher) {113        val settings = SettingsStore(repository, backgroundScope) {}114        val vm = viewModel(settings)115        runCurrent()116117        vm.onEvent(LibraryEvent.Search("alp"))118        assertEquals("alp", vm.state.value.query.search)119        assertEquals("", settings.db.value.libraryQuery(ItemType.Manga).search)120        advanceTimeBy(251)121122        assertEquals("alp", settings.db.value.libraryQuery(ItemType.Manga).search)123    }124}125126private object NoScanLibrary : LocalLibraryService {127    override suspend fun scan() = LocalLibraryScanResult(0, 0, 0)128    override suspend fun reconcile() = LocalLibraryReconciliationResult()129    override suspend fun importTorrent(pathOrMagnet: String, displayName: String?): Long? = null130}131132private object NoMetadata : MetadataService {133    override suspend fun health(baseUrl: String) = false134    override suspend fun search(baseUrl: String, query: String, itemType: DbItemType, limit: Int) = emptyList<MetadataMedia>()135    override suspend fun library(baseUrl: String, token: String, itemType: DbItemType): List<Pair<MetadataLibraryEntry, MetadataMedia>> = emptyList()136    override suspend fun upsertLibraryEntry(137        baseUrl: String,138        token: String,139        mediaId: String,140        status: String,141        progress: Int,142        score: Int?,143        isFavorite: Boolean,144    ) = false145}146147private object NoUpdater : LibraryUpdaterService {148    override suspend fun updateLibrary(149        itemType: DbItemType,150        onProgress: (LibraryUpdateProgress) -> Unit,151        shouldCancel: () -> Boolean,152        titleIds: Set<Long>?,153        mayStartTitle: suspend () -> Boolean,154    ) = LibraryUpdateResult(0, 0, 0)155    override suspend fun updateTitle(mangaId: Long) = false156}157158private object NoDownloads : DownloadService {159    override suspend fun initialize(poolSize: Int) = Unit160    override fun enqueue(request: DownloadRequest, onProgress: (DownloadProgress) -> Unit) = Unit161    override fun cancel(taskId: String) = Unit162    override fun pause(taskId: String) = Unit163    override fun resume(taskId: String) = Unit164    override fun retry(taskId: String) = false165    override fun storageLocation(taskId: String): String? = null166    override fun deleteArtifacts(taskId: String) = Unit167    override fun activeDownloads(): List<DownloadProgress> = emptyList()168}169

Search chapters, reference and source filenames.