-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUID2PrebidTests.swift
More file actions
186 lines (166 loc) · 4.97 KB
/
Copy pathUID2PrebidTests.swift
File metadata and controls
186 lines (166 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//
// UID2PrebidTests.swift
//
//
// Created by Dave Snabel-Caunt on 21/07/2024.
//
import Foundation
import PrebidMobile
import TestHelpers
@testable import UID2
@testable import UID2Prebid
import XCTest
@MainActor
final class TestUserIDUpdater: Sendable, UserIDUpdater {
var observer: (([ExternalUserId]) -> Void)?
func updateUserIDs(_ userIDs: [ExternalUserId]) {
observer?(userIDs)
}
}
final class UID2PrebidTests: XCTestCase {
var prebid: UID2Prebid!
@MainActor
func testObservation() async throws {
let manager = UID2Manager(
uid2Client: UID2Client(
sdkVersion: "1.0",
environment: Environment(UID2.Environment.production)
),
storage: .null,
sdkVersion: (1, 0, 0),
log: .disabled
)
let updater = TestUserIDUpdater()
let (stream, continuation) = AsyncStream<UID2Manager.State?>.makeStream()
prebid = UID2Prebid(
manager: manager,
userIDUpdater: updater,
initialToken: {
"cat"
},
stateStream: {
stream
}
)
await observation(
of: [
ExternalUserId(source: "uidapi.com", uids: [.init(id: "cat", aType: 3)])
],
by: updater
)
continuation.yield(.optout)
await observation(
of: [],
by: updater
)
continuation.yield(
.established(.established(advertisingToken: "turtle"))
)
await observation(
of: [
ExternalUserId(source: "uidapi.com", uids: [.init(id: "turtle", aType: 3)])
],
by: updater
)
}
@MainActor
func testObservationWithThirdPartyUserIDs() async throws {
let manager = UID2Manager(
uid2Client: UID2Client(
sdkVersion: "1.0",
environment: Environment(UID2.Environment.production)
),
storage: .null,
sdkVersion: (1, 0, 0),
log: .disabled
)
let updater = TestUserIDUpdater()
let (stream, continuation) = AsyncStream<UID2Manager.State?>.makeStream()
prebid = UID2Prebid(
manager: manager,
thirdPartyUserIDs: {
[
ExternalUserId(source: "example.com", uids: [.init(id: "dog", aType: 3)])
]
},
userIDUpdater: updater,
initialToken: {
"cat"
},
stateStream: {
stream
}
)
await observation(
of: [
ExternalUserId(source: "example.com", uids: [.init(id: "dog", aType: 3)]),
ExternalUserId(source: "uidapi.com", uids: [.init(id: "cat", aType: 3)]),
],
by: updater
)
continuation.yield(.invalid)
await observation(
of: [
ExternalUserId(source: "example.com", uids: [.init(id: "dog", aType: 3)]),
],
by: updater
)
}
@MainActor
func observation(of expectedUserIds: [ExternalUserId], by updater: TestUserIDUpdater) async {
let expectation = XCTestExpectation(description: "Expected Test Updater to observe specific value")
updater.observer = { userIds in
if Self.isEqual(expectedUserIds, userIds) {
expectation.fulfill()
}
}
await fulfillment(of: [expectation], timeout: 1)
}
}
extension UID2PrebidTests {
static func isEqual(
_ lhs: [ExternalUserId],
_ rhs: [ExternalUserId]
) -> Bool {
let lhs = lhs.map(ExternalUserIdEquatable.init)
let rhs = rhs.map(ExternalUserIdEquatable.init)
return lhs == rhs
}
struct ExternalUserIdEquatable: Equatable {
var source: String
var uids: [UserUniqueIDEquatable] = []
init(_ userId: ExternalUserId) {
self.source = userId.source
self.uids = userId.uids.map(UserUniqueIDEquatable.init)
}
}
struct UserUniqueIDEquatable: Equatable {
var id: String
var aType: Int
init(_ userId: UserUniqueID) {
self.id = userId.id
self.aType = userId.aType.intValue
}
}
}
private extension UID2Identity {
static func established(advertisingToken: String) -> UID2Identity {
.init(
advertisingToken: advertisingToken,
refreshToken: "r",
identityExpires: Date().millisecondsSince1970 + 100000,
refreshFrom: Date().millisecondsSince1970 + 100000,
refreshExpires: Date().millisecondsSince1970 + 100000,
refreshResponseKey: ""
)
}
}
private extension Storage {
static var null: Self {
Storage(
loadIdentity: { nil },
saveIdentity: { _ in },
clearIdentity: {}
)
}
}