|
| 1 | +package dev.typetype.server.services |
| 2 | + |
| 3 | +import io.mockk.coEvery |
| 4 | +import io.mockk.coVerify |
| 5 | +import io.mockk.every |
| 6 | +import io.mockk.mockk |
| 7 | +import kotlinx.coroutines.test.runTest |
| 8 | +import org.junit.jupiter.api.Assertions.assertNull |
| 9 | +import org.junit.jupiter.api.Assertions.assertSame |
| 10 | +import org.junit.jupiter.api.Test |
| 11 | + |
| 12 | +class SabrPlaybackInfoResolverTest { |
| 13 | + @Test |
| 14 | + fun `connected account uses authenticated playback info`() = runTest { |
| 15 | + val store = mockk<SabrSessionStore>() |
| 16 | + val authenticated = mockk<AuthenticatedSabrInfoService>() |
| 17 | + val prepared = mockk<SabrPreparedInfo>() |
| 18 | + coEvery { authenticated.fetch(USER_ID, VIDEO_ID) } returns AuthenticatedSabrInfoResult.Ready(prepared) |
| 19 | + |
| 20 | + val result = SabrPlaybackInfoResolver(store, authenticated).initial(USER_ID, VIDEO_ID, 0L) |
| 21 | + |
| 22 | + assertSame(prepared, result) |
| 23 | + coVerify(exactly = 0) { store.fetchInfo(any(), any(), any(), any()) } |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + fun `authenticated failure never falls back to public playback info`() = runTest { |
| 28 | + val store = mockk<SabrSessionStore>() |
| 29 | + val authenticated = mockk<AuthenticatedSabrInfoService>() |
| 30 | + coEvery { authenticated.fetch(USER_ID, VIDEO_ID) } returns AuthenticatedSabrInfoResult.Failed |
| 31 | + |
| 32 | + val result = SabrPlaybackInfoResolver(store, authenticated).initial(USER_ID, VIDEO_ID, 0L) |
| 33 | + |
| 34 | + assertNull(result) |
| 35 | + coVerify(exactly = 0) { store.fetchInfo(any(), any(), any(), any()) } |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + fun `account without YouTube connection uses public playback info`() = runTest { |
| 40 | + val store = mockk<SabrSessionStore>() |
| 41 | + val authenticated = mockk<AuthenticatedSabrInfoService>() |
| 42 | + val prepared = mockk<SabrPreparedInfo>() |
| 43 | + coEvery { authenticated.fetch(USER_ID, VIDEO_ID) } returns AuthenticatedSabrInfoResult.NotConnected |
| 44 | + coEvery { store.fetchInfo(VIDEO_ID, 5_000L, true, false) } returns prepared |
| 45 | + |
| 46 | + val result = SabrPlaybackInfoResolver(store, authenticated).initial(USER_ID, VIDEO_ID, 5_000L) |
| 47 | + |
| 48 | + assertSame(prepared, result) |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun `authenticated replacement remains authenticated`() = runTest { |
| 53 | + val store = mockk<SabrSessionStore>() |
| 54 | + val authenticated = mockk<AuthenticatedSabrInfoService>() |
| 55 | + val prepared = mockk<SabrPreparedInfo>() |
| 56 | + val holder = holder(SabrPreparedSource.AUTHENTICATED_YOUTUBE) |
| 57 | + coEvery { authenticated.fetch(USER_ID, VIDEO_ID) } returns AuthenticatedSabrInfoResult.Ready(prepared) |
| 58 | + |
| 59 | + val result = SabrPlaybackInfoResolver(store, authenticated).replacement(holder, 60_000L) |
| 60 | + |
| 61 | + assertSame(prepared, result) |
| 62 | + coVerify(exactly = 0) { store.fetchInfo(any(), any(), any(), any()) } |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + fun `authenticated replacement failure never changes source`() = runTest { |
| 67 | + val store = mockk<SabrSessionStore>() |
| 68 | + val authenticated = mockk<AuthenticatedSabrInfoService>() |
| 69 | + val holder = holder(SabrPreparedSource.AUTHENTICATED_YOUTUBE) |
| 70 | + coEvery { authenticated.fetch(USER_ID, VIDEO_ID) } returns AuthenticatedSabrInfoResult.Failed |
| 71 | + |
| 72 | + val result = SabrPlaybackInfoResolver(store, authenticated).replacement(holder, 60_000L) |
| 73 | + |
| 74 | + assertNull(result) |
| 75 | + coVerify(exactly = 0) { store.fetchInfo(any(), any(), any(), any()) } |
| 76 | + } |
| 77 | + |
| 78 | + private fun holder(source: SabrPreparedSource): SabrSessionHolder = mockk { |
| 79 | + every { this@mockk.source } returns source |
| 80 | + every { key } returns SabrSessionKey(VIDEO_ID, USER_ID, 140, null, 137, 0L) |
| 81 | + } |
| 82 | + |
| 83 | + private companion object { |
| 84 | + const val USER_ID = "user-id" |
| 85 | + const val VIDEO_ID = "video-id" |
| 86 | + } |
| 87 | +} |
0 commit comments