ParityStateContractTest.kt
composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/ui/ParityStateContractTest.kt
1package eu.friedcutlet.yokan.ui23import kotlinx.coroutines.test.runTest4import eu.friedcutlet.yokan.data.DefaultYokanRepository5import eu.friedcutlet.yokan.database.DbItemType6import eu.friedcutlet.yokan.database.DbSource7import eu.friedcutlet.yokan.database.InMemoryYokanDatabase8import eu.friedcutlet.yokan.database.seedDatabaseSnapshot9import eu.friedcutlet.yokan.domain.History10import eu.friedcutlet.yokan.domain.ItemType11import eu.friedcutlet.yokan.domain.Source12import eu.friedcutlet.yokan.services.download.downloadTaskId13import eu.friedcutlet.yokan.ui.navigation.NavSection14import eu.friedcutlet.yokan.ui.navigation.navigationOrder15import eu.friedcutlet.yokan.ui.navigation.testNavigator16import kotlin.test.Test17import kotlin.test.assertEquals18import kotlin.test.assertFalse19import kotlin.test.assertTrue2021class ParityStateContractTest {22 @Test23 fun markSourceUsedSelectsOneSourceWithoutChangingOtherMediaTypes() {24 val database = InMemoryYokanDatabase(25 seedDatabaseSnapshot().copy(26 sources = listOf(27 source(id = 1L, itemType = DbItemType.Manga, lastUsed = true),28 source(id = 2L, itemType = DbItemType.Manga),29 source(id = 3L, itemType = DbItemType.Anime, lastUsed = true),30 ),31 ),32 )33 val repository = DefaultYokanRepository(database)3435 repository.markSourceUsed(2L)3637 val sources = database.sources().associateBy(DbSource::id)38 assertFalse(sources.getValue(1L).lastUsed)39 assertTrue(sources.getValue(2L).lastUsed)40 assertTrue(sources.getValue(3L).lastUsed)41 }4243 @Test44 fun sourceStateMappingPreservesObsoleteAndLastUsedFlags() {45 val database = InMemoryYokanDatabase(46 seedDatabaseSnapshot().copy(47 sources = listOf(48 source(id = 7L, itemType = DbItemType.Manga, lastUsed = true, isObsolete = true),49 ),50 ),51 )52 val repository = DefaultYokanRepository(database)5354 val source = repository.sources(ItemType.Manga).single()5556 assertTrue(source.lastUsed)57 assertTrue(source.isObsolete)58 }5960 @Test61 fun downloadTaskIdentityPrefersChapterAndHasStableFallback() {62 assertEquals("chapter-42", downloadTaskId(chapterId = 42L, downloadId = 9L))63 assertEquals("download-9", downloadTaskId(chapterId = null, downloadId = 9L))64 }6566 @Test67 fun seededNavigationMatchesMangayomiAndIncludesTracking() {68 // A stored order is an existing install: Home is appended rather than made the start screen.69 assertEquals(mangayomiNavigationOrder + NavSection.Home, navigationOrder(seedDatabaseSnapshot().settings))70 }7172 @Test73 fun previousYokanDefaultNavigationOrderMigratesAndPersists() = runTest {74 val seed = seedDatabaseSnapshot()75 val database = InMemoryYokanDatabase(76 seed.copy(77 settings = seed.settings.copy(78 navigationOrder = listOf(79 "Updates",80 "MangaLibrary",81 "AnimeLibrary",82 "NovelLibrary",83 "History",84 "Browse",85 "More",86 "TrackerLibrary",87 ),88 ),89 ),90 )9192 val nav = testNavigator(DefaultYokanRepository(database))9394 // An untouched previous default migrates to the current default, which starts at Home.95 assertEquals(listOf(NavSection.Home) + mangayomiNavigationOrder, nav.navigationOrder.value)96 // The persisted default includes LocalLibrary; the visible order hides it until enabled.97 val persistedDefault = listOf(NavSection.Home) + mangayomiNavigationOrder.take(3) + NavSection.LocalLibrary + mangayomiNavigationOrder.drop(3)98 assertEquals(persistedDefault.map(NavSection::name), database.settings().navigationOrder)99 }100101102 @Test103 fun validStoredNavigationOrderIsPreserved() = runTest {104 val seed = seedDatabaseSnapshot()105 val database = InMemoryYokanDatabase(106 seed.copy(107 settings = seed.settings.copy(108 navigationOrder = listOf(109 "More",110 "Browse",111 "History",112 "Updates",113 "MangaLibrary",114 "AnimeLibrary",115 "NovelLibrary",116 "TrackerLibrary",117 ),118 hideItems = null,119 ),120 ),121 )122 val nav = testNavigator(DefaultYokanRepository(database))123124 assertEquals(customNavigationOrder + NavSection.Home, nav.navigationOrder.value)125 assertTrue(nav.hiddenSections.value.isEmpty())126 }127128 @Test129 fun showPagesNumberSettingCanBeToggledGlobally() {130 val database = InMemoryYokanDatabase(seedDatabaseSnapshot())131 val repository = DefaultYokanRepository(database)132 assertTrue(repository.settings().showPagesNumber)133134 repository.updateSettings { it.copy(showPagesNumber = false) }135 assertFalse(repository.settings().showPagesNumber)136137 repository.updateSettings { it.copy(showPagesNumber = true) }138 assertTrue(repository.settings().showPagesNumber)139 }140141 private val mangayomiNavigationOrder = listOf(142 NavSection.MangaLibrary,143 NavSection.AnimeLibrary,144 NavSection.NovelLibrary,145 NavSection.Updates,146 NavSection.History,147 NavSection.Browse,148 NavSection.More,149 NavSection.TrackerLibrary,150 )151152 private val customNavigationOrder = listOf(153 NavSection.More,154 NavSection.Browse,155 NavSection.History,156 NavSection.Updates,157 NavSection.MangaLibrary,158 NavSection.AnimeLibrary,159 NavSection.NovelLibrary,160 NavSection.TrackerLibrary,161 )162163 private fun source(164 id: Long,165 itemType: DbItemType,166 lastUsed: Boolean = false,167 isObsolete: Boolean = false,168 ) = DbSource(169 id = id,170 name = "Source $id",171 baseUrl = "https://source$id.example",172 lang = "en",173 itemType = itemType,174 lastUsed = lastUsed,175 isObsolete = isObsolete,176 )177}178