Modern Apps git

← Modern-Apps.git

travel: round-trip recents no longer lose the return leg

Commit
4151c77776ece60857b2722a0e461296288fb68d
Author
Vayun Mathur <[email protected]>
When
2026-12-01 05:39 UTC (5 days ago)
Parents
5102f88
A round-trip search builds two-leg slices, but the recent-
search writer hardcoded returnDate = null and parseFirstLeg
discarded everything after the first comma โ€” and the
round-trip entry point (startPartialSearch) never recorded a
recent at all. The reader already knew better: openRecent
rebuilds the two-leg slices only if recent.returnDate is
non-blank, a branch that could never fire. Re-running a
recent therefore searched one-way and silently dropped the
return itinerary; round-trip searches via the picker flow
left no recent at all.

TravelSearchOps (the owning layer) now extracts the second
leg's date into the existing RecentSearch.returnDate column โ€”
no schema change โ€” and startPartialSearch records the recent
like searchFlights does. Three new tests pin the round-trip
recent via startPartialSearch, two-leg searchFlights and the
one-way case. Stored-trip sync, seat/ancillary handling,
money and the airport catalog were audited and cleared.
Verified: :travel:testDevUnitTest 419 tests pass; lint
errors are pre-existing on untouched files.

Diff

travel/src/main/java/com/vayunmathur/travel/platform/TravelSearchOps.kt
diff --git a/travel/src/main/java/com/vayunmathur/travel/platform/TravelSearchOps.kt b/travel/src/main/java/com/vayunmathur/travel/platform/TravelSearchOps.ktindex 18568c22da..c4852d8227 100644--- a/travel/src/main/java/com/vayunmathur/travel/platform/TravelSearchOps.kt+++ b/travel/src/main/java/com/vayunmathur/travel/platform/TravelSearchOps.kt@@ -18,15 +18,26 @@ suspend fun TravelViewModel.autocomplete(query: String): List<PlaceDto> {     return runCatching { AirportCatalog.search(query) }.getOrDefault(emptyList()) } -/** Parsed first-leg details used for the recent-search label. */-private data class FirstLeg(val origin: String, val destination: String, val depart: String)+/** Parsed first-leg details used for the recent-search label, plus the return date of a second leg. */+private data class FirstLeg(+    val origin: String,+    val destination: String,+    val depart: String,+    val returnDate: String? = null,+)  private fun parseFirstLeg(slices: String): FirstLeg {-    val parts = slices.substringBefore(',').split(':')+    val legs = slices.split(',')+    val parts = legs.firstOrNull().orEmpty().split(':')+    // The second leg of a round trip carries the return date; dropping it+    // would re-run the recent as a one-way search (see openRecent, which+    // rebuilds the two-leg slices from returnDate).+    val returnDate = legs.getOrNull(1)?.split(':')?.getOrNull(2)?.ifBlank { null }     return FirstLeg(         origin = parts.getOrNull(0).orEmpty(),         destination = parts.getOrNull(1).orEmpty(),-        depart = parts.getOrNull(2).orEmpty()+        depart = parts.getOrNull(2).orEmpty(),+        returnDate = returnDate,     ) } @@ -38,7 +49,7 @@ private fun TravelViewModel.recordFlightSearch(query: FlightQuery, leg: FirstLeg             origin = leg.origin,             destination = leg.destination,             depart = leg.depart,-            returnDate = null,+            returnDate = leg.returnDate,             adults = query.adults,             cabin = query.cabin,         )@@ -167,6 +178,9 @@ fun TravelViewModel.refreshOffer(offerId: String) {  * on-device (outbound slices first).  */ fun TravelViewModel.startPartialSearch(query: FlightQuery) {+    // Round trips enter through here, not searchFlights: record the recent+    // here too, or a round-trip search leaves no (or a one-way) recent.+    recordFlightSearch(query, parseFirstLeg(query.slices))     partialFlowMutable.value = PartialFlowState(loading = true)     viewModelScope.launch {         val loyalty = loyaltyParam()
travel/src/test/java/com/vayunmathur/travel/platform/TravelRecentSearchTest.kt
diff --git a/travel/src/test/java/com/vayunmathur/travel/platform/TravelRecentSearchTest.kt b/travel/src/test/java/com/vayunmathur/travel/platform/TravelRecentSearchTest.ktnew file mode 100644index 0000000000..afc3bdcd01--- /dev/null+++ b/travel/src/test/java/com/vayunmathur/travel/platform/TravelRecentSearchTest.kt@@ -0,0 +1,109 @@+package com.vayunmathur.travel.platform++import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.travel.data.BookedTrip+import com.vayunmathur.travel.data.Customer+import com.vayunmathur.travel.data.FrequentFlyer+import com.vayunmathur.travel.data.RecentSearch+import com.vayunmathur.travel.data.TravelRepository+import com.vayunmathur.travel.network.TravelRaw+import com.vayunmathur.travel.network.travelTransport+import kotlin.test.AfterTest+import kotlin.test.BeforeTest+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNull+import kotlinx.coroutines.flow.Flow+import kotlinx.coroutines.flow.MutableStateFlow+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config+import org.robolectric.shadows.ShadowLooper++/**+ * Regression: a round-trip search must be recorded as a round-trip recent.+ * The re-run path (`HomePage.openRecent`) rebuilds the two-leg slices only+ * when [RecentSearch.returnDate] is stored; before the fix the writer+ * hardcoded it to null and the round-trip entry point never recorded at all,+ * so the return leg was silently lost on re-run.+ */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class TravelRecentSearchTest {++    private class FakeRepo(ctx: android.content.Context) : TravelRepository(ctx) {+        val inserted = mutableListOf<RecentSearch>()+        override fun observeRecent(limit: Int): Flow<List<RecentSearch>> = MutableStateFlow(emptyList())+        override fun observeBookedTrips(): Flow<List<BookedTrip>> = MutableStateFlow(emptyList())+        override fun observeFrequentFlyers(): Flow<List<FrequentFlyer>> = MutableStateFlow(emptyList())+        override fun observeCustomers(): Flow<List<Customer>> = MutableStateFlow(emptyList())+        override suspend fun getAllFrequentFlyers(): List<FrequentFlyer> = emptyList()+        override suspend fun insertRecent(value: RecentSearch) { inserted += value }+        override suspend fun clearRecent() {}+        override suspend fun trimRecent(keep: Int) {}+    }++    private lateinit var repo: FakeRepo++    private val roundTripOfferJson = """{"offerId":"off_rt","itineraryId":"itin_rt","totalAmount":"400.00","currency":"USD","passengerIds":["pax_1"],"passengers":[{"id":"pax_1","type":"adult"}],"slices":[{"origin":"SFO","destination":"JFK","departureAt":"2027-06-24T07:15:00","arrivalAt":"2027-06-24T15:50:00","segments":[{"carrierIata":"AS","flightNumber":"100","origin":"SFO","destination":"JFK","departureAt":"2027-06-24T07:15:00","arrivalAt":"2027-06-24T15:50:00"}]},{"origin":"JFK","destination":"SFO","departureAt":"2027-06-28T09:00:00","arrivalAt":"2027-06-28T12:30:00","segments":[{"carrierIata":"AS","flightNumber":"101","origin":"JFK","destination":"SFO","departureAt":"2027-06-28T09:00:00","arrivalAt":"2027-06-28T12:30:00"}]}]}"""++    @BeforeTest+    fun open() {+        repo = FakeRepo(ApplicationProvider.getApplicationContext())+        travelTransport = { _, url, _ ->+            if (url.contains("/flights")) {+                TravelRaw(200, """{"offerRequestId":"req_1","offers":[$roundTripOfferJson]}""")+            } else {+                TravelRaw(404, """{"message":"not found"}""")+            }+        }+    }++    @AfterTest fun clear() { travelTransport = null }++    private fun vm(): TravelViewModel {+        val ctx = ApplicationProvider.getApplicationContext<android.content.Context>()+        return TravelViewModel(ctx as android.app.Application, repo)+    }++    private fun idle() { repeat(5) { ShadowLooper.idleMainLooper() } }++    @Test+    fun startPartialSearch_recordsRoundTripRecentWithReturnDate() {+        val v = vm()+        v.startPartialSearch(+            FlightQuery(+                slices = "SFO:JFK:2027-06-24,JFK:SFO:2027-06-28",+                adults = 1,+                isRoundTrip = true,+            )+        )+        idle()+        val recent = repo.inserted.singleOrNull()+        assertEquals("SFO", recent?.origin, "inserted=${repo.inserted}")+        assertEquals("JFK", recent?.destination, "inserted=${repo.inserted}")+        assertEquals("2027-06-24", recent?.depart, "inserted=${repo.inserted}")+        assertEquals("2027-06-28", recent?.returnDate, "inserted=${repo.inserted}")+    }++    @Test+    fun searchFlights_twoLegSlices_recordsReturnDate() {+        val v = vm()+        v.searchFlights(+            FlightQuery(slices = "SFO:JFK:2027-06-24,JFK:SFO:2027-06-28", adults = 1)+        )+        idle()+        val recent = repo.inserted.singleOrNull()+        assertEquals("2027-06-28", recent?.returnDate, "inserted=${repo.inserted}")+    }++    @Test+    fun searchFlights_oneWay_recordsNoReturnDate() {+        val v = vm()+        v.searchFlights(FlightQuery(slices = "SFO:JFK:2027-06-24", adults = 1))+        idle()+        val recent = repo.inserted.singleOrNull()+        assertEquals("2027-06-24", recent?.depart, "inserted=${repo.inserted}")+        assertNull(recent?.returnDate, "inserted=${repo.inserted}")+    }+}