package eu.friedcutlet.yokan.ext import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import eu.kanade.tachiyomi.animesource.model.Credit import eu.kanade.tachiyomi.animesource.model.SAnime import kotlinx.serialization.json.Json import m_extension_server.model.toJAnime import java.net.URLClassLoader import java.nio.file.Files import javax.tools.ToolProvider import kotlin.test.* class CastBridgeCompatibilityTest { @Test fun classCompiledAgainstAnOlderInterfaceLinksAgainstTheCurrentHost() { val directory = Files.createTempDirectory("yokan-legacy-anime-api").toFile() try { val source = directory.resolve("eu/kanade/tachiyomi/animesource/model").apply { mkdirs() } // Deliberately compile without the new cast member, then load against the real host. source.resolve("SAnime.java").writeText(""" package eu.kanade.tachiyomi.animesource.model; public interface SAnime { String getUrl(); String getTitle(); String getArtist(); String getAuthor(); String getDescription(); String getGenre(); String getThumbnail_url(); int getStatus(); boolean getInitialized(); } """.trimIndent()) source.resolve("LegacyAnime.java").writeText(""" package eu.kanade.tachiyomi.animesource.model; public class LegacyAnime implements SAnime { public String getUrl() { return "/legacy-binary"; } public String getTitle() { return "Old binary"; } public String getArtist() { return null; } public String getAuthor() { return "Author"; } public String getDescription() { return null; } public String getGenre() { return null; } public String getThumbnail_url() { return null; } public int getStatus() { return 1; } public boolean getInitialized() { return true; } } """.trimIndent()) val compiler = requireNotNull(ToolProvider.getSystemJavaCompiler()) { "Run bridge unit tests with a JDK" } assertEquals(0, compiler.run(null, null, null, "-d", directory.path, source.resolve("SAnime.java").path, source.resolve("LegacyAnime.java").path)) URLClassLoader(arrayOf(directory.toURI().toURL()), SAnime::class.java.classLoader).use { loader -> val old = loader.loadClass("eu.kanade.tachiyomi.animesource.model.LegacyAnime") .getDeclaredConstructor().newInstance() as SAnime assertEquals("Old binary", old.toJAnime().title) assertNull(old.toJAnime().cast) assertEquals("Episodes", old.toJAnime().fetch_type) assertEquals(-1.0, old.toJAnime().season_number) val copy = SAnime.create().apply { url = old.url; copyFrom(old) } assertEquals("Author", copy.author) assertNull(copy.cast) assertEquals(eu.kanade.tachiyomi.animesource.model.FetchType.Episodes, copy.fetch_type) } } finally { directory.deleteRecursively() } } @Test fun hostAnimeCreditsSurviveCopyAndBridgeSerialization() { val credit = Credit("Actor", "Voice actor", "Hero", "https://cdn.example/actor.jpg") val anime = SAnime.create().apply { url = "/title" title = "Title" status = SAnime.UPCOMING cast = listOf(credit) } val copy = SAnime.create().apply { url = anime.url; copyFrom(anime) } assertEquals(listOf(credit), copy.cast) assertEquals(SAnime.UPCOMING, copy.status) assertEquals(credit, Json.decodeFromString(Json.encodeToString(Credit.serializer(), credit))) val mapper = jacksonObjectMapper() val json = mapper.readTree(mapper.writeValueAsString(copy.toJAnime())) assertEquals("Actor", json["cast"][0]["name"].asText()) assertEquals("https://cdn.example/actor.jpg", json["cast"][0]["image_url"].asText()) assertEquals("Hero", json["cast"][0]["character"].asText()) assertEquals(7, json["status"].asInt()) } @Test fun legacyExtensionWithoutAccessorStillReturnsDetails() { val base = SAnime.create().apply { url = "/legacy"; title = "Legacy title" } val oldBinary = object : SAnime by base { override var cast: List? get() = throw AbstractMethodError("Older APK has no getCast accessor") set(_) { throw AbstractMethodError("Older APK has no setCast accessor") } } assertEquals("Legacy title", oldBinary.toJAnime().title) assertNull(oldBinary.toJAnime().cast) val copy = SAnime.create().apply { url = oldBinary.url; copyFrom(oldBinary) } assertEquals("Legacy title", copy.title) assertNull(copy.cast) } @Test fun explicitEmptyCreditsRemainAnExplicitClear() { val anime = SAnime.create().apply { url = "/title"; title = "Title"; cast = emptyList() } val copy = SAnime.create().apply { url = anime.url; cast = listOf(Credit("Old")); copyFrom(anime) } assertEquals(emptyList(), copy.toJAnime().cast) } }