Skip to content

Commit

Permalink
[MOB-9233] Updated code according to the new discussion
Browse files Browse the repository at this point in the history
  • Loading branch information
sumeruchat committed Jan 7, 2025
1 parent 448d07d commit 8c6bc93
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 174 deletions.
4 changes: 2 additions & 2 deletions swift-sdk.xcodeproj/xcshareddata/xcschemes/swift-sdk.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = ""
selectedLauncherIdentifier = "Xcode.IDEFoundation.Launcher.PosixSpawn"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES"
onlyGenerateCoverageForSpecifiedTargets = "YES">
Expand Down
160 changes: 39 additions & 121 deletions tests/unit-tests/InAppPersistenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
)
}

Expand All @@ -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 {
Expand All @@ -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)
}
}
77 changes: 26 additions & 51 deletions tests/unit-tests/InAppTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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()
}
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -1910,11 +1893,3 @@ extension IterableInAppMessage {
}


extension IterableJsonInAppContent {
override public var description: String {
IterableUtil.describe("type", type,
"json", json,
pairSeparator: " = ",
separator: ", ")
}
}

0 comments on commit 8c6bc93

Please sign in to comment.