Project

General

Profile

User Story #82 » 0002-test-diag-cross-language-wire-format-golden-lock-Bla.patch

knight8241, 08/07/2026 18:33

View differences:

common/src/commonTest/kotlin/com/aether/mofe/diag/BlackBoxWireFormatTest.kt
package com.aether.mofe.diag
import kotlin.test.Test
import kotlin.test.assertEquals
/**
* Cross-language golden: locks the black-box wire format so the on-device Kotlin encoder and the agent's
* offline python decoder (scratchpad/blackbox/bb.py) stay byte-identical. The golden values were computed by
* the python port for the FIXED state below; if a future change to [BlackBoxCodec]/[BlackBoxSchema] drifts the
* format, this fails and the python decoder would silently mis-read real captures. Integer fields only ⇒ exact.
*/
class BlackBoxWireFormatTest {
@Test
fun wire_format_matches_the_cross_language_golden() {
val s = BlackBoxState(
frameIdx = 7, tEngineMicros = 1234567890123L,
ranges = listOf(BbRange("85b92d", distanceMm = 3697, sq = 90, rssiDbm = -91, azCentiDeg = 27010, elCentiDeg = 220)),
imu = listOf(BbImu(dtMicros = 5000, axMilli = 120, ayMilli = -30, azMilli = 9810, gxMilli = 5, gyMilli = -4, gzMilli = 2)),
pose = BbPose(xMm = 812, yMm = -1503, zMm = 1100, qwE4 = 9998, qxE4 = 100, qyE4 = -50, qzE4 = 30),
)
val payload = BlackBoxState.encode(s)
assertEquals(
"01000000070000011f71fb04cb010185b92d0e715aa5698200dc0113880078ffe226520005fffc0002032cfa21044c270e0064ffce001e",
payload.joinToString("") { (it.toInt() and 0xFF).toString(16).padStart(2, '0') },
"schema wire format drifted from the python decoder golden",
)
val luma = BlackBoxCodec.encodeToLuma(payload, cols = 80, rows = 130, repeat = 5)
assertEquals(930, luma.count { it == 255 }, "luma set-cell count drifted")
val lumaBytes = ByteArray(luma.size) { (luma[it] and 0xFF).toByte() }
assertEquals(0xc16edcfc.toInt(), BlackBoxCodec.crc32(lumaBytes), "luma bit-placement drifted from the golden")
assertEquals(s, BlackBoxState.decode(BlackBoxCodec.decodeFromLuma(luma, 80, 130, 5)!!), "must self-decode")
}
}
(2-2/5)