HtmlDomRuntimeTest.kt
composeApp/src/nonWebTest/kotlin/eu/friedcutlet/yokan/services/extensions/js/HtmlDomRuntimeTest.kt
1package eu.friedcutlet.yokan.services.extensions.js23import kotlin.test.Test4import kotlin.test.assertEquals56/**7 * Exercises the injected Jsoup-like HTML DOM ([HTML_DOM_JS]) inside the real8 * QuickJS runtime, proving element selection, attribute/text extraction, CSS9 * combinators/pseudo-classes, `abs:` URL resolution, dual property/method text10 * access, and base64 helpers behave correctly on the engine extensions run on.11 */12class HtmlDomRuntimeTest {13 private fun evalWithDom(script: String): String {14 val engine = QuickJsJavaScriptEngine(bridge = JavaScriptMessageBridge { _, _ -> "null" })15 return try {16 engine.evaluate(HTML_DOM_JS)17 engine.evaluate(script)18 } finally {19 engine.close()20 }21 }2223 @Test24 fun parsesListingsAttributesAndText() {25 val result = evalWithDom(26 """27 var html = '<div class="listupd">' +28 '<article class="bs"><a class="tip" href="/anime/naruto"><div class="tt">Naruto</div>' +29 '<img data-src="/img/n.jpg"></a></article>' +30 '<article class="bs"><a class="tip" href="https://x.tld/anime/bleach"><div class="tt">Bleach</div>' +31 '<img data-src="/img/b.jpg"></a></article></div>';32 var doc = new Document(html, "https://site.tld/browse/page");33 var out = doc.select("div.listupd article a.tip").map(function (a) {34 return { name: a.selectFirst("div.tt").text, link: a.attr("href"), img: a.selectFirst("img").attr("abs:data-src") };35 });36 JSON.stringify(out);37 """.trimIndent(),38 )39 assertEquals(40 "[{\"name\":\"Naruto\",\"link\":\"/anime/naruto\",\"img\":\"https://site.tld/img/n.jpg\"}," +41 "{\"name\":\"Bleach\",\"link\":\"https://x.tld/anime/bleach\",\"img\":\"https://site.tld/img/b.jpg\"}]",42 result,43 )44 }4546 @Test47 fun supportsCombinatorsPseudoClassesAndIndexing() {48 val result = evalWithDom(49 """50 var html = '<ul class="eplister"><li><a href="/e/1"><span class="epl-num">EP 1</span></a></li>' +51 '<li><a href="/e/2"><span class="epl-num">EP 2</span></a></li></ul>' +52 '<div class="pg"><a class="prev" href="#">Prev</a><a class="next" href="/p/2">Next</a></div>' +53 '<div class="s"><span>a</span><span>b</span><span>c</span></div>';54 var doc = new Document(html);55 var eps = doc.select("ul.eplister > li > a").map(function (a) { return a.attr("href") + ":" + a.selectFirst("span.epl-num").text(); });56 var next = doc.selectFirst("div.pg a:not(.prev)").text();57 var second = doc.select("div.s span:nth-child(2)").first().text();58 var last = doc.selectFirst("div.s span:last-child").text();59 var count = doc.select("ul.eplister a").size();60 JSON.stringify({ eps: eps, next: next, second: second, last: last, count: count });61 """.trimIndent(),62 )63 assertEquals(64 "{\"eps\":[\"/e/1:EP 1\",\"/e/2:EP 2\"],\"next\":\"Next\",\"second\":\"b\",\"last\":\"c\",\"count\":2}",65 result,66 )67 }6869 @Test70 fun decodesEntitiesScriptDataAndBase64() {71 val result = evalWithDom(72 """73 var html = '<div id="d"><p>Hello World & more</p></div>' +74 '<script>var config = {"file":"master.m3u8"};</script>' +75 '<style>p > b { color: red }</STYLE ><p class="after">tail</p><script>var second = 2;</script>';76 var doc = new Document(html);77 var text = doc.selectFirst("#d p").text;78 var scriptHasFile = String(doc.selectFirst("script").data()).indexOf("master.m3u8") !== -1;79 var style = doc.selectFirst("style").data();80 var after = doc.selectFirst("p.after").text();81 var scripts = doc.select("script").size();82 var round = atob(btoa("hello-world"));83 var typeofText = typeof doc.selectFirst("#d p").text;84 JSON.stringify({ text: text, scriptHasFile: scriptHasFile, style: style, after: after, scripts: scripts, round: round, typeofText: typeofText });85 """.trimIndent(),86 )87 assertEquals(88 "{\"text\":\"Hello World & more\",\"scriptHasFile\":true,\"style\":\"p > b { color: red }\",\"after\":\"tail\"," +89 "\"scripts\":2,\"round\":\"hello-world\",\"typeofText\":\"function\"}",90 result,91 )92 }9394 @Test95 fun querySelectorHelpersReturnElements() {96 val result = evalWithDom(97 """98 var html = '<a href="x" data-em="e1">M1</a><a href="y" data-em="e2">M2</a>';99 var all = querySelectorAll(html, "a[data-em]").map(function (a) { return a.attr("data-em"); });100 var one = querySelector(html, "a[data-em]").text();101 JSON.stringify({ all: all, one: one });102 """.trimIndent(),103 )104 assertEquals("{\"all\":[\"e1\",\"e2\"],\"one\":\"M1\"}", result)105 }106 @Test107 fun supportsMangayomiCollectionIterationAndAccessors() {108 val result = evalWithDom(109 """110 var doc = new Document('<div><a href="/one"><img src="/1.jpg"></a><a href="/two"><img src="/2.jpg"></a></div>');111 var links = [];112 for (var anchor of doc.select("a")) {113 links.push({ href: anchor.getHref, src: anchor.selectFirst("img").getSrc });114 }115 JSON.stringify(links);116 """.trimIndent(),117 )118 assertEquals(119 "[{\"href\":\"/one\",\"src\":\"/1.jpg\"},{\"href\":\"/two\",\"src\":\"/2.jpg\"}]",120 result,121 )122 }123124}125