refactor(test): add a common.test module and move the captured payloads into fixtures (#97)

This commit is contained in:
Fiereu authored and GitHub committed 2026-08-07 10:47:27 +02:00
1 parent 6427c3498b
commit 9866c1b0fb
65 files changed
+158 -211

No files matched your search

@@ -0,0 +1,12 @@
package de.fiereu.openmmo.common.test
import io.kotest.core.annotation.Condition
import io.kotest.core.spec.Spec
import kotlin.reflect.KClass
import org.testcontainers.DockerClientFactory
/** Gates Testcontainers specs so machines without Docker skip them instead of failing. */
class DockerAvailable : Condition {
override fun evaluate(kclass: KClass<out Spec>): Boolean =
runCatching { DockerClientFactory.instance().isDockerAvailable() }.getOrDefault(false)
}
@@ -0,0 +1,16 @@
package de.fiereu.openmmo.common.test
import de.fiereu.bytecodec.ByteArrayReadBuffer
private object Fixtures
/**
* Reads a captured payload from a module's `src/test/resources/fixtures`. The path is
* `<server>/<direction>/<packet id>/<name>.bin` and the name ends in the client version the capture
* came from, when that is known.
*/
fun fixture(path: String): ByteArray =
Fixtures.javaClass.getResourceAsStream("/fixtures/$path")?.readBytes()
?: error("no fixture at /fixtures/$path")
fun fixtureBuffer(path: String): ByteArrayReadBuffer = ByteArrayReadBuffer(fixture(path))
@@ -0,0 +1,46 @@
package de.fiereu.openmmo.common.test
import de.fiereu.bytecodec.ByteArrayReadBuffer
import de.fiereu.bytecodec.Codec
import de.fiereu.bytecodec.GrowableWriteBuffer
import de.fiereu.openmmo.common.utils.toHex
private fun contentEqual(a: Any?, b: Any?): Boolean =
when {
a is ByteArray && b is ByteArray -> a.contentEquals(b)
a is Array<*> && b is Array<*> -> a.contentDeepEquals(b)
else -> a == b
}
fun <T> Codec<T>.encodeToBytes(value: T): ByteArray {
val buf = GrowableWriteBuffer()
write(buf, value)
return buf.toByteArray()
}
fun <T> Codec<T>.decodeBytes(bytes: ByteArray): T {
val buf = ByteArrayReadBuffer(bytes)
val value = read(buf)
check(buf.remaining() == 0) { "decode left ${buf.remaining()} bytes unread" }
return value
}
fun <T> Codec<T>.assertValueRoundtrip(value: T) {
val bytes = encodeToBytes(value)
val decoded = decodeBytes(bytes)
if (!contentEqual(decoded, value)) {
throw AssertionError(
"value roundtrip mismatch: original=$value decoded=$decoded bytes=${bytes.toHex()}",
)
}
}
fun <T> Codec<T>.assertBytesRoundtrip(bytes: ByteArray) {
val value = decodeBytes(bytes)
val reencoded = encodeToBytes(value)
if (!reencoded.contentEquals(bytes)) {
throw AssertionError(
"bytes roundtrip mismatch: original=${bytes.toHex()} reencoded=${reencoded.toHex()} value=$value",
)
}
}