@@ -777,6 +777,56 @@ class InAppTests: XCTestCase {
777777 wait ( for: [ expectation4] , timeout: testExpectationTimeout)
778778 }
779779
780+ func testShowBackgroundSyncedMessageOnForegroundWithinCooldown( ) {
781+ let expectation1 = expectation ( description: " initial sync completes " )
782+ let expectation2 = expectation ( description: " background sync completes " )
783+ let expectation3 = expectation ( description: " message shown on foreground within cooldown " )
784+
785+ let payload = TestInAppPayloadGenerator . createPayloadWithUrl ( numMessages: 1 )
786+
787+ let mockInAppFetcher = MockInAppFetcher ( )
788+ let mockDateProvider = MockDateProvider ( )
789+
790+ let mockInAppDisplayer = MockInAppDisplayer ( )
791+ mockInAppDisplayer. onShow. onSuccess { _ in
792+ expectation3. fulfill ( )
793+ }
794+
795+ let config = IterableConfig ( )
796+ config. inAppDisplayInterval = 1.0
797+
798+ let mockApplicationStateProvider = MockApplicationStateProvider ( applicationState: . active)
799+ let mockNotificationCenter = MockNotificationCenter ( )
800+
801+ let internalApi = InternalIterableAPI . initializeForTesting (
802+ config: config,
803+ dateProvider: mockDateProvider,
804+ inAppFetcher: mockInAppFetcher,
805+ inAppDisplayer: mockInAppDisplayer,
806+ applicationStateProvider: mockApplicationStateProvider,
807+ notificationCenter: mockNotificationCenter
808+ )
809+
810+ // 1. Initial sync with no messages (sets lastSyncTime)
811+ mockInAppFetcher. mockMessagesAvailableFromServer ( internalApi: internalApi, messages: [ ] ) . onSuccess { _ in
812+ expectation1. fulfill ( )
813+ }
814+ wait ( for: [ expectation1] , timeout: testExpectationTimeout)
815+
816+ // 2. App goes to background, new message arrives via background sync
817+ mockApplicationStateProvider. applicationState = . background
818+ mockInAppFetcher. mockInAppPayloadFromServer ( internalApi: internalApi, payload) . onSuccess { _ in
819+ expectation2. fulfill ( )
820+ }
821+ wait ( for: [ expectation2] , timeout: testExpectationTimeout)
822+
823+ // 3. App returns to foreground within cooldown (no time advance)
824+ mockApplicationStateProvider. applicationState = . active
825+ mockNotificationCenter. post ( name: UIApplication . didBecomeActiveNotification, object: nil , userInfo: nil )
826+
827+ wait ( for: [ expectation3] , timeout: testExpectationTimeout)
828+ }
829+
780830 func testDontShowNewlyArrivedMessageWithinRetryInterval( ) {
781831 let expectation1 = expectation ( description: " show first message " )
782832 let expectation2 = expectation ( description: " don't show second message within interval " )
0 commit comments