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
import Foundation
import Observation
import UIKit
import XCTest
@testable import OldFella
extension NotificationLoadingTests {
func testPadRefreshPublishesOnePageAndLeavesContinuationManual() 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: nil))
let before = service.feedRequests
await model.loadPadFeed(id: PadColumn.following.id, refresh: true)
let state = model.padFeedState(id: PadColumn.following.id)
XCTAssertEqual(state.rowIDs, ["one"])
XCTAssertFalse(state.isLoading)
XCTAssertTrue(state.hasMore)
XCTAssertEqual(service.feedRequests, before + 1)
await model.loadNextPadFeedPage(id: PadColumn.following.id)
XCTAssertEqual(state.rowIDs, ["one", "two"])
XCTAssertEqual(service.feedRequests, before + 2)
XCTAssertFalse(state.hasMore)
}
func testFeedRefreshWaitsForInFlightPageThenLoadsFirstPage() 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")
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: "more"))
await model.loadFeed()
service.suspendFeedRequest(for: "more")
let page = Task { await model.loadNextPage() }
await service.waitUntilSuspendedFeedRequest()
service.suspendRequest(for: "initial")
let refresh = Task { await model.refreshFeed() }
await service.waitUntilSuspendedRequest()
XCTAssertEqual(service.feedCursors.suffix(2), ["initial", "more"])
XCTAssertTrue(model.isRefreshingFeed)
service.resumeSuspendedRequest(with: .success(.init(notifications: [], cursor: nil)))
service.feedPages["initial"] = .success(.init(posts: [], cursor: nil))
service.resumeSuspendedFeedRequest(with: .success(.init(posts: [], cursor: nil)))
await page.value
await refresh.value
XCTAssertEqual(service.feedCursors.suffix(3), ["initial", "more", "initial"])
XCTAssertFalse(model.hasMorePosts)
XCTAssertFalse(model.isRefreshingFeed)
}
func testFeedMutedPostIDsDoNotRejectConcurrentNotifications() async {
let service = NotificationLoadingService()
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
service.suspendRequest(for: "initial")
service.feedPages["initial"] = .success(.init(posts: [], cursor: nil, mutedPostIDs: ["muted-post"]))
let notifications = Task { await model.loadNotifications() }
await service.waitUntilSuspendedRequest()
await model.loadFeed()
service.resumeSuspendedRequest(with: .success(.init(notifications: [notification("new")], cursor: nil)))
let accepted = await notifications.value
XCTAssertTrue(accepted)
XCTAssertEqual(model.notifications.map(\.id), ["new"])
XCTAssertTrue(model.hasUnreadNotifications)
XCTAssertNil(model.notificationsErrorMessage)
}
func testFeedFailureStillLoadsNotificationsAndBadge() async {
let service = NotificationLoadingService()
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
service.feedPages["initial"] = .failure(ServiceError.invalidResponse)
service.pages["initial"] = .success(.init(notifications: [notification("new")], cursor: nil))
await model.refreshFeed()
XCTAssertNotNil(model.feedErrorMessage)
XCTAssertEqual(model.notifications.map(\.id), ["new"])
XCTAssertTrue(model.hasUnreadNotifications)
XCTAssertFalse(model.isRefreshingFeed)
}
func testStaleNotificationResponseKeepsRowsReadAfterMarkRead() async throws {
let service = NotificationLoadingService()
let old = notification("old")
service.pages["initial"] = .success(.init(notifications: [old], cursor: nil))
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
_ = await model.loadNotifications()
service.suspendRequest(for: "initial")
let refresh = Task { await model.loadNotifications() }
await service.waitUntilSuspendedRequest()
try await model.markNotificationsRead(seenAt: old.indexedAt.addingTimeInterval(1))
XCTAssertTrue(model.notifications[0].isRead)
XCTAssertFalse(model.hasUnreadNotifications)
service.resumeSuspendedRequest(with: .success(.init(notifications: [old], cursor: nil)))
let accepted = await refresh.value
XCTAssertTrue(accepted)
XCTAssertEqual(model.notifications.map(\.id), ["old"])
XCTAssertTrue(model.notifications[0].isRead)
XCTAssertFalse(model.hasUnreadNotifications)
}
func testSignOutDuringRefreshDoesNotRestoreNotificationsOrBadge() async {
let service = NotificationLoadingService()
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
service.suspendRequest(for: "initial")
service.suspendFeedRequest(for: "initial")
let refresh = Task { await model.refreshFeed() }
await service.waitUntilSuspendedRequest()
await service.waitUntilSuspendedFeedRequest()
let stalePosts = model.posts
await model.signOut()
service.resumeSuspendedFeedRequest(with: .success(.init(posts: stalePosts, cursor: nil)))
service.resumeSuspendedRequest(with: .success(.init(notifications: [notification("stale")], cursor: nil)))
await refresh.value
XCTAssertNil(model.account)
XCTAssertTrue(model.notifications.isEmpty)
XCTAssertFalse(model.hasUnreadNotifications)
XCTAssertTrue(model.posts.isEmpty)
}
func testReadAcknowledgmentsNeverSendAnOlderTimestamp() async throws {
let service = NotificationLoadingService()
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let newer = Date()
try await model.markNotificationsRead(seenAt: newer)
try await model.markNotificationsRead(seenAt: newer.addingTimeInterval(-60))
XCTAssertEqual(service.readDates, [newer, newer])
}
func testNotificationRefreshDoesNotRefreshFeed() async {
let service = NotificationLoadingService()
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let feedRequests = service.feedRequests
_ = await model.loadNotifications()
XCTAssertEqual(service.feedRequests, feedRequests)
}
func testUnreadStateClearsOnlyAfterServerSuccessAndKeepsNewerActivity() async throws {
let service = NotificationLoadingService()
let old = notification("old")
let seenAt = old.indexedAt.addingTimeInterval(1)
let newer = FeedNotification(
id: "new", author: old.author, reason: .follow,
indexedAt: seenAt.addingTimeInterval(1), isRead: false, postID: nil)
service.pages["initial"] = .success(.init(notifications: [old], cursor: nil))
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
_ = await model.loadNotifications()
service.readError = ServiceError.invalidResponse
do {
try await model.markNotificationsRead(seenAt: seenAt)
XCTFail("Expected the server request to fail")
} catch {}
XCTAssertTrue(model.hasUnreadNotifications)
service.readError = nil
try await model.markNotificationsRead(seenAt: seenAt)
XCTAssertFalse(model.hasUnreadNotifications)
XCTAssertTrue(model.notifications[0].isRead)
service.pages["initial"] = .success(.init(notifications: [newer, old], cursor: nil))
_ = await model.loadNotifications()
XCTAssertTrue(model.hasUnreadNotifications)
XCTAssertFalse(model.notifications[0].isRead)
XCTAssertTrue(model.notifications[1].isRead)
}
func testFeedRefreshEndsAfterNotificationFailure() async {
let service = NotificationLoadingService()
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
service.pages["initial"] = .failure(ServiceError.invalidResponse)
await model.refreshFeed()
XCTAssertFalse(model.isRefreshingFeed)
XCTAssertNotNil(model.notificationsErrorMessage)
XCTAssertFalse(model.posts.isEmpty)
}
}