M3u8MasterTest.kt
composeApp/src/desktopTest/kotlin/eu/friedcutlet/yokan/services/media/extractors/M3u8MasterTest.kt
1package eu.friedcutlet.yokan.services.media.extractors23import eu.friedcutlet.yokan.services.http.HttpResponse4import eu.friedcutlet.yokan.services.http.HttpService5import kotlin.test.Test6import kotlin.test.assertEquals7import kotlin.test.assertNull8import kotlin.test.assertTrue9import kotlinx.coroutines.test.runTest1011class M3u8MasterTest {12 private val masterUrl = "https://cdn.example/hls/master.m3u8"1314 private fun serving(status: Int, body: String) = HttpService { HttpResponse(statusCode = status, body = body) }1516 @Test17 fun m3u8VideosSplitsVariantsAndResolvesRelativeUrls() = runTest {18 val body = "#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=1,RESOLUTION=1280x720\n720.m3u8\n"19 val videos = serving(200, body).m3u8Videos(masterUrl, labelPrefix = "Voe:", fallbackQuality = "unused")20 assertEquals("https://cdn.example/hls/720.m3u8", videos.single().url)21 assertEquals("Voe: 720p", videos.single().quality)22 }2324 @Test25 fun m3u8VideosFallsBackToMasterOnlyWhenRequested() = runTest {26 val http = serving(200, "#EXTM3U\n#EXTINF:10,\nseg.ts\n")27 assertTrue(http.m3u8Videos(masterUrl).isEmpty())28 val fallback = http.m3u8Videos(masterUrl, fallbackQuality = "Original").single()29 assertEquals(masterUrl, fallback.url)30 assertEquals("Original", fallback.quality)31 }3233 @Test34 fun m3u8VideosReturnsNothingOnHttpError() = runTest {35 assertTrue(serving(404, "").m3u8Videos(masterUrl, fallbackQuality = "Original").isEmpty())36 }3738 @Test39 fun hostOfMatchesOnlyAbsoluteHttpUrls() {40 assertEquals("sendvid.com", hostOf("https://sendvid.com/embed/abc"))41 assertNull(hostOf("/relative/path"))42 assertNull(hostOf("not a url"))43 }44}45