Yokan / rebuild
Course 1 8624561

DefaultLibraryCategoryTest.kt

composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/data/DefaultLibraryCategoryTest.kt

test / fixture102 lines
1package eu.friedcutlet.yokan.data23import eu.friedcutlet.yokan.database.*4import eu.friedcutlet.yokan.domain.ItemType5import kotlin.test.Test6import kotlin.test.assertEquals7import kotlin.test.assertNull8import kotlinx.coroutines.ExperimentalCoroutinesApi9import kotlinx.coroutines.flow.collect10import kotlinx.coroutines.launch11import kotlinx.coroutines.test.UnconfinedTestDispatcher12import kotlinx.coroutines.test.runTest1314class DefaultLibraryCategoryTest {15    private val database = InMemoryYokanDatabase(emptySnapshot().copy(16        mangas = DbItemType.entries.mapIndexed { index, type ->17            DbManga(id = index + 1L, name = type.name, link = "/${type.name}", itemType = type)18        },19    ))20    private val repository = DefaultYokanRepository(database)2122    @Test fun eachMediaTypeUsesItsOwnDefaultAndSerializedSettingsSurviveRestart() {23        val categories = ItemType.entries.map { repository.createCategory(it, it.name) }24        categories.forEach { category ->25            repository.updateDbSettings { db -> db.copy(features = db.features.copy(26                library = db.features.library.withDefaultCategory(category.forItemType.toDb(), category.id),27            )) }28        }29        val saved = checkNotNull(decodeDatabaseSnapshot(encodeDatabaseSnapshot(database.snapshot())))30        val restarted = DefaultYokanRepository(InMemoryYokanDatabase(saved))31        categories.forEachIndexed { index, category ->32            restarted.setMangaFavorite(index + 1L, true)33            assertEquals(listOf(category.id), restarted.manga(index + 1L)?.categories)34        }35        assertEquals(listOf(false, false, false), database.mangas().map { it.favorite })36    }3738    @Test fun missingDeletedAndOtherMediaDefaultsNeverAssignForeignCategories() {39        val anime = repository.createCategory(ItemType.Anime, "Anime")40        for (invalid in listOf(anime.id, 999_999L, -1L)) {41            default(invalid)42            repository.setMangaFavorite(1, true)43            assertEquals(emptyList(), repository.manga(1)?.categories)44            repository.setMangaFavorite(1, false)45        }46        val manga = repository.createCategory(ItemType.Manga, "Manga")47        default(manga.id)48        repository.updateDbSettings { db -> db.copy(features = db.features.copy(library = db.features.library49            .withCategory(DbItemType.Manga, manga.id, DbCategoryLibraryPreferences(sort = DbSortLibraryManga(index = 4)))50            .withCategory(DbItemType.Anime, anime.id, DbCategoryLibraryPreferences(sort = DbSortLibraryManga(index = 3))))) }51        repository.removeCategory(manga.id)52        assertNull(repository.dbSettings().features.library.defaultCategory(DbItemType.Manga))53        assertEquals(emptyMap(), repository.dbSettings().features.library.categories(DbItemType.Manga))54        assertEquals(DbSortLibraryManga(index = 3), repository.dbSettings().features.library.categories(DbItemType.Anime)[anime.id]?.sort)55        repository.setMangaFavorite(1, true)56        assertEquals(emptyList(), repository.manga(1)?.categories)57    }5859    @Test fun existingAssignmentsAndRepeatedFavoriteCallsKeepExplicitChoices() {60        val automatic = repository.createCategory(ItemType.Manga, "Automatic")61        val explicit = repository.createCategory(ItemType.Manga, "Explicit")62        default(automatic.id)63        repository.setMangaCategories(1, listOf(explicit.id))64        repository.setMangaFavorite(1, true)65        assertEquals(listOf(explicit.id), repository.manga(1)?.categories)66        repository.setMangaCategories(1, emptyList())67        repository.setMangaFavorite(1, true)68        assertEquals(emptyList(), repository.manga(1)?.categories)69        repository.setMangaFavorite(1, false)70        repository.setMangaFavorite(1, true)71        assertEquals(listOf(automatic.id), repository.manga(1)?.categories)72    }7374    @Test fun uncategorizedAndAskKeepTheirDistinctSettingsWithoutInventingACategory() {75        default(0)76        repository.setMangaFavorite(1, true)77        assertEquals(0L, repository.dbSettings().features.library.defaultCategory(DbItemType.Manga))78        assertEquals(emptyList(), repository.manga(1)?.categories)79        default(null)80        assertNull(repository.dbSettings().features.library.defaultCategory(DbItemType.Manga))81        repository.setMangaFavorite(1, false)82        repository.setMangaFavorite(1, true)83        assertEquals(emptyList(), repository.manga(1)?.categories)84    }8586    @OptIn(ExperimentalCoroutinesApi::class)87    @Test fun explicitFavoriteAndCategorySelectionPublishOneAtomicState() = runTest {88        val automatic = repository.createCategory(ItemType.Manga, "Automatic")89        default(automatic.id)90        val observed = mutableListOf<DbManga>()91        backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) {92            repository.snapshots.collect { snapshot -> observed += snapshot.mangas.single { it.id == 1L } }93        }94        repository.setMangaCategories(1, emptyList(), favorite = true)95        assertEquals(listOf(false to emptyList<Long>(), true to emptyList<Long>()),96            observed.map { it.favorite to it.categories.orEmpty() })97    }9899    private fun default(id: Long?) = repository.updateDbSettings { db -> db.copy(features = db.features.copy(100        library = db.features.library.withDefaultCategory(DbItemType.Manga, id),101    )) }102}103

Search chapters, reference and source filenames.