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
import Foundation
import Observation
import UIKit
import XCTest
@testable import OldFella
extension NotificationLoadingTests {
func testWaitingRefreshLoadsAfterSuspendedRequestIsCancelled() async {
let service = NotificationLoadingService()
service.pages["initial"] = .success(.init(notifications: [notification("fresh")], cursor: nil))
service.suspendRequest(for: "initial")
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let first = Task { await model.loadNotifications() }
await service.waitUntilSuspendedRequest()
first.cancel()
let second = Task { await model.loadNotifications() }
await Task.yield()
XCTAssertEqual(service.requests, ["initial"])
service.resumeSuspendedRequest(with: .success(.init(notifications: [notification("discarded")], cursor: nil)))
let firstAccepted = await first.value
let secondAccepted = await second.value
XCTAssertFalse(firstAccepted)
XCTAssertTrue(secondAccepted)
XCTAssertEqual(model.notifications.map(\.id), ["fresh"])
XCTAssertEqual(service.requests, ["initial", "initial"])
}
func testInitialVisiblePagePublishesWithoutLoadingAnotherPage() async {
let service = NotificationLoadingService()
service.pages["initial"] = .success(.init(notifications: [notification("first")], cursor: "second"))
service.suspendRequest(for: "second")
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let updates = ObservationProbe { model.notifications.map(\.id) }
XCTAssertTrue(model.showsNotificationsPlaceholder)
_ = await model.loadNotificationsIfNeeded()
await Task.yield()
XCTAssertEqual(model.visibleNotifications.map(\.id), ["first"])
XCTAssertEqual(updates.values, [["first"]])
XCTAssertEqual(service.requests, ["initial"])
XCTAssertTrue(model.hasMoreNotifications)
XCTAssertFalse(model.showsNotificationsPlaceholder)
}
func testRefreshKeepsOldRowsUntilFirstPagePublishesOnce() 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()
let updates = ObservationProbe { model.notifications.map(\.id) }
service.suspendRequest(for: "initial")
let loading = Task { await model.loadNotifications() }
await service.waitUntilSuspendedRequest()
XCTAssertEqual(model.notifications.map(\.id), ["old"])
await Task.yield()
XCTAssertTrue(updates.values.isEmpty)
service.resumeSuspendedRequest(with: .success(.init(notifications: [notification("first")], cursor: "second")))
_ = await loading.value
await Task.yield()
XCTAssertEqual(updates.values, [["first"]])
XCTAssertEqual(service.requests, ["initial", "initial"])
XCTAssertTrue(model.hasMoreNotifications)
}
func testLoadMorePublishesFirstVisiblePageWithoutPrefetch() async {
let service = NotificationLoadingService()
let first = [notification("old")]
service.pages["initial"] = .success(.init(notifications: first, cursor: "more"))
service.pages["more"] = .success(.init(notifications: [notification("new")], cursor: "last"))
service.suspendRequest(for: "last")
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
_ = await model.loadNotifications()
let updates = ObservationProbe { model.notifications.map(\.id) }
await model.loadNextNotificationsPage()
await Task.yield()
XCTAssertEqual(updates.values, [["old", "new"]])
XCTAssertEqual(service.requests, ["initial", "more"])
XCTAssertTrue(model.hasMoreNotifications)
}
func testRefreshWithExistingRowsOnlyPublishesLoadingState() async {
let service = NotificationLoadingService()
service.pages["initial"] = .success(.init(notifications: [notification("old")], cursor: "more"))
service.pages["more"] = .success(.init(notifications: [], cursor: nil))
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
_ = await model.loadNotifications()
let loadingState = ObservationProbe { model.isLoadingNotifications }
let rows = ObservationProbe { model.notifications }
service.suspendRequest(for: "initial")
let loading = Task { await model.loadNotifications() }
await service.waitUntilSuspendedRequest()
await Task.yield()
XCTAssertEqual(loadingState.values, [true])
XCTAssertTrue(rows.values.isEmpty)
XCTAssertTrue(model.isLoadingNotifications)
XCTAssertEqual(model.notifications.map(\.id), ["old"])
await model.loadNextNotificationsPage()
XCTAssertEqual(service.requests, ["initial", "initial"])
service.resumeSuspendedRequest(with: .success(.init(notifications: model.notifications, cursor: nil)))
_ = await loading.value
await Task.yield()
XCTAssertEqual(loadingState.values, [true, false])
XCTAssertTrue(rows.values.isEmpty)
XCTAssertFalse(model.isLoadingNotifications)
}
func testEmptyStateRequiresACompletedLoad() async {
let service = NotificationLoadingService()
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
XCTAssertFalse(model.hasLoadedNotifications)
XCTAssertTrue(model.showsNotificationsPlaceholder)
_ = await model.loadNotificationsIfNeeded()
XCTAssertTrue(model.hasLoadedNotifications)
XCTAssertFalse(model.showsNotificationsPlaceholder)
XCTAssertTrue(model.notifications.isEmpty)
}
func testInitialFailureShowsErrorInsteadOfPermanentPlaceholder() async {
let service = NotificationLoadingService()
service.pages["initial"] = .failure(URLError(.timedOut))
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
_ = await model.loadNotificationsIfNeeded()
XCTAssertFalse(model.hasLoadedNotifications)
XCTAssertNotNil(model.notificationsErrorMessage)
XCTAssertFalse(model.showsNotificationsPlaceholder)
await model.signOut()
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
XCTAssertNil(model.notificationsErrorMessage)
XCTAssertTrue(model.showsNotificationsPlaceholder)
}
func testTabReentryDoesNotRefreshLoadedNotifications() async {
let service = NotificationLoadingService()
service.pages["initial"] = .success(.init(notifications: [notification("first")], cursor: nil))
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
_ = await model.loadNotificationsIfNeeded()
_ = await model.loadNotificationsIfNeeded()
XCTAssertEqual(service.requests, ["initial"])
_ = await model.loadNotifications()
XCTAssertEqual(service.requests, ["initial", "initial"])
}
func testTabCancellationDoesNotCancelInitialLoadOrStartAnother() async {
let service = NotificationLoadingService()
service.suspendRequest(for: "initial")
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let firstVisit = Task { await model.loadNotificationsIfNeeded() }
await service.waitUntilSuspendedRequest()
firstVisit.cancel()
let nextVisit = Task { await model.loadNotificationsIfNeeded() }
service.resumeSuspendedRequest(with: .success(.init(notifications: [notification("first")], cursor: nil)))
_ = await firstVisit.value
let accepted = await nextVisit.value
XCTAssertTrue(accepted)
XCTAssertEqual(model.notifications.map(\.id), ["first"])
XCTAssertEqual(service.requests, ["initial"])
}
func testCancellationDoesNotShowErrorAndRefreshFailureKeepsOldRows() async {
let service = NotificationLoadingService()
service.pages["initial"] = .success(.init(notifications: [notification("a")], cursor: nil))
let model = AppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
_ = await model.loadNotifications()
service.pages["initial"] = .failure(CancellationError())
let accepted = await model.loadNotifications()
XCTAssertFalse(accepted)
XCTAssertEqual(model.notifications.map(\.id), ["a"])
XCTAssertNil(model.notificationsErrorMessage)
}
}