feat: Enhance test coverage (#4847)

Signed-off-by: James Rich <2199651+jamesarich@users.noreply.github.com>
This commit is contained in:
James Rich 2026-03-18 22:09:19 -05:00 committed by GitHub
parent 1b0dc75dfe
commit 06b9f8c77a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
41 changed files with 1715 additions and 502 deletions

View file

@ -0,0 +1,93 @@
/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.meshtastic.core.network.radio
import dev.mokkery.MockMode
import dev.mokkery.answering.returns
import dev.mokkery.every
import dev.mokkery.mock
import dev.mokkery.verify
import io.kotest.property.Arb
import io.kotest.property.arbitrary.byte
import io.kotest.property.arbitrary.byteArray
import io.kotest.property.arbitrary.int
import io.kotest.property.checkAll
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runTest
import org.meshtastic.core.network.transport.StreamFrameCodec
import org.meshtastic.core.repository.RadioInterfaceService
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertTrue
class StreamInterfaceTest {
private val radioService: RadioInterfaceService = mock(MockMode.autofill)
private lateinit var fakeStream: FakeStreamInterface
class FakeStreamInterface(service: RadioInterfaceService) : StreamInterface(service) {
val sentBytes = mutableListOf<ByteArray>()
override fun sendBytes(p: ByteArray) {
sentBytes.add(p)
}
override fun flushBytes() {
/* no-op */
}
override fun keepAlive() {
/* no-op */
}
fun feed(b: Byte) = readChar(b)
public override fun connect() = super.connect()
}
@BeforeTest
fun setUp() {
every { radioService.serviceScope } returns TestScope()
}
@Test
fun `handleSendToRadio property test`() = runTest {
fakeStream = FakeStreamInterface(radioService)
checkAll(Arb.byteArray(Arb.int(0, 512), Arb.byte())) { payload -> fakeStream.handleSendToRadio(payload) }
}
@Test
fun `readChar property test`() = runTest {
fakeStream = FakeStreamInterface(radioService)
checkAll(Arb.byteArray(Arb.int(0, 100), Arb.byte())) { data ->
data.forEach { fakeStream.feed(it) }
// Ensure no crash
}
}
@Test
fun `connect sends wake bytes`() {
fakeStream = FakeStreamInterface(radioService)
fakeStream.connect()
assertTrue(fakeStream.sentBytes.isNotEmpty())
assertTrue(fakeStream.sentBytes[0].contentEquals(StreamFrameCodec.WAKE_BYTES))
verify { radioService.onConnect() }
}
}

View file

@ -16,6 +16,14 @@
*/
package org.meshtastic.core.network.transport
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.shouldBe
import io.kotest.property.Arb
import io.kotest.property.arbitrary.byte
import io.kotest.property.arbitrary.byteArray
import io.kotest.property.arbitrary.int
import io.kotest.property.checkAll
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
@ -56,6 +64,31 @@ class StreamFrameCodecTest {
assertEquals(listOf(0x55.toByte()), receivedPackets[0].toList())
}
@Test
fun `frameAndSend and processInputByte are inverse`() = runTest {
checkAll(Arb.byteArray(Arb.int(0, 512), Arb.byte())) { payload ->
var received: ByteArray? = null
val codec = StreamFrameCodec(onPacketReceived = { received = it })
val bytes = mutableListOf<ByteArray>()
codec.frameAndSend(payload, sendBytes = { bytes.add(it) })
bytes.forEach { arr -> arr.forEach { codec.processInputByte(it) } }
received.shouldNotBeNull()
received.shouldBe(payload)
}
}
@Test
fun `processInputByte is robust against random noise`() = runTest {
checkAll(Arb.byteArray(Arb.int(0, 1000), Arb.byte())) { noise ->
val codec = StreamFrameCodec(onPacketReceived = { /* ignore */ })
noise.forEach { codec.processInputByte(it) }
// Should not crash
}
}
@Test
fun `processInputByte handles multiple packets sequentially`() {
val packet1 = byteArrayOf(0x94.toByte(), 0xc3.toByte(), 0x00, 0x01, 0x11)