@@ -361,6 +361,55 @@ final class CollectOrderPaymentUseCaseTests: XCTestCase {
361361 // Then
362362 XCTAssertEqual ( mockPaymentOrchestrator. spyChannel, . pos)
363363 }
364+
365+ func test_completion_called_after_alert_presentation( ) throws {
366+ receiptEligibilityUseCase. isEligibleForBackendReceipts = true
367+ let paymentMethod = PaymentMethod . cardPresent ( details: . fake( ) )
368+ let intent = PaymentIntent . fake ( ) . copy ( charges: [ . fake( ) . copy ( paymentMethod: paymentMethod) ] )
369+ let capturedPaymentData = CardPresentCapturedPaymentData ( paymentMethod: paymentMethod, receiptParameters: . fake( ) )
370+ mockSuccessfulCardPresentPaymentActions ( intent: intent, capturedPaymentData: capturedPaymentData)
371+ enum Event {
372+ case receiptEligibilityCheck
373+ case alertPresented
374+ case paymentCompletion
375+ }
376+ var eventOrder : [ Event ] = [ ]
377+
378+ receiptEligibilityUseCase. mockIsEligibleForBackendReceiptsHandler = { completion in
379+ // Force receiptEligibilityCheck completion delay
380+ DispatchQueue . main. async {
381+ eventOrder. append ( . receiptEligibilityCheck)
382+ completion ( true )
383+ }
384+ }
385+
386+ // Track when receipt alert is presented
387+ alertsPresenter. onPresentCalled = { viewModel in
388+ if viewModel is CardPresentModalSuccessWithoutEmail ||
389+ viewModel is CardPresentModalSuccessEmailSent {
390+ eventOrder. append ( . alertPresented)
391+ }
392+ }
393+
394+ // When payment succeeds
395+ waitFor { promise in
396+ self . useCase. collectPayment (
397+ using: . bluetoothScan,
398+ channel: . storeManagement,
399+ onFailure: { _ in } ,
400+ onCancel: { } ,
401+ onPaymentCompletion: {
402+ eventOrder. append ( . paymentCompletion)
403+ promise ( ( ) )
404+ } ,
405+ onCompleted: { }
406+ )
407+ self . mockPreflightController. completeConnection ( reader: MockCardReader . wisePad3 ( ) , gatewayID: Mocks . paymentGatewayAccount)
408+ }
409+
410+ // Then ensure payment completion happens after alert presentation to avoid CollectOrderPaymentUseCase deinit before alert presentation
411+ XCTAssertEqual ( eventOrder, [ . receiptEligibilityCheck, . alertPresented, . paymentCompletion] )
412+ }
364413}
365414
366415private extension CollectOrderPaymentUseCaseTests {
0 commit comments