|
| 1 | +package dev.typetype.server |
| 2 | + |
| 3 | +import dev.typetype.server.models.ExtractionResult |
| 4 | +import dev.typetype.server.routes.streamRoutes |
| 5 | +import dev.typetype.server.services.AuthService |
| 6 | +import dev.typetype.server.services.StreamService |
| 7 | +import dev.typetype.server.services.YOUTUBE_SESSION_RECONNECT_ERROR |
| 8 | +import io.ktor.client.request.get |
| 9 | +import io.ktor.client.request.headers |
| 10 | +import io.ktor.client.statement.bodyAsText |
| 11 | +import io.ktor.http.HttpHeaders |
| 12 | +import io.ktor.http.HttpStatusCode |
| 13 | +import io.ktor.serialization.kotlinx.json.json |
| 14 | +import io.ktor.server.application.install |
| 15 | +import io.ktor.server.plugins.contentnegotiation.ContentNegotiation |
| 16 | +import io.ktor.server.routing.routing |
| 17 | +import io.ktor.server.testing.testApplication |
| 18 | +import io.mockk.coEvery |
| 19 | +import io.mockk.coVerify |
| 20 | +import io.mockk.mockk |
| 21 | +import org.junit.jupiter.api.Assertions.assertEquals |
| 22 | +import org.junit.jupiter.api.Assertions.assertTrue |
| 23 | +import org.junit.jupiter.api.Test |
| 24 | + |
| 25 | +class StreamRoutesYoutubeSessionTest { |
| 26 | + private val streamService: StreamService = mockk() |
| 27 | + |
| 28 | + @Test |
| 29 | + fun `GET streams uses authenticated YouTube session when bearer is valid`() = testApplication { |
| 30 | + coEvery { streamService.getStreamInfo(any()) } returns ExtractionResult.Failure("public path") |
| 31 | + application { testRoutes { userId, _ -> if (userId == TEST_USER_ID) ExtractionResult.Success(testStreamResponse()) else null } } |
| 32 | + val response = client.get("/streams?url=https://youtube.com/watch?v=test") { |
| 33 | + headers.append(HttpHeaders.Authorization, "Bearer test-jwt") |
| 34 | + } |
| 35 | + assertEquals(HttpStatusCode.OK, response.status) |
| 36 | + assertEquals("no-store", response.headers[HttpHeaders.CacheControl]) |
| 37 | + coVerify(exactly = 1) { streamService.getStreamInfo(any()) } |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + fun `GET streams merges authenticated HLS with public direct streams`() = testApplication { |
| 42 | + coEvery { streamService.getStreamInfo(any()) } returns ExtractionResult.Success( |
| 43 | + testStreamResponse(videoOnlyStreams = listOf(testVideoStream()), audioStreams = listOf(testAudioStream())) |
| 44 | + ) |
| 45 | + application { |
| 46 | + testRoutes { _, _ -> |
| 47 | + ExtractionResult.Success(testStreamResponse(videoOnlyStreams = emptyList(), audioStreams = emptyList()).copy(hlsUrl = "https://manifest.googlevideo.com/hls.m3u8")) |
| 48 | + } |
| 49 | + } |
| 50 | + val response = client.get("/streams?url=https://youtube.com/watch?v=test") { |
| 51 | + headers.append(HttpHeaders.Authorization, "Bearer test-jwt") |
| 52 | + } |
| 53 | + assertEquals(HttpStatusCode.OK, response.status) |
| 54 | + assertEquals("no-store", response.headers[HttpHeaders.CacheControl]) |
| 55 | + val body = response.bodyAsText() |
| 56 | + assertTrue(body.contains("\"hlsUrl\":\"https://manifest.googlevideo.com/hls.m3u8\"")) |
| 57 | + assertTrue(body.contains("\"videoOnlyStreams\":[")) |
| 58 | + assertTrue(body.contains("\"audioStreams\":[")) |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + fun `GET streams ignores authenticated path without valid bearer`() = testApplication { |
| 63 | + coEvery { streamService.getStreamInfo(any()) } returns ExtractionResult.Success(testStreamResponse()) |
| 64 | + application { testRoutes { _, _ -> ExtractionResult.Failure("unexpected session path") } } |
| 65 | + val response = client.get("/streams?url=https://youtube.com/watch?v=test") |
| 66 | + assertEquals(HttpStatusCode.OK, response.status) |
| 67 | + assertEquals("public, max-age=21600, stale-while-revalidate=3600", response.headers[HttpHeaders.CacheControl]) |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun `GET streams returns stable code when YouTube session needs reconnect`() = testApplication { |
| 72 | + coEvery { streamService.getStreamInfo(any()) } returns ExtractionResult.Failure("public path") |
| 73 | + application { testRoutes { _, _ -> ExtractionResult.BadRequest(YOUTUBE_SESSION_RECONNECT_ERROR) } } |
| 74 | + val response = client.get("/streams?url=https://youtube.com/watch?v=test") { |
| 75 | + headers.append(HttpHeaders.Authorization, "Bearer test-jwt") |
| 76 | + } |
| 77 | + assertEquals(HttpStatusCode.BadRequest, response.status) |
| 78 | + assertTrue(response.bodyAsText().contains("\"code\":\"youtube_session_needs_reconnect\"")) |
| 79 | + } |
| 80 | + |
| 81 | + private fun io.ktor.server.application.Application.testRoutes( |
| 82 | + block: suspend (String, String) -> ExtractionResult<dev.typetype.server.models.StreamResponse>?, |
| 83 | + ): Unit { |
| 84 | + install(ContentNegotiation) { json() } |
| 85 | + routing { streamRoutes(streamService, AuthService.fixed(TEST_USER_ID), block) } |
| 86 | + } |
| 87 | +} |
0 commit comments