Skip to content

Commit

Permalink
fix: Reconciliation bug
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria committed May 1, 2024
1 parent 0ca65ea commit 6bdbf95
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions Sources/Confidence/Confidence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public class Confidence: ConfidenceEventSender {
var map = confidence.contextFlow.value
for removedKey in removedKeys {
map.removeValue(forKey: removedKey)
confidence.removedContextKeys.insert(removedKey)
}
for entry in context {
map.updateValue(entry.value, forKey: entry.key)
Expand Down
1 change: 1 addition & 0 deletions Sources/Confidence/Contextual.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public protocol Contextual: ConfidenceContextProvider {
/// Removes entry from local data
/// It hides entries with this key from parents' data (without modifying parents' data)
func removeContextEntry(key: String)
func putContext(context: ConfidenceStruct, removedKeys: [String])
/// Creates a child Contextual instance that maintains access to its parent's data
func withContext(_ context: ConfidenceStruct) -> Self
}
26 changes: 26 additions & 0 deletions Tests/ConfidenceTests/ConfidenceTests.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import XCTest
@testable import Confidence

// swiftlint:disable type_body_length
final class ConfidenceTests: XCTestCase {
func testWithContext() {
let confidenceParent = Confidence.init(
Expand Down Expand Up @@ -45,6 +46,30 @@ final class ConfidenceTests: XCTestCase {
XCTAssertEqual(confidenceChild.getContext(), expected)
}

func testWithContextUpdateParentRemoveKeys() {
let confidenceParent = Confidence.init(
clientSecret: "",
timeout: TimeInterval(),
region: .europe,
eventSenderEngine: EventSenderEngineMock(),
initializationStrategy: .activateAndFetchAsync,
context: ["k1": ConfidenceValue(string: "v1")],
parent: nil
)
let confidenceChild: ConfidenceEventSender = confidenceParent.withContext(
["k2": ConfidenceValue(string: "v2")]
)
confidenceChild.putContext(
context: ["k3": ConfidenceValue(string: "v3")],
removedKeys: ["k1"]
)
let expected = [
"k2": ConfidenceValue(string: "v2"),
"k3": ConfidenceValue(string: "v3"),
]
XCTAssertEqual(confidenceChild.getContext(), expected)
}

func testUpdateLocalContext() {
let confidence = Confidence.init(
clientSecret: "",
Expand Down Expand Up @@ -246,3 +271,4 @@ final class ConfidenceTests: XCTestCase {
XCTAssertNil(confidence.getContext()["visitorId"])
}
}
// swiftlint:enable type_body_length

0 comments on commit 6bdbf95

Please sign in to comment.