Modern Apps git

← Modern-Apps.git

fooddelivery: delivery instructions typed after pricing now reach the server order

Commit
9a84c47e543e767d094818bd54fd44dbd6126e11
Author
Vayun Mathur <[email protected]>
When
2026-12-01 03:49 UTC (5 days ago)
Parents
c2f24fd
CheckoutPricingEffect builds the only checkout request and
bakes deliveryInstructions into it, but its LaunchedEffect
keys omitted deliveryInstructions. Typing 'Leave at the
gate' after the first pricing pass changed state, changed no
key, and fired no follow-up POST — Place Order then presented
a PaymentIntent for a draft created without the instructions.
The text was persisted in the local draft and never reached
the server.

The key is now in the list, so editing instructions re-runs
the existing 400 ms debounced pass, which updates the same
draft via uuid = lastOrderUuid. The regression test captures
checkout bodies over a fake transport and asserts the
follow-up body carries the instructions and reuses the uuid
(failed pre-fix with exactly one body, instructions absent).
Cart guards, totals, order stages and addresses were audited
and cleared. Verified: :fooddelivery:testDevUnitTest 169
tests pass; lint errors are pre-existing on untouched files.

Diff

fooddelivery/src/main/java/com/vayunmathur/fooddelivery/ui/CheckoutPricingEffect.kt
diff --git a/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/ui/CheckoutPricingEffect.kt b/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/ui/CheckoutPricingEffect.ktindex e2d7ac051e..46d45daf4d 100644--- a/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/ui/CheckoutPricingEffect.kt+++ b/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/ui/CheckoutPricingEffect.kt@@ -35,7 +35,14 @@ fun CheckoutPricingEffect(     onFetchingPricesChange: (Boolean) -> Unit,     onLastOrderUuidChange: (String?) -> Unit, ) {-    LaunchedEffect(isPickup, tipCents, selectedAddress?.id, promoCode, customer?.uuid, selectedDealId) {+    // Every input baked into the request below must be a key: deliveryInstructions+    // is not a pricing input, but this effect is the only place the checkout (and+    // its draft order / PaymentIntent) is created, so without it as a key, typing+    // instructions after the first pricing pass silently never reaches the server.+    LaunchedEffect(+        isPickup, tipCents, selectedAddress?.id, promoCode, customer?.uuid, selectedDealId,+        deliveryInstructions,+    ) {         if (!canFetch || orderSuccess) return@LaunchedEffect         AppInit.awaitReady()         onCheckoutResponseChange(null)
fooddelivery/src/test/java/com/vayunmathur/fooddelivery/ui/CheckoutDeliveryInstructionsTest.kt
diff --git a/fooddelivery/src/test/java/com/vayunmathur/fooddelivery/ui/CheckoutDeliveryInstructionsTest.kt b/fooddelivery/src/test/java/com/vayunmathur/fooddelivery/ui/CheckoutDeliveryInstructionsTest.ktnew file mode 100644index 0000000000..2197762e9c--- /dev/null+++ b/fooddelivery/src/test/java/com/vayunmathur/fooddelivery/ui/CheckoutDeliveryInstructionsTest.kt@@ -0,0 +1,132 @@+package com.vayunmathur.fooddelivery.ui++import android.app.Application+import android.content.Context+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onNodeWithText+import androidx.compose.ui.test.performTextInput+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.fooddelivery.data.AddressStore+import com.vayunmathur.fooddelivery.data.CartItem+import com.vayunmathur.fooddelivery.data.CheckoutDraftStore+import com.vayunmathur.fooddelivery.data.MenuItem+import com.vayunmathur.fooddelivery.data.SavedAddress+import com.vayunmathur.fooddelivery.network.BitesAuth+import com.vayunmathur.fooddelivery.network.BitesCore+import com.vayunmathur.fooddelivery.platform.AppInit+import com.vayunmathur.library.network.SimpleResponse+import java.util.concurrent.CopyOnWriteArrayList+import kotlinx.coroutines.runBlocking+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.assertTrue++/**+ * Regression: delivery instructions typed after the first pricing pass must be+ * sent to the server in a follow-up checkout call (which UPDATEs the same draft+ * order uuid). Before the fix, [CheckoutPricingEffect] did not relaunch when the+ * instructions changed, so Place Order presented the stale PaymentIntent and+ * the instructions were silently lost.+ */+@RunWith(RobolectricTestRunner::class)+@Config(application = Application::class)+class CheckoutDeliveryInstructionsTest {+    @get:Rule val rule = createComposeRule()+    private val ctx: Context get() = ApplicationProvider.getApplicationContext()+    private val realTransport = BitesCore.transport+    private val checkoutBodies = CopyOnWriteArrayList<String>()++    @org.junit.After fun restore() {+        BitesCore.transport = realTransport+        BitesAuth.clearToken()+    }++    private fun installFake() {+        checkoutBodies.clear()+        BitesCore.transport = { url, _, _, body ->+            when {+                url.contains("/checkout") -> {+                    if (body != null) checkoutBodies.add(body)+                    SimpleResponse(+                        200, "",+                        """{"serviceable":{"ok":true},"clientSecret":"pi_test_secret_abc",""" ++                            """"order":{"id":9,"uuid":"u9","foodTotal":1000,"orderTotal":1000}}""",+                        emptyMap(), url,+                    )+                }+                url.contains("/orders/past/all") ->+                    SimpleResponse(200, "", """{"data":[]}""", emptyMap(), url)+                url.contains("/customers/me") -> SimpleResponse(+                    200, "",+                    """{"data":{"id":5,"firstName":"Ann","lastName":"Lee","email":"[email protected]","uuid":"cust-uuid"}}""",+                    emptyMap(), url,+                )+                else -> SimpleResponse(404, "", "{}", emptyMap(), url)+            }+        }+        AppInit.start(ctx)+    }++    private fun awaitTrue(description: String, condition: () -> Boolean) {+        val deadline = System.currentTimeMillis() + 15_000+        var ok = false+        while (System.currentTimeMillis() < deadline) {+            rule.mainClock.autoAdvance = false+            rule.mainClock.advanceTimeBy(600L)+            rule.mainClock.autoAdvance = true+            org.robolectric.Shadows.shadowOf(android.os.Looper.getMainLooper())+                .idleFor(300, java.util.concurrent.TimeUnit.MILLISECONDS)+            rule.waitForIdle()+            ok = condition()+            if (ok) break+            Thread.sleep(50)+        }+        assertTrue(ok, description)+    }++    @Test fun typingDeliveryInstructionsRepricesCheckoutWithThem() { runBlocking {+        installFake()+        CheckoutDraftStore.clear(ctx)+        AddressStore.save(+            ctx,+            SavedAddress(+                id = "home", label = "Home", addressStreet = "1 Main",+                latitude = 37.5, longitude = -122.3, isDefault = true,+            ),+        )+        val items = listOf(+            CartItem(+                menuItem = MenuItem(id = 10, name = "Burger", price = 999),+                quantity = 1, merchantId = 1,+            ),+        )+        rule.setContent { CheckoutScreen(items, {}, {}) }+        // First priced checkout exists (Place Order becomes available).+        awaitTrue("initial checkout request never fired") { checkoutBodies.isNotEmpty() }+        awaitTrue("Place Order never appeared") {+            try {+                rule.onNodeWithText("Place Order", substring = true).fetchSemanticsNode() != null+            } catch (_: AssertionError) { false }+        }+        val bodiesBeforeTyping = checkoutBodies.size++        rule.onNodeWithText("Delivery instructions (optional)")+            .performTextInput("Leave at the gate")++        awaitTrue(+            "typing delivery instructions fired no follow-up checkout; " ++                "bodies before=$bodiesBeforeTyping after=${checkoutBodies.size}: $checkoutBodies",+        ) {+            checkoutBodies.size > bodiesBeforeTyping &&+                checkoutBodies.last().contains("Leave at the gate")+        }+        // The follow-up must UPDATE the priced draft, not create a second order.+        assertTrue(+            checkoutBodies.last().contains("\"uuid\":\"u9\""),+            "follow-up checkout did not reuse the draft order uuid: ${checkoutBodies.last()}",+        )+    } }+}