From c43a721a0e2358938011cc29fa2d61684136fed1 Mon Sep 17 00:00:00 2001 From: Noah Durell Date: Fri, 22 Mar 2024 17:02:24 -0400 Subject: [PATCH] fix a bunch of warnings --- Sources/KlaviyoSwift/KlaviyoState.swift | 2 +- .../APIRequestErrorHandlingTests.swift | 16 +++++++++++++- .../AppLifeCycleEventsTests.swift | 3 ++- Tests/KlaviyoSwiftTests/KlaviyoAPITests.swift | 6 +---- .../KlaviyoSwiftTests/KlaviyoStateTests.swift | 1 - .../NetworkSessionTests.swift | 2 +- .../StateChangePublisherTests.swift | 4 +++- .../StateManagementEdgeCaseTests.swift | 22 ++++++++++++++++++- .../StateManagementTests.swift | 11 +++++++++- 9 files changed, 54 insertions(+), 13 deletions(-) diff --git a/Sources/KlaviyoSwift/KlaviyoState.swift b/Sources/KlaviyoSwift/KlaviyoState.swift index 5791c23d..0eef7aa4 100644 --- a/Sources/KlaviyoSwift/KlaviyoState.swift +++ b/Sources/KlaviyoSwift/KlaviyoState.swift @@ -190,7 +190,7 @@ struct KlaviyoState: Equatable, Codable { } var attributes = profile.data.attributes var location = profile.data.attributes.location ?? .init() - var properties = profile.data.attributes.properties.value as? [String: Any] ?? [:] + let properties = profile.data.attributes.properties.value as? [String: Any] ?? [:] let updatedProfile = Profile.updateProfileWithProperties(dict: pendingProfile) if let firstName = updatedProfile.firstName { diff --git a/Tests/KlaviyoSwiftTests/APIRequestErrorHandlingTests.swift b/Tests/KlaviyoSwiftTests/APIRequestErrorHandlingTests.swift index 0d161c70..53db5cae 100644 --- a/Tests/KlaviyoSwiftTests/APIRequestErrorHandlingTests.swift +++ b/Tests/KlaviyoSwiftTests/APIRequestErrorHandlingTests.swift @@ -11,14 +11,15 @@ import XCTest let TIMEOUT_NANOSECONDS: UInt64 = 10_000_000_000 // 10 seconds -@MainActor class APIRequestErrorHandlingTests: XCTestCase { + @MainActor override func setUp() async throws { environment = KlaviyoEnvironment.test() } // MARK: - http error + @MainActor func testSendRequestHttpFailureDequesRequest() async throws { var initialState = INITIALIZED_TEST_STATE() let request = initialState.buildProfileRequest(apiKey: initialState.apiKey!, anonymousId: initialState.anonymousId!) @@ -35,6 +36,7 @@ class APIRequestErrorHandlingTests: XCTestCase { } } + @MainActor func testSendRequestHttpFailureForPhoneNumberResetsStateAndDequesRequest() async throws { var initialState = INITIALIZED_TEST_STATE_INVALID_PHONE() let request = initialState.buildProfileRequest(apiKey: initialState.apiKey!, anonymousId: initialState.anonymousId!) @@ -57,6 +59,7 @@ class APIRequestErrorHandlingTests: XCTestCase { } } + @MainActor func testSendRequestHttpFailureForEmailResetsStateAndDequesRequest() async throws { var initialState = INITIALIZED_TEST_STATE_INVALID_EMAIL() let request = initialState.buildProfileRequest(apiKey: initialState.apiKey!, anonymousId: initialState.anonymousId!) @@ -81,6 +84,7 @@ class APIRequestErrorHandlingTests: XCTestCase { // MARK: - network error + @MainActor func testSendRequestFailureIncrementsRetryCount() async throws { var initialState = INITIALIZED_TEST_STATE() let request = initialState.buildProfileRequest(apiKey: initialState.apiKey!, anonymousId: initialState.anonymousId!) @@ -100,6 +104,7 @@ class APIRequestErrorHandlingTests: XCTestCase { } } + @MainActor func testSendRequestFailureWithBackoff() async throws { var initialState = INITIALIZED_TEST_STATE() initialState.retryInfo = .retryWithBackoff(requestCount: 1, totalRetryCount: 1, currentBackoff: 1) @@ -120,6 +125,7 @@ class APIRequestErrorHandlingTests: XCTestCase { } } + @MainActor func testSendRequestMaxRetries() async throws { var initialState = INITIALIZED_TEST_STATE() initialState.retryInfo = .retry(ErrorHandlingConstants.maxRetries) @@ -144,6 +150,7 @@ class APIRequestErrorHandlingTests: XCTestCase { // MARK: - internal error + @MainActor func testSendRequestInternalError() async throws { // NOTE: should really happen but putting this in for possible future cases and test coverage var initialState = INITIALIZED_TEST_STATE() @@ -166,6 +173,7 @@ class APIRequestErrorHandlingTests: XCTestCase { // MARK: - internal request error + @MainActor func testSendRequestInternalRequestError() async throws { var initialState = INITIALIZED_TEST_STATE() @@ -187,6 +195,7 @@ class APIRequestErrorHandlingTests: XCTestCase { // MARK: - unknown error + @MainActor func testSendRequestUnknownError() async throws { var initialState = INITIALIZED_TEST_STATE() @@ -208,6 +217,7 @@ class APIRequestErrorHandlingTests: XCTestCase { // MARK: - data decoding error + @MainActor func testSendRequestDataDecodingError() async throws { var initialState = INITIALIZED_TEST_STATE() let request = initialState.buildProfileRequest(apiKey: initialState.apiKey!, anonymousId: initialState.anonymousId!) @@ -228,6 +238,7 @@ class APIRequestErrorHandlingTests: XCTestCase { // MARK: - invalid data + @MainActor func testSendRequestInvalidData() async throws { var initialState = INITIALIZED_TEST_STATE() let request = initialState.buildProfileRequest(apiKey: initialState.apiKey!, anonymousId: initialState.anonymousId!) @@ -248,6 +259,7 @@ class APIRequestErrorHandlingTests: XCTestCase { // MARK: - rate limit error + @MainActor func testRateLimitErrorWithExistingRetry() async throws { var initialState = INITIALIZED_TEST_STATE() let request = initialState.buildProfileRequest(apiKey: initialState.apiKey!, anonymousId: initialState.anonymousId!) @@ -266,6 +278,7 @@ class APIRequestErrorHandlingTests: XCTestCase { } } + @MainActor func testRateLimitErrorWithExistingBackoffRetry() async throws { var initialState = INITIALIZED_TEST_STATE() initialState.retryInfo = .retryWithBackoff(requestCount: 2, totalRetryCount: 2, currentBackoff: 4) @@ -287,6 +300,7 @@ class APIRequestErrorHandlingTests: XCTestCase { // MARK: - Missing or invalid response + @MainActor func testMissingOrInvalidResponse() async throws { var initialState = INITIALIZED_TEST_STATE() initialState.retryInfo = .retryWithBackoff(requestCount: 2, totalRetryCount: 2, currentBackoff: 4) diff --git a/Tests/KlaviyoSwiftTests/AppLifeCycleEventsTests.swift b/Tests/KlaviyoSwiftTests/AppLifeCycleEventsTests.swift index 4443181b..64e53866 100644 --- a/Tests/KlaviyoSwiftTests/AppLifeCycleEventsTests.swift +++ b/Tests/KlaviyoSwiftTests/AppLifeCycleEventsTests.swift @@ -10,7 +10,6 @@ import Combine import Foundation import XCTest -@MainActor class AppLifeCycleEventsTests: XCTestCase { let passThroughSubject = PassthroughSubject() @@ -25,12 +24,14 @@ class AppLifeCycleEventsTests: XCTestCase { } } + @MainActor override func setUp() { environment = KlaviyoEnvironment.test() } // MARK: - App Terminate + @MainActor func testAppTerminateStopsReachability() { environment.notificationCenterPublisher = getFilteredNotificaitonPublished(name: UIApplication.willTerminateNotification) let expection = XCTestExpectation(description: "Stop reachability is called.") diff --git a/Tests/KlaviyoSwiftTests/KlaviyoAPITests.swift b/Tests/KlaviyoSwiftTests/KlaviyoAPITests.swift index 934387bc..769c0fd3 100644 --- a/Tests/KlaviyoSwiftTests/KlaviyoAPITests.swift +++ b/Tests/KlaviyoSwiftTests/KlaviyoAPITests.swift @@ -9,18 +9,14 @@ import SnapshotTesting import XCTest -@MainActor final class KlaviyoAPITests: XCTestCase { + @MainActor override func setUpWithError() throws { // Put setup code here. This method is called before the invocation of each test method in the class. environment = KlaviyoEnvironment.test() } - override func tearDownWithError() throws { - // Put teardown code here. This method is called after the invocation of each test method in the class. - } - func testInvalidURL() async throws { environment.analytics.apiURL = "" diff --git a/Tests/KlaviyoSwiftTests/KlaviyoStateTests.swift b/Tests/KlaviyoSwiftTests/KlaviyoStateTests.swift index 8677f494..a59445c8 100644 --- a/Tests/KlaviyoSwiftTests/KlaviyoStateTests.swift +++ b/Tests/KlaviyoSwiftTests/KlaviyoStateTests.swift @@ -11,7 +11,6 @@ import Foundation import SnapshotTesting import XCTest -@MainActor final class KlaviyoStateTests: XCTestCase { let TEST_EVENT = [ "event": "$opened_push", diff --git a/Tests/KlaviyoSwiftTests/NetworkSessionTests.swift b/Tests/KlaviyoSwiftTests/NetworkSessionTests.swift index 86a8c04a..bd02e58e 100644 --- a/Tests/KlaviyoSwiftTests/NetworkSessionTests.swift +++ b/Tests/KlaviyoSwiftTests/NetworkSessionTests.swift @@ -9,8 +9,8 @@ import SnapshotTesting import XCTest -@MainActor class NetworkSessionTests: XCTestCase { + @MainActor override func setUpWithError() throws { environment = KlaviyoEnvironment.test() } diff --git a/Tests/KlaviyoSwiftTests/StateChangePublisherTests.swift b/Tests/KlaviyoSwiftTests/StateChangePublisherTests.swift index 94b01934..4cfe4dec 100644 --- a/Tests/KlaviyoSwiftTests/StateChangePublisherTests.swift +++ b/Tests/KlaviyoSwiftTests/StateChangePublisherTests.swift @@ -11,12 +11,13 @@ import Foundation import XCTest @_spi(KlaviyoPrivate) @testable import KlaviyoSwift -@MainActor final class StateChangePublisherTests: XCTestCase { + @MainActor override func setUpWithError() throws { environment = KlaviyoEnvironment.test() } + @MainActor func testStateChangePublisher() throws { let savedCalledExpectation = XCTestExpectation(description: "Save called on initialization") // Third call set email should trigger again @@ -76,6 +77,7 @@ final class StateChangePublisherTests: XCTestCase { XCTAssertEqual(count, 2) } + @MainActor func testStateChangeDuplicateAreRemoved() throws { let savedCalledExpectation = XCTestExpectation(description: "Save called on initialization") savedCalledExpectation.assertForOverFulfill = true diff --git a/Tests/KlaviyoSwiftTests/StateManagementEdgeCaseTests.swift b/Tests/KlaviyoSwiftTests/StateManagementEdgeCaseTests.swift index 137d5911..48c6e083 100644 --- a/Tests/KlaviyoSwiftTests/StateManagementEdgeCaseTests.swift +++ b/Tests/KlaviyoSwiftTests/StateManagementEdgeCaseTests.swift @@ -9,14 +9,15 @@ import Foundation import XCTest -@MainActor class StateManagementEdgeCaseTests: XCTestCase { + @MainActor override func setUp() async throws { environment = KlaviyoEnvironment.test() } // MARK: - initialization + @MainActor func testInitializeWhileInitializing() async throws { let initialState = KlaviyoState(queue: [], requestsInFlight: []) let store = TestStore(initialState: initialState, reducer: KlaviyoReducer()) @@ -39,6 +40,7 @@ class StateManagementEdgeCaseTests: XCTestCase { _ = await store.send(.initialize(apiKey)) } + @MainActor func testInitializeAfterInitialized() async throws { let initialState = INITIALIZED_TEST_STATE() let store = TestStore(initialState: initialState, reducer: KlaviyoReducer()) @@ -57,6 +59,7 @@ class StateManagementEdgeCaseTests: XCTestCase { // MARK: - Send Request + @MainActor func testSendRequestBeforeInitialization() async throws { let apiKey = "fake-key" let initialState = KlaviyoState(apiKey: apiKey, @@ -71,6 +74,7 @@ class StateManagementEdgeCaseTests: XCTestCase { // MARK: - Complete Initialization + @MainActor func testCompleteInitializationWhileAlreadyInitialized() async throws { let apiKey = "fake-key" let initialState = KlaviyoState(apiKey: apiKey, @@ -87,6 +91,7 @@ class StateManagementEdgeCaseTests: XCTestCase { _ = await store.send(.completeInitialization(initialState)) } + @MainActor func testCompleteInitializationWithExistingIdentifiers() async throws { let apiKey = "fake-key" let initialState = KlaviyoState(apiKey: apiKey, @@ -110,6 +115,7 @@ class StateManagementEdgeCaseTests: XCTestCase { // MARK: - Set Email + @MainActor func testSetEmailUninitialized() async throws { let expection = XCTestExpectation(description: "fatal error expected") environment.emitDeveloperWarning = { _ in @@ -130,6 +136,7 @@ class StateManagementEdgeCaseTests: XCTestCase { await fulfillment(of: [expection]) } + @MainActor func testSetEmailMissingAnonymousIdStillSetsEmail() async throws { let apiKey = "fake-key" let initialState = KlaviyoState(apiKey: apiKey, @@ -146,6 +153,7 @@ class StateManagementEdgeCaseTests: XCTestCase { // MARK: - Set External Id + @MainActor func testSetExternalIdUninitialized() async throws { let apiKey = "fake-key" let initialState = KlaviyoState(apiKey: apiKey, @@ -159,6 +167,7 @@ class StateManagementEdgeCaseTests: XCTestCase { _ = await store.send(.setExternalId("external-blob-id")) } + @MainActor func testSetExternalIdMissingAnonymousIdStillSetsExternalId() async throws { let apiKey = "fake-key" let initialState = KlaviyoState(apiKey: apiKey, @@ -175,6 +184,7 @@ class StateManagementEdgeCaseTests: XCTestCase { // MARK: - Set Phone number + @MainActor func testSetPhoneNumberUninitialized() async throws { let apiKey = "fake-key" let initialState = KlaviyoState(apiKey: apiKey, @@ -188,6 +198,7 @@ class StateManagementEdgeCaseTests: XCTestCase { _ = await store.send(.setPhoneNumber("1-800-Blobs4u")) } + @MainActor func testSetPhoneNumberMissingApiKeyStillSetsPhoneNumber() async throws { let initialState = KlaviyoState(anonymousId: environment.analytics.uuid().uuidString, queue: [], @@ -203,6 +214,7 @@ class StateManagementEdgeCaseTests: XCTestCase { // MARK: - Set Push Token + @MainActor func testSetPushTokenUninitialized() async throws { let apiKey = "fake-key" let initialState = KlaviyoState(apiKey: apiKey, @@ -216,6 +228,7 @@ class StateManagementEdgeCaseTests: XCTestCase { _ = await store.send(.setPushToken("blob_token", .authorized)) } + @MainActor func testSetPushTokenWithMissingAnonymousId() async throws { let apiKey = "fake-key" let initialState = KlaviyoState(apiKey: apiKey, @@ -233,6 +246,7 @@ class StateManagementEdgeCaseTests: XCTestCase { // MARK: - Stop + @MainActor func testStopUninitialized() async { let apiKey = "fake-key" let initialState = KlaviyoState(apiKey: apiKey, @@ -246,6 +260,7 @@ class StateManagementEdgeCaseTests: XCTestCase { _ = await store.send(.stop) } + @MainActor func testStopInitializing() async { let apiKey = "fake-key" let initialState = KlaviyoState(apiKey: apiKey, @@ -261,6 +276,7 @@ class StateManagementEdgeCaseTests: XCTestCase { // MARK: - Start + @MainActor func testStartUninitialized() async { let apiKey = "fake-key" let initialState = KlaviyoState(apiKey: apiKey, @@ -276,6 +292,7 @@ class StateManagementEdgeCaseTests: XCTestCase { // MARK: - Network Status Changed + @MainActor func testNetworkStatusChangedUninitialized() async { let apiKey = "fake-key" let initialState = KlaviyoState(apiKey: apiKey, @@ -291,6 +308,7 @@ class StateManagementEdgeCaseTests: XCTestCase { // MARK: - Missing api key for token request + @MainActor func testTokenRequestMissingApiKey() async { let initialState = KlaviyoState( anonymousId: environment.analytics.uuid().uuidString, @@ -308,6 +326,7 @@ class StateManagementEdgeCaseTests: XCTestCase { // MARK: - set enqueue event uninitialized + @MainActor func testEnqueueEventUninitialized() async throws { let expection = XCTestExpectation(description: "fatal error expected") environment.emitDeveloperWarning = { _ in @@ -322,6 +341,7 @@ class StateManagementEdgeCaseTests: XCTestCase { // MARK: - set profile uninitialized + @MainActor func testSetProfileUnitialized() async throws { let expection = XCTestExpectation(description: "fatal error expected") environment.emitDeveloperWarning = { _ in diff --git a/Tests/KlaviyoSwiftTests/StateManagementTests.swift b/Tests/KlaviyoSwiftTests/StateManagementTests.swift index aa90c734..2bdcc109 100644 --- a/Tests/KlaviyoSwiftTests/StateManagementTests.swift +++ b/Tests/KlaviyoSwiftTests/StateManagementTests.swift @@ -11,14 +11,15 @@ import Combine import Foundation import XCTest -@MainActor class StateManagementTests: XCTestCase { + @MainActor override func setUp() async throws { environment = KlaviyoEnvironment.test() } // MARK: - Initialization + @MainActor func testInitialize() async throws { let initialState = KlaviyoState(queue: [], requestsInFlight: []) let store = TestStore(initialState: initialState, reducer: KlaviyoReducer()) @@ -41,6 +42,7 @@ class StateManagementTests: XCTestCase { await store.receive(.flushQueue) } + @MainActor func testInitializeSubscribesToAppropriatePublishers() async throws { let lifecycleExpectation = XCTestExpectation(description: "lifecycle is subscribed") let stateChangeIsSubscribed = XCTestExpectation(description: "state change is subscribed") @@ -73,6 +75,7 @@ class StateManagementTests: XCTestCase { // MARK: - Set Email + @MainActor func testSetEmail() async throws { let initialState = INITIALIZED_TEST_STATE() let store = TestStore(initialState: initialState, reducer: KlaviyoReducer()) @@ -87,6 +90,7 @@ class StateManagementTests: XCTestCase { // MARK: Set Phone Number + @MainActor func testSetPhoneNumber() async throws { let initialState = INITIALIZED_TEST_STATE() let store = TestStore(initialState: initialState, reducer: KlaviyoReducer()) @@ -101,6 +105,7 @@ class StateManagementTests: XCTestCase { // MARK: - Set External Id. + @MainActor func testSetExternalId() async throws { let initialState = INITIALIZED_TEST_STATE() let store = TestStore(initialState: initialState, reducer: KlaviyoReducer()) @@ -115,6 +120,7 @@ class StateManagementTests: XCTestCase { // MARK: - Set Push Token + @MainActor func testSetPushToken() async throws { var initialState = INITIALIZED_TEST_STATE() initialState.pushTokenData = nil @@ -141,6 +147,7 @@ class StateManagementTests: XCTestCase { } } + @MainActor func testSetPushTokenMultipleTimes() async throws { var initialState = INITIALIZED_TEST_STATE() initialState.pushTokenData = nil @@ -171,6 +178,7 @@ class StateManagementTests: XCTestCase { // MARK: - flush + @MainActor func testFlushUninitializedQueueDoesNotFlush() async throws { let apiKey = "fake-key" let initialState = KlaviyoState(apiKey: apiKey, @@ -182,6 +190,7 @@ class StateManagementTests: XCTestCase { _ = await store.send(.flushQueue) } + @MainActor func testQueueThatIsFlushingDoesNotFlush() async throws { let apiKey = "fake-key" let initialState = KlaviyoState(apiKey: apiKey,