ProfilePlayerScreenshotTest.kt
composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/services/media/player/ProfilePlayerScreenshotTest.kt
1@file:OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)23package eu.friedcutlet.yokan.services.media.player45import kotlinx.coroutines.CancellationException6import kotlinx.coroutines.CompletableDeferred7import kotlinx.coroutines.NonCancellable8import kotlinx.coroutines.async9import kotlinx.coroutines.awaitCancellation10import kotlinx.coroutines.cancelAndJoin11import kotlinx.coroutines.test.runCurrent12import kotlinx.coroutines.test.runTest13import kotlinx.coroutines.withContext14import kotlin.test.Test15import kotlin.test.assertEquals16import kotlin.test.assertFailsWith17import kotlin.test.assertFalse18import kotlin.test.assertTrue1920class ProfilePlayerScreenshotTest {21 @Test fun screenshotPathIsPublishedOnlyAfterNativeCaptureCompletes() = runTest {22 val f = ProfilePlayerServicesTest.Fixture(this)23 val controller = ProfilePlayerServicesTest.Controller()24 val finish = CompletableDeferred<Unit>()25 controller.onAwaitedCommand = { args ->26 finish.await()27 f.files.bytes[args[2]] = byteArrayOf(1, 2, 3)28 true29 }30 val request = async { f.reader.requestScreenshot(controller, "Book", "Episode") }31 try {32 runCurrent()33 assertFalse(request.isCompleted)34 assertEquals(0, f.files.bytes.values.single().size)35 finish.complete(Unit)36 val path = requireNotNull(request.await())37 assertEquals(listOf("sync", "screenshot-to-file", path, "video"), controller.commands.single())38 assertEquals(listOf<Byte>(1, 2, 3), f.files.bytes.getValue(path).toList())39 assertTrue(f.files.deletions.isEmpty())40 } finally { finish.complete(Unit); request.cancelAndJoin(); f.close() }41 }4243 @Test fun canceledCaptureJoinsLateNativeWriteBeforeRemovingItsFile() = runTest {44 val f = ProfilePlayerServicesTest.Fixture(this)45 val controller = ProfilePlayerServicesTest.Controller()46 val finish = CompletableDeferred<Unit>()47 controller.onAwaitedCommand = { args ->48 try { awaitCancellation() }49 finally { withContext(NonCancellable) {50 finish.await()51 assertTrue(f.files.deletions.isEmpty())52 f.files.bytes[args[2]] = byteArrayOf(1)53 } }54 }55 val request = async { f.reader.requestScreenshot(controller, "Book", "Episode") }56 try {57 runCurrent(); request.cancel(); runCurrent()58 assertFalse(request.isCompleted)59 assertTrue(f.files.deletions.isEmpty())60 finish.complete(Unit); request.join()61 assertEquals(f.files.writes, f.files.deletions)62 assertTrue(f.files.bytes.isEmpty())63 assertTrue(f.service.isAvailable)64 } finally { finish.complete(Unit); request.cancelAndJoin(); f.close() }65 }6667 @Test fun serviceRetirementJoinsCaptureAndPreservesAnotherOwnersFiles() = runTest {68 val a = ProfilePlayerServicesTest.Fixture(this, "A")69 val b = ProfilePlayerServicesTest.Fixture(this, "B")70 val controller = ProfilePlayerServicesTest.Controller()71 val finish = CompletableDeferred<Unit>()72 controller.onAwaitedCommand = {73 try { awaitCancellation() }74 finally { withContext(NonCancellable) { finish.await() } }75 }76 val request = async { a.reader.requestScreenshot(controller, "Book", "Episode") }77 try {78 runCurrent()79 val close = async { a.service.closeAndJoin() }80 runCurrent()81 assertFalse(close.isCompleted); assertFalse(request.isCompleted)82 val otherPath = requireNotNull(b.reader.requestScreenshot(ProfilePlayerServicesTest.Controller(), "Other", "Episode"))83 finish.complete(Unit); close.await()84 assertFailsWith<CancellationException> { request.await() }85 assertTrue(a.files.bytes.isEmpty()); assertTrue(a.owner.isActive)86 assertTrue(b.files.bytes.containsKey(otherPath)); assertTrue(b.service.isAvailable)87 assertTrue(b.files.deletions.isEmpty())88 } finally { finish.complete(Unit); request.cancelAndJoin(); a.close(); b.close() }89 }9091 @Test fun failedCaptureRemovesOnlyItsOwnPartialFile() = runTest {92 val f = ProfilePlayerServicesTest.Fixture(this)93 val controller = ProfilePlayerServicesTest.Controller()94 try {95 val prior = requireNotNull(f.reader.requestScreenshot(controller, "Book", "Episode"))96 controller.onAwaitedCommand = { args -> f.files.bytes[args[2]] = byteArrayOf(1); false }97 assertEquals(null, f.reader.requestScreenshot(controller, "Book", "Episode"))98 assertEquals(listOf(f.files.writes.last()), f.files.deletions)99 assertTrue(f.files.bytes.containsKey(prior))100 } finally { f.close() }101 }102103 @Test fun thrownCaptureFailureRemovesItsSeedWithoutRetiringOwner() = runTest {104 val f = ProfilePlayerServicesTest.Fixture(this)105 val controller = ProfilePlayerServicesTest.Controller()106 controller.onAwaitedCommand = { throw IllegalStateException("Capture failed") }107 try {108 assertFailsWith<IllegalStateException> { f.reader.requestScreenshot(controller, "Book", "Episode") }109 assertEquals(f.files.writes, f.files.deletions)110 assertTrue(f.files.bytes.isEmpty()); assertTrue(f.service.isAvailable)111 } finally { f.close() }112 }113114 @Test fun expiryAfterNativeSuccessCannotPublishOrKeepCapturedFile() = runTest {115 val f = ProfilePlayerServicesTest.Fixture(this)116 val controller = ProfilePlayerServicesTest.Controller()117 controller.onAwaitedCommand = { args ->118 f.files.bytes[args[2]] = byteArrayOf(1)119 f.reader.retire()120 true121 }122 try {123 assertFailsWith<CancellationException> { f.reader.requestScreenshot(controller, "Book", "Episode") }124 assertEquals(f.files.writes, f.files.deletions)125 assertTrue(f.files.bytes.isEmpty())126 } finally { f.close() }127 }128129 @Test fun queueAdmissionAloneCannotReportCaptureSuccess() = runTest {130 val f = ProfilePlayerServicesTest.Fixture(this)131 var queued = false132 val fallback = createComposeMediaPlayerController(NoopMediaStreamProxy)133 val queueOnly = object : MediaPlayerController by fallback {134 override val isMpvBackend = true135 override fun executeMpvCommand(vararg arguments: String): Boolean { queued = true; return true }136 }137 try {138 assertEquals(null, f.reader.requestScreenshot(queueOnly, "Book", "Episode"))139 assertFalse(queued)140 assertEquals(f.files.writes, f.files.deletions)141 } finally { fallback.releaseAndJoin(); f.close() }142 }143}144