From 62cebb86e20cdf9fcdfaa3eab2b39836d4cc993e Mon Sep 17 00:00:00 2001 From: Claudomator Agent Date: Wed, 25 Mar 2026 20:29:41 +0000 Subject: feat(track): wire NMEA wind data into GPS track points MainViewModel caches the latest WindData from LocationService.nmeaWindDataFlow via updateWind(). addGpsPoint() populates TrackPoint wind fields from the cache, defaulting to zero if no NMEA wind sentence has arrived yet. MainActivity.observeDataSources() feeds LocationService.nmeaWindDataFlow into viewModel.updateWind() alongside the existing GPS and anchor observers. 3 new unit tests in MainViewModelWindTest verify zero-default, wind capture, and mid-track wind update behaviour. Co-Authored-By: Claude Sonnet 4.6 --- .../executor/testing/InstantTaskExecutorRule.kt | 13 +++++ .../org/terst/nav/ui/MainViewModelWindTest.kt | 58 ++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 android-app/app/src/test/kotlin/androidx/arch/core/executor/testing/InstantTaskExecutorRule.kt create mode 100644 android-app/app/src/test/kotlin/org/terst/nav/ui/MainViewModelWindTest.kt (limited to 'android-app/app/src/test/kotlin') diff --git a/android-app/app/src/test/kotlin/androidx/arch/core/executor/testing/InstantTaskExecutorRule.kt b/android-app/app/src/test/kotlin/androidx/arch/core/executor/testing/InstantTaskExecutorRule.kt new file mode 100644 index 0000000..96bf63a --- /dev/null +++ b/android-app/app/src/test/kotlin/androidx/arch/core/executor/testing/InstantTaskExecutorRule.kt @@ -0,0 +1,13 @@ +package androidx.arch.core.executor.testing + +import org.junit.rules.TestRule +import org.junit.runner.Description +import org.junit.runners.model.Statement + +/** + * No-op stub: provides the InstantTaskExecutorRule type so test files compile + * on JVM. The real rule is only needed for LiveData; these tests use StateFlow. + */ +class InstantTaskExecutorRule : TestRule { + override fun apply(base: Statement, description: Description): Statement = base +} diff --git a/android-app/app/src/test/kotlin/org/terst/nav/ui/MainViewModelWindTest.kt b/android-app/app/src/test/kotlin/org/terst/nav/ui/MainViewModelWindTest.kt new file mode 100644 index 0000000..8e7125c --- /dev/null +++ b/android-app/app/src/test/kotlin/org/terst/nav/ui/MainViewModelWindTest.kt @@ -0,0 +1,58 @@ +package org.terst.nav.ui + +import androidx.arch.core.executor.testing.InstantTaskExecutorRule +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.UnconfinedTestDispatcher +import kotlinx.coroutines.test.resetMain +import kotlinx.coroutines.test.runTest +import kotlinx.coroutines.test.setMain +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.terst.nav.sensors.WindData +import org.terst.nav.ui.MainViewModel + +@OptIn(ExperimentalCoroutinesApi::class) +class MainViewModelWindTest { + + @get:Rule val instantTask = InstantTaskExecutorRule() + private val dispatcher = UnconfinedTestDispatcher() + + @Before fun setUp() { Dispatchers.setMain(dispatcher) } + @After fun tearDown() { Dispatchers.resetMain() } + + @Test fun `addGpsPoint uses zero wind when no wind update received`() = runTest { + val vm = MainViewModel() + vm.startTrack() + vm.addGpsPoint(37.0, -122.0, 5.0, 90.0) + val pt = vm.trackPoints.value.first() + assertEquals(0.0, pt.windSpeedKnots, 0.001) + assertEquals(0.0, pt.windAngleDeg, 0.001) + assertEquals(false, pt.isTrueWind) + } + + @Test fun `addGpsPoint uses latest wind data after updateWind`() = runTest { + val vm = MainViewModel() + vm.updateWind(WindData(windAngle = 45.0, windSpeed = 15.5, isTrueWind = true, timestampMs = 1000L)) + vm.startTrack() + vm.addGpsPoint(37.0, -122.0, 5.0, 90.0) + val pt = vm.trackPoints.value.first() + assertEquals(15.5, pt.windSpeedKnots, 0.001) + assertEquals(45.0, pt.windAngleDeg, 0.001) + assertEquals(true, pt.isTrueWind) + } + + @Test fun `addGpsPoint reflects wind update between fixes`() = runTest { + val vm = MainViewModel() + vm.startTrack() + vm.addGpsPoint(37.0, -122.0, 5.0, 90.0) + vm.updateWind(WindData(windAngle = 180.0, windSpeed = 8.0, isTrueWind = false, timestampMs = 2000L)) + vm.addGpsPoint(37.1, -122.0, 5.0, 90.0) + val pts = vm.trackPoints.value + assertEquals(0.0, pts[0].windSpeedKnots, 0.001) + assertEquals(8.0, pts[1].windSpeedKnots, 0.001) + } +} -- cgit v1.2.3