JavaScriptExtensionRuntimeServiceTest.kt
composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/services/extensions/js/JavaScriptExtensionRuntimeServiceTest.kt
1package eu.friedcutlet.yokan.services.extensions.js23import eu.friedcutlet.yokan.database.DbItemType4import eu.friedcutlet.yokan.database.DbSource5import eu.friedcutlet.yokan.database.DbSourceCodeLanguage6import eu.friedcutlet.yokan.domain.TitleCredit7import eu.friedcutlet.yokan.services.http.HttpRequest8import eu.friedcutlet.yokan.services.http.HttpResponse9import eu.friedcutlet.yokan.services.http.HttpService10import kotlin.test.Test11import kotlin.test.assertEquals12import kotlin.test.assertTrue13import kotlinx.coroutines.test.runTest1415/**16 * Wires a synthetic Yokan-style JS extension through the JS runtime service. Verifies17 * popular / search / detail round-trip the JSON shapes the JS bridge expects.18 *19 * Tests that execute JavaScript itself live in desktopTest because QuickJS has no Android JVM20 * native library. These tests keep the runtime-service mapping and retry coverage platform-neutral.21 */22class JavaScriptExtensionRuntimeServiceTest {23 private val source = DbSource(24 id = 42L,25 name = "Sample JS",26 baseUrl = "https://example.org",27 lang = "en",28 itemType = DbItemType.Manga,29 sourceCodeLanguage = DbSourceCodeLanguage.JavaScript,30 sourceCode = SampleExtensionSource,31 supportLatest = true,32 )3334 @Test35 fun popularReturnsMappedListing() = runTest {36 val service = JavaScriptExtensionRuntimeService(37 source = source,38 http = NoopHttpService(),39 engineFactory = { _ -> SampleEngine() },40 )41 val listing = service.popular(1)42 assertEquals(2, listing.items.size)43 assertEquals("Title A", listing.items.first().title)44 assertTrue(listing.hasNextPage)45 service.dispose()46 }4748 @Test49 fun searchReturnsMappedListing() = runTest {50 val service = JavaScriptExtensionRuntimeService(51 source = source,52 http = NoopHttpService(),53 engineFactory = { _ -> SampleEngine() },54 )55 val listing = service.search("alpha", 1, emptyList())56 assertEquals(1, listing.items.size)57 assertEquals("Search Match", listing.items.first().title)58 service.dispose()59 }6061 @Test62 fun detailReturnsParsedMetadata() = runTest {63 val service = JavaScriptExtensionRuntimeService(64 source = source,65 http = NoopHttpService(),66 engineFactory = { _ -> SampleEngine() },67 )68 val detail = service.detail("/manga/title-a")69 assertEquals("Title A", detail.title)70 assertEquals("Author A", detail.author)71 assertEquals(listOf(TitleCredit("Actor A", "Voice actor", "Hero", "/portraits/a.jpg")), detail.cast)72 assertEquals(1, detail.chapters.size)73 service.dispose()74 }7576 @Test77 fun retriesOnceAfterTransientQuickJsStackFailure() = runTest {78 var creations = 079 var failedEngineCloses = 080 val service = JavaScriptExtensionRuntimeService(81 source = source,82 http = NoopHttpService(),83 engineFactory = {84 creations += 185 if (creations == 1) {86 object : JavaScriptEngine {87 override fun evaluate(script: String, isCancelled: () -> Boolean): String = error("stack overflow")88 override fun close() {89 failedEngineCloses += 190 }91 }92 } else {93 SampleEngine()94 }95 },96 )9798 val listing = service.popular(1)99100 assertEquals("Title A", listing.items.first().title)101 assertEquals(2, creations)102 assertEquals(1, failedEngineCloses)103 service.dispose()104 }105}106107private const val SampleExtensionSource: String = "// fixture (engine emulates extension methods)"108109private class NoopHttpService : HttpService {110 override suspend fun request(request: HttpRequest): HttpResponse =111 HttpResponse(statusCode = 200, body = "", headers = emptyMap())112}113114/** Stand-in for QuickJS-backed evaluation used by mapping tests. */115private class SampleEngine : JavaScriptEngine {116 override fun evaluate(script: String, isCancelled: () -> Boolean): String {117 val expression = Regex("var __yokanValue = (extention\\.[^;]+);")118 .find(script)119 ?.groupValues120 ?.get(1)121 ?: return ""122 return when {123 expression.startsWith("extention.getPopular") -> """124 {125 "list": [126 {"name": "Title A", "imageUrl": "https://example.org/a.jpg", "link": "/manga/title-a"},127 {"name": "Title B", "imageUrl": "https://example.org/b.jpg", "link": "/manga/title-b"}128 ],129 "hasNextPage": true130 }131 """.trimIndent()132 expression.startsWith("extention.search") -> """133 {"list": [{"name": "Search Match", "link": "/manga/match"}], "hasNextPage": false}134 """.trimIndent()135 expression.startsWith("extention.getDetail") -> """136 {137 "name": "Title A", "author": "Author A", "artist": "Artist A",138 "description": "Sample synopsis.", "genre": ["Action", "Adventure"],139 "status": 0, "imageUrl": "https://example.org/a.jpg",140 "cast": [{"name":"Actor A","role":"Voice actor","character":"Hero","image_url":"/portraits/a.jpg"}],141 "chapters": [{"name": "Chapter 1", "url": "/manga/title-a/c1", "dateUpload": "1700000000000"}]142 }143 """.trimIndent()144 else -> "null"145 }146 }147148 override fun close() = Unit149}150