package eu.friedcutlet.yokan.services.library import eu.friedcutlet.yokan.database.DbItemType import eu.friedcutlet.yokan.database.DbManga import eu.friedcutlet.yokan.database.DbTitleMetadata import eu.friedcutlet.yokan.database.seedDatabaseSnapshot import eu.friedcutlet.yokan.domain.ItemType import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertSame import kotlin.test.assertNotSame class LocalLibraryProjectionTest { @Test fun bothScreensShareMappingAndOnlyTitleDependenciesInvalidateIt() { val snapshot = seedDatabaseSnapshot().copy(mangas = listOf( DbManga(id = 1, name = "Local", description = "Description", isLocalArchive = true, itemType = DbItemType.Manga), DbManga(id = 2, name = "Torrent", source = "TORRENT", isLocalArchive = true, itemType = DbItemType.Manga), DbManga(id = 3, name = "Remote", itemType = DbItemType.Manga), )) val cache = LocalLibraryProjectionCache() val initial = cache.get(snapshot) assertEquals(listOf(1L), initial.titlesByType.getValue(ItemType.Manga).map { it.id }) assertEquals(listOf(1L, 2L), initial.browseTitlesByType.getValue(ItemType.Manga).map { it.id }) assertSame(initial.titlesByType.getValue(ItemType.Manga).single(), initial.browseTitlesByType.getValue(ItemType.Manga).first()) assertEquals(listOf(1L), initial.reviewQueue.map { it.id }) assertSame(initial, cache.get(snapshot.copy(settings = snapshot.settings.copy(themeIsDark = !snapshot.settings.themeIsDark)))) val updated = cache.get(snapshot.copy(settings = snapshot.settings.copy(titleMetadata = listOf(DbTitleMetadata(1, metadataServerId = "linked"))))) assertNotSame(initial, updated) assertEquals(emptyList(), updated.reviewQueue) assertEquals("linked", updated.browseTitlesByType.getValue(ItemType.Manga).first().metadataServerId) assertEquals(null, initial.browseTitlesByType.getValue(ItemType.Manga).first().metadataServerId) val deleted = cache.get(snapshot.copy(mangas = emptyList())) assertEquals(0, deleted.overview(emptyList(), LocalLibraryScanState()).manga) } }