From 8c6bc93814827474e632028a9d46c19690282018 Mon Sep 17 00:00:00 2001 From: Sumeru Chatterjee Date: Tue, 7 Jan 2025 14:46:01 +0100 Subject: [PATCH] [MOB-9233] Updated code according to the new discussion --- .../xcshareddata/xcschemes/swift-sdk.xcscheme | 4 +- tests/unit-tests/InAppPersistenceTests.swift | 160 +++++------------- tests/unit-tests/InAppTests.swift | 77 +++------ 3 files changed, 67 insertions(+), 174 deletions(-) diff --git a/swift-sdk.xcodeproj/xcshareddata/xcschemes/swift-sdk.xcscheme b/swift-sdk.xcodeproj/xcshareddata/xcschemes/swift-sdk.xcscheme index a1714130..f990bb92 100644 --- a/swift-sdk.xcodeproj/xcshareddata/xcschemes/swift-sdk.xcscheme +++ b/swift-sdk.xcodeproj/xcshareddata/xcschemes/swift-sdk.xcscheme @@ -80,8 +80,8 @@ diff --git a/tests/unit-tests/InAppPersistenceTests.swift b/tests/unit-tests/InAppPersistenceTests.swift index 7873cacf..482feaff 100644 --- a/tests/unit-tests/InAppPersistenceTests.swift +++ b/tests/unit-tests/InAppPersistenceTests.swift @@ -95,6 +95,8 @@ class InAppPersistenceTests: XCTestCase { } func testJsonOnlyMessagePersistence() { + let expectation1 = expectation(description: "testJsonOnlyMessagePersistence") + // Test 1: Basic JSON-only message with customPayload let customPayload: [AnyHashable: Any] = [ "key1": "value1", @@ -113,7 +115,8 @@ class InAppPersistenceTests: XCTestCase { inboxMetadata: nil, customPayload: customPayload, read: false, - priorityLevel: 0.0 + priorityLevel: 0.0, + jsonOnly: true ) // Test persistence to file @@ -152,10 +155,8 @@ class InAppPersistenceTests: XCTestCase { XCTAssertEqual(jsonData["jsonOnly"] as? Int, 1) XCTAssertFalse(jsonData["saveToInbox"] as? Bool ?? true) XCTAssertNotNil(jsonData["customPayload"]) - // Content should not be encoded for JSON-only messages - if let content = jsonData["content"] as? [String: Any] { - XCTAssertEqual(content.count, 0, "Content should be empty for JSON-only messages") - } + // Content should be minimal for JSON-only messages + XCTAssertTrue(jsonData["content"] == nil || (jsonData["content"] as? [String: Any])?.isEmpty == true) } // Test 3: Message without customPayload @@ -170,136 +171,57 @@ class InAppPersistenceTests: XCTestCase { inboxMetadata: nil, customPayload: nil, read: false, - priorityLevel: 0.0 + priorityLevel: 0.0, + jsonOnly: true ) persister.clear() persister.persist([messageWithoutPayload]) let retrievedEmptyMessages = persister.getMessages() - XCTAssertEqual(retrievedEmptyMessages.count, 0, "JSON-only messages without customPayload should be ignored") + XCTAssertEqual(retrievedEmptyMessages.count, 1, "JSON-only messages without customPayload should still be persisted") - // Cleanup - persister.clear() - } - - func testJsonOnlyMessagePersistenceWithFilePersister() { - let customPayload: [AnyHashable: Any] = [ - "id": 1, - "score": 42.5, - "active": true, - "name": "Jane Doe" + // Test 4: Array of JSON-only messages + let messagesArray = [ + createJsonOnlyMessage(id: "json-1", payload: ["type": "notification", "priority": 1]), + createJsonOnlyMessage(id: "json-2", payload: ["type": "alert", "priority": 2]), + createJsonOnlyMessage(id: "json-3", payload: ["type": "message", "priority": 3]) ] - let message = IterableInAppMessage( - messageId: "test-json-2", - campaignId: 456, - trigger: .neverTrigger, - createdAt: Date(), - expiresAt: Date().addingTimeInterval(86400), - content: IterableJsonInAppContent(json: [:]), - saveToInbox: false, - inboxMetadata: nil, - customPayload: customPayload, - read: false, - priorityLevel: 0.0 - ) - - let filename = "test_json_persistence" - let persister = InAppFilePersister(filename: filename) - - // Clear any existing data persister.clear() + persister.persist(messagesArray) + let retrievedArray = persister.getMessages() - // Save message - persister.persist([message]) - - // Read back message - let retrievedMessages = persister.getMessages() - XCTAssertEqual(retrievedMessages.count, 1) - - guard let retrievedMessage = retrievedMessages.first else { - XCTFail("No message retrieved") - return - } - - XCTAssertEqual(message.messageId, retrievedMessage.messageId) - XCTAssertEqual(message.campaignId?.intValue, retrievedMessage.campaignId?.intValue) - XCTAssertFalse(retrievedMessage.saveToInbox) + XCTAssertEqual(retrievedArray.count, messagesArray.count) - guard let retrievedContent = retrievedMessage.content as? IterableJsonInAppContent else { - XCTFail("Content type mismatch") - return + // Verify each message in array + for (original, retrieved) in zip(messagesArray, retrievedArray) { + XCTAssertEqual(original.messageId, retrieved.messageId) + XCTAssertEqual(original.customPayload?["type"] as? String, retrieved.customPayload?["type"] as? String) + XCTAssertEqual(original.customPayload?["priority"] as? Int, retrieved.customPayload?["priority"] as? Int) } - XCTAssertEqual(retrievedContent.json["id"] as? Int, 1) - XCTAssertEqual(retrievedContent.json["score"] as? Double, 42.5) - XCTAssertEqual(retrievedContent.json["active"] as? Bool, true) - XCTAssertEqual(retrievedContent.json["name"] as? String, "Jane Doe") + expectation1.fulfill() // Cleanup persister.clear() - } - - func testJsonOnlyMessageArrayPersistence() { - let messages = [ - createJsonOnlyMessage( - id: "json-1", - payload: ["type": "notification", "priority": 1] - ), - createJsonOnlyMessage( - id: "json-2", - payload: ["type": "alert", "priority": 2] - ), - createJsonOnlyMessage( - id: "json-3", - payload: ["type": "message", "priority": 3] - ) - ] - - let filename = "test_json_array" - let persister = InAppFilePersister(filename: filename) - - // Clear any existing data - persister.clear() - - // Save messages - persister.persist(messages) - - // Read back messages - let retrievedMessages = persister.getMessages() - XCTAssertEqual(retrievedMessages.count, messages.count) - - // Verify each message - for (original, retrieved) in zip(messages, retrievedMessages) { - XCTAssertEqual(original.messageId, retrieved.messageId) - - guard let originalContent = original.content as? IterableJsonInAppContent, - let retrievedContent = retrieved.content as? IterableJsonInAppContent else { - XCTFail("Content type mismatch") - continue - } - - XCTAssertEqual(originalContent.json["type"] as? String, retrievedContent.json["type"] as? String) - XCTAssertEqual(originalContent.json["priority"] as? Int, retrievedContent.json["priority"] as? Int) - } - // Cleanup - persister.clear() + wait(for: [expectation1], timeout: testExpectationTimeout) } private func createJsonOnlyMessage(id: String, payload: [AnyHashable: Any]) -> IterableInAppMessage { IterableInAppMessage( messageId: id, - campaignId: Int.random(in: 1...1000) as NSNumber, + campaignId: Int.random(in: 1...1000) as NSNumber, trigger: .neverTrigger, createdAt: Date(), expiresAt: Date().addingTimeInterval(86400), - content: IterableJsonInAppContent(json: payload), + content: IterableHtmlInAppContent(edgeInsets: .zero, html: ""), saveToInbox: false, inboxMetadata: nil, - customPayload: nil, + customPayload: payload, read: false, - priorityLevel: 0.0 + priorityLevel: 0.0, + jsonOnly: true ) } @@ -309,23 +231,19 @@ class InAppPersistenceTests: XCTestCase { "key2": 42 ] - let contentPayload: [AnyHashable: Any] = [ - "key1": "contentValue", - "key2": 100 - ] - let message = IterableInAppMessage( messageId: "test-json-priority", campaignId: 789, trigger: .neverTrigger, createdAt: nil, expiresAt: nil, - content: IterableJsonInAppContent(json: contentPayload), + content: IterableHtmlInAppContent(edgeInsets: .zero, html: ""), saveToInbox: false, inboxMetadata: nil, customPayload: customPayload, read: false, - priorityLevel: 0.0 + priorityLevel: 0.0, + jsonOnly: true ) guard let encodedMessage = try? JSONEncoder().encode(message) else { @@ -338,13 +256,13 @@ class InAppPersistenceTests: XCTestCase { return } - guard let decodedContent = decodedMessage.content as? IterableJsonInAppContent else { - XCTFail("Content type mismatch") - return - } + // Verify that customPayload values are preserved + XCTAssertEqual(decodedMessage.customPayload?["key1"] as? String, "customValue") + XCTAssertEqual(decodedMessage.customPayload?["key2"] as? Int, 42) - // Verify that customPayload values are used instead of content.payload - XCTAssertEqual(decodedContent.json["key1"] as? String, "customValue") - XCTAssertEqual(decodedContent.json["key2"] as? Int, 42) + // Verify that content is ignored for JSON-only messages + XCTAssertTrue(decodedMessage.content is IterableHtmlInAppContent) + XCTAssertTrue(decodedMessage.jsonOnly) + XCTAssertFalse(decodedMessage.saveToInbox) } } diff --git a/tests/unit-tests/InAppTests.swift b/tests/unit-tests/InAppTests.swift index 74475e0f..e7ff7467 100644 --- a/tests/unit-tests/InAppTests.swift +++ b/tests/unit-tests/InAppTests.swift @@ -1427,13 +1427,10 @@ class InAppTests: XCTestCase { let mockInAppDelegate = MockInAppDelegate(showInApp: .show) mockInAppDelegate.onNewMessageCallback = { message in - if let jsonContent = message.content as? IterableJsonInAppContent { - XCTAssertEqual(jsonContent.json["key"] as? String, "value") - expectation1.fulfill() - } else { - XCTFail("Expected JSON content") - } + XCTAssertEqual(message.customPayload?["key"] as? String, "value") + expectation1.fulfill() } + let config = IterableConfig() config.inAppDelegate = mockInAppDelegate @@ -1534,15 +1531,12 @@ class InAppTests: XCTestCase { let messages = internalApi.inAppManager.getMessages() XCTAssertEqual(messages.count, 1) - - if let jsonContent = messages[0].content as? IterableJsonInAppContent { - XCTAssertEqual(jsonContent.json["key1"] as? String, "value1") - XCTAssertEqual(jsonContent.json["key2"] as? Int, 42) - XCTAssertEqual((jsonContent.json["key3"] as? [String: Any])?["nested"] as? Bool, true) - expectation1.fulfill() - } else { - XCTFail("Expected JSON content") - } + + let message = messages[0] + XCTAssertEqual(message.customPayload?["key1"] as? String, "value1") + XCTAssertEqual(message.customPayload?["key2"] as? Int, 42) + XCTAssertEqual((message.customPayload?["key3"] as? [String: Any])?["nested"] as? Bool, true) + expectation1.fulfill() } wait(for: [expectation1], timeout: testExpectationTimeout) @@ -1556,25 +1550,20 @@ class InAppTests: XCTestCase { let mockInAppFetcher = MockInAppFetcher() let mockInAppDisplayer = MockInAppDisplayer() - // This should never be called since JSON messages don't display mockInAppDisplayer.onShow.onSuccess { _ in XCTFail("JSON-only messages should not be displayed") } let mockInAppDelegate = MockInAppDelegate(showInApp: .show) mockInAppDelegate.onNewMessageCallback = { message in - if let jsonContent = message.content as? IterableJsonInAppContent { - if message.messageId == "message1" { - // Verify immediate trigger message - XCTAssertEqual(jsonContent.json["key"] as? String, "immediate") - expectation1.fulfill() - } else if message.messageId == "message2" { - // Never trigger message should not call onNew - XCTFail("onNew should not be called for never trigger") - expectation2.fulfill() - } - } else { - XCTFail("Expected JSON content") + if message.messageId == "message1" { + // Verify immediate trigger message + XCTAssertEqual(message.customPayload?["key"] as? String, "immediate") + expectation1.fulfill() + } else if message.messageId == "message2" { + // Never trigger message should not call onNew + XCTFail("onNew should not be called for never trigger") + expectation2.fulfill() } } @@ -1733,13 +1722,10 @@ class InAppTests: XCTestCase { let messages = internalApi.inAppManager.getMessages() XCTAssertEqual(messages.count, 1) - - if let jsonContent = messages[0].content as? IterableJsonInAppContent { - XCTAssertTrue(jsonContent.json.isEmpty) - expectation1.fulfill() - } else { - XCTFail("Expected JSON content") - } + + let message = messages[0] + XCTAssertTrue(message.customPayload?.isEmpty ?? false) + expectation1.fulfill() } wait(for: [expectation1], timeout: testExpectationTimeout) @@ -1852,14 +1838,11 @@ class InAppTests: XCTestCase { let messages = internalApi.inAppManager.getMessages() XCTAssertEqual(messages.count, 1) - - if let jsonContent = messages[0].content as? IterableJsonInAppContent { - // Verify we use customPayload and ignore content.payload - XCTAssertEqual(jsonContent.json["key"] as? String, "customValue") - expectation1.fulfill() - } else { - XCTFail("Expected JSON content") - } + + let message = messages[0] + // Verify we use customPayload and ignore content.payload + XCTAssertEqual(message.customPayload?["key"] as? String, "customValue") + expectation1.fulfill() } wait(for: [expectation1], timeout: testExpectationTimeout) @@ -1910,11 +1893,3 @@ extension IterableInAppMessage { } -extension IterableJsonInAppContent { - override public var description: String { - IterableUtil.describe("type", type, - "json", json, - pairSeparator: " = ", - separator: ", ") - } -}