package eu.friedcutlet.yokan.services.library import eu.friedcutlet.yokan.domain.ProfileId import eu.friedcutlet.yokan.data.DefaultYokanRepository import eu.friedcutlet.yokan.database.* import eu.friedcutlet.yokan.domain.Chapter import eu.friedcutlet.yokan.services.extensions.ChapterPageResolver import eu.friedcutlet.yokan.services.extensions.ProfileReaderContent import eu.friedcutlet.yokan.services.extensions.ProfileReaderContentTest import eu.friedcutlet.yokan.services.files.* import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancelAndJoin import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.runTest import kotlin.test.* class LocalEpisodePlaybackTest { @Test fun scannedSourcelessEpisodePlaysTheFileFromItsConfiguredFolder() = runTest { val files = MemoryVideoFiles() files.put("/anime/Show/Episode 1.mkv") val db = InMemoryYokanDatabase(emptySnapshot().copy(settings = DbSettings(namedLocalFolders = listOf(DbLocalFolder("Anime", "/anime"))))) DefaultLocalLibraryService(db, files, NoArchives).scan() val title = db.mangas().single() assertTrue(title.isLocalArchive) assertEquals("local", title.source) assertEquals(DbItemType.Anime, title.itemType) val stored = db.chapters(title.id).single() assertEquals("Anime/Show/Episode 1.mkv", stored.archivePath) val episode = Chapter(id = stored.id, mangaId = title.id, name = stored.name.orEmpty(), url = stored.url, archivePath = stored.archivePath) val factory = ProfileReaderContentTest.RuntimeFactory(db, "local") val owner = SupervisorJob() val content = ProfileReaderContent( ProfileId.Default, db, factory, ChapterPageResolver(DefaultYokanRepository(db), db, factory), files, NoArchives, CoroutineScope(owner + StandardTestDispatcher(testScheduler)), StandardTestDispatcher(testScheduler), ) try { val video = content.videos(null, episode).single() assertEquals("file:///anime/Show/Episode 1.mkv", video.url) assertEquals("Downloaded", video.quality) // The virtual path follows the folder when the user re-points it. files.data["/moved/Show/Episode 1.mkv"] = files.data.remove("/anime/Show/Episode 1.mkv")!! db.updateSettings(db.settings().copy(namedLocalFolders = listOf(DbLocalFolder("Anime", "/moved")))) assertEquals("file:///moved/Show/Episode 1.mkv", content.videos(null, episode).single().url) // A missing file never becomes a fabricated stream. files.data.remove("/moved/Show/Episode 1.mkv") assertEquals(emptyList(), content.videos(null, episode)) assertEquals(0, factory.created) } finally { content.closeAndJoin(); owner.cancelAndJoin() } } }