package eu.friedcutlet.yokan.domain import eu.friedcutlet.yokan.data.DefaultYokanRepository import eu.friedcutlet.yokan.data.toDomainChapter import eu.friedcutlet.yokan.database.* import eu.friedcutlet.yokan.services.library.resolveReadingChapterSequence import kotlinx.coroutines.test.runTest import kotlin.test.* class ExplicitChapterNumberTest { private fun chapter(id: Long, number: Double, season: Int = 1, group: String = "") = Chapter( id, 1, "Custom title $id", "/episode/$id", scanlator = group, number = number, season = season, numberingName = "Episode $number [en]", ) @Test fun renamedEpisodesKeepOrderGapDetectionAndPlaybackNumbering() { val chapters = listOf(chapter(1, 3.0), chapter(2, 1.0), chapter(3, 4.0)) assertEquals(listOf(2L, 1L, 3L), chapterReadingOrder("Show", chapters).map { it.id }) assertEquals(1, ChapterRecognition.missingChapterCount("Show", chapters)) assertEquals(0, ChapterRecognition.unrecognizedChapterNumberCount("Show", chapters)) assertEquals(3.0, ChapterRecognition.parseEpisodeNumber("Show", chapters[0])) assertEquals(1 to 3.0, ChapterRecognition.rawSeasonAndNumber("Show", chapters[0])) val invalid = chapters[0].copy(number = Double.NaN, season = -1, numberingName = "Episode 12.5") assertEquals(0 to 12.5, ChapterRecognition.rawSeasonAndNumber("Show", invalid)) assertEquals(0, ChapterRecognition.parseSeasonNumber("Season 0")) assertEquals(2, ChapterRecognition.parseSeasonNumber("S02E12")) assertNull(ChapterRecognition.parseSeasonNumber("Opening Night")) } @Test fun releaseSelectionPreservesLanguageEditionAndSeasonAcrossCustomTitles() { val preferred = chapter(1, 1.0, group = "Preferred") val alternate = chapter(2, 1.0, group = "Other") val french = chapter(3, 1.0).copy(numberingName = "Episode 1 [fr]") val special = chapter(4, 1.0).copy(numberingName = "Episode 1 Special [en]") val seasonTwo = chapter(5, 1.0, season = 2) val raw = listOf(alternate, french, special, seasonTwo, preferred) val selected = selectChapterReleases("Show", raw, ChapterReleasePreferences(listOf("Preferred"), true)) assertEquals(setOf(1L, 3L, 4L, 5L), selected.map { it.id }.toSet()) assertEquals(setOf(1L, 2L), equivalentReleaseIds("Show", raw, setOf(2))) val sequence = resolveReadingChapterSequence("Show", 2, selected, raw) assertTrue(sequence.any { it.id == 2L }) assertFalse(sequence.any { it.id == 1L }) assertTrue(sequence.any { it.id == 5L }) } @Test fun persistedNumberingMapsToDomainAndDuplicateReadActionsUseIt() = runTest { val source = DbChapter(id = 1, mangaId = 1, name = "Opening", url = "/one", number = 1.0, season = 2, numberingName = "Episode 1 [en]") val alternate = source.copy(id = 2, name = "Different title", url = "/alternate", scanlator = "Other") val otherSeason = source.copy(id = 3, name = "Opening", url = "/season-one", season = 1) val db = InMemoryYokanDatabase(emptySnapshot().copy(mangas = listOf(DbManga(id = 1, itemType = DbItemType.Anime)), chapters = listOf(source, alternate, otherSeason))) val repository = DefaultYokanRepository(db) repository.setChaptersRead(setOf(1), true, autoReadDuplicates = true) assertTrue(assertNotNull(db.chapter(2)).isRead) assertFalse(assertNotNull(db.chapter(3)).isRead) val mapped = toDomainChapter(source) assertEquals(1.0, mapped.number) assertEquals(2, mapped.season) assertEquals("Episode 1 [en]", mapped.numberingName) val restored = DatabaseJson.decodeFromString(DbChapter.serializer(), DatabaseJson.encodeToString(DbChapter.serializer(), source)) assertEquals(source, restored) } }