|
| 1 | +/* |
| 2 | + * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. |
| 3 | + * This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | + * Copyright 2019-Present Datadog, Inc. |
| 5 | + */ |
| 6 | + |
| 7 | +import HTTPServerMock |
| 8 | +import TestUtilities |
| 9 | +import XCTest |
| 10 | +import DatadogInternal |
| 11 | + |
| 12 | +private extension ExampleApplication { |
| 13 | + func tapStartLoginFlow() { |
| 14 | + buttons["Start Login"].tap() |
| 15 | + } |
| 16 | + |
| 17 | + func tapSucceedLoginFlow() { |
| 18 | + buttons["Succeed Login"].tap() |
| 19 | + } |
| 20 | + |
| 21 | + func tapFailLoginFlow() { |
| 22 | + buttons["Fail Login"].tap() |
| 23 | + } |
| 24 | + |
| 25 | + func tapStartConcurrentOperations() { |
| 26 | + buttons["Start Photo"].tap() |
| 27 | + } |
| 28 | + |
| 29 | + func tapSucceedConcurrentOperations() { |
| 30 | + buttons["Succeed Photo"].tap() |
| 31 | + } |
| 32 | + |
| 33 | + func tapFailConcurrentOperations() { |
| 34 | + buttons["Fail Photo"].tap() |
| 35 | + } |
| 36 | + |
| 37 | + func tapPushNextScreen() { |
| 38 | + buttons["Push Next Screen"].tap() |
| 39 | + } |
| 40 | + |
| 41 | + func tapSucceedLoginOnNextView() { |
| 42 | + buttons["Succeed Login on Next View"].tap() |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +class RUMFeatureOperationsScenarioTests: IntegrationTests, RUMCommonAsserts { |
| 47 | + |
| 48 | + // MARK: - Basic Operation Test |
| 49 | + |
| 50 | + func testBasicFeatureOperation() throws { |
| 51 | + let rumServerSession = server.obtainUniqueRecordingSession() |
| 52 | + let app = ExampleApplication() |
| 53 | + app.launchWith( |
| 54 | + testScenarioClassName: "RUMFeatureOperationsScenario", |
| 55 | + serverConfiguration: HTTPServerMockConfiguration( |
| 56 | + rumEndpoint: rumServerSession.recordingURL |
| 57 | + ) |
| 58 | + ) |
| 59 | + |
| 60 | + let screen = ExampleApplication() |
| 61 | + |
| 62 | + // Test basic login flow (no operation key) |
| 63 | + screen.tapStartLoginFlow() |
| 64 | + screen.tapSucceedLoginFlow() |
| 65 | + |
| 66 | + try app.endRUMSession() |
| 67 | + |
| 68 | + let recordedRUMRequests = try rumServerSession.pullRecordedRequests(timeout: dataDeliveryTimeout) { requests in |
| 69 | + try RUMSessionMatcher.singleSession(from: requests)?.hasEnded() ?? false |
| 70 | + } |
| 71 | + |
| 72 | + assertRUM(requests: recordedRUMRequests) |
| 73 | + let session = try XCTUnwrap(RUMSessionMatcher.singleSession(from: recordedRUMRequests)) |
| 74 | + |
| 75 | + // Verify login flow vital events |
| 76 | + let vitalEvents = session.vitalEvents |
| 77 | + let loginStartEvents = vitalEvents.filter { $0.vital.name == "login_flow" && $0.vital.stepType == .start } |
| 78 | + let loginSucceedEvents = vitalEvents.filter { $0.vital.name == "login_flow" && $0.vital.stepType == .end && $0.vital.failureReason == nil } |
| 79 | + |
| 80 | + XCTAssertEqual(loginStartEvents.count, 1, "Should have one login flow start event") |
| 81 | + XCTAssertEqual(loginSucceedEvents.count, 1, "Should have one login flow succeed event") |
| 82 | + |
| 83 | + // Verify no operation key for basic operation |
| 84 | + for event in vitalEvents { |
| 85 | + XCTAssertNil(event.vital.operationKey, "Basic operation should not have operation key") |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // MARK: - Failure Scenarios Test |
| 90 | + |
| 91 | + func testFeatureOperationWithFailure() throws { |
| 92 | + let rumServerSession = server.obtainUniqueRecordingSession() |
| 93 | + let app = ExampleApplication() |
| 94 | + app.launchWith( |
| 95 | + testScenarioClassName: "RUMFeatureOperationsScenario", |
| 96 | + serverConfiguration: HTTPServerMockConfiguration( |
| 97 | + rumEndpoint: rumServerSession.recordingURL |
| 98 | + ) |
| 99 | + ) |
| 100 | + |
| 101 | + let screen = ExampleApplication() |
| 102 | + |
| 103 | + // Test different failure scenarios |
| 104 | + screen.tapStartLoginFlow() |
| 105 | + screen.tapFailLoginFlow() |
| 106 | + |
| 107 | + try app.endRUMSession() |
| 108 | + |
| 109 | + let recordedRUMRequests = try rumServerSession.pullRecordedRequests(timeout: dataDeliveryTimeout) { requests in |
| 110 | + try RUMSessionMatcher.singleSession(from: requests)?.hasEnded() ?? false |
| 111 | + } |
| 112 | + |
| 113 | + assertRUM(requests: recordedRUMRequests) |
| 114 | + let session = try XCTUnwrap(RUMSessionMatcher.singleSession(from: recordedRUMRequests)) |
| 115 | + |
| 116 | + // Verify error vital events |
| 117 | + let vitalEvents = session.vitalEvents |
| 118 | + let loginStartEvents = vitalEvents.filter { $0.vital.name == "login_flow" && $0.vital.stepType == .start } |
| 119 | + let loginFailEvents = vitalEvents.filter { $0.vital.name == "login_flow" && $0.vital.stepType == .end && $0.vital.failureReason == .error } |
| 120 | + |
| 121 | + XCTAssertEqual(loginStartEvents.count, 1, "Should have one login flow start event") |
| 122 | + XCTAssertEqual(loginFailEvents.count, 1, "Should have one login flow fail event") |
| 123 | + |
| 124 | + // Verify failure reason |
| 125 | + let failEvent = try XCTUnwrap(loginFailEvents.first) |
| 126 | + XCTAssertEqual(failEvent.vital.failureReason, .error, "Should have correct failure reason") |
| 127 | + } |
| 128 | + |
| 129 | + // MARK: - Multiple Operation Instances Test |
| 130 | + |
| 131 | + func testParallelFeatureOperations() throws { |
| 132 | + let rumServerSession = server.obtainUniqueRecordingSession() |
| 133 | + let app = ExampleApplication() |
| 134 | + app.launchWith( |
| 135 | + testScenarioClassName: "RUMFeatureOperationsScenario", |
| 136 | + serverConfiguration: HTTPServerMockConfiguration( |
| 137 | + rumEndpoint: rumServerSession.recordingURL |
| 138 | + ) |
| 139 | + ) |
| 140 | + |
| 141 | + let screen = ExampleApplication() |
| 142 | + |
| 143 | + // Test parallel photo uploads with different keys |
| 144 | + screen.tapStartConcurrentOperations() |
| 145 | + screen.tapSucceedConcurrentOperations() |
| 146 | + |
| 147 | + try app.endRUMSession() |
| 148 | + |
| 149 | + let recordedRUMRequests = try rumServerSession.pullRecordedRequests(timeout: dataDeliveryTimeout) { requests in |
| 150 | + try RUMSessionMatcher.singleSession(from: requests)?.hasEnded() ?? false |
| 151 | + } |
| 152 | + |
| 153 | + assertRUM(requests: recordedRUMRequests) |
| 154 | + let session = try XCTUnwrap(RUMSessionMatcher.singleSession(from: recordedRUMRequests)) |
| 155 | + |
| 156 | + // Verify parallel operations vital events |
| 157 | + let vitalEvents = session.vitalEvents |
| 158 | + let photoUploadStartEvents = vitalEvents.filter { $0.vital.name == "photo_upload" && $0.vital.stepType == .start } |
| 159 | + let photoUploadSucceedEvents = vitalEvents.filter { $0.vital.name == "photo_upload" && $0.vital.stepType == .end && $0.vital.failureReason == nil } |
| 160 | + |
| 161 | + XCTAssertEqual(photoUploadStartEvents.count, 3, "Should have three photo upload start events") |
| 162 | + XCTAssertEqual(photoUploadSucceedEvents.count, 3, "Should have three photo upload succeed events") |
| 163 | + |
| 164 | + // Verify operation keys for parallel instances |
| 165 | + let operationKeys = Set(vitalEvents.compactMap { $0.vital.operationKey }) |
| 166 | + let expectedKeys = Set(["photo1", "photo2", "photo3"]) |
| 167 | + XCTAssertEqual(operationKeys, expectedKeys, "Parallel operations should have the correct operation keys") |
| 168 | + } |
| 169 | + |
| 170 | + func testParallelFeatureOperationsWithFailure() throws { |
| 171 | + let rumServerSession = server.obtainUniqueRecordingSession() |
| 172 | + let app = ExampleApplication() |
| 173 | + app.launchWith( |
| 174 | + testScenarioClassName: "RUMFeatureOperationsScenario", |
| 175 | + serverConfiguration: HTTPServerMockConfiguration( |
| 176 | + rumEndpoint: rumServerSession.recordingURL |
| 177 | + ) |
| 178 | + ) |
| 179 | + |
| 180 | + let screen = ExampleApplication() |
| 181 | + |
| 182 | + // Test parallel photo uploads with different keys |
| 183 | + screen.tapStartConcurrentOperations() |
| 184 | + screen.tapFailConcurrentOperations() |
| 185 | + |
| 186 | + try app.endRUMSession() |
| 187 | + |
| 188 | + let recordedRUMRequests = try rumServerSession.pullRecordedRequests(timeout: dataDeliveryTimeout) { requests in |
| 189 | + try RUMSessionMatcher.singleSession(from: requests)?.hasEnded() ?? false |
| 190 | + } |
| 191 | + |
| 192 | + assertRUM(requests: recordedRUMRequests) |
| 193 | + let session = try XCTUnwrap(RUMSessionMatcher.singleSession(from: recordedRUMRequests)) |
| 194 | + |
| 195 | + // Verify parallel operations vital events |
| 196 | + let vitalEvents = session.vitalEvents |
| 197 | + let startEvents = vitalEvents.filter { $0.vital.name == "photo_upload" && $0.vital.stepType == .start } |
| 198 | + let failureEvents = vitalEvents.filter { |
| 199 | + $0.vital.name == "photo_upload" && |
| 200 | + $0.vital.stepType == .end && |
| 201 | + $0.vital.failureReason != nil |
| 202 | + } |
| 203 | + |
| 204 | + XCTAssertEqual(startEvents.count, 3, "Should have three photo upload start events") |
| 205 | + XCTAssertEqual(failureEvents.count, 3, "Should have three photo upload failure events") |
| 206 | + |
| 207 | + let expectedFailures = [ |
| 208 | + ("photo1", RUMVitalEvent.Vital.FailureReason.error), |
| 209 | + ("photo2", RUMVitalEvent.Vital.FailureReason.abandoned), |
| 210 | + ("photo3", RUMVitalEvent.Vital.FailureReason.other) |
| 211 | + ] |
| 212 | + |
| 213 | + zip(failureEvents.sorted { $0.vital.operationKey ?? "" < $1.vital.operationKey ?? "" }, expectedFailures) |
| 214 | + .forEach { event, expectedFailure in |
| 215 | + XCTAssertEqual(event.vital.operationKey, expectedFailure.0) |
| 216 | + XCTAssertEqual(event.vital.failureReason, expectedFailure.1) |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + // MARK: - Multiple Views Test |
| 221 | + |
| 222 | + func testFeatureOperationAcrossMultipleViews() throws { |
| 223 | + let rumServerSession = server.obtainUniqueRecordingSession() |
| 224 | + let app = ExampleApplication() |
| 225 | + app.launchWith( |
| 226 | + testScenarioClassName: "RUMFeatureOperationsScenario", |
| 227 | + serverConfiguration: HTTPServerMockConfiguration( |
| 228 | + rumEndpoint: rumServerSession.recordingURL |
| 229 | + ) |
| 230 | + ) |
| 231 | + |
| 232 | + let screen = ExampleApplication() |
| 233 | + |
| 234 | + // Start operation in current view |
| 235 | + screen.tapStartLoginFlow() |
| 236 | + |
| 237 | + // Navigate to next screen (operation continues) |
| 238 | + screen.tapPushNextScreen() |
| 239 | + |
| 240 | + // Complete operation in different view |
| 241 | + screen.tapSucceedLoginOnNextView() |
| 242 | + |
| 243 | + screen.swipeDown(velocity: XCUIGestureVelocity.fast) |
| 244 | + |
| 245 | + try app.endRUMSession() |
| 246 | + |
| 247 | + let recordedRUMRequests = try rumServerSession.pullRecordedRequests(timeout: dataDeliveryTimeout) { requests in |
| 248 | + try RUMSessionMatcher.singleSession(from: requests)?.hasEnded() ?? false |
| 249 | + } |
| 250 | + |
| 251 | + assertRUM(requests: recordedRUMRequests) |
| 252 | + let session = try XCTUnwrap(RUMSessionMatcher.singleSession(from: recordedRUMRequests)) |
| 253 | + print("session: \(session)") |
| 254 | + |
| 255 | + // Verify we have multiple views |
| 256 | + XCTAssertGreaterThan(session.views.count, 2, "Should have multiple views") |
| 257 | + |
| 258 | + // Verify vital events capture the correct view context |
| 259 | + let vitalEvents = session.vitalEvents |
| 260 | + let loginStartEvents = vitalEvents.filter { $0.vital.name == "login_flow" && $0.vital.stepType == .start } |
| 261 | + let loginSucceedEvents = vitalEvents.filter { $0.vital.name == "login_flow" && $0.vital.stepType == .end && $0.vital.failureReason == nil } |
| 262 | + |
| 263 | + XCTAssertEqual(loginStartEvents.count, 1, "Should have one login flow start event") |
| 264 | + XCTAssertEqual(loginSucceedEvents.count, 1, "Should have one login flow succeed event") |
| 265 | + |
| 266 | + // Verify start event is associated with 1st view |
| 267 | + let startEvent = try XCTUnwrap(loginStartEvents.first) |
| 268 | + let firstView = session.views[1] // Skip application launch view |
| 269 | + XCTAssertEqual(startEvent.view.id, firstView.viewID, "Start event should be associated with the view where it was started") |
| 270 | + |
| 271 | + // Verify second event is associated with 2nd view |
| 272 | + let secondEvent = try XCTUnwrap(loginSucceedEvents.first) |
| 273 | + let secondView = session.views[2] |
| 274 | + XCTAssertEqual(secondEvent.view.id, secondView.viewID, "Second event should be associated with the view where it was started") |
| 275 | + } |
| 276 | +} |
0 commit comments