package eu.friedcutlet.yokan.ui import kotlinx.coroutines.test.runTest import eu.friedcutlet.yokan.data.DefaultYokanRepository import eu.friedcutlet.yokan.database.DbItemType import eu.friedcutlet.yokan.database.DbSource import eu.friedcutlet.yokan.database.InMemoryYokanDatabase import eu.friedcutlet.yokan.database.seedDatabaseSnapshot import eu.friedcutlet.yokan.domain.History import eu.friedcutlet.yokan.domain.ItemType import eu.friedcutlet.yokan.domain.Source import eu.friedcutlet.yokan.services.download.downloadTaskId import eu.friedcutlet.yokan.ui.navigation.NavSection import eu.friedcutlet.yokan.ui.navigation.navigationOrder import eu.friedcutlet.yokan.ui.navigation.testNavigator import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue class ParityStateContractTest { @Test fun markSourceUsedSelectsOneSourceWithoutChangingOtherMediaTypes() { val database = InMemoryYokanDatabase( seedDatabaseSnapshot().copy( sources = listOf( source(id = 1L, itemType = DbItemType.Manga, lastUsed = true), source(id = 2L, itemType = DbItemType.Manga), source(id = 3L, itemType = DbItemType.Anime, lastUsed = true), ), ), ) val repository = DefaultYokanRepository(database) repository.markSourceUsed(2L) val sources = database.sources().associateBy(DbSource::id) assertFalse(sources.getValue(1L).lastUsed) assertTrue(sources.getValue(2L).lastUsed) assertTrue(sources.getValue(3L).lastUsed) } @Test fun sourceStateMappingPreservesObsoleteAndLastUsedFlags() { val database = InMemoryYokanDatabase( seedDatabaseSnapshot().copy( sources = listOf( source(id = 7L, itemType = DbItemType.Manga, lastUsed = true, isObsolete = true), ), ), ) val repository = DefaultYokanRepository(database) val source = repository.sources(ItemType.Manga).single() assertTrue(source.lastUsed) assertTrue(source.isObsolete) } @Test fun downloadTaskIdentityPrefersChapterAndHasStableFallback() { assertEquals("chapter-42", downloadTaskId(chapterId = 42L, downloadId = 9L)) assertEquals("download-9", downloadTaskId(chapterId = null, downloadId = 9L)) } @Test fun seededNavigationMatchesMangayomiAndIncludesTracking() { // A stored order is an existing install: Home is appended rather than made the start screen. assertEquals(mangayomiNavigationOrder + NavSection.Home, navigationOrder(seedDatabaseSnapshot().settings)) } @Test fun previousYokanDefaultNavigationOrderMigratesAndPersists() = runTest { val seed = seedDatabaseSnapshot() val database = InMemoryYokanDatabase( seed.copy( settings = seed.settings.copy( navigationOrder = listOf( "Updates", "MangaLibrary", "AnimeLibrary", "NovelLibrary", "History", "Browse", "More", "TrackerLibrary", ), ), ), ) val nav = testNavigator(DefaultYokanRepository(database)) // An untouched previous default migrates to the current default, which starts at Home. assertEquals(listOf(NavSection.Home) + mangayomiNavigationOrder, nav.navigationOrder.value) // The persisted default includes LocalLibrary; the visible order hides it until enabled. val persistedDefault = listOf(NavSection.Home) + mangayomiNavigationOrder.take(3) + NavSection.LocalLibrary + mangayomiNavigationOrder.drop(3) assertEquals(persistedDefault.map(NavSection::name), database.settings().navigationOrder) } @Test fun validStoredNavigationOrderIsPreserved() = runTest { val seed = seedDatabaseSnapshot() val database = InMemoryYokanDatabase( seed.copy( settings = seed.settings.copy( navigationOrder = listOf( "More", "Browse", "History", "Updates", "MangaLibrary", "AnimeLibrary", "NovelLibrary", "TrackerLibrary", ), hideItems = null, ), ), ) val nav = testNavigator(DefaultYokanRepository(database)) assertEquals(customNavigationOrder + NavSection.Home, nav.navigationOrder.value) assertTrue(nav.hiddenSections.value.isEmpty()) } @Test fun showPagesNumberSettingCanBeToggledGlobally() { val database = InMemoryYokanDatabase(seedDatabaseSnapshot()) val repository = DefaultYokanRepository(database) assertTrue(repository.settings().showPagesNumber) repository.updateSettings { it.copy(showPagesNumber = false) } assertFalse(repository.settings().showPagesNumber) repository.updateSettings { it.copy(showPagesNumber = true) } assertTrue(repository.settings().showPagesNumber) } private val mangayomiNavigationOrder = listOf( NavSection.MangaLibrary, NavSection.AnimeLibrary, NavSection.NovelLibrary, NavSection.Updates, NavSection.History, NavSection.Browse, NavSection.More, NavSection.TrackerLibrary, ) private val customNavigationOrder = listOf( NavSection.More, NavSection.Browse, NavSection.History, NavSection.Updates, NavSection.MangaLibrary, NavSection.AnimeLibrary, NavSection.NovelLibrary, NavSection.TrackerLibrary, ) private fun source( id: Long, itemType: DbItemType, lastUsed: Boolean = false, isObsolete: Boolean = false, ) = DbSource( id = id, name = "Source $id", baseUrl = "https://source$id.example", lang = "en", itemType = itemType, lastUsed = lastUsed, isObsolete = isObsolete, ) }