package eu.friedcutlet.yokan.services.extensions.js import eu.friedcutlet.yokan.database.DbItemType import eu.friedcutlet.yokan.database.DbSource import eu.friedcutlet.yokan.database.DbSourceCodeLanguage import eu.friedcutlet.yokan.domain.TitleCredit import eu.friedcutlet.yokan.services.http.HttpRequest import eu.friedcutlet.yokan.services.http.HttpResponse import eu.friedcutlet.yokan.services.http.HttpService import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertTrue import kotlinx.coroutines.test.runTest /** * Wires a synthetic Yokan-style JS extension through the JS runtime service. Verifies * popular / search / detail round-trip the JSON shapes the JS bridge expects. * * Tests that execute JavaScript itself live in desktopTest because QuickJS has no Android JVM * native library. These tests keep the runtime-service mapping and retry coverage platform-neutral. */ class JavaScriptExtensionRuntimeServiceTest { private val source = DbSource( id = 42L, name = "Sample JS", baseUrl = "https://example.org", lang = "en", itemType = DbItemType.Manga, sourceCodeLanguage = DbSourceCodeLanguage.JavaScript, sourceCode = SampleExtensionSource, supportLatest = true, ) @Test fun popularReturnsMappedListing() = runTest { val service = JavaScriptExtensionRuntimeService( source = source, http = NoopHttpService(), engineFactory = { _ -> SampleEngine() }, ) val listing = service.popular(1) assertEquals(2, listing.items.size) assertEquals("Title A", listing.items.first().title) assertTrue(listing.hasNextPage) service.dispose() } @Test fun searchReturnsMappedListing() = runTest { val service = JavaScriptExtensionRuntimeService( source = source, http = NoopHttpService(), engineFactory = { _ -> SampleEngine() }, ) val listing = service.search("alpha", 1, emptyList()) assertEquals(1, listing.items.size) assertEquals("Search Match", listing.items.first().title) service.dispose() } @Test fun detailReturnsParsedMetadata() = runTest { val service = JavaScriptExtensionRuntimeService( source = source, http = NoopHttpService(), engineFactory = { _ -> SampleEngine() }, ) val detail = service.detail("/manga/title-a") assertEquals("Title A", detail.title) assertEquals("Author A", detail.author) assertEquals(listOf(TitleCredit("Actor A", "Voice actor", "Hero", "/portraits/a.jpg")), detail.cast) assertEquals(1, detail.chapters.size) service.dispose() } @Test fun retriesOnceAfterTransientQuickJsStackFailure() = runTest { var creations = 0 var failedEngineCloses = 0 val service = JavaScriptExtensionRuntimeService( source = source, http = NoopHttpService(), engineFactory = { creations += 1 if (creations == 1) { object : JavaScriptEngine { override fun evaluate(script: String, isCancelled: () -> Boolean): String = error("stack overflow") override fun close() { failedEngineCloses += 1 } } } else { SampleEngine() } }, ) val listing = service.popular(1) assertEquals("Title A", listing.items.first().title) assertEquals(2, creations) assertEquals(1, failedEngineCloses) service.dispose() } } private const val SampleExtensionSource: String = "// fixture (engine emulates extension methods)" private class NoopHttpService : HttpService { override suspend fun request(request: HttpRequest): HttpResponse = HttpResponse(statusCode = 200, body = "", headers = emptyMap()) } /** Stand-in for QuickJS-backed evaluation used by mapping tests. */ private class SampleEngine : JavaScriptEngine { override fun evaluate(script: String, isCancelled: () -> Boolean): String { val expression = Regex("var __yokanValue = (extention\\.[^;]+);") .find(script) ?.groupValues ?.get(1) ?: return "" return when { expression.startsWith("extention.getPopular") -> """ { "list": [ {"name": "Title A", "imageUrl": "https://example.org/a.jpg", "link": "/manga/title-a"}, {"name": "Title B", "imageUrl": "https://example.org/b.jpg", "link": "/manga/title-b"} ], "hasNextPage": true } """.trimIndent() expression.startsWith("extention.search") -> """ {"list": [{"name": "Search Match", "link": "/manga/match"}], "hasNextPage": false} """.trimIndent() expression.startsWith("extention.getDetail") -> """ { "name": "Title A", "author": "Author A", "artist": "Artist A", "description": "Sample synopsis.", "genre": ["Action", "Adventure"], "status": 0, "imageUrl": "https://example.org/a.jpg", "cast": [{"name":"Actor A","role":"Voice actor","character":"Hero","image_url":"/portraits/a.jpg"}], "chapters": [{"name": "Chapter 1", "url": "/manga/title-a/c1", "dateUpload": "1700000000000"}] } """.trimIndent() else -> "null" } } override fun close() = Unit }