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
import Foundation
import Observation
import UIKit
import XCTest
@testable import OldFella
@MainActor
final class NotificationLoadingTests: XCTestCase {
func notification(_ id: String, reason: FeedNotification.Reason = .follow, postID: String? = nil)
-> FeedNotification {
FeedNotification(
id: id, author: Post.Author(id: id, name: id, handle: id), reason: reason,
indexedAt: Date(), isRead: false, postID: postID)
}
func testNotificationFiltersCombineMutualsAndTypes() {
var mutualLike = notification("mutual", reason: .like)
mutualLike.isFollowedBy = true
let otherLike = notification("other", reason: .like)
var mutualReply = notification("reply", reason: .reply)
mutualReply.isFollowedBy = true
let filter = NotificationFilter(mutualsOnly: true, excludedTypesRaw: "reply,unknown-value")
XCTAssertTrue(filter.includes(mutualLike, follows: true))
XCTAssertFalse(filter.includes(mutualLike, follows: false))
XCTAssertFalse(filter.includes(otherLike, follows: true))
XCTAssertFalse(filter.includes(mutualReply, follows: true))
XCTAssertEqual(filter.excludedTypes, [.reply])
XCTAssertTrue(filter.isActive)
XCTAssertFalse(NotificationFilter(mutualsOnly: false, excludedTypesRaw: "").isActive)
}
func testNotificationTypeSelectionKeepsAtLeastOneType() {
let all = NotificationFilter(mutualsOnly: false, excludedTypesRaw: "")
XCTAssertTrue(all.showsAllTypes)
XCTAssertFalse(all.typeIsSelected(.like))
let one = NotificationFilter(mutualsOnly: false, excludedTypesRaw: all.settingType(.like, enabled: true))
XCTAssertFalse(one.showsAllTypes)
XCTAssertTrue(one.typeIsSelected(.like))
XCTAssertFalse(one.typeIsSelected(.reply))
XCTAssertEqual(
one.settingType(.like, enabled: false), one.excludedTypes.map(\.rawValue).sorted().joined(separator: ","))
let two = NotificationFilter(mutualsOnly: false, excludedTypesRaw: one.settingType(.reply, enabled: true))
XCTAssertTrue(two.typeIsSelected(.like))
XCTAssertTrue(two.typeIsSelected(.reply))
let emptySavedSelection = FeedNotification.Reason.allCases.map(\.rawValue).joined(separator: ",")
XCTAssertTrue(NotificationFilter(mutualsOnly: false, excludedTypesRaw: emptySavedSelection).showsAllTypes)
}
func testSharedRefreshDrivesNativeControlWithoutFiringManualRefresh() {
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 390, height: 844))
let container = UIView(frame: window.bounds)
window.addSubview(container)
let scroll = UIScrollView(frame: container.bounds)
scroll.contentSize = CGSize(width: 390, height: 2000)
let control = UIRefreshControl()
scroll.refreshControl = control
container.addSubview(scroll)
let probe = ScrollRefreshControl.Probe(frame: container.bounds)
container.addSubview(probe)
window.isHidden = false
defer { window.isHidden = true }
probe.isActive = true
probe.isRefreshing = true
probe.synchronize()
XCTAssertTrue(control.isRefreshing)
XCTAssertLessThan(scroll.contentOffset.y, 0)
probe.isRefreshing = false
probe.synchronize()
XCTAssertFalse(control.isRefreshing)
}
func testSharedRefreshDoesNotMoveOlderNotificationsOrEndManualRefresh() {
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 390, height: 844))
let container = UIView(frame: window.bounds)
window.addSubview(container)
let scroll = UIScrollView(frame: container.bounds)
scroll.contentSize = CGSize(width: 390, height: 2000)
let control = UIRefreshControl()
scroll.refreshControl = control
container.addSubview(scroll)
let probe = ScrollRefreshControl.Probe(frame: container.bounds)
container.addSubview(probe)
window.isHidden = false
defer { window.isHidden = true }
scroll.contentOffset.y = 300
probe.isActive = true
probe.isRefreshing = true
probe.synchronize()
XCTAssertEqual(scroll.contentOffset.y, 300)
probe.finish()
control.beginRefreshing()
probe.synchronize()
probe.finish()
XCTAssertTrue(control.isRefreshing)
control.endRefreshing()
}
func testFeedRefreshDoesNotWaitForNotifications() async {
let service = NotificationLoadingService()
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
service.feedPages["initial"] = .success(.init(posts: [], cursor: nil))
let feedRequests = service.feedRequests
service.suspendRequest(for: "initial")
let refresh = Task { await model.refreshFeed() }
await service.waitUntilSuspendedRequest()
await refresh.value
XCTAssertFalse(model.isRefreshingFeed)
XCTAssertEqual(service.requests, ["initial"])
XCTAssertEqual(service.feedRequests, feedRequests + 1)
service.resumeSuspendedRequest(with: .success(.init(notifications: [notification("new")], cursor: nil)))
for _ in 0..<100 where !model.hasUnreadNotifications { await Task.yield() }
XCTAssertTrue(model.hasUnreadNotifications)
}
func testRefreshPublishesOnePageAndManualPaginationRetriesFailure() async {
let service = NotificationLoadingService()
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let author = Post.Author(id: "a", name: "A", handle: "a")
func post(_ id: String) -> Post {
Post(
id: id, author: author, text: id, likeCount: 0, repostCount: 0, isLiked: false, isReposted: false,
imageURLs: [], replyIDs: [])
}
service.feedPages["initial"] = .success(.init(posts: [post("one")], cursor: "second"))
service.feedPages["second"] = .success(.init(posts: [post("two")], cursor: "third"))
service.feedPages["third"] = .failure(ServiceError.invalidResponse)
await model.refreshFeed()
XCTAssertFalse(model.isRefreshingFeed)
XCTAssertEqual(model.timelineRowIDs, ["one"])
XCTAssertEqual(service.feedCursors.last, "initial")
await model.loadNextPage()
XCTAssertEqual(model.timelineRowIDs, ["one", "two"])
XCTAssertNil(model.feedErrorMessage)
await model.loadNextPage()
XCTAssertTrue(model.retryFeedPage)
XCTAssertEqual(model.timelineRowIDs, ["one", "two"])
service.feedPages["third"] = .success(.init(posts: [post("three")], cursor: nil))
await model.loadNextPage()
XCTAssertEqual(model.timelineRowIDs, ["one", "two", "three"])
XCTAssertEqual(service.feedCursors.suffix(4), ["initial", "second", "third", "third"])
}
func testRefreshDoesNotPrefetchOlderPagesBeforeAnotherRefresh() async {
let service = NotificationLoadingService()
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let author = Post.Author(id: "a", name: "A", handle: "a")
func post(_ id: String) -> Post {
Post(
id: id, author: author, text: id, likeCount: 0, repostCount: 0, isLiked: false, isReposted: false,
imageURLs: [], replyIDs: [])
}
service.feedPages["initial"] = .success(.init(posts: [post("one")], cursor: "second"))
service.suspendFeedRequest(for: "second")
let before = service.feedRequests
await model.refreshFeed()
XCTAssertEqual(model.timelineRowIDs, ["one"])
XCTAssertEqual(service.feedRequests, before + 1)
service.feedPages["initial"] = .success(.init(posts: [post("fresh")], cursor: nil))
await model.refreshFeed()
XCTAssertEqual(model.timelineRowIDs, ["fresh"])
XCTAssertEqual(service.feedRequests, before + 2)
}
func testRefreshDoesNotRepublishUnchangedPageOrRequestContinuation() async {
let service = NotificationLoadingService()
let author = Post.Author(id: "a", name: "A", handle: "a")
let posts = (0..<15).map {
Post(
id: "p\($0)", author: author, text: "safe", likeCount: 0, repostCount: 0, isLiked: false,
isReposted: false, imageURLs: [], replyIDs: [])
}
service.feedPages["initial"] = .success(.init(posts: posts, cursor: "second"))
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let changes = ObservationProbe { model.posts }
service.suspendFeedRequest(for: "second")
await model.refreshFeed()
await Task.yield()
XCTAssertEqual(model.posts, posts)
XCTAssertTrue(changes.values.isEmpty)
XCTAssertEqual(service.feedCursors, ["initial", "initial"])
XCTAssertTrue(model.hasMorePosts)
}
func testRefreshStopsAfterFivePagesAndLeavesLoadMoreManual() async {
let service = NotificationLoadingService()
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
service.feedPages["initial"] = .success(.init(posts: [], cursor: "1"))
for page in 1...5 { service.feedPages["\(page)"] = .success(.init(posts: [], cursor: "\(page + 1)")) }
await model.refreshFeed()
for _ in 0..<100 where service.feedCursors.suffix(5) != ["initial", "1", "2", "3", "4"] { await Task.yield() }
XCTAssertEqual(service.feedCursors.suffix(5), ["initial", "1", "2", "3", "4"])
XCTAssertTrue(model.hasMorePosts)
await model.loadNextPage()
XCTAssertTrue(service.feedCursors.contains("5"))
}
func testSignOutRejectsPendingEmptyPageFallbackResponse() async {
let service = NotificationLoadingService()
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
service.feedPages["initial"] = .success(.init(posts: [], cursor: "second"))
service.feedPages["second"] = .success(.init(posts: [], cursor: "third"))
service.suspendFeedRequest(for: "third")
let refresh = Task { await model.refreshFeed() }
await service.waitUntilSuspendedFeedRequest()
await model.signOut()
let author = Post.Author(id: "a", name: "A", handle: "a")
let stale = Post(
id: "stale", author: author, text: "stale", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false,
imageURLs: [], replyIDs: [])
service.resumeSuspendedFeedRequest(with: .success(.init(posts: [stale], cursor: nil)))
await refresh.value
XCTAssertNil(model.account)
XCTAssertTrue(model.posts.isEmpty)
}
}