fooddelivery: a required modifier group needs a selection even when the server sends min 0
- Commit
9ef82676aed1cf3b24e208fbc5897e7905addd78- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 15:47 UTC (4 days ago)
- Parents
e12df9d
requiredMet was !required || count >= minSelections, vacuously true for required=true, minSelections=0 (the model default): the dialog showed 'Required' beside the group yet enabled Add with zero selections, producing an empty selection for a required group. The assistant-side cart validation already encodes the intended rule (effectiveMin = max(minSelections, 1) for required groups); the dialog disagreed with it. Required groups now need count >= max(minSelections, 1); optional groups may stay empty but a partial pick must still reach their minimum. Well-formed payloads behave as before. Regression test drives the exact Burger fixture: Add disabled at .99, enabled at 2.99 after picking Large, confirming modifier 100 in group 1 (failed pre-fix). Totals, order state and address validation were audited and cleared. Verified: :fooddelivery:testDevUnitTest 172 tests pass; lint errors are pre-existing on untouched files.
Diff
fooddelivery/src/main/java/com/vayunmathur/fooddelivery/ui/ModifierDialog.kt
diff --git a/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/ui/ModifierDialog.kt b/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/ui/ModifierDialog.ktindex fcd525cdc4..2675fc2500 100644--- a/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/ui/ModifierDialog.kt+++ b/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/ui/ModifierDialog.kt@@ -69,8 +69,17 @@ fun ModifierDialog( val extrasTotal = allModifiers.sumOf { it.priceDollars } val totalPrice = item.priceDollars + extrasTotal + // A required group needs at least one pick even when the server sends+ // minSelections = 0 for it (the model default) — same rule the assistant-side+ // cart validation applies. An optional group may be left empty, but a partial+ // pick still has to reach its minimum. val requiredMet = item.modifierGroups.all { group ->- !group.required || (selections[group.id]?.size ?: 0) >= group.minSelections+ val count = selections[group.id]?.size ?: 0+ if (group.required) {+ count >= maxOf(group.minSelections, 1)+ } else {+ count == 0 || count >= group.minSelections+ } } AlertDialog(
fooddelivery/src/test/java/com/vayunmathur/fooddelivery/ui/ModifierRequiredValidationTest.kt
diff --git a/fooddelivery/src/test/java/com/vayunmathur/fooddelivery/ui/ModifierRequiredValidationTest.kt b/fooddelivery/src/test/java/com/vayunmathur/fooddelivery/ui/ModifierRequiredValidationTest.ktnew file mode 100644index 0000000000..7a9d8b1d28--- /dev/null+++ b/fooddelivery/src/test/java/com/vayunmathur/fooddelivery/ui/ModifierRequiredValidationTest.kt@@ -0,0 +1,67 @@+package com.vayunmathur.fooddelivery.ui++import android.app.Application+import androidx.compose.ui.test.assertIsEnabled+import androidx.compose.ui.test.assertIsNotEnabled+import androidx.compose.ui.test.isSelectable+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onNodeWithText+import androidx.compose.ui.test.performClick+import com.vayunmathur.fooddelivery.data.MenuItem+import com.vayunmathur.fooddelivery.data.Modifier+import com.vayunmathur.fooddelivery.data.ModifierGroup+import com.vayunmathur.fooddelivery.data.SelectedModifier+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config+import kotlin.test.Test+import kotlin.test.assertEquals++/**+ * Regression: a required modifier group must block confirming the item even+ * when the server sends minSelections = 0 for it (the model default). The+ * assistant-side cart validation already treats required as at least one+ * selection (see FoodAssistantBackend.selectModifiers); the dialog allowed+ * zero, so a $9.99 Burger could be added with no pick from its Required+ * "Size" group.+ */+@RunWith(RobolectricTestRunner::class)+@Config(application = Application::class)+class ModifierRequiredValidationTest {+ @get:Rule val rule = createComposeRule()++ private fun burgerWithRequiredSize(minSelections: Int) = MenuItem(+ id = 10,+ name = "Burger",+ price = 999,+ modifierGroups = listOf(+ ModifierGroup(+ id = 1,+ name = "Size",+ required = true,+ minSelections = minSelections,+ maxSelections = 1,+ modifiers = listOf(Modifier(id = 100, name = "Large", price = 300)),+ ),+ ),+ )++ @Test fun requiredGroupWithZeroMinStillNeedsOneSelection() {+ var confirmed: List<SelectedModifier>? = null+ rule.setContent {+ ModifierDialog(+ item = burgerWithRequiredSize(minSelections = 0),+ onDismiss = {},+ onConfirm = { confirmed = it },+ )+ }+ // No selection yet: "Add $9.99" must be disabled despite minSelections = 0.+ rule.onNodeWithText("Add $9.99").assertIsNotEnabled()+ rule.onNode(isSelectable()).performClick()+ rule.onNodeWithText("Add $12.99").assertIsEnabled()+ rule.onNodeWithText("Add $12.99").performClick()+ assertEquals(listOf(100), confirmed?.map { it.modifierId })+ assertEquals(listOf(1), confirmed?.map { it.modifierGroupId })+ }+}