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

@ -1,5 +1,5 @@
/*
* Copyright (c) 2025-2026 Meshtastic LLC
* 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
@ -16,33 +16,53 @@
*/
package org.meshtastic.feature.map
/**
* Bootstrap tests for BaseMapViewModel.
*
* Tests map functionality using FakeNodeRepository and test data.
*/
import app.cash.turbine.test
import dev.mokkery.answering.returns
import dev.mokkery.every
import dev.mokkery.mock
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.meshtastic.core.model.ConnectionState
import org.meshtastic.core.repository.MapPrefs
import org.meshtastic.core.repository.PacketRepository
import org.meshtastic.core.testing.FakeNodeRepository
import org.meshtastic.core.testing.FakeRadioController
import org.meshtastic.core.testing.TestDataFactory
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
@OptIn(ExperimentalCoroutinesApi::class)
class BaseMapViewModelTest {
/*
private val testDispatcher = UnconfinedTestDispatcher()
private lateinit var viewModel: BaseMapViewModel
private lateinit var nodeRepository: FakeNodeRepository
private lateinit var radioController: FakeRadioController
private lateinit var mapPrefs: MapPrefs
private lateinit var packetRepository: PacketRepository
private val mapPrefs: MapPrefs = mock()
private val packetRepository: PacketRepository = mock()
@BeforeTest
fun setUp() {
Dispatchers.setMain(testDispatcher)
nodeRepository = FakeNodeRepository()
radioController = FakeRadioController()
radioController.setConnectionState(ConnectionState.Disconnected)
mapPrefs =
every { showOnlyFavorites } returns MutableStateFlow(false)
every { showWaypointsOnMap } returns MutableStateFlow(false)
every { showPrecisionCircleOnMap } returns MutableStateFlow(false)
every { lastHeardFilter } returns MutableStateFlow(0L)
every { lastHeardTrackFilter } returns MutableStateFlow(0L)
}
every { mapPrefs.showOnlyFavorites } returns MutableStateFlow(false)
every { mapPrefs.showWaypointsOnMap } returns MutableStateFlow(false)
every { mapPrefs.showPrecisionCircleOnMap } returns MutableStateFlow(false)
every { mapPrefs.lastHeardFilter } returns MutableStateFlow(0L)
every { mapPrefs.lastHeardTrackFilter } returns MutableStateFlow(0L)
every { packetRepository.getWaypoints() } returns MutableStateFlow(emptyList())
viewModel =
BaseMapViewModel(
@ -53,41 +73,52 @@ class BaseMapViewModelTest {
)
}
@Test
fun testInitialization() = runTest {
setUp()
assertTrue(true, "BaseMapViewModel initialized successfully")
@AfterTest
fun tearDown() {
Dispatchers.resetMain()
}
@Test
fun testMyNodeInfoFlow() = runTest {
setUp()
val myNodeInfo = viewModel.myNodeInfo.value
assertTrue(myNodeInfo == null, "myNodeInfo starts as null")
fun testInitialization() {
assertNotNull(viewModel)
}
@Test
fun testNodesWithPositionStartsEmpty() = runTest {
setUp()
"nodesWithPosition should start empty" shouldBe emptyList<Any>(), viewModel.nodesWithPosition.value
fun testMyNodeInfoFlow() = runTest(testDispatcher) {
viewModel.myNodeInfo.test {
assertEquals(null, awaitItem())
cancelAndIgnoreRemainingEvents()
}
}
@Test
fun testConnectionStateFlow() = runTest {
setUp()
radioController.setConnectionState(org.meshtastic.core.model.ConnectionState.Disconnected)
// isConnected should reflect radioController state
assertTrue(true, "Connection state flow is reactive")
fun testNodesWithPositionStartsEmpty() = runTest(testDispatcher) {
viewModel.nodesWithPosition.test {
assertEquals(emptyList(), awaitItem())
cancelAndIgnoreRemainingEvents()
}
}
@Test
fun testNodeRepositoryIntegration() = runTest {
setUp()
fun testConnectionStateFlow() = runTest(testDispatcher) {
viewModel.isConnected.test {
// Initially reflects radioController state (which is Disconnected in FakeRadioController default)
assertEquals(false, awaitItem())
radioController.setConnectionState(ConnectionState.Connected)
assertEquals(true, awaitItem())
radioController.setConnectionState(ConnectionState.Disconnected)
assertEquals(false, awaitItem())
cancelAndIgnoreRemainingEvents()
}
}
@Test
fun testNodeRepositoryIntegration() = runTest(testDispatcher) {
val testNodes = TestDataFactory.createTestNodes(3)
nodeRepository.setNodes(testNodes)
"Nodes added to repository" shouldBe 3, nodeRepository.nodeDBbyNum.value.size
assertEquals(3, nodeRepository.nodeDBbyNum.value.size)
}
*/
}