From 59febbdb10320541aff704026cc3202ae582ef41 Mon Sep 17 00:00:00 2001 From: "Oleksandr (Sasha) Sochka" Date: Thu, 12 Nov 2026 21:23:51 +0000 Subject: [PATCH 1/2] Automation: a new glucose condition starts at the editor's lowest value A new glucose or temp target condition started with the threshold 0.0. The editor shows 0.0 as an empty field and only clamps it when the field is focused, so a condition saved untouched compared the glucose against 0: "below" never fired and "above" always did, with nothing on the screen to show it. The Java InputBg started at the lowest allowed value (e6ae4f03d56); that line was lost when the class moved to dagger (a3b0c8ce3bf). Start at the lowest value of the units again (54 mg/dL or 3.0 mmol/L), so the editor shows the threshold that is used. Stored rules are read as stored. Rules already saved with 0 keep behaving as before; flagging those is left for a separate change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01D2s3Q2DrsGAkKcHZPfBx4N --- .../triggers/NewGlucoseConditionTest.kt | 48 +++++++++++++++++++ .../plugins/automation/elements/InputBg.kt | 3 ++ 2 files changed, 51 insertions(+) create mode 100644 plugins/automation/src/androidHostTest/kotlin/app/aaps/plugins/automation/triggers/NewGlucoseConditionTest.kt 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..1aa3d0892e37 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 @@ -20,6 +20,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 { From 7e6794080f5360c40fb2de0b96b59d5609c31e1c Mon Sep 17 00:00:00 2001 From: olorinmaia <114103483+olorinmaia@users.noreply.github.com> Date: Mon, 16 Nov 2026 16:02:22 -0700 Subject: [PATCH 2/2] Automation: InputBg drops three fields that nothing reads maxValue, step and decimalFormat were private and only written. The editor takes its range, step and format from the constants and the units. --- .../aaps/plugins/automation/elements/InputBg.kt | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) 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 1aa3d0892e37..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) @@ -31,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 }