BrowserStorageInteropTest.kt
composeApp/src/wasmJsTest/kotlin/eu/friedcutlet/yokan/services/files/BrowserStorageInteropTest.kt
1package eu.friedcutlet.yokan.services.files23import eu.friedcutlet.yokan.domain.ProfileId4import eu.friedcutlet.yokan.app.profiles.deletePlatformProfileStorage5import kotlinx.coroutines.test.runTest6import kotlin.test.*78class BrowserStorageInteropTest {9 @Test fun streamedCbzCreationCrossesTheKotlinBoundaryAndPreservesTheArchiveOnReload() = runTest {10 val id = ProfileId.random()11 val files = createPlatformLocalFileService(id)12 val archives = createPlatformLocalArchiveService(id)13 files.prepare()14 val folder = "${files.downloadRoot}/Manga/Test/Title/CBZ"15 val path = "$folder.cbz"16 try {17 files.writeBytesAsync("$folder/0001.jpg", "first page".encodeToByteArray())18 files.writeBytesAsync("$folder/0002.png", "second page".encodeToByteArray())19 files.writeBytesAsync("$folder/metadata.json", "{\"name\":\"CBZ\"}".encodeToByteArray())20 files.writeBytesAsync("$folder/0001.jpg.complete", byteArrayOf())21 assertEquals(path, archives.zipDirectoryToCbzAsync(folder, deleteSource = true))22 assertFalse(files.exists(folder))23 assertTrue(files.exists(path))24 val bytes = files.readBytesAsync(path, 4096)25 assertContentEquals(byteArrayOf(80, 75, 3, 4), bytes.take(4).toByteArray())26 assertTrue(bytes.decodeToString().contains("metadata.json"))27 files.close()28 val reopened = createPlatformLocalFileService(id)29 try {30 reopened.prepare()31 assertContentEquals(bytes, reopened.readBytesAsync(path, 4096))32 } finally { reopened.deleteRecursivelyAsync(path); reopened.close() }33 } finally { files.close(); deletePlatformProfileStorage(id, emptyList()) }34 }35 @Test fun legacyBackupsRemainListedSizedAndReadable() = runTest {36 val files = createPlatformLocalFileService(ProfileId.Default)37 val folder = "${files.backupRoot}/interop-legacy"38 val path = "$folder/saved.json"39 try {40 files.writeBytes(path, "backup".encodeToByteArray())41 assertTrue(files.exists(folder))42 assertEquals(6L, files.directorySize(folder))43 assertEquals(listOf(path), files.listFiles(folder).map { it.path })44 assertContentEquals("backup".encodeToByteArray(), files.readBytes(path))45 } finally { files.deleteRecursivelyAsync(folder); files.close() }46 }4748 @Test fun profileDeletionJoinsStorageAndPreservesExplicitBackupExports() = runTest {49 val id = ProfileId.random()50 val files = createPlatformLocalFileService(id)51 files.prepare()52 val path = "${files.downloadRoot}/Manga/Test/Title/Chapter/0001.jpg"53 val exported = "${files.backupRoot}/exported.json"54 files.writeBytesAsync(path, byteArrayOf(1, 2, 3))55 files.writeBytes(exported, "export".encodeToByteArray())56 assertFails { deletePlatformProfileStorage(id, listOf(exported)) }57 files.close()58 deletePlatformProfileStorage(id, listOf(exported))59 val reopened = createPlatformLocalFileService(id)60 try {61 reopened.prepare()62 assertFalse(reopened.exists(path))63 assertTrue(reopened.exists(exported))64 assertContentEquals("export".encodeToByteArray(), reopened.readBytes(exported))65 } finally {66 reopened.deleteRecursivelyAsync(exported)67 reopened.close()68 deletePlatformProfileStorage(id, emptyList())69 }70 }7172 @Test fun streamedFilesRoundTripThroughTheKotlinWasmBoundary() = runTest {73 val files = createPlatformLocalFileService(ProfileId.Default)74 files.prepare()75 val folder = "${files.downloadRoot}/Manga/Test/Interop/Chapter"76 val partial = "$folder/0001.jpg.part"77 val final = "$folder/0001.jpg"78 try {79 files.writeChunks(partial) { write -> write("first".encodeToByteArray()); write("-second".encodeToByteArray()) }80 assertEquals(12L, files.directorySize(partial))81 assertTrue(files.promoteAsync(partial, final))82 assertFalse(files.exists(partial))83 assertContentEquals("first-second".encodeToByteArray(), files.readBytesAsync(final, 20))84 files.appendChunks(final) { it("-third".encodeToByteArray()) }85 assertContentEquals("first-second-third".encodeToByteArray(), files.readBytesAsync(final, 20))86 assertFails { files.readBytesAsync(final, 4) }87 files.writeBytesAsync("$folder/0001.jpg.complete", byteArrayOf())88 files.awaitPersistence()89 assertEquals(2, files.listFiles(folder).size)90 } finally {91 assertTrue(files.deleteRecursivelyAsync(folder))92 assertFalse(files.exists(folder))93 files.close()94 }95 }9697 @Test fun interruptedWriteKeepsItsPartialBytesAndReleasesWriter() = runTest {98 val files = createPlatformLocalFileService(ProfileId.Default)99 files.prepare()100 val folder = "${files.downloadRoot}/Manga/Test/Interop/Interrupted"101 val path = "$folder/0001.jpg.part"102 try {103 assertFailsWith<IllegalStateException> {104 files.writeChunks(path) { write -> write("partial".encodeToByteArray()); error("Connection interrupted") }105 }106 assertContentEquals("partial".encodeToByteArray(), files.readBytesAsync(path, 20))107 files.appendChunks(path) { it("-resumed".encodeToByteArray()) }108 assertContentEquals("partial-resumed".encodeToByteArray(), files.readBytesAsync(path, 20))109 } finally { files.deleteRecursivelyAsync(folder); files.close() }110 }111}112