diff --git a/plugins/automation/src/androidHostTest/kotlin/app/aaps/plugins/automation/triggers/NewGlucoseConditionTest.kt b/plugins/automation/src/androidHostTest/kotlin/app/aaps/plugins/automation/triggers/NewGlucoseConditionTest.kt new file mode 100644 index 000000000000..dc3477ecf138 --- /dev/null +++ b/plugins/automation/src/androidHostTest/kotlin/app/aaps/plugins/automation/triggers/NewGlucoseConditionTest.kt @@ -0,0 +1,48 @@ +package app.aaps.plugins.automation.triggers + +import app.aaps.core.data.iob.InMemoryGlucoseValue +import app.aaps.core.data.model.GlucoseUnit +import app.aaps.core.data.model.SourceSensor +import app.aaps.core.data.model.TrendArrow +import app.aaps.plugins.automation.elements.Comparator +import app.aaps.plugins.automation.elements.InputBg +import com.google.common.truth.Truth.assertThat +import kotlinx.coroutines.test.runTest +import org.junit.jupiter.api.Test +import org.mockito.kotlin.whenever + +/** + * A new glucose or temp target condition starts at the lowest threshold the editor offers, not at 0.0. + * The editor shows 0.0 as an empty field, and a condition saved untouched compared against 0. + */ +class NewGlucoseConditionTest : TriggerTestBase() { + + @Test + fun `a new condition starts at the lowest threshold of the profile units`() { + whenever(profileFunction.getUnits()).thenReturn(GlucoseUnit.MGDL) + assertThat(TriggerBg(triggerDeps).bg.value).isEqualTo(InputBg.MGDL_MIN) + assertThat(TriggerTempTargetValue(triggerDeps).ttValue.value).isEqualTo(InputBg.MGDL_MIN) + + whenever(profileFunction.getUnits()).thenReturn(GlucoseUnit.MMOL) + assertThat(TriggerBg(triggerDeps).bg.value).isEqualTo(InputBg.MMOL_MIN) + assertThat(TriggerTempTargetValue(triggerDeps).ttValue.value).isEqualTo(InputBg.MMOL_MIN) + } + + @Test + fun `an untouched new above condition does not fire for a low glucose`() = runTest { + whenever(profileFunction.getUnits()).thenReturn(GlucoseUnit.MGDL) + whenever(autosensDataStore.getBucketedDataTableCopy()).thenReturn( + mutableListOf(InMemoryGlucoseValue(value = 40.0, timestamp = now - 1, trendArrow = TrendArrow.FLAT, sourceSensor = SourceSensor.UNKNOWN)) + ) + + // Before, "above" an untouched field compared 40 mg/dL against 0 and fired. + assertThat(TriggerBg(triggerDeps).comparator(Comparator.Compare.IS_GREATER).shouldRun()).isFalse() + } + + @Test + fun `a stored threshold is still read as stored`() { + whenever(profileFunction.getUnits()).thenReturn(GlucoseUnit.MGDL) + val restored = TriggerBg(triggerDeps).fromJSON("""{"bg":0,"comparator":"IS_GREATER","units":"mg/dl"}""") as TriggerBg + assertThat(restored.bg.value).isEqualTo(0.0) + } +} diff --git a/plugins/automation/src/commonMain/kotlin/app/aaps/plugins/automation/elements/InputBg.kt b/plugins/automation/src/commonMain/kotlin/app/aaps/plugins/automation/elements/InputBg.kt index 031c6b367d11..f8c86911eb2b 100644 --- a/plugins/automation/src/commonMain/kotlin/app/aaps/plugins/automation/elements/InputBg.kt +++ b/plugins/automation/src/commonMain/kotlin/app/aaps/plugins/automation/elements/InputBg.kt @@ -1,6 +1,5 @@ package app.aaps.plugins.automation.elements -import app.aaps.core.data.format.NumberFormat import app.aaps.core.data.model.GlucoseUnit import app.aaps.core.interfaces.profile.ProfileFunction @@ -9,9 +8,6 @@ class InputBg(profileFunction: ProfileFunction) { var units = GlucoseUnit.MGDL var value = 0.0 var minValue = 0.0 - private var maxValue = 0.0 - private var step = 0.0 - private var decimalFormat: NumberFormat? = null constructor(profileFunction: ProfileFunction, value: Double, units: GlucoseUnit) : this(profileFunction) { setUnits(units) @@ -20,6 +16,9 @@ class InputBg(profileFunction: ProfileFunction) { init { setUnits(profileFunction.getUnits()) + // Start inside the editor's range. 0.0 shows as an empty field, and a condition saved untouched + // then compared against 0: "below" never fired and "above" always did. + value = minValue } fun setValue(value: Double): InputBg { @@ -28,17 +27,7 @@ class InputBg(profileFunction: ProfileFunction) { } fun setUnits(units: GlucoseUnit): InputBg { - if (units == GlucoseUnit.MMOL) { - minValue = MMOL_MIN - maxValue = MMOL_MAX - step = 0.1 - decimalFormat = NumberFormat.DECIMAL_1 - } else { - minValue = MGDL_MIN - maxValue = MGDL_MAX - step = 1.0 - decimalFormat = NumberFormat.INTEGER - } + minValue = if (units == GlucoseUnit.MMOL) MMOL_MIN else MGDL_MIN this.units = units return this }