package eu.friedcutlet.yokan.services.files import eu.friedcutlet.yokan.domain.ProfileId import eu.friedcutlet.yokan.app.profiles.deletePlatformProfileStorage import kotlinx.coroutines.test.runTest import kotlin.test.* class BrowserStorageInteropTest { @Test fun streamedCbzCreationCrossesTheKotlinBoundaryAndPreservesTheArchiveOnReload() = runTest { val id = ProfileId.random() val files = createPlatformLocalFileService(id) val archives = createPlatformLocalArchiveService(id) files.prepare() val folder = "${files.downloadRoot}/Manga/Test/Title/CBZ" val path = "$folder.cbz" try { files.writeBytesAsync("$folder/0001.jpg", "first page".encodeToByteArray()) files.writeBytesAsync("$folder/0002.png", "second page".encodeToByteArray()) files.writeBytesAsync("$folder/metadata.json", "{\"name\":\"CBZ\"}".encodeToByteArray()) files.writeBytesAsync("$folder/0001.jpg.complete", byteArrayOf()) assertEquals(path, archives.zipDirectoryToCbzAsync(folder, deleteSource = true)) assertFalse(files.exists(folder)) assertTrue(files.exists(path)) val bytes = files.readBytesAsync(path, 4096) assertContentEquals(byteArrayOf(80, 75, 3, 4), bytes.take(4).toByteArray()) assertTrue(bytes.decodeToString().contains("metadata.json")) files.close() val reopened = createPlatformLocalFileService(id) try { reopened.prepare() assertContentEquals(bytes, reopened.readBytesAsync(path, 4096)) } finally { reopened.deleteRecursivelyAsync(path); reopened.close() } } finally { files.close(); deletePlatformProfileStorage(id, emptyList()) } } @Test fun legacyBackupsRemainListedSizedAndReadable() = runTest { val files = createPlatformLocalFileService(ProfileId.Default) val folder = "${files.backupRoot}/interop-legacy" val path = "$folder/saved.json" try { files.writeBytes(path, "backup".encodeToByteArray()) assertTrue(files.exists(folder)) assertEquals(6L, files.directorySize(folder)) assertEquals(listOf(path), files.listFiles(folder).map { it.path }) assertContentEquals("backup".encodeToByteArray(), files.readBytes(path)) } finally { files.deleteRecursivelyAsync(folder); files.close() } } @Test fun profileDeletionJoinsStorageAndPreservesExplicitBackupExports() = runTest { val id = ProfileId.random() val files = createPlatformLocalFileService(id) files.prepare() val path = "${files.downloadRoot}/Manga/Test/Title/Chapter/0001.jpg" val exported = "${files.backupRoot}/exported.json" files.writeBytesAsync(path, byteArrayOf(1, 2, 3)) files.writeBytes(exported, "export".encodeToByteArray()) assertFails { deletePlatformProfileStorage(id, listOf(exported)) } files.close() deletePlatformProfileStorage(id, listOf(exported)) val reopened = createPlatformLocalFileService(id) try { reopened.prepare() assertFalse(reopened.exists(path)) assertTrue(reopened.exists(exported)) assertContentEquals("export".encodeToByteArray(), reopened.readBytes(exported)) } finally { reopened.deleteRecursivelyAsync(exported) reopened.close() deletePlatformProfileStorage(id, emptyList()) } } @Test fun streamedFilesRoundTripThroughTheKotlinWasmBoundary() = runTest { val files = createPlatformLocalFileService(ProfileId.Default) files.prepare() val folder = "${files.downloadRoot}/Manga/Test/Interop/Chapter" val partial = "$folder/0001.jpg.part" val final = "$folder/0001.jpg" try { files.writeChunks(partial) { write -> write("first".encodeToByteArray()); write("-second".encodeToByteArray()) } assertEquals(12L, files.directorySize(partial)) assertTrue(files.promoteAsync(partial, final)) assertFalse(files.exists(partial)) assertContentEquals("first-second".encodeToByteArray(), files.readBytesAsync(final, 20)) files.appendChunks(final) { it("-third".encodeToByteArray()) } assertContentEquals("first-second-third".encodeToByteArray(), files.readBytesAsync(final, 20)) assertFails { files.readBytesAsync(final, 4) } files.writeBytesAsync("$folder/0001.jpg.complete", byteArrayOf()) files.awaitPersistence() assertEquals(2, files.listFiles(folder).size) } finally { assertTrue(files.deleteRecursivelyAsync(folder)) assertFalse(files.exists(folder)) files.close() } } @Test fun interruptedWriteKeepsItsPartialBytesAndReleasesWriter() = runTest { val files = createPlatformLocalFileService(ProfileId.Default) files.prepare() val folder = "${files.downloadRoot}/Manga/Test/Interop/Interrupted" val path = "$folder/0001.jpg.part" try { assertFailsWith { files.writeChunks(path) { write -> write("partial".encodeToByteArray()); error("Connection interrupted") } } assertContentEquals("partial".encodeToByteArray(), files.readBytesAsync(path, 20)) files.appendChunks(path) { it("-resumed".encodeToByteArray()) } assertContentEquals("partial-resumed".encodeToByteArray(), files.readBytesAsync(path, 20)) } finally { files.deleteRecursivelyAsync(folder); files.close() } } }