-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFCMTokenSyncHandlerTests.swift
More file actions
176 lines (149 loc) · 5.79 KB
/
Copy pathFCMTokenSyncHandlerTests.swift
File metadata and controls
176 lines (149 loc) · 5.79 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
//
// FCMTokenSyncHandlerTests.swift
// DevLogAppTests
//
// Created by opfic on 6/13/26.
//
import Foundation
import Testing
import DevLogData
@testable import DevLogApp
struct FCMTokenSyncHandlerTests {
@Test("현재 FCM token 동기화 요청 시 token이 있으면 저장한다")
func 현재_FCM_token_동기화_요청_시_token이_있으면_저장한다() async throws {
let notificationCenter = NotificationCenter()
let registrationObserver = NotificationObserver(
notificationCenter: notificationCenter,
name: .didRequestRemoteNotificationRegistration
)
let messagingService = PushMessagingServiceSpy(currentFCMToken: "current-token")
let userService = UserServiceSpy()
let handler = FCMTokenSyncHandler(
messagingService: messagingService,
userService: userService,
notificationCenter: notificationCenter
)
notificationCenter.post(name: .didRequestFCMTokenSync, object: nil)
try await waitUntil {
await userService.updatedFCMTokens == ["current-token"]
}
#expect(registrationObserver.didReceiveNotification)
_ = handler
}
@Test("현재 FCM token 동기화 요청 시 token이 없으면 저장하지 않는다")
func 현재_FCM_token_동기화_요청_시_token이_없으면_저장하지_않는다() async throws {
let notificationCenter = NotificationCenter()
let messagingService = PushMessagingServiceSpy(currentFCMToken: nil)
let userService = UserServiceSpy()
let handler = FCMTokenSyncHandler(
messagingService: messagingService,
userService: userService,
notificationCenter: notificationCenter
)
notificationCenter.post(name: .didRequestFCMTokenSync, object: nil)
try await Task.sleep(for: .milliseconds(100))
#expect(await userService.updatedFCMTokens.isEmpty)
_ = handler
}
@Test("갱신된 FCM token 이벤트 수신 시 저장한다")
func 갱신된_FCM_token_이벤트_수신_시_저장한다() async throws {
let notificationCenter = NotificationCenter()
let messagingService = PushMessagingServiceSpy(currentFCMToken: nil)
let userService = UserServiceSpy()
let handler = FCMTokenSyncHandler(
messagingService: messagingService,
userService: userService,
notificationCenter: notificationCenter
)
notificationCenter.post(
name: .didRefreshFCMToken,
object: nil,
userInfo: ["fcmToken": "refreshed-token"]
)
try await waitUntil {
await userService.updatedFCMTokens == ["refreshed-token"]
}
_ = handler
}
@Test("APNs token 이벤트 수신 시 APNs token을 적용하고 현재 FCM token을 저장한다")
func APNs_token_이벤트_수신_시_APNs_token을_적용하고_현재_FCM_token을_저장한다() async throws {
let notificationCenter = NotificationCenter()
let messagingService = PushMessagingServiceSpy(currentFCMToken: "current-token")
let userService = UserServiceSpy()
let handler = FCMTokenSyncHandler(
messagingService: messagingService,
userService: userService,
notificationCenter: notificationCenter
)
let deviceToken = Data([0x01, 0x02, 0x03])
notificationCenter.post(
name: .didReceiveAPNSToken,
object: nil,
userInfo: ["deviceToken": deviceToken]
)
try await waitUntil {
await userService.updatedFCMTokens == ["current-token"]
}
#expect(messagingService.apnsTokens == [deviceToken])
_ = handler
}
}
private actor UserServiceSpy: UserService {
private(set) var updatedFCMTokens = [String]()
func upsertUser(_ response: AuthDataResponse) async throws { }
func fetchUserProfile() async throws -> UserProfileResponse { fatalError() }
func upsertStatusMessage(_ message: String) async throws { }
func updateFCMToken(_ fcmToken: String) async throws {
updatedFCMTokens.append(fcmToken)
}
func updateUserTimeZone() async throws { }
}
private final class PushMessagingServiceSpy: PushMessagingService {
private let currentFCMToken: String?
private(set) var apnsTokens = [Data]()
init(currentFCMToken: String?) {
self.currentFCMToken = currentFCMToken
}
func setDelegate(_ delegate: PushMessagingServiceDelegate?) { }
func setAPNSToken(_ deviceToken: Data) {
apnsTokens.append(deviceToken)
}
func isNotificationAuthorized() async -> Bool { true }
func fetchFCMToken() async throws -> String? {
currentFCMToken
}
}
private final class NotificationObserver {
private(set) var didReceiveNotification = false
private var token: NSObjectProtocol?
private let notificationCenter: NotificationCenter
init(notificationCenter: NotificationCenter, name: Notification.Name) {
self.notificationCenter = notificationCenter
self.token = notificationCenter.addObserver(
forName: name,
object: nil,
queue: nil
) { [weak self] _ in
self?.didReceiveNotification = true
}
}
deinit {
if let token {
notificationCenter.removeObserver(token)
}
}
}
private func waitUntil(
timeout: Duration = .seconds(1),
pollInterval: Duration = .milliseconds(10),
condition: @escaping @Sendable () async -> Bool
) async throws {
let deadline = ContinuousClock.now + timeout
while ContinuousClock.now < deadline {
if await condition() {
return
}
try await Task.sleep(for: pollInterval)
}
Issue.record("조건을 만족하지 못함")
}