Yokan / rebuild
Course 1 8624561

ChapterRecognitionTest.kt

composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/domain/ChapterRecognitionTest.kt

test / fixture171 lines
1package eu.friedcutlet.yokan.domain23import kotlin.test.Test4import kotlin.test.assertEquals5import kotlin.test.assertNull6import kotlin.test.assertTrue78class ChapterRecognitionTest {910    private fun dummyChapter(id: Long, name: String) = Chapter(11        id = id,12        mangaId = 1L,13        url = "/chapter/$id",14        name = name,15    )1617    @Test18    fun parsesStandardChaptersAndDecimalNumbers() {19        val (s1, ep1) = ChapterRecognition.rawSeasonAndNumber("One Piece", "Chapter 1050")20        assertEquals(0, s1)21        assertEquals(1050.0, ep1)2223        val (s2, ep2) = ChapterRecognition.rawSeasonAndNumber("Spy x Family", "Chapter 12.5 - Extra")24        assertEquals(0, s2)25        assertEquals(12.5, ep2)2627        val (s3, ep3) = ChapterRecognition.rawSeasonAndNumber("Attack on Titan", "Season 2 Episode 5")28        assertEquals(2, s3)29        assertEquals(5.0, ep3)30    }3132    @Test33    fun distinguishesUnnumberedChaptersFromChapterZero() {34        val (s0, ep0) = ChapterRecognition.rawSeasonAndNumber("Jujutsu Kaisen", "Chapter 0")35        assertEquals(0.0, ep0)3637        val (sSpec, epSpec) = ChapterRecognition.rawSeasonAndNumber("Jujutsu Kaisen", "Special")38        assertNull(epSpec)3940        val (sPrologue, epPrologue) = ChapterRecognition.rawSeasonAndNumber("Solo Leveling", "Prologue")41        assertNull(epPrologue)4243        val (sOmake, epOmake) = ChapterRecognition.rawSeasonAndNumber("Naruto", "Omake")44        assertNull(epOmake)45    }4647    @Test48    fun detectsMissingChaptersInSequence() {49        val chapters = listOf(50            dummyChapter(1L, "Chapter 1"),51            dummyChapter(2L, "Chapter 2"),52            dummyChapter(4L, "Chapter 4"),53            dummyChapter(5L, "Chapter 5"),54        )55        // Missing chapter 356        assertEquals(1, ChapterRecognition.missingChapterCount("Manga", chapters))5758        val complete = listOf(59            dummyChapter(1L, "Chapter 1"),60            dummyChapter(2L, "Chapter 2"),61            dummyChapter(3L, "Chapter 3"),62        )63        assertEquals(0, ChapterRecognition.missingChapterCount("Manga", complete))6465        // Multiple missing (chapters 2 and 4 missing)66        val gap2 = listOf(67            dummyChapter(1L, "Chapter 1"),68            dummyChapter(3L, "Chapter 3"),69            dummyChapter(5L, "Chapter 5"),70        )71        assertEquals(2, ChapterRecognition.missingChapterCount("Manga", gap2))7273        // Decimals do not create false gaps74        val withDecimal = listOf(75            dummyChapter(1L, "Chapter 1"),76            dummyChapter(2L, "Chapter 1.5"),77            dummyChapter(3L, "Chapter 2"),78        )79        assertEquals(0, ChapterRecognition.missingChapterCount("Manga", withDecimal))80    }8182    @Test83    fun countsUnrecognizedChapters() {84        val chapters = listOf(85            dummyChapter(1L, "Prologue"),86            dummyChapter(2L, "Chapter 1"),87            dummyChapter(3L, "Special"),88            dummyChapter(4L, "Extra"),89            dummyChapter(5L, "Chapter 2"),90        )91        assertEquals(3, ChapterRecognition.unrecognizedChapterNumberCount("Manga", chapters))92    }9394    @Test95    fun chapterSortKeysPreservesUnnumberedAndDecimals() {96        val chapters = listOf(97            dummyChapter(1L, "Special"),98            dummyChapter(2L, "Chapter 12.5"),99            dummyChapter(3L, "Prologue"),100            dummyChapter(4L, "Chapter 13"),101        )102        val keys = ChapterRecognition.chapterSortKeys("Manga", chapters)103104        assertNull(keys[1L]) // Special has null key105        assertEquals(12.5, keys[2L])106        assertNull(keys[3L]) // Prologue has null key107        assertEquals(13.0, keys[4L])108    }109110    @Test111    fun sameEpisodeNumberInTwoSeasonsIsTwoChapters() {112        val s1 = ChapterRecognition.chapterIdentityKey("Clevatess", "Staffel 1 Folge 1")113        val s2 = ChapterRecognition.chapterIdentityKey("Clevatess", "Staffel 2 Folge 1")114        assertTrue(s1 != s2)115    }116117    @Test118    fun shorterSecondSeasonSurvivesTheFirst() {119        val keys = mutableSetOf<String?>()120        for (e in 1..12) {121            keys += ChapterRecognition.chapterIdentityKey("Clevatess", "Staffel 1 Folge $e")122        }123        for (e in 1..8) {124            keys += ChapterRecognition.chapterIdentityKey("Clevatess", "Staffel 2 Folge $e")125        }126        assertEquals(20, keys.size)127    }128129    @Test130    fun twoFullSeasonsOfSameLengthBothSurvive() {131        val keys = mutableSetOf<String?>()132        for (e in 1..25) {133            keys += ChapterRecognition.chapterIdentityKey("Code Geass", "Season 1 Episode $e")134        }135        for (e in 1..25) {136            keys += ChapterRecognition.chapterIdentityKey("Code Geass", "Season 2 Episode $e")137        }138        assertEquals(50, keys.size)139    }140141    @Test142    fun deduplicationWithinOneSeasonStillWorks() {143        val k1 = ChapterRecognition.chapterIdentityKey("Clevatess", "Staffel 1 Folge 3")144        val k2 = ChapterRecognition.chapterIdentityKey("Clevatess", "Staffel 1 Folge 3")145        assertEquals(k1, k2)146    }147148    @Test149    fun nameWithNoNumberIsNotAnIdentity() {150        assertNull(ChapterRecognition.chapterIdentityKey("Code Geass", "Special"))151        assertNull(ChapterRecognition.chapterIdentityKey("Code Geass", "Prologue"))152    }153154    @Test155    fun decimalChaptersStayApart() {156        val ch12 = ChapterRecognition.chapterIdentityKey("One Piece", "Chapter 12")157        val ch12_5 = ChapterRecognition.chapterIdentityKey("One Piece", "Chapter 12.5")158        assertTrue(ch12 != ch12_5)159    }160161    @Test162    fun scanlatorSeparatesReleasesAndIsOptional() {163        val groupA = ChapterRecognition.chapterIdentityKey("One Piece", "Chapter 12", "GroupA")164        val groupB = ChapterRecognition.chapterIdentityKey("One Piece", "Chapter 12", "GroupB")165        val noGroup = ChapterRecognition.chapterIdentityKey("One Piece", "Chapter 12")166167        assertTrue(groupA != groupB)168        assertEquals(noGroup, ChapterRecognition.chapterIdentityKey("One Piece", "Chapter 12"))169    }170}171172

Search chapters, reference and source filenames.