package com.onthelevel.core.sensors

import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

class SettlingDetectorTest {

    @Test
    fun `settles only after low movement has held for its dwell time`() {
        val detector = SettlingDetector()

        assertTrue(detector.update(0.0, 0.0, 0).isSettling)
        assertTrue(detector.update(0.2, 0.0, 100_000_000).isSettling) // 2°/s: moving
        assertTrue(detector.update(0.2, 0.0, 200_000_000).isSettling)
        assertTrue(detector.update(0.2, 0.0, 600_000_000).isSettling)
        assertFalse(detector.update(0.2, 0.0, 700_000_000).isSettling)
    }

    @Test
    fun `movement re-enters settling and resets the dwell`() {
        val detector = SettlingDetector()
        detector.update(0.0, 0.0, 0)
        detector.update(0.0, 0.0, 100_000_000)
        detector.update(0.0, 0.0, 600_000_000)
        assertFalse(detector.update(0.0, 0.0, 700_000_000).isSettling)

        assertTrue(detector.update(0.2, 0.0, 800_000_000).isSettling)
        assertTrue(detector.update(0.2, 0.0, 1_200_000_000).isSettling)
    }

    @Test
    fun `mid band movement resets the quiet dwell without leaving settling`() {
        val detector = SettlingDetector()

        detector.update(0.0, 0.0, 0)
        detector.update(0.0, 0.0, 100_000_000) // quiet dwell starts
        detector.update(0.08, 0.0, 300_000_000) // 0.4°/s: between exit and enter
        assertTrue(detector.update(0.08, 0.0, 600_000_000).isSettling)
        assertTrue(detector.update(0.08, 0.0, 1_000_000_000).isSettling)
        assertFalse(detector.update(0.08, 0.0, 1_100_000_000).isSettling)
    }
}
