|
| 1 | +//// Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. |
| 2 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 3 | +// Copyright 2025-Present Datadog, Inc. |
| 4 | + |
| 5 | +import Foundation |
| 6 | +import Testing |
| 7 | +import Flutter |
| 8 | +import DatadogInternal |
| 9 | + |
| 10 | +@testable import datadog_session_replay |
| 11 | + |
| 12 | +extension FlutterError: EquatableInTests { |
| 13 | + |
| 14 | +} |
| 15 | + |
| 16 | +func checkResult(_ result: ResultStatus, expectedError: NSObject) { |
| 17 | + switch result { |
| 18 | + case .called(let value): |
| 19 | + #expect((value as? NSObject) === expectedError) |
| 20 | + default: |
| 21 | + #expect(Bool(false), "Unexpected result: \(result)") |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +func checkResult(_ result: ResultStatus, errorCode: String, errorMessage: String) throws { |
| 26 | + switch result { |
| 27 | + case .called(let value): |
| 28 | + let error = value as? FlutterError |
| 29 | + try #require(error != nil, "Unexpected error type: \(type(of: value))") |
| 30 | + #expect(error?.code == errorCode) |
| 31 | + #expect(error?.message == errorMessage) |
| 32 | + default: |
| 33 | + #expect(Bool(false), "Unexpected result: \(result)") |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +@Suite(.serialized) |
| 38 | +class SessionReplayPluginTests { |
| 39 | + let mockCore = PassthroughCoreMock() |
| 40 | + let mockChannel = FlutterMethodChannel() |
| 41 | + |
| 42 | + init() { |
| 43 | + CoreRegistry.register(default: mockCore) |
| 44 | + } |
| 45 | + |
| 46 | + deinit { |
| 47 | + CoreRegistry.unregisterDefault() |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + func returnNotImplemented_WhenUnknownMethod() throws { |
| 52 | + // Given |
| 53 | + let plugin = DatadogSessionReplayPlugin(channel: FlutterMethodChannelMock()) |
| 54 | + let arguments: [String: Any] = [:] |
| 55 | + let methodCall = FlutterMethodCall(methodName: "unknown", arguments: arguments) |
| 56 | + |
| 57 | + // When |
| 58 | + var status: ResultStatus = .notCalled |
| 59 | + plugin.handle(methodCall) { result in |
| 60 | + status = .called(value: result) |
| 61 | + } |
| 62 | + |
| 63 | + // Then |
| 64 | + checkResult(status, expectedError: FlutterMethodNotImplemented) |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + func returnInvalidOperation_WhenBadArguments() throws { |
| 69 | + // Given |
| 70 | + let plugin = DatadogSessionReplayPlugin(channel: FlutterMethodChannelMock()) |
| 71 | + let methodCall = FlutterMethodCall(methodName: "unknown", arguments: 22) |
| 72 | + |
| 73 | + // When |
| 74 | + var status: ResultStatus = .notCalled |
| 75 | + plugin.handle(methodCall) { result in |
| 76 | + status = .called(value: result) |
| 77 | + } |
| 78 | + |
| 79 | + // Then |
| 80 | + try checkResult(status, errorCode: "DatadogSdk:InvalidOperation", errorMessage: "No arguments in call to unknown") |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + func enablesFeature_WhenEnableMethodCall() { |
| 85 | + // Given |
| 86 | + let plugin = DatadogSessionReplayPlugin(channel: FlutterMethodChannelMock()) |
| 87 | + let arguments: [String: Any] = [ |
| 88 | + "configuration": [String: Any]() |
| 89 | + ] |
| 90 | + let methodCall = FlutterMethodCall(methodName: "enable", arguments: arguments) |
| 91 | + |
| 92 | + // When |
| 93 | + var status: ResultStatus = .notCalled |
| 94 | + plugin.handle(methodCall) { result in |
| 95 | + status = .called(value: result) |
| 96 | + } |
| 97 | + |
| 98 | + // Then |
| 99 | + #expect(status == .called(value: nil)) |
| 100 | + #expect(mockCore.get(feature: FlutterSessionReplayFeature.self) != nil) |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + func returnsError_WhenEnableMethodCall_MissingConfiguration() throws { |
| 105 | + // Given |
| 106 | + let plugin = DatadogSessionReplayPlugin(channel: FlutterMethodChannelMock()) |
| 107 | + let arguments: [String: Any] = [:] |
| 108 | + let methodCall = FlutterMethodCall(methodName: "enable", arguments: arguments) |
| 109 | + |
| 110 | + // When |
| 111 | + var status: ResultStatus = .notCalled |
| 112 | + plugin.handle(methodCall) { result in |
| 113 | + status = .called(value: result) |
| 114 | + } |
| 115 | + |
| 116 | + // Then |
| 117 | + try checkResult(status, errorCode: "DatadogSdk:ContractViolation", errorMessage: "Missing parameter in call to enable") |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + func setsContext_WhenSetHasReplayMethodCall() throws { |
| 122 | + // Given |
| 123 | + let plugin = DatadogSessionReplayPlugin(channel: FlutterMethodChannelMock()) |
| 124 | + let expectedValue: Bool = .mockRandom() |
| 125 | + let arguments: [String: Any] = [ |
| 126 | + "hasReplay": expectedValue |
| 127 | + ] |
| 128 | + let methodCall = FlutterMethodCall(methodName: "setHasReplay", arguments: arguments) |
| 129 | + |
| 130 | + // When |
| 131 | + plugin.enable(configuration: .init()) |
| 132 | + var status: ResultStatus = .notCalled |
| 133 | + plugin.handle(methodCall) { result in |
| 134 | + status = .called(value: result) |
| 135 | + } |
| 136 | + |
| 137 | + // Then |
| 138 | + #expect(status == .called(value: nil)) |
| 139 | + let value = try mockCore.context.baggages["sr_has_replay"]?.encode() as? Bool |
| 140 | + #expect(value == expectedValue) |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + func returnsError_WhenSetHasReplayMethodCall_InvalidParameter() throws { |
| 145 | + // Given |
| 146 | + let plugin = DatadogSessionReplayPlugin(channel: FlutterMethodChannelMock()) |
| 147 | + let arguments: [String: Any] = [ |
| 148 | + "hasReplay": "true" |
| 149 | + ] |
| 150 | + let methodCall = FlutterMethodCall(methodName: "setHasReplay", arguments: arguments) |
| 151 | + |
| 152 | + // When |
| 153 | + plugin.enable(configuration: .init()) |
| 154 | + var status: ResultStatus = .notCalled |
| 155 | + plugin.handle(methodCall) { result in |
| 156 | + status = .called(value: result) |
| 157 | + } |
| 158 | + |
| 159 | + // Then |
| 160 | + try checkResult(status, errorCode: "DatadogSdk:ContractViolation", errorMessage: "Missing parameter in call to setHasReplay") |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + func setsContext_WhenSetRecordCountMethodCall() throws { |
| 165 | + let plugin = DatadogSessionReplayPlugin(channel: FlutterMethodChannelMock()) |
| 166 | + let expectedValue: Int = .mockRandom() |
| 167 | + let expectedViewId: String = .mockRandom() |
| 168 | + let arguments: [String: Any] = [ |
| 169 | + "viewId": expectedViewId, |
| 170 | + "count": expectedValue |
| 171 | + ] |
| 172 | + let methodCall = FlutterMethodCall(methodName: "setRecordCount", arguments: arguments) |
| 173 | + |
| 174 | + // When |
| 175 | + plugin.enable(configuration: .init()) |
| 176 | + var status: ResultStatus = .notCalled |
| 177 | + plugin.handle(methodCall) { result in |
| 178 | + status = .called(value: result) |
| 179 | + } |
| 180 | + |
| 181 | + // Then |
| 182 | + #expect(status == .called(value: nil)) |
| 183 | + let value = try mockCore.context.baggages["sr_records_count_by_view_id"]?.encode() as? [String: Any] |
| 184 | + #expect(value?.count == 1) |
| 185 | + #expect(value?[expectedViewId] as? Int == expectedValue) |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + func broadcastsRumContext_WhenContextChanges() throws { |
| 190 | + // Given |
| 191 | + let methodChannelMock = FlutterMethodChannelMock() |
| 192 | + let plugin = DatadogSessionReplayPlugin(channel: methodChannelMock) |
| 193 | + plugin.enable(configuration: .init()) |
| 194 | + |
| 195 | + // When |
| 196 | + let expectedRumContext: RUMContext = .mockRandom() |
| 197 | + let datadogContext: DatadogContext = .mockRandom( |
| 198 | + withBaggages: [ |
| 199 | + RUMContext.key: .init(expectedRumContext)] |
| 200 | + ) |
| 201 | + mockCore.send(message: .context(datadogContext), else: {}) |
| 202 | + |
| 203 | + // Then |
| 204 | + // Push to the back of the main queue, as method channel must send |
| 205 | + // callbacks on the main queue |
| 206 | + try DispatchQueue.main.sync { |
| 207 | + try #require(methodChannelMock.invocations.count == 1) |
| 208 | + let argument = methodChannelMock.invocations[0].arguments as? [String: Any?] |
| 209 | + try #require(argument != nil) |
| 210 | + if let argument = argument { |
| 211 | + #expect(argument["applicationId"] as? String == expectedRumContext.applicationID) |
| 212 | + #expect(argument["sessionId"] as? String == expectedRumContext.sessionID) |
| 213 | + #expect(argument["viewId"] as? String == expectedRumContext.viewID) |
| 214 | + #expect(argument["viewServerTimeOffset"] as? TimeInterval == expectedRumContext.viewServerTimeOffset) |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | +} |
0 commit comments