DownloadMetadataExportTest.kt
composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/services/download/DownloadMetadataExportTest.kt
1package eu.friedcutlet.yokan.services.download23import eu.friedcutlet.yokan.database.DbItemType4import eu.friedcutlet.yokan.database.InMemoryYokanDatabase5import eu.friedcutlet.yokan.database.seedDatabaseSnapshot6import eu.friedcutlet.yokan.services.files.MemoryFiles7import eu.friedcutlet.yokan.services.http.HttpResponse8import eu.friedcutlet.yokan.services.http.RecordingHttpService9import kotlin.test.Test10import kotlin.test.assertEquals11import kotlin.test.assertFailsWith12import kotlin.test.assertFalse13import kotlinx.coroutines.CancellationException14import kotlinx.coroutines.test.StandardTestDispatcher15import kotlinx.coroutines.test.runTest16import kotlinx.serialization.json.Json17import kotlinx.serialization.json.jsonObject18import kotlinx.serialization.json.jsonPrimitive1920class DownloadMetadataExportTest {21 @Test22 fun controlCharactersInMetadataStillWriteParseableJson() = runTest {23 val files = MemoryFiles()24 val metadata = mapOf("name" to "Title\u0001 \"quoted\"\n", "author" to "A\\B")2526 writeMetadata(files, request(metadata), "/downloads/Title/Chapter")2728 val json = Json.parseToJsonElement(files.readBytes("/downloads/Title/Chapter/metadata.json").decodeToString()).jsonObject29 assertEquals(listOf("author", "name"), json.keys.toList())30 assertEquals(metadata, json.mapValues { it.value.jsonPrimitive.content })31 }3233 @Test34 fun cancellationDuringMetadataWritePropagates() = runTest {35 val files = object : MemoryFiles() {36 override fun writeBytes(path: String, bytes: ByteArray) = throw CancellationException("stopped")37 }3839 assertFailsWith<CancellationException> { writeMetadata(files, request(mapOf("name" to "Title")), "/downloads/Title") }40 }4142 @Test43 fun cancellationDuringPromotionPropagatesInsteadOfFailingThePage() = runTest {44 val files = object : MemoryFiles() {45 override fun promote(sourcePath: String, targetPath: String): Boolean = throw CancellationException("stopped")46 }47 val transfer = PageTransfer(48 database = InMemoryYokanDatabase(seedDatabaseSnapshot()),49 http = RecordingHttpService { HttpResponse(statusCode = 404, body = "") },50 files = files,51 imageHttp = RecordingHttpService { HttpResponse(statusCode = 404, body = "") },52 imageDataSaver = null,53 ioDispatcher = StandardTestDispatcher(testScheduler),54 awaitAllowedNetwork = {},55 )5657 assertFailsWith<CancellationException> { transfer.promoteWithMarker("/d/0001.jpg.part", "/d/0001.jpg", "/d/0001.jpg.complete") }58 assertFalse(files.exists("/d/0001.jpg.complete"))59 }6061 private fun request(metadata: Map<String, String>) = DownloadRequest(62 taskId = "metadata-task",63 chapterId = 1L,64 itemType = DbItemType.Manga,65 mangaName = "Title",66 chapterName = "Chapter",67 metadata = metadata,68 )69}70