|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 Meshtastic LLC |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | +package org.meshtastic.core.data.repository |
| 18 | + |
| 19 | +import dev.mokkery.MockMode |
| 20 | +import dev.mokkery.answering.returns |
| 21 | +import dev.mokkery.every |
| 22 | +import dev.mokkery.mock |
| 23 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 24 | +import kotlinx.coroutines.flow.first |
| 25 | +import kotlinx.coroutines.test.UnconfinedTestDispatcher |
| 26 | +import kotlinx.coroutines.test.advanceUntilIdle |
| 27 | +import kotlinx.coroutines.test.runTest |
| 28 | +import okio.ByteString.Companion.toByteString |
| 29 | +import org.meshtastic.core.data.datasource.NodeInfoReadDataSource |
| 30 | +import org.meshtastic.core.data.manager.MeshMessageProcessorImpl |
| 31 | +import org.meshtastic.core.database.entity.MyNodeEntity |
| 32 | +import org.meshtastic.core.di.CoroutineDispatchers |
| 33 | +import org.meshtastic.core.model.MeshLog |
| 34 | +import org.meshtastic.core.repository.FromRadioPacketHandler |
| 35 | +import org.meshtastic.core.repository.MeshDataHandler |
| 36 | +import org.meshtastic.core.repository.NodeManager |
| 37 | +import org.meshtastic.core.repository.ServiceStateWriter |
| 38 | +import org.meshtastic.core.testing.FakeDatabaseProvider |
| 39 | +import org.meshtastic.core.testing.FakeMeshLogPrefs |
| 40 | +import org.meshtastic.proto.AirQualityMetrics |
| 41 | +import org.meshtastic.proto.Data |
| 42 | +import org.meshtastic.proto.FromRadio |
| 43 | +import org.meshtastic.proto.MeshPacket |
| 44 | +import org.meshtastic.proto.PortNum |
| 45 | +import org.meshtastic.proto.Telemetry |
| 46 | +import kotlin.test.AfterTest |
| 47 | +import kotlin.test.Test |
| 48 | +import kotlin.test.assertEquals |
| 49 | +import kotlin.test.assertTrue |
| 50 | + |
| 51 | +/** |
| 52 | + * Repro for the field report: a node's air-quality telemetry shows up in the in-app Debug log but never appears in the |
| 53 | + * Air Quality chart (PR #5701). |
| 54 | + * |
| 55 | + * Uses Brian's real packet: a TRANSPORT_INTERNAL TELEMETRY_APP packet from the locally-connected node (num [localNum]) |
| 56 | + * carrying non-zero PM (pm10_standard=1, pm25_standard=2, pm100_standard=2). |
| 57 | + * |
| 58 | + * The chart reads [MeshLogRepositoryImpl.getTelemetryFrom], which resolves the viewed node through `effectiveLogId` to |
| 59 | + * [MeshLog.NODE_NUM_LOCAL] and filters `WHERE from_num = :fromNum`. The Debug screen reads the unfiltered |
| 60 | + * `getAllLogsUnbounded`. So the chart is sensitive to the stored `from_num` column; the Debug log is not. |
| 61 | + * |
| 62 | + * `MeshMessageProcessorImpl.processReceivedMeshPacket` stores `fromNum = if (packet.from == myNodeNum) NODE_NUM_LOCAL |
| 63 | + * else packet.from`, where the insert-time `myNodeNum` is `nodeManager.myNodeNum.value` read at packet arrival |
| 64 | + * (MeshServiceOrchestrator). That StateFlow starts null and is only set once MyNodeInfo is processed, so a local packet |
| 65 | + * that arrives during the null window is stored under its raw `from_num` and orphaned from the chart while still |
| 66 | + * visible in the Debug log. |
| 67 | + */ |
| 68 | +class AirQualityChartReproTest { |
| 69 | + |
| 70 | + private lateinit var dbProvider: FakeDatabaseProvider |
| 71 | + private lateinit var meshLogPrefs: FakeMeshLogPrefs |
| 72 | + private lateinit var nodeInfoReadDataSource: NodeInfoReadDataSource |
| 73 | + private val testDispatcher = UnconfinedTestDispatcher() |
| 74 | + private val dispatchers = CoroutineDispatchers(main = testDispatcher, io = testDispatcher, default = testDispatcher) |
| 75 | + private lateinit var repository: MeshLogRepositoryImpl |
| 76 | + |
| 77 | + private val nowMillis = 1_000_000_000L |
| 78 | + |
| 79 | + /** Brian's connected node number, taken verbatim from his Debug log (`from=-93009324`). */ |
| 80 | + private val localNum = -93009324 |
| 81 | + |
| 82 | + private fun setup(myNodeNum: Int?) { |
| 83 | + dbProvider = FakeDatabaseProvider() |
| 84 | + meshLogPrefs = FakeMeshLogPrefs().apply { setLoggingEnabled(true) } |
| 85 | + nodeInfoReadDataSource = mock(MockMode.autofill) |
| 86 | + every { nodeInfoReadDataSource.myNodeInfoFlow() } returns MutableStateFlow(myNodeNum?.let(::myNodeEntity)) |
| 87 | + repository = MeshLogRepositoryImpl(dbProvider, dispatchers, meshLogPrefs, nodeInfoReadDataSource) |
| 88 | + } |
| 89 | + |
| 90 | + @AfterTest |
| 91 | + fun tearDown() { |
| 92 | + if (::dbProvider.isInitialized) dbProvider.close() |
| 93 | + } |
| 94 | + |
| 95 | + private fun myNodeEntity(num: Int) = MyNodeEntity( |
| 96 | + myNodeNum = num, |
| 97 | + model = "model", |
| 98 | + firmwareVersion = "1.0", |
| 99 | + couldUpdate = false, |
| 100 | + shouldUpdate = false, |
| 101 | + currentPacketId = 0L, |
| 102 | + messageTimeoutMsec = 0, |
| 103 | + minAppVersion = 0, |
| 104 | + maxChannels = 0, |
| 105 | + hasWifi = false, |
| 106 | + ) |
| 107 | + |
| 108 | + /** Brian's real reading: pm10_standard=1, pm25_standard=2, pm100_standard=2. */ |
| 109 | + private fun brianAirQualityTelemetry() = Telemetry( |
| 110 | + air_quality_metrics = |
| 111 | + AirQualityMetrics( |
| 112 | + pm10_standard = 1, |
| 113 | + pm25_standard = 2, |
| 114 | + pm100_standard = 2, |
| 115 | + pm10_environmental = 1, |
| 116 | + pm25_environmental = 2, |
| 117 | + pm100_environmental = 2, |
| 118 | + ), |
| 119 | + ) |
| 120 | + |
| 121 | + private fun airQualityPacket() = MeshPacket( |
| 122 | + from = localNum, |
| 123 | + rx_time = 1_700_000_000, |
| 124 | + decoded = |
| 125 | + Data(payload = brianAirQualityTelemetry().encode().toByteString(), portnum = PortNum.TELEMETRY_APP), |
| 126 | + ) |
| 127 | + |
| 128 | + private fun airQualityLog(fromNum: Int) = MeshLog( |
| 129 | + uuid = "aq-$fromNum", |
| 130 | + message_type = "Packet", |
| 131 | + received_date = nowMillis, |
| 132 | + raw_message = "", |
| 133 | + fromNum = fromNum, |
| 134 | + portNum = PortNum.TELEMETRY_APP.value, |
| 135 | + fromRadio = FromRadio(packet = airQualityPacket()), |
| 136 | + ) |
| 137 | + |
| 138 | + /** Checkpoint 1: the parse + query round-trip preserves the air-quality payload (rules out content loss). */ |
| 139 | + @Test |
| 140 | + fun `checkpoint 1 - air quality payload survives the telemetry round-trip`() = runTest(testDispatcher) { |
| 141 | + setup(myNodeNum = null) // effectiveLogId(0) -> 0 |
| 142 | + repository.insert(airQualityLog(fromNum = 0)) |
| 143 | + |
| 144 | + val result = repository.getTelemetryFrom(0).first() |
| 145 | + |
| 146 | + assertEquals(1, result.size, "the air-quality telemetry row should round-trip") |
| 147 | + assertEquals(2, result[0].air_quality_metrics?.pm25_standard, "pm25_standard must survive decode") |
| 148 | + } |
| 149 | + |
| 150 | + /** Checkpoint 2: local-node air-quality stored under NODE_NUM_LOCAL is returned to the chart (the happy path). */ |
| 151 | + @Test |
| 152 | + fun `checkpoint 2 - local AQ stored under NODE_NUM_LOCAL is charted`() = runTest(testDispatcher) { |
| 153 | + setup(myNodeNum = localNum) // viewing the local node -> effectiveLogId -> NODE_NUM_LOCAL |
| 154 | + // myNodeNum known at insert -> processReceivedMeshPacket would store NODE_NUM_LOCAL. |
| 155 | + repository.insert(airQualityLog(fromNum = MeshLog.NODE_NUM_LOCAL)) |
| 156 | + |
| 157 | + val result = repository.getTelemetryFrom(localNum).first() |
| 158 | + |
| 159 | + assertEquals(1, result.size, "AQ stored under NODE_NUM_LOCAL should be visible to the local node's chart") |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Checkpoint 3 (query invariant — the rationale for the insert-side fix): the per-node chart query keys the local |
| 164 | + * node on NODE_NUM_LOCAL, so a row stored under the raw myNodeNum is not returned (though the unfiltered Debug log |
| 165 | + * still shows it). This is *why* the insert path must key local packets under NODE_NUM_LOCAL — verified in |
| 166 | + * checkpoint 4. This query behavior is intentional and unchanged by the fix. |
| 167 | + */ |
| 168 | + @Test |
| 169 | + fun `checkpoint 3 - query keys local node on NODE_NUM_LOCAL not the raw from_num`() = runTest(testDispatcher) { |
| 170 | + setup(myNodeNum = localNum) // viewing the local node -> effectiveLogId -> NODE_NUM_LOCAL |
| 171 | + repository.insert(airQualityLog(fromNum = localNum)) // a hypothetical mis-keyed row |
| 172 | + |
| 173 | + // Debug screen (unfiltered) sees it: |
| 174 | + assertEquals( |
| 175 | + 1, |
| 176 | + repository.getAllLogsUnbounded().first().size, |
| 177 | + "Debug log shows rows regardless of from_num", |
| 178 | + ) |
| 179 | + |
| 180 | + // Chart query (from_num = NODE_NUM_LOCAL) does not — hence the insert must never produce a raw-keyed local |
| 181 | + // row: |
| 182 | + val charted = repository.getTelemetryFrom(localNum).first() |
| 183 | + assertTrue(charted.isEmpty(), "the local-node query only matches NODE_NUM_LOCAL") |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Checkpoint 4 (regression test for the fix, end-to-end through the real insert path): a local air-quality packet |
| 188 | + * received while myNodeNum is still null is BUFFERED (not stored under its raw from_num). Once myNodeNum resolves, |
| 189 | + * the buffer flushes and the packet is stored under NODE_NUM_LOCAL, so the local node's Air Quality chart sees it. |
| 190 | + * |
| 191 | + * Before the fix this packet was stored immediately under its raw from_num and orphaned from the chart. |
| 192 | + */ |
| 193 | + @Test |
| 194 | + fun `checkpoint 4 - local AQ received before myNodeNum resolves is buffered then charted`() = |
| 195 | + runTest(testDispatcher) { |
| 196 | + setup(myNodeNum = localNum) // query side: phone is connected to localNum |
| 197 | + |
| 198 | + val myNodeNumFlow = MutableStateFlow<Int?>(null) // not yet resolved |
| 199 | + val nodeManager = mock<NodeManager>(MockMode.autofill) |
| 200 | + every { nodeManager.isNodeDbReady } returns MutableStateFlow(true) |
| 201 | + every { nodeManager.myNodeNum } returns myNodeNumFlow |
| 202 | + |
| 203 | + val processor = |
| 204 | + MeshMessageProcessorImpl( |
| 205 | + nodeManager = nodeManager, |
| 206 | + serviceStateWriter = mock<ServiceStateWriter>(MockMode.autofill), |
| 207 | + meshLogRepository = lazy { repository }, |
| 208 | + dataHandler = lazy { mock<MeshDataHandler>(MockMode.autofill) }, |
| 209 | + fromRadioDispatcher = mock<FromRadioPacketHandler>(MockMode.autofill), |
| 210 | + scope = backgroundScope, |
| 211 | + ) |
| 212 | + |
| 213 | + // Arrives before MyNodeInfo resolves -> buffered, NOT written to the log table (so not orphaned in the DB). |
| 214 | + processor.handleReceivedMeshPacket(airQualityPacket(), myNodeNum = null) |
| 215 | + advanceUntilIdle() |
| 216 | + assertEquals(0, repository.getAllLogsUnbounded().first().size, "packet should be buffered, not yet stored") |
| 217 | + |
| 218 | + // MyNodeInfo resolves -> the buffer flushes and the packet is stored under NODE_NUM_LOCAL. |
| 219 | + myNodeNumFlow.value = localNum |
| 220 | + advanceUntilIdle() |
| 221 | + |
| 222 | + val charted = repository.getTelemetryFrom(localNum).first() |
| 223 | + assertEquals(1, charted.size, "after myNodeNum resolves, the local AQ packet must reach the chart") |
| 224 | + assertEquals( |
| 225 | + 2, |
| 226 | + charted[0].air_quality_metrics?.pm25_standard, |
| 227 | + "the real reading (pm25_standard=2) survives", |
| 228 | + ) |
| 229 | + } |
| 230 | +} |
0 commit comments