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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import Foundation
import Observation
import UIKit
import XCTest
@testable import OldFella
extension NotificationLoadingTests {
func testFeedPaginationPublishesChangedCollectionsOnce() async {
let service = NotificationLoadingService()
let author = Post.Author(id: "other", name: "Other", handle: "other")
func post(_ id: String, likes: Int = 0) -> Post {
Post(
id: id, author: author, text: id, likeCount: likes, repostCount: 0, isLiked: false, isReposted: false,
imageURLs: [], replyIDs: [])
}
let first = post("first")
let context = post("context")
service.feedPages["initial"] = .success(
FeedPage(posts: [first], cursor: "next", replyContexts: [first.id: [context]]))
service.feedPages["next"] = .failure(ServiceError.invalidResponse)
seedPaginationNotification(service, author: author, post: first)
let model = AppModel(service: service)
await model.signInWithDemo()
await model.loadNotifications()
let changed = post(first.id, likes: 3)
let second = post("second")
let third = post("third")
service.feedPages["next"] = .success(
FeedPage(
posts: [changed, second, third, second], cursor: "duplicate",
replyContexts: [first.id: [context], second.id: [post("new-context")]]))
service.feedPages["duplicate"] = .failure(ServiceError.invalidResponse)
let posts = ObservationProbe { model.posts }
let replies = ObservationProbe { model.replyContexts }
let notifications = ObservationProbe { model.notifications }
let preparations = model.contentPreparationCount
await model.loadNextPage()
await Task.yield()
XCTAssertEqual(posts.values.count, 1)
XCTAssertEqual(replies.values.count, 1)
XCTAssertEqual(notifications.values.count, 1)
XCTAssertEqual(posts.values.first?.map(\.id), [first.id, second.id, third.id])
XCTAssertEqual(replies.values.first?[second.id]?.map(\.id), ["new-context"])
XCTAssertEqual(notifications.values.first?.first?.relatedPost?.likeCount, 3)
XCTAssertEqual(model.posts.map(\.id), [first.id, second.id, third.id])
XCTAssertEqual(model.timelinePosts.map(\.id), [first.id, second.id, third.id])
XCTAssertEqual(model.visibleNotifications.first?.relatedPost?.likeCount, 3)
XCTAssertEqual(model.contentPreparationCount, preparations + 1)
let publicationCounts = [posts.values.count, replies.values.count, notifications.values.count]
let evaluations = model.moderationEvaluationCount
let prepared = model.contentPreparationCount
service.feedPages["duplicate"] = .success(
FeedPage(posts: [changed, second, third], cursor: nil, replyContexts: model.replyContexts))
await model.loadNextPage()
await Task.yield()
XCTAssertEqual([posts.values.count, replies.values.count, notifications.values.count], publicationCounts)
XCTAssertEqual(model.contentPreparationCount, prepared)
XCTAssertEqual(model.moderationEvaluationCount, evaluations)
}
func testManualPagesKeepRelatedNotificationsGroupedWithinEachPage() async {
let service = NotificationLoadingService()
service.pages = [
"initial": .success(
.init(notifications: [notification("a", reason: .like, postID: "post")], cursor: "one")),
"one": .success(
.init(
notifications: [
notification("b", reason: .like, postID: "post"),
notification("c", reason: .like, postID: "post")
], cursor: "two")),
"two": .success(.init(notifications: [notification("last")], cursor: nil))
]
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
_ = await model.loadNotifications()
XCTAssertEqual(service.requests, ["initial"])
XCTAssertEqual(model.notificationGroups.first?.notifications.map(\.id), ["a"])
await model.loadNextNotificationsPage()
XCTAssertEqual(service.requests, ["initial", "one"])
XCTAssertEqual(model.notificationGroups.map { $0.notifications.map(\.id) }, [["a"], ["b", "c"]])
await model.loadNextNotificationsPage()
XCTAssertEqual(service.requests, ["initial", "one", "two"])
}
func testFilteredNotificationsDoNotCountTowardPageSize() async {
let service = NotificationLoadingService()
service.mutedWords = [.init(value: "hidden")]
let author = Post.Author(id: "author", name: "Author", handle: "author")
let post = Post(
id: "post", author: author, text: "hidden", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false,
imageURLs: [], replyIDs: [])
let hidden = FeedNotification(
id: "hidden", author: author, reason: .mention, indexedAt: Date(), isRead: false, postID: post.id,
relatedPost: post)
service.pages = [
"initial": .success(.init(notifications: [hidden], cursor: "one")),
"one": .success(.init(notifications: [notification("last")], cursor: nil))
]
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
_ = await model.loadNotifications()
XCTAssertEqual(model.visibleNotifications.map(\.id), ["last"])
XCTAssertEqual(service.requests, ["initial", "one"])
}
func testEmptyDuplicatePagesAndCursorCycleStop() async {
let service = NotificationLoadingService()
let item = notification("a")
service.pages = [
"initial": .success(.init(notifications: [item, item], cursor: "one")),
"one": .success(.init(notifications: [], cursor: "two")),
"two": .success(.init(notifications: [item], cursor: "one"))
]
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
_ = await model.loadNotifications()
XCTAssertEqual(model.notifications.map(\.id), ["a"])
XCTAssertEqual(service.requests, ["initial"])
await model.loadNextNotificationsPage()
XCTAssertEqual(service.requests, ["initial", "one", "two"])
XCTAssertFalse(model.hasMoreNotifications)
}
func testPartialFailureRetriesFromFailedCursor() async {
let service = NotificationLoadingService()
service.pages = [
"initial": .success(.init(notifications: [notification("a")], cursor: "one")),
"one": .failure(ServiceError.invalidResponse)
]
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
_ = await model.loadNotifications()
XCTAssertEqual(model.notifications.map(\.id), ["a"])
XCTAssertFalse(model.retryNotificationsPage)
await model.loadNextNotificationsPage()
XCTAssertTrue(model.retryNotificationsPage)
service.pages["one"] = .success(.init(notifications: [notification("b")], cursor: nil))
await model.loadNextNotificationsPage()
XCTAssertEqual(model.notifications.map(\.id), ["a", "b"])
XCTAssertEqual(service.requests, ["initial", "one", "one"])
}
func testTenEmptyRefreshPagesPreserveCursorForLoadMore() async {
let service = NotificationLoadingService()
service.pages["initial"] = .success(.init(notifications: [], cursor: "cursor-1"))
for page in 1..<10 {
service.pages["cursor-\(page)"] = .success(.init(notifications: [], cursor: "cursor-\(page + 1)"))
}
service.pages["cursor-10"] = .success(.init(notifications: [notification("visible")], cursor: nil))
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
_ = await model.loadNotifications()
XCTAssertTrue(model.notifications.isEmpty)
XCTAssertTrue(model.hasMoreNotifications)
XCTAssertEqual(service.requests, ["initial"] + (1...9).map { "cursor-\($0)" })
await model.loadNextNotificationsPage()
XCTAssertEqual(model.notifications.map(\.id), ["visible"])
XCTAssertFalse(model.hasMoreNotifications)
XCTAssertEqual(service.requests, ["initial"] + (1...10).map { "cursor-\($0)" })
}
func testRefreshPublishesBeforeLaterPageFailureAndRetriesFailedCursor() async {
let service = NotificationLoadingService()
service.pages["initial"] = .success(.init(notifications: [notification("old")], cursor: nil))
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
_ = await model.loadNotifications()
service.pages["initial"] = .success(.init(notifications: [notification("new")], cursor: "refresh-next"))
service.pages["refresh-next"] = .failure(ServiceError.invalidResponse)
let accepted = await model.loadNotifications()
XCTAssertTrue(accepted)
XCTAssertEqual(model.notifications.map(\.id), ["new"])
XCTAssertFalse(model.retryNotificationsPage)
await model.loadNextNotificationsPage()
XCTAssertEqual(model.notifications.map(\.id), ["new"])
XCTAssertTrue(model.retryNotificationsPage)
XCTAssertTrue(model.hasMoreNotifications)
service.pages["refresh-next"] = .success(.init(notifications: [notification("later")], cursor: nil))
await model.loadNextNotificationsPage()
XCTAssertEqual(model.notifications.map(\.id), ["new", "later"])
XCTAssertEqual(service.requests, ["initial", "initial", "refresh-next", "refresh-next"])
}
func testCancellationOnSecondRefreshPageKeepsOldRows() async {
let service = NotificationLoadingService()
service.pages["initial"] = .success(.init(notifications: [notification("old")], cursor: nil))
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
_ = await model.loadNotifications()
service.pages["initial"] = .success(.init(notifications: [], cursor: "second"))
service.suspendRequest(for: "second")
let refresh = Task { await model.loadNotifications() }
await service.waitUntilSuspendedRequest()
refresh.cancel()
service.resumeSuspendedRequest(with: .failure(CancellationError()))
let accepted = await refresh.value
XCTAssertFalse(accepted)
XCTAssertEqual(model.notifications.map(\.id), ["old"])
XCTAssertNil(model.notificationsErrorMessage)
XCTAssertEqual(service.requests, ["initial", "initial", "second"])
}
private func seedPaginationNotification(
_ service: NotificationLoadingService, author: Post.Author, post: Post
) {
service.pages["initial"] = .success(
NotificationPage(
notifications: [
FeedNotification(
id: "notification", author: author, reason: .like, indexedAt: Date(), isRead: false,
postID: post.id, relatedPost: post)
], cursor: nil))
}
}