ExplicitChapterNumberTest.kt
composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/domain/ExplicitChapterNumberTest.kt
1package eu.friedcutlet.yokan.domain23import eu.friedcutlet.yokan.data.DefaultYokanRepository4import eu.friedcutlet.yokan.data.toDomainChapter5import eu.friedcutlet.yokan.database.*6import eu.friedcutlet.yokan.services.library.resolveReadingChapterSequence7import kotlinx.coroutines.test.runTest8import kotlin.test.*910class ExplicitChapterNumberTest {11 private fun chapter(id: Long, number: Double, season: Int = 1, group: String = "") = Chapter(12 id, 1, "Custom title $id", "/episode/$id", scanlator = group,13 number = number, season = season, numberingName = "Episode $number [en]",14 )1516 @Test fun renamedEpisodesKeepOrderGapDetectionAndPlaybackNumbering() {17 val chapters = listOf(chapter(1, 3.0), chapter(2, 1.0), chapter(3, 4.0))18 assertEquals(listOf(2L, 1L, 3L), chapterReadingOrder("Show", chapters).map { it.id })19 assertEquals(1, ChapterRecognition.missingChapterCount("Show", chapters))20 assertEquals(0, ChapterRecognition.unrecognizedChapterNumberCount("Show", chapters))21 assertEquals(3.0, ChapterRecognition.parseEpisodeNumber("Show", chapters[0]))22 assertEquals(1 to 3.0, ChapterRecognition.rawSeasonAndNumber("Show", chapters[0]))23 val invalid = chapters[0].copy(number = Double.NaN, season = -1, numberingName = "Episode 12.5")24 assertEquals(0 to 12.5, ChapterRecognition.rawSeasonAndNumber("Show", invalid))25 assertEquals(0, ChapterRecognition.parseSeasonNumber("Season 0"))26 assertEquals(2, ChapterRecognition.parseSeasonNumber("S02E12"))27 assertNull(ChapterRecognition.parseSeasonNumber("Opening Night"))28 }2930 @Test fun releaseSelectionPreservesLanguageEditionAndSeasonAcrossCustomTitles() {31 val preferred = chapter(1, 1.0, group = "Preferred")32 val alternate = chapter(2, 1.0, group = "Other")33 val french = chapter(3, 1.0).copy(numberingName = "Episode 1 [fr]")34 val special = chapter(4, 1.0).copy(numberingName = "Episode 1 Special [en]")35 val seasonTwo = chapter(5, 1.0, season = 2)36 val raw = listOf(alternate, french, special, seasonTwo, preferred)37 val selected = selectChapterReleases("Show", raw, ChapterReleasePreferences(listOf("Preferred"), true))38 assertEquals(setOf(1L, 3L, 4L, 5L), selected.map { it.id }.toSet())39 assertEquals(setOf(1L, 2L), equivalentReleaseIds("Show", raw, setOf(2)))40 val sequence = resolveReadingChapterSequence("Show", 2, selected, raw)41 assertTrue(sequence.any { it.id == 2L })42 assertFalse(sequence.any { it.id == 1L })43 assertTrue(sequence.any { it.id == 5L })44 }4546 @Test fun persistedNumberingMapsToDomainAndDuplicateReadActionsUseIt() = runTest {47 val source = DbChapter(id = 1, mangaId = 1, name = "Opening", url = "/one", number = 1.0, season = 2, numberingName = "Episode 1 [en]")48 val alternate = source.copy(id = 2, name = "Different title", url = "/alternate", scanlator = "Other")49 val otherSeason = source.copy(id = 3, name = "Opening", url = "/season-one", season = 1)50 val db = InMemoryYokanDatabase(emptySnapshot().copy(mangas = listOf(DbManga(id = 1, itemType = DbItemType.Anime)), chapters = listOf(source, alternate, otherSeason)))51 val repository = DefaultYokanRepository(db)52 repository.setChaptersRead(setOf(1), true, autoReadDuplicates = true)53 assertTrue(assertNotNull(db.chapter(2)).isRead)54 assertFalse(assertNotNull(db.chapter(3)).isRead)55 val mapped = toDomainChapter(source)56 assertEquals(1.0, mapped.number)57 assertEquals(2, mapped.season)58 assertEquals("Episode 1 [en]", mapped.numberingName)59 val restored = DatabaseJson.decodeFromString(DbChapter.serializer(), DatabaseJson.encodeToString(DbChapter.serializer(), source))60 assertEquals(source, restored)61 }62}63