IosLocalMediaInputTest.kt
composeApp/src/iosTest/kotlin/eu/friedcutlet/yokan/services/files/IosLocalMediaInputTest.kt
1@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)23package eu.friedcutlet.yokan.services.files45import kotlinx.coroutines.CompletableDeferred6import kotlinx.coroutines.awaitCancellation7import kotlinx.coroutines.cancelAndJoin8import kotlinx.coroutines.launch9import kotlinx.coroutines.test.runTest10import platform.Foundation.NSFileManager11import platform.Foundation.NSURL12import kotlin.random.Random13import kotlin.test.*1415class IosLocalMediaInputTest {16 @Test fun minimalBookmarksAndCoordinatedReadsPreserveTheSourceAndBoundChunks() = runTest {17 val root = "${IosLocalFileService.cacheRoot}/test-import-${Random.nextLong().toULong()}"18 val path = "$root/clip.mp4"19 val bytes = ByteArray(3 * LOCAL_IMPORT_CHUNK_BYTES + 3) { it.toByte() }20 try {21 IosLocalFileService.writeBytes(path, bytes)22 assertContentEquals(bytes, IosLocalFileService.readBytes(path))23 assertEquals(path, IosSecurityScopedBookmarks.persist(NSURL.fileURLWithPath(path)))24 val chunks = mutableListOf<ByteArray>()25 IosLocalMediaInput("clip.mp4", bytes.size.toLong(), path).readChunks { chunks += it }26 assertEquals(listOf(65536, 65536, 65536, 3), chunks.map { it.size })27 assertContentEquals(bytes, chunks.reduce { a, b -> a + b })28 assertContentEquals(bytes, IosLocalFileService.readBytes(path))29 } finally { NSFileManager.defaultManager.removeItemAtPath(root, error = null) }30 }3132 @Test fun cancellationInsideACoordinatedReadStopsBeforeAnotherChunk() = runTest {33 val root = "${IosLocalFileService.cacheRoot}/test-import-${Random.nextLong().toULong()}"34 val path = "$root/clip.mp4"35 val bytes = ByteArray(3 * LOCAL_IMPORT_CHUNK_BYTES) { it.toByte() }36 try {37 IosLocalFileService.writeBytes(path, bytes)38 assertContentEquals(bytes, IosLocalFileService.readBytes(path))39 val first = CompletableDeferred<Unit>()40 var chunks = 041 val work = launch {42 IosLocalMediaInput("clip.mp4", bytes.size.toLong(), path).readChunks {43 chunks++44 first.complete(Unit)45 awaitCancellation()46 }47 }48 first.await()49 work.cancelAndJoin()50 assertEquals(1, chunks)51 assertContentEquals(bytes, IosLocalFileService.readBytes(path))52 } finally { NSFileManager.defaultManager.removeItemAtPath(root, error = null) }53 }54}55