|
| 1 | +package gg.grounds.proxy.velocity.motd |
| 2 | + |
| 3 | +import com.google.gson.JsonParser |
| 4 | +import net.kyori.adventure.text.Component |
| 5 | +import net.kyori.adventure.text.TextComponent |
| 6 | +import net.kyori.adventure.text.format.Style |
| 7 | +import net.kyori.adventure.text.format.TextDecoration |
| 8 | +import org.junit.jupiter.api.Assertions.* |
| 9 | +import org.junit.jupiter.api.Test |
| 10 | + |
| 11 | +class NativeMotdAlignmentTest { |
| 12 | + private fun characters(component: Component): List<Pair<String, Boolean>> { |
| 13 | + val chars = mutableListOf<Pair<String, Boolean>>() |
| 14 | + fun walk(node: Component, inherited: Style) { |
| 15 | + val style = node.style().merge(inherited, Style.Merge.Strategy.IF_ABSENT_ON_TARGET) |
| 16 | + if (node is TextComponent) |
| 17 | + node.content().codePoints().forEach { |
| 18 | + chars.add( |
| 19 | + String(Character.toChars(it)) to |
| 20 | + (style.decoration(TextDecoration.BOLD) == TextDecoration.State.TRUE) |
| 21 | + ) |
| 22 | + } |
| 23 | + node.children().forEach { walk(it, style) } |
| 24 | + } |
| 25 | + walk(component, Style.empty()) |
| 26 | + return chars |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + fun `matches the shared native padding and resolved placeholder fixtures`() { |
| 31 | + val samples = |
| 32 | + JsonParser.parseReader( |
| 33 | + javaClass.getResourceAsStream("/server-list/native-padding.json")!!.reader() |
| 34 | + ) |
| 35 | + .asJsonArray |
| 36 | + val advances = |
| 37 | + JsonParser.parseReader( |
| 38 | + javaClass.getResourceAsStream("/motd/java-1.21.1-advances.json")!!.reader() |
| 39 | + ) |
| 40 | + .asJsonObject |
| 41 | + for (sample in samples) { |
| 42 | + val s = sample.asJsonObject |
| 43 | + val source = s.get("source").asString |
| 44 | + val document = |
| 45 | + JsonParser.parseString( |
| 46 | + """{"nodes":[{"type":"opaque","format":"minimessage","raw":${s.get("source")}}]}""" |
| 47 | + ) |
| 48 | + .asJsonObject |
| 49 | + val values = |
| 50 | + s.getAsJsonObject("values").entrySet().associate { it.key to it.value.asString } |
| 51 | + val result = |
| 52 | + CompiledMotd(document, s.getAsJsonArray("alignments").map { it.asString }) |
| 53 | + .render(values, s.get("width").asInt) |
| 54 | + val lines = componentLines(result) |
| 55 | + assertEquals(s.getAsJsonArray("expected").size(), lines.size, s.get("id").asString) |
| 56 | + for ((index, line) in lines.withIndex()) { |
| 57 | + val expected = s.getAsJsonArray("expected")[index].asJsonObject |
| 58 | + val chars = characters(line) |
| 59 | + val count = expected.get("regular").asInt + expected.get("bold").asInt |
| 60 | + val prefix = chars.take(count) |
| 61 | + assertTrue(prefix.all { it.first == " " }, source) |
| 62 | + assertEquals(expected.get("bold").asInt, prefix.count { it.second }, source) |
| 63 | + assertEquals( |
| 64 | + expected.get("padding").asInt, |
| 65 | + prefix.sumOf { if (it.second) 5 else 4 }, |
| 66 | + source, |
| 67 | + ) |
| 68 | + assertEquals( |
| 69 | + expected.get("width").asInt, |
| 70 | + chars.drop(count).sumOf { |
| 71 | + (advances[it.first]?.asInt ?: 6) + if (it.second) 1 else 0 |
| 72 | + }, |
| 73 | + source, |
| 74 | + ) |
| 75 | + assertFalse(chars.joinToString("") { it.first }.contains("{{"), source) |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + fun `uses the closest representable padding for short and full-width lines`() { |
| 82 | + val document = |
| 83 | + JsonParser.parseString("""{"nodes":[{"type":"text","value":"A"}]}""").asJsonObject |
| 84 | + for (alignment in listOf("center", "right")) for (remaining in 0..280) { |
| 85 | + val target = if (alignment == "center") remaining / 2.0 else remaining.toDouble() |
| 86 | + val candidates = |
| 87 | + (0..3) |
| 88 | + .flatMap { b -> (0..(remaining / 4)).map { r -> r * 4 + b * 5 } } |
| 89 | + .filter { it <= remaining } |
| 90 | + val expected = |
| 91 | + candidates.minWith( |
| 92 | + compareBy<Int> { kotlin.math.abs(it - target) }.thenByDescending { it } |
| 93 | + ) |
| 94 | + val chars = |
| 95 | + characters( |
| 96 | + CompiledMotd(document, listOf(alignment)).render(emptyMap(), remaining + 6) |
| 97 | + ) |
| 98 | + assertEquals("A", chars.last().first) |
| 99 | + assertFalse(chars.last().second, "padding bold must not affect the MOTD") |
| 100 | + assertEquals( |
| 101 | + expected, |
| 102 | + chars.dropLast(1).sumOf { if (it.second) 5 else 4 }, |
| 103 | + "$alignment $remaining", |
| 104 | + ) |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments