|
| 1 | +// TrackerTests |
| 2 | +// Copyright © 2022 Split. All rights reserved. |
| 3 | + |
| 4 | +import XCTest |
| 5 | +@testable import Tracker |
| 6 | + |
| 7 | +class TrackerTest: XCTestCase { |
| 8 | + |
| 9 | + var tracker: Tracker! |
| 10 | + var eventValidator: TrackerEventValidatorStub! |
| 11 | + var propertyValidator: TrackerPropertyValidatorStub! |
| 12 | + var logger: TrackerLoggerStub! |
| 13 | + var pushedEvents: [TrackerEvent] = [] |
| 14 | + var recordedLatencies: [Int64] = [] |
| 15 | + |
| 16 | + override func setUp() { |
| 17 | + eventValidator = TrackerEventValidatorStub() |
| 18 | + propertyValidator = TrackerPropertyValidatorStub() |
| 19 | + logger = TrackerLoggerStub() |
| 20 | + pushedEvents = [] |
| 21 | + recordedLatencies = [] |
| 22 | + |
| 23 | + tracker = DefaultTracker( |
| 24 | + defaultTrafficType: "user", |
| 25 | + initialEventSizeInBytes: 1024, |
| 26 | + eventValidator: eventValidator, |
| 27 | + propertyValidator: propertyValidator, |
| 28 | + logger: logger, |
| 29 | + onEventPush: { [weak self] event in |
| 30 | + self?.pushedEvents.append(event) |
| 31 | + }, |
| 32 | + onTrackLatency: { [weak self] latency in |
| 33 | + self?.recordedLatencies.append(latency) |
| 34 | + } |
| 35 | + ) |
| 36 | + } |
| 37 | + |
| 38 | + func testTrackEnabled() { |
| 39 | + tracker.isTrackingEnabled = true |
| 40 | + propertyValidator.result = TrackerPropertyResult.valid(properties: nil, sizeInBytes: 100) |
| 41 | + |
| 42 | + let result = tracker.track( |
| 43 | + eventType: "test_event", |
| 44 | + trafficType: "test_tt", |
| 45 | + value: nil, |
| 46 | + properties: nil, |
| 47 | + matchingKey: "key1", |
| 48 | + isSdkReady: true |
| 49 | + ) |
| 50 | + |
| 51 | + XCTAssertTrue(result) |
| 52 | + XCTAssertEqual(pushedEvents.count, 1) |
| 53 | + XCTAssertEqual(recordedLatencies.count, 1) |
| 54 | + } |
| 55 | + |
| 56 | + func testTrackDisabled() { |
| 57 | + tracker.isTrackingEnabled = false |
| 58 | + |
| 59 | + let result = tracker.track( |
| 60 | + eventType: "test_event", |
| 61 | + trafficType: "test_tt", |
| 62 | + value: nil, |
| 63 | + properties: nil, |
| 64 | + matchingKey: "key1", |
| 65 | + isSdkReady: true |
| 66 | + ) |
| 67 | + |
| 68 | + XCTAssertFalse(result) |
| 69 | + XCTAssertEqual(pushedEvents.count, 0) |
| 70 | + XCTAssertEqual(recordedLatencies.count, 0) |
| 71 | + } |
| 72 | + |
| 73 | + func testTrackWithValidationError() { |
| 74 | + tracker.isTrackingEnabled = true |
| 75 | + eventValidator.validationError = TrackerValidationError(isError: true, message: "Invalid key") |
| 76 | + |
| 77 | + let result = tracker.track( |
| 78 | + eventType: "test_event", |
| 79 | + trafficType: "test_tt", |
| 80 | + value: nil, |
| 81 | + properties: nil, |
| 82 | + matchingKey: "key1", |
| 83 | + isSdkReady: true |
| 84 | + ) |
| 85 | + |
| 86 | + XCTAssertFalse(result) |
| 87 | + XCTAssertEqual(pushedEvents.count, 0) |
| 88 | + } |
| 89 | + |
| 90 | + func testTrackWithPropertyValidationError() { |
| 91 | + tracker.isTrackingEnabled = true |
| 92 | + propertyValidator.result = TrackerPropertyResult.invalid(message: "Properties too large", sizeInBytes: 50000) |
| 93 | + |
| 94 | + let result = tracker.track( |
| 95 | + eventType: "test_event", |
| 96 | + trafficType: "test_tt", |
| 97 | + value: nil, |
| 98 | + properties: ["key": "value"], |
| 99 | + matchingKey: "key1", |
| 100 | + isSdkReady: true |
| 101 | + ) |
| 102 | + |
| 103 | + XCTAssertFalse(result) |
| 104 | + XCTAssertEqual(pushedEvents.count, 0) |
| 105 | + } |
| 106 | + |
| 107 | + func testTrackUsesDefaultTrafficType() { |
| 108 | + tracker.isTrackingEnabled = true |
| 109 | + propertyValidator.result = TrackerPropertyResult.valid(properties: nil, sizeInBytes: 100) |
| 110 | + |
| 111 | + let result = tracker.track( |
| 112 | + eventType: "test_event", |
| 113 | + trafficType: nil, |
| 114 | + value: nil, |
| 115 | + properties: nil, |
| 116 | + matchingKey: "key1", |
| 117 | + isSdkReady: true |
| 118 | + ) |
| 119 | + |
| 120 | + XCTAssertTrue(result) |
| 121 | + XCTAssertEqual(pushedEvents.count, 1) |
| 122 | + XCTAssertEqual(pushedEvents.first?.trafficType, "user") |
| 123 | + } |
| 124 | + |
| 125 | + func testTrackWithValue() { |
| 126 | + tracker.isTrackingEnabled = true |
| 127 | + propertyValidator.result = TrackerPropertyResult.valid(properties: nil, sizeInBytes: 100) |
| 128 | + |
| 129 | + let result = tracker.track( |
| 130 | + eventType: "purchase", |
| 131 | + trafficType: "user", |
| 132 | + value: 99.99, |
| 133 | + properties: nil, |
| 134 | + matchingKey: "key1", |
| 135 | + isSdkReady: true |
| 136 | + ) |
| 137 | + |
| 138 | + XCTAssertTrue(result) |
| 139 | + XCTAssertEqual(pushedEvents.first?.value, 99.99) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +// MARK: - Test Stubs |
| 144 | + |
| 145 | +final class TrackerEventValidatorStub: TrackerEventValidator, @unchecked Sendable { |
| 146 | + var validationError: TrackerValidationError? |
| 147 | + |
| 148 | + func validate(key: String?, trafficTypeName: String?, eventTypeId: String?, value: Double?, properties: [String: Any]?, isSdkReady: Bool) -> TrackerValidationError? { |
| 149 | + validationError |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +final class TrackerPropertyValidatorStub: TrackerPropertyValidator, @unchecked Sendable { |
| 154 | + var result = TrackerPropertyResult.valid(properties: nil, sizeInBytes: 0) |
| 155 | + |
| 156 | + func validate(properties: [String: Any]?, initialSizeInBytes: Int, validationTag: String) -> TrackerPropertyResult { |
| 157 | + result |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +final class TrackerLoggerStub: TrackerLogger, @unchecked Sendable { |
| 162 | + var loggedErrors: [TrackerValidationError] = [] |
| 163 | + var errorMessages: [String] = [] |
| 164 | + var verboseMessages: [String] = [] |
| 165 | + |
| 166 | + func log(errorInfo: TrackerValidationError, tag: String) { |
| 167 | + loggedErrors.append(errorInfo) |
| 168 | + } |
| 169 | + |
| 170 | + func e(message: String, tag: String) { |
| 171 | + errorMessages.append(message) |
| 172 | + } |
| 173 | + |
| 174 | + func v(_ message: String) { |
| 175 | + verboseMessages.append(message) |
| 176 | + } |
| 177 | +} |
0 commit comments