summaryrefslogtreecommitdiff
path: root/android-app/app/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'android-app/app/src/test')
-rw-r--r--android-app/app/src/test/kotlin/com/example/androidapp/gps/LocationServiceTest.kt117
1 files changed, 117 insertions, 0 deletions
diff --git a/android-app/app/src/test/kotlin/com/example/androidapp/gps/LocationServiceTest.kt b/android-app/app/src/test/kotlin/com/example/androidapp/gps/LocationServiceTest.kt
new file mode 100644
index 0000000..d9192c6
--- /dev/null
+++ b/android-app/app/src/test/kotlin/com/example/androidapp/gps/LocationServiceTest.kt
@@ -0,0 +1,117 @@
+package com.example.androidapp.gps
+
+import com.example.androidapp.data.model.SensorData
+import kotlinx.coroutines.flow.first
+import kotlinx.coroutines.runBlocking
+import org.junit.Assert.*
+import org.junit.Test
+
+class LocationServiceTest {
+
+ private fun service() = LocationService()
+
+ // ── snapshot with no data ─────────────────────────────────────────────────
+
+ @Test
+ fun snapshot_noData_allFieldsNull() {
+ val snap = service().snapshot()
+ assertNull(snap.windSpeedKt)
+ assertNull(snap.windDirectionDeg)
+ assertNull(snap.currentSpeedKt)
+ assertNull(snap.currentDirectionDeg)
+ }
+
+ // ── true-wind resolution ──────────────────────────────────────────────────
+
+ @Test
+ fun updateSensorData_withFullReading_resolvesTrueWind() = runBlocking {
+ val svc = service()
+ // Head north (hdg = 0°), AWS = 10 kt coming from ahead (AWA = 0°), BSP = 5 kt
+ // → TW comes FROM ahead at 5 kt
+ svc.updateSensorData(
+ SensorData(
+ headingTrueDeg = 0.0,
+ apparentWindSpeedKt = 10.0,
+ apparentWindAngleDeg = 0.0,
+ speedOverGroundKt = 5.0
+ )
+ )
+ val tw = svc.latestTrueWind.first()
+ assertNotNull(tw)
+ assertTrue("Expected TWS > 0", tw!!.speedKt > 0.0)
+ }
+
+ @Test
+ fun updateSensorData_missingHeading_doesNotResolveTrueWind() = runBlocking {
+ val svc = service()
+ svc.updateSensorData(
+ SensorData(
+ apparentWindSpeedKt = 10.0,
+ apparentWindAngleDeg = 45.0,
+ speedOverGroundKt = 5.0
+ // headingTrueDeg omitted
+ )
+ )
+ assertNull(svc.latestTrueWind.first())
+ }
+
+ // ── current conditions ────────────────────────────────────────────────────
+
+ @Test
+ fun updateCurrentConditions_reflectedInSnapshot() {
+ val svc = service()
+ svc.updateCurrentConditions(speedKt = 1.5, directionDeg = 135.0)
+
+ val snap = svc.snapshot()
+ assertEquals(1.5, snap.currentSpeedKt!!, 0.001)
+ assertEquals(135.0, snap.currentDirectionDeg!!, 0.001)
+ }
+
+ @Test
+ fun updateCurrentConditions_nullClears() {
+ val svc = service()
+ svc.updateCurrentConditions(speedKt = 2.0, directionDeg = 90.0)
+ svc.updateCurrentConditions(speedKt = null, directionDeg = null)
+
+ val snap = svc.snapshot()
+ assertNull(snap.currentSpeedKt)
+ assertNull(snap.currentDirectionDeg)
+ }
+
+ // ── combined snapshot ─────────────────────────────────────────────────────
+
+ @Test
+ fun snapshot_afterFullUpdate_populatesAllFields() = runBlocking {
+ val svc = service()
+
+ // Head east (hdg = 90°), wind from starboard bow, BSP proxy = 6 kt
+ svc.updateSensorData(
+ SensorData(
+ headingTrueDeg = 90.0,
+ apparentWindSpeedKt = 12.0,
+ apparentWindAngleDeg = 45.0,
+ speedOverGroundKt = 6.0
+ )
+ )
+ svc.updateCurrentConditions(speedKt = 0.8, directionDeg = 270.0)
+
+ val snap = svc.snapshot()
+ assertNotNull(snap.windSpeedKt)
+ assertNotNull(snap.windDirectionDeg)
+ assertEquals(0.8, snap.currentSpeedKt!!, 0.001)
+ assertEquals(270.0, snap.currentDirectionDeg!!, 0.001)
+ }
+
+ // ── latestSensor flow ─────────────────────────────────────────────────────
+
+ @Test
+ fun updateSensorData_updatesLatestSensorFlow() = runBlocking {
+ val svc = service()
+ assertNull(svc.latestSensor.first())
+
+ val data = SensorData(latitude = 41.5, longitude = -71.3)
+ svc.updateSensorData(data)
+
+ assertEquals(data, svc.latestSensor.first())
+ }
+}