package eu.friedcutlet.yokan.services.extensions import eu.friedcutlet.yokan.database.DbItemType import eu.friedcutlet.yokan.services.files.JvmLocalFileService import eu.friedcutlet.yokan.services.http.HttpRequest import eu.friedcutlet.yokan.services.http.HttpResponse import eu.friedcutlet.yokan.services.http.HttpService import java.nio.file.Path import kotlin.test.Test import kotlin.test.assertTrue import kotlinx.coroutines.test.runTest import eu.friedcutlet.yokan.services.extensions.js.JavaScriptExtensionRuntimeService /** * Drives every generated source's popular / search / detail / video-or-page * paths through the real QuickJS runtime using canned HTML (no network), to * prove the host contract is complete — no missing globals, JS builtins, * `SharedPreferences` accessors, `Crypto` helpers, or DOM methods. Errors are * classified: host/runtime defects fail the test; domain errors (a source * throwing because the fixture doesn't match its site, or JSON.parse on HTML) * are expected and ignored. * * Deterministic and offline, so it runs in the default suite whenever * `-Dyokan.generated.repository` points at a built anime index. */ class ExtensionHostContractTest { @Test fun everySourceExercisesHostContractWithoutDefects() = runTest { val repoProperty = System.getProperty("yokan.generated.repository") ?: return@runTest val repositoryPath = Path.of(repoProperty).toAbsolutePath().normalize() val repositoryUrl = repositoryPath.toUri().toString() val files = JvmLocalFileService( downloadRoot = repositoryPath.parent.toString(), backupRoot = repositoryPath.parent.toString(), cacheRoot = repositoryPath.parent.toString(), ) val catalog = HttpSourceCatalogService(FixtureHttpService, files) val sources = catalog.fetchSources(repositoryUrl, DbItemType.Anime) assertTrue(sources.isNotEmpty(), "no anime sources found") val defects = mutableListOf() for (source in sources) { val payload = catalog.fetchSourcePayload(source)?.decodeToString() ?: continue val runtime = JavaScriptExtensionRuntimeService(source = source.copy(sourceCode = payload), http = FixtureHttpService) try { exercise(source.name, "popular") { runtime.popular(1) }?.let { defects += it } exercise(source.name, "search") { runtime.search("a", 1, emptyList()) }?.let { defects += it } val listing = runCatching { runtime.popular(1) }.getOrNull() val entry = listing?.items?.firstOrNull() if (entry != null) { val detail = runCatching { runtime.detail(entry.url) }.getOrNull() exercise(source.name, "detail") { runtime.detail(entry.url) }?.let { defects += it } val chapter = detail?.chapters?.firstOrNull() if (chapter != null) { exercise(source.name, "videoList") { runtime.videoList(chapter.url) }?.let { defects += it } } } } finally { runtime.dispose() } } assertTrue( defects.isEmpty(), "Host-contract defects in ${defects.size} path(s):\n${defects.joinToString("\n")}", ) } /** Returns a defect description if the step hit a host/runtime gap, else null. */ private suspend fun exercise(name: String, step: String, block: suspend () -> Any?): String? { return try { block() null } catch (error: Throwable) { val message = (generateSequence(error) { it.cause }.lastOrNull()?.message ?: error.message).orEmpty() if (isHostDefect(message)) "$name.$step: ${message.take(160)}" else null } } private companion object { fun isHostDefect(message: String): Boolean { val lower = message.lowercase() return "is not a function" in lower || "is not defined" in lower || "no setter" in lower || "not a constructor" in lower || "bridge channel" in lower || "is not an object" in lower } } } /** Serves a rich generic anime/listing HTML for any URL so parsing code paths execute. */ private object FixtureHttpService : HttpService { private val html = """ Fixture
DL

Title

Desc
""".trimIndent() override suspend fun request(request: HttpRequest): HttpResponse = HttpResponse(statusCode = 200, body = html, headers = mapOf("content-type" to "text/html")) }