Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / shop / src / test / java / com / vayunmathur / shop / domain / NativeCheckoutStateTest.kt
NativeCheckoutStateTest.kt233 lines · 12.7 KiBRaw
| 1 | package com.vayunmathur.shop.domain |
| 2 | |
| 3 | import com.vayunmathur.shop.data.CartLine |
| 4 | import com.vayunmathur.shop.data.CheckoutInputs |
| 5 | import com.vayunmathur.shop.data.CheckoutPhase |
| 6 | import com.vayunmathur.shop.data.NativeCheckoutState |
| 7 | import com.vayunmathur.shop.data.PlacedOrder |
| 8 | import com.vayunmathur.shop.data.SubmittedLine |
| 9 | import com.vayunmathur.shop.data.UcpCheckout |
| 10 | import com.vayunmathur.shop.data.UcpError |
| 11 | import kotlinx.coroutines.CompletableDeferred |
| 12 | import kotlinx.coroutines.CoroutineStart |
| 13 | import kotlinx.coroutines.CoroutineScope |
| 14 | import kotlinx.coroutines.cancelAndJoin |
| 15 | import kotlinx.coroutines.launch |
| 16 | import kotlinx.coroutines.runBlocking |
| 17 | import kotlinx.serialization.encodeToString |
| 18 | import kotlinx.serialization.json.Json |
| 19 | import kotlin.test.Test |
| 20 | import kotlin.test.assertEquals |
| 21 | import kotlin.test.assertFalse |
| 22 | import kotlin.test.assertNotNull |
| 23 | import kotlin.test.assertNull |
| 24 | import kotlin.test.assertTrue |
| 25 | |
| 26 | class NativeCheckoutStateTest { |
| 27 | private class FakeGateway : NativeCheckoutGateway { |
| 28 | val intents = mutableListOf<String>() |
| 29 | val sentInputs = mutableListOf<CheckoutInputs>() |
| 30 | var reads = 0 |
| 31 | var response: Result<UcpCheckout> = Result.success(NativeCheckoutFixtures.checkout()) |
| 32 | var gate: CompletableDeferred<Unit>? = null |
| 33 | override suspend fun create(shop: String, lines: List<CartLine>, inputs: CheckoutInputs, intent: String): Result<UcpCheckout> { |
| 34 | intents += intent; sentInputs += inputs |
| 35 | gate?.await() |
| 36 | return response |
| 37 | } |
| 38 | override suspend fun update(shop: String, sessionId: String, lines: List<CartLine>, inputs: CheckoutInputs, intent: String): Result<UcpCheckout> { |
| 39 | intents += intent; sentInputs += inputs |
| 40 | gate?.await() |
| 41 | return response |
| 42 | } |
| 43 | override suspend fun get(shop: String, sessionId: String): Result<UcpCheckout> { reads++; return Result.success(NativeCheckoutFixtures.checkout(status = "ready_for_complete")) } |
| 44 | } |
| 45 | private val lines = NativeCheckoutFixtures.lines |
| 46 | private val stored = mutableListOf<PlacedOrder>() |
| 47 | private val sink = PendingCheckoutSink { stored += it } |
| 48 | private fun coordinator(gateway: FakeGateway, clock: () -> Long = { 1793034388000L }): NativeCheckoutCoordinator { |
| 49 | var intent = 0 |
| 50 | return NativeCheckoutCoordinator(gateway, now = clock, newIntent = { "intent-${++intent}" }) |
| 51 | } |
| 52 | |
| 53 | @Test fun rejectedCreateKeepsMerchantHandoffUrl() = runBlocking { |
| 54 | val url = "https://allbirds.com/checkouts/cn/abc" |
| 55 | val gateway = FakeGateway().apply { |
| 56 | response = Result.failure(com.vayunmathur.shop.network.ShopBackendException(UcpError.Unsupported("Website only", url))) |
| 57 | } |
| 58 | val coordinator = coordinator(gateway) |
| 59 | coordinator.submit("generic", "shop.example", lines, sink) |
| 60 | val state = coordinator.snapshot.single() |
| 61 | assertEquals(CheckoutPhase.BrowserRequired, state.phase) |
| 62 | assertEquals(url, state.handoffUrl) |
| 63 | assertEquals("Website only", state.message) |
| 64 | } |
| 65 | |
| 66 | @Test fun duplicateTapsAndUnchangedSessionReuseNeverDuplicateMutation() = runBlocking { |
| 67 | val gateway = FakeGateway().apply { gate = CompletableDeferred() } |
| 68 | val coordinator = coordinator(gateway) |
| 69 | val first = launch(start = CoroutineStart.UNDISPATCHED) { coordinator.submit("generic", "shop.example", lines, sink) } |
| 70 | assertEquals(CheckoutPhase.Preparing, coordinator.snapshot.single().phase) |
| 71 | coordinator.submit("generic", "shop.example", lines, sink) |
| 72 | assertEquals(listOf("intent-1"), gateway.intents) |
| 73 | gateway.gate!!.complete(Unit); first.join() |
| 74 | coordinator.submit("generic", "shop.example", lines, sink) |
| 75 | assertEquals(listOf("intent-1"), gateway.intents) |
| 76 | assertEquals(CheckoutPhase.Reviewing, coordinator.snapshot.single().phase) |
| 77 | assertEquals("", coordinator.snapshot.single().checkout!!.continueUrl) |
| 78 | assertEquals(lines, coordinator.snapshot.single().submittedLines) |
| 79 | assertEquals("pending", stored.single().status) |
| 80 | assertFalse(coordinator.snapshot.single().completionEnabled) |
| 81 | } |
| 82 | |
| 83 | @Test fun inputEditsCreateNewIntentAndPendingHistoryContainsNoBuyerPii() = runBlocking { |
| 84 | val gateway = FakeGateway() |
| 85 | val coordinator = coordinator(gateway) |
| 86 | val input = CheckoutInputs(email = "[email protected]", firstName = "PrivateFirstName", phone = "+14155550123") |
| 87 | coordinator.edit("generic", "shop.example", input) |
| 88 | coordinator.submit("generic", "shop.example", lines, sink) |
| 89 | coordinator.edit("generic", "shop.example", input.copy(street = "PrivateStreet", city = "Test", region = "CA", country = "US", postalCode = "94043", options = mapOf("merchant-shipping/merchant-group" to "express"))) |
| 90 | coordinator.submit("generic", "shop.example", lines, sink) |
| 91 | assertEquals(listOf("intent-1", "intent-2"), gateway.intents) |
| 92 | assertEquals("PrivateStreet", gateway.sentInputs.last().street) |
| 93 | assertEquals(gateway.sentInputs.last(), coordinator.snapshot.single().submittedInputs) |
| 94 | val history = Json.encodeToString(stored) |
| 95 | assertFalse(history.contains("PrivateFirstName")) |
| 96 | assertFalse(history.contains("PrivateStreet")) |
| 97 | assertFalse(history.contains("[email protected]")) |
| 98 | assertTrue(stored.all { it.status == "pending" }) |
| 99 | assertEquals(lines, coordinator.snapshot.single().submittedLines) |
| 100 | } |
| 101 | |
| 102 | @Test fun uncertainCreateCannotReplayEvenAfterInputChanges() = runBlocking { |
| 103 | val gateway = FakeGateway().apply { response = Result.failure(IllegalStateException("delivery unknown")) } |
| 104 | val coordinator = coordinator(gateway) |
| 105 | coordinator.submit("generic", "shop.example", lines, sink) |
| 106 | assertEquals(CheckoutPhase.Uncertain, coordinator.snapshot.single().phase) |
| 107 | coordinator.edit("generic", "shop.example", CheckoutInputs(email = "[email protected]")) |
| 108 | coordinator.submit("generic", "shop.example", lines, sink) |
| 109 | coordinator.reconcile("generic", "shop.example") |
| 110 | assertEquals(listOf("intent-1"), gateway.intents) |
| 111 | assertEquals(0, gateway.reads) |
| 112 | assertTrue(stored.isEmpty()) |
| 113 | assertFalse(coordinator.snapshot.single().completionEnabled) |
| 114 | } |
| 115 | |
| 116 | @Test fun uncertainUpdateAllowsOnlyGetAndNeverCompletesReadySession() = runBlocking { |
| 117 | val gateway = FakeGateway() |
| 118 | val coordinator = coordinator(gateway) |
| 119 | coordinator.submit("generic", "shop.example", lines, sink) |
| 120 | gateway.response = Result.failure(IllegalStateException("write timeout")) |
| 121 | coordinator.edit("generic", "shop.example", CheckoutInputs(email = "[email protected]")) |
| 122 | coordinator.submit("generic", "shop.example", lines, sink) |
| 123 | coordinator.reconcile("generic", "shop.example") |
| 124 | coordinator.submit("generic", "shop.example", lines, sink) |
| 125 | assertEquals(1, gateway.reads) |
| 126 | assertEquals(listOf("intent-1", "intent-2"), gateway.intents) |
| 127 | assertEquals(CheckoutPhase.Uncertain, coordinator.snapshot.single().phase) |
| 128 | assertEquals("ready_for_complete", coordinator.snapshot.single().checkout!!.status) |
| 129 | assertFalse(coordinator.snapshot.single().completionEnabled) |
| 130 | assertEquals("pending", stored.single().status) |
| 131 | } |
| 132 | |
| 133 | @Test fun staleCartAndExpiryDisableMutationWithoutLosingPendingLines() = runBlocking { |
| 134 | val gateway = FakeGateway() |
| 135 | var clock = 1793034388000L |
| 136 | val coordinator = coordinator(gateway) { clock } |
| 137 | coordinator.submit("generic", "shop.example", lines, sink) |
| 138 | val changed = lines.map { it.copy(quantity = 3) } |
| 139 | coordinator.cartChanged(changed) |
| 140 | coordinator.submit("generic", "shop.example", changed, sink) |
| 141 | assertEquals(CheckoutPhase.BrowserRequired, coordinator.snapshot.single().phase) |
| 142 | assertEquals(lines, stored.single().lines) |
| 143 | assertEquals(listOf("intent-1"), gateway.intents) |
| 144 | clock = 1793145600000L |
| 145 | coordinator.refreshExpiry() |
| 146 | assertEquals(CheckoutPhase.Expired, coordinator.snapshot.single().phase) |
| 147 | coordinator.edit("generic", "shop.example", CheckoutInputs(email = "[email protected]")) |
| 148 | coordinator.submit("generic", "shop.example", lines, sink) |
| 149 | assertEquals(listOf("intent-1"), gateway.intents) |
| 150 | } |
| 151 | |
| 152 | @Test fun oneMerchantFailureDoesNotDiscardAnotherPendingSession() = runBlocking { |
| 153 | val gateway = FakeGateway() |
| 154 | val coordinator = coordinator(gateway) |
| 155 | coordinator.submit("generic", "shop.example", lines, sink) |
| 156 | gateway.response = Result.failure(IllegalStateException("unknown")) |
| 157 | val second = lines.map { it.copy(shopDomain = "other.example") } |
| 158 | coordinator.submit("generic", "other.example", second, sink) |
| 159 | assertEquals(listOf(CheckoutPhase.Reviewing, CheckoutPhase.Uncertain), coordinator.snapshot.map { it.phase }) |
| 160 | assertEquals("shop.example", stored.single().shopDomain) |
| 161 | assertEquals(lines, stored.single().lines) |
| 162 | assertFalse(coordinator.snapshot.any { it.completionEnabled }) |
| 163 | } |
| 164 | |
| 165 | @Test fun browserOnlyRequirementsNeverUpdateAndReadyNeverCompletes() = runBlocking { |
| 166 | val gateway = FakeGateway().apply { response = Result.success(NativeCheckoutFixtures.checkout(status = "ready_for_complete")) } |
| 167 | val coordinator = coordinator(gateway) |
| 168 | coordinator.submit("generic", "shop.example", lines, sink) |
| 169 | assertEquals(CheckoutPhase.Reviewing, coordinator.snapshot.single().phase) |
| 170 | assertFalse(coordinator.snapshot.single().completionEnabled) |
| 171 | val unsupported = FakeGateway().apply { response = Result.success(NativeCheckoutFixtures.checkout(methodType = "custom")) } |
| 172 | val browser = coordinator(unsupported) |
| 173 | browser.submit("generic", "shop.example", lines, sink) |
| 174 | browser.edit("generic", "shop.example", CheckoutInputs(email = "[email protected]")) |
| 175 | browser.submit("generic", "shop.example", lines, sink) |
| 176 | assertEquals(CheckoutPhase.BrowserRequired, browser.snapshot.single().phase) |
| 177 | assertEquals(1, unsupported.intents.size) |
| 178 | } |
| 179 | |
| 180 | @Test fun cancelledMutationRemainsUncertainAndCannotReplay() = runBlocking { |
| 181 | val gateway = FakeGateway().apply { gate = CompletableDeferred() } |
| 182 | val coordinator = coordinator(gateway) |
| 183 | val write = launch(start = CoroutineStart.UNDISPATCHED) { coordinator.submit("generic", "shop.example", lines, sink) } |
| 184 | write.cancelAndJoin() |
| 185 | coordinator.submit("generic", "shop.example", lines, sink) |
| 186 | assertEquals(CheckoutPhase.Uncertain, coordinator.snapshot.single().phase) |
| 187 | assertEquals(listOf("intent-1"), gateway.intents) |
| 188 | assertTrue(stored.isEmpty()) |
| 189 | } |
| 190 | |
| 191 | @Test fun staleMerchantLineItemsAreBrowserOnlyButPendingCartIsPreserved() = runBlocking { |
| 192 | val gateway = FakeGateway().apply { |
| 193 | response = Result.success(NativeCheckoutFixtures.checkout(submitted = listOf(SubmittedLine("stale-variant", 2)))) |
| 194 | } |
| 195 | val coordinator = coordinator(gateway) |
| 196 | coordinator.submit("generic", "shop.example", lines, sink) |
| 197 | assertEquals(CheckoutPhase.BrowserRequired, coordinator.snapshot.single().phase) |
| 198 | assertEquals(lines, stored.single().lines) |
| 199 | assertEquals("pending", stored.single().status) |
| 200 | assertFalse(coordinator.snapshot.single().completionEnabled) |
| 201 | coordinator.edit("generic", "shop.example", CheckoutInputs(email = "[email protected]")) |
| 202 | coordinator.submit("generic", "shop.example", lines, sink) |
| 203 | assertEquals(1, gateway.intents.size) |
| 204 | } |
| 205 | |
| 206 | @Test fun duplicateMerchantLineItemsDoNotMaskMissingVariant() = runBlocking { |
| 207 | val twoLines = listOf( |
| 208 | CartLine(variantId = "variant-1", productTitle = "One", platformId = "generic", shopDomain = "shop.example", quantity = 1), |
| 209 | CartLine(variantId = "variant-2", productTitle = "Two", platformId = "generic", shopDomain = "shop.example", quantity = 1), |
| 210 | ) |
| 211 | val gateway = FakeGateway().apply { |
| 212 | response = Result.success( |
| 213 | NativeCheckoutFixtures.checkout( |
| 214 | submitted = listOf(SubmittedLine("variant-1", 1), SubmittedLine("variant-1", 1)), |
| 215 | ), |
| 216 | ) |
| 217 | } |
| 218 | val coordinator = coordinator(gateway) |
| 219 | coordinator.submit("generic", "shop.example", twoLines, sink) |
| 220 | assertEquals(CheckoutPhase.BrowserRequired, coordinator.snapshot.single().phase) |
| 221 | assertEquals(twoLines, stored.single().lines) |
| 222 | } |
| 223 | |
| 224 | @Test fun invalidInputsNeverReachGateway() = runBlocking { |
| 225 | val gateway = FakeGateway() |
| 226 | val coordinator = coordinator(gateway) |
| 227 | coordinator.edit("generic", "shop.example", CheckoutInputs(phone = "not-e164")) |
| 228 | coordinator.submit("generic", "shop.example", lines, sink) |
| 229 | assertTrue(gateway.intents.isEmpty()) |
| 230 | assertEquals(CheckoutPhase.Error, coordinator.snapshot.single().phase) |
| 231 | assertTrue(stored.isEmpty()) |
| 232 | } |
| 233 | } |