package eu.friedcutlet.yokan.services.files import kotlin.test.Test import kotlin.test.assertContentEquals import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue import platform.Foundation.NSURL class IosLocalFileServiceTest { @Test fun writesEmptyFilesWithoutDereferencingMissingStorage() { val files = createPlatformLocalFileService() val path = "${files.cacheRoot}/tests/empty-${kotlin.random.Random.nextLong()}.bin" try { files.writeBytes(path, ByteArray(0)) assertTrue(files.exists(path)) assertContentEquals(ByteArray(0), files.readBytes(path)) } finally { files.deleteRecursively(path) } } @Test fun boundedReadOfSmallFileDoesNotAllocateTheLargeAllowance() { val files = createPlatformLocalFileService() val path = "${files.cacheRoot}/tests/bounded-${kotlin.random.Random.nextLong()}.bin" val bytes = byteArrayOf(1, 2, 3, 4) try { files.writeBytes(path, bytes) assertContentEquals(bytes, files.readBytesBounded(path, 512L * 1024 * 1024)) } finally { files.deleteRecursively(path) } } @Test fun persistedFolderBookmarkKeepsLogicalPathsForLaterReads() { val files = createPlatformLocalFileService() val root = "${files.cacheRoot}/bookmark-${kotlin.random.Random.nextLong()}" val content = byteArrayOf(4, 5, 6) files.writeBytes("$root/book.cbz", content) try { assertEquals(root, IosSecurityScopedBookmarks.persist(NSURL.fileURLWithPath(root))) assertTrue(files.exists(root)) assertEquals("$root/book.cbz", files.listLocalEntries(root)?.single()?.path) assertContentEquals(content, files.readBytes("$root/book.cbz")) } finally { files.deleteRecursively(root) } } @Test fun publishesStagedDirectoryAndRefusesExistingDestination() { val files = createPlatformLocalFileService() val root = "${files.cacheRoot}/publish-${kotlin.random.Random.nextLong()}" val staged = "$root/staged" val destination = "$root/job" val existingStage = "$root/staged-existing" val existingDestination = "$root/job-existing" files.writeBytes("$staged/page", byteArrayOf(1, 2, 3)) files.writeBytes("$existingStage/page", byteArrayOf(4, 5, 6)) files.writeBytes("$existingDestination/keep", byteArrayOf(7)) try { assertTrue(files.publishDirectoryIfAbsent(staged, destination)) assertFalse(files.exists(staged)) assertContentEquals(byteArrayOf(1, 2, 3), files.readBytes("$destination/page")) assertFalse(files.publishDirectoryIfAbsent(existingStage, existingDestination)) assertTrue(files.exists("$existingStage/page")) assertContentEquals(byteArrayOf(7), files.readBytes("$existingDestination/keep")) } finally { files.deleteRecursively(root) } } }