LocalSeasonRecordsTest.kt
composeApp/src/desktopTest/kotlin/eu/friedcutlet/yokan/services/library/LocalSeasonRecordsTest.kt
1package eu.friedcutlet.yokan.services.library23import eu.friedcutlet.yokan.data.DefaultYokanRepository4import eu.friedcutlet.yokan.database.*5import eu.friedcutlet.yokan.domain.ItemType6import eu.friedcutlet.yokan.services.files.*7import java.io.File8import java.nio.file.Files9import kotlinx.coroutines.test.runTest10import kotlin.test.*1112class LocalSeasonRecordsTest {13 @Test fun foldersWithoutEpisodeFilesBecomeSeasonTitlesAndKeepEarlierProgressAndLibraryMembership() = runTest {14 val temp = Files.createTempDirectory("yokan-local-seasons").toFile()15 try {16 val root = File(temp, "anime").apply { mkdirs() }17 fun put(path: String) = File(root, path).apply { parentFile.mkdirs(); writeText("video") }18 put("Show/cover.jpg")19 put("Show/Season 1/Episode 1.mp4")20 put("Show/Season 2/Episode 1.mp4")21 put("Show/Season 2/Extras/Bonus.mp4")22 put("Show/.hidden/Episode 9.mp4")23 put("Group/S1/Episode 1.mp4")24 put("Flat/Episode 1.mp4")25 put("Flat/Season 2/Episode 2.mp4")26 // State left by the previous scanner: a cover made `Show` one flattened title, and `Group`'s27 // only season was a separate library title.28 val db = InMemoryYokanDatabase(emptySnapshot().copy(29 settings = DbSettings(namedLocalFolders = listOf(DbLocalFolder("Anime", root.path))),30 mangas = listOf(31 DbManga(id = 1, name = "Show", link = "Anime/Show", itemType = DbItemType.Anime, source = "local", isLocalArchive = true, favorite = true),32 DbManga(id = 2, name = "S1", link = "Anime/Group/S1", itemType = DbItemType.Anime, source = "local", isLocalArchive = true,33 favorite = true, categories = listOf(5)),34 ),35 chapters = listOf(DbChapter(id = 10, mangaId = 1, name = "Episode 1", url = "Anime/Show/Season 1/Episode 1.mp4",36 archivePath = "Anime/Show/Season 1/Episode 1.mp4", isRead = true, lastPageRead = "77", isBookmarked = true)),37 histories = listOf(DbHistory(id = 20, mangaId = 1, chapterId = 10, itemType = DbItemType.Anime, date = "1")),38 ))39 val files = JvmLocalFileService(temp.path, File(temp, "backup").path, File(temp, "cache").path)40 val scanner = DefaultLocalLibraryService(db, files, JvmLocalArchiveService(files.cacheRoot))41 scanner.scan()4243 val show = assertNotNull(db.manga(1))44 assertEquals(DbFetchType.Seasons, show.fetchType)45 assertNull(show.parentId)46 assertTrue(db.chapters(1).isEmpty())47 val seasons = db.mangas().filter { it.parentId == 1L }.sortedBy { it.seasonNumber }48 assertEquals(listOf("Season 1", "Season 2"), seasons.map { it.name })49 assertEquals(listOf(1.0, 2.0), seasons.map { it.seasonNumber })50 assertTrue(seasons.all { it.fetchType == DbFetchType.Episodes && !it.favorite })51 val moved = assertNotNull(db.chapter(10))52 assertEquals(seasons[0].id, moved.mangaId)53 assertEquals("77", moved.lastPageRead)54 assertTrue(moved.isRead && moved.isBookmarked)55 assertEquals(seasons[0].id, db.histories().single().mangaId)56 assertEquals(setOf("Episode 1", "Bonus"), db.chapters(seasons[1].id).map { it.name }.toSet())5758 val group = db.mangas().single { it.link == "Anime/Group" }59 assertEquals(DbFetchType.Seasons, group.fetchType)60 assertTrue(group.favorite)61 assertEquals(listOf(5L), group.categories)62 assertEquals(group.id, db.manga(2)?.parentId)6364 val flat = db.mangas().single { it.link == "Anime/Flat" }65 assertEquals(DbFetchType.Episodes, flat.fetchType)66 assertEquals(2, db.chapters(flat.id).size)6768 assertEquals(setOf(1L, group.id), DefaultYokanRepository(db).library(ItemType.Anime).map { it.id }.toSet())69 assertTrue(db.mangas().none { it.name == ".hidden" })7071 // Removing the show from the library later is not undone by the next scan.72 db.upsertManga(db.manga(group.id)!!.copy(favorite = false))73 val before = db.mangas().map { it.id }.toSet()74 scanner.scanFresh()75 assertEquals(before, db.mangas().map { it.id }.toSet())76 assertFalse(db.manga(group.id)!!.favorite)77 assertEquals(10L, db.chapters(seasons[0].id).single().id)78 assertEquals(2, DefaultYokanRepository(db).cleanDatabase().let { db.mangas().count { m -> m.parentId == 1L } })79 } finally { temp.deleteRecursively() }80 }81}82