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
import Observation
import XCTest
@testable import OldFella
extension AppModelTests {
func testOverlappingPreferenceRefreshesShareOneRequest() async {
let service = ModerationTestService()
let model = makeAppModel(service: service)
await model.signIn(handle: "demo", appPassword: "password")
let initialCalls = service.preferenceCalls
let gate = ThreadGate()
service.preferenceGate = gate
service.mutedWords = [.init(value: "updated")]
let first = Task { await model.reloadContentPreferences() }
await gate.waitUntilStarted()
var secondStarted = false
let second = Task {
secondStarted = true
await model.reloadContentPreferences()
}
while !secondStarted { await Task.yield() }
XCTAssertEqual(service.preferenceCalls, initialCalls + 1)
await gate.release()
await first.value
await second.value
XCTAssertEqual(service.preferenceCalls, initialCalls + 1)
XCTAssertEqual(model.contentPreferences?.mutedWords.map(\.value), ["updated"])
service.preferenceGate = nil
await model.reloadContentPreferences()
XCTAssertEqual(service.preferenceCalls, initialCalls + 2)
}
func testPreferenceRefreshCannotRestoreContentAfterSignOut() async {
let service = ModerationTestService()
let model = makeAppModel(service: service)
await model.signIn(handle: "demo", appPassword: "password")
let gate = ThreadGate()
service.preferenceGate = gate
let refresh = Task { await model.reloadContentPreferences() }
await gate.waitUntilStarted()
await model.signOut()
await gate.release()
await refresh.value
XCTAssertNil(model.account)
XCTAssertNil(model.contentPreferences)
XCTAssertTrue(model.timelinePosts.isEmpty)
service.preferenceGate = nil
await model.signIn(handle: "demo", appPassword: "password")
XCTAssertNotNil(model.contentPreferences)
}
func testNotificationGroupsKeepOrderAndSeparateReasonsAndPosts() {
func notification(_ id: String, _ reason: FeedNotification.Reason, _ postID: String?) -> FeedNotification {
FeedNotification(
id: id, author: Post.Author(id: id, name: id, handle: id), reason: reason,
indexedAt: .now, isRead: false, postID: postID)
}
let notifications = [
notification("1", .like, "a"), notification("2", .like, "a"),
notification("3", .repost, "a"), notification("4", .repost, "a"),
notification("5", .like, "a"), notification("6", .like, "b"),
notification("7", .like, nil), notification("8", .like, nil)
]
XCTAssertEqual(
NotificationGroup.make(from: notifications).map { $0.notifications.map(\.id) },
[["1", "2", "5"], ["3", "4"], ["6"], ["7"], ["8"]])
XCTAssertEqual(
NotificationGroup.make(from: notifications, pageStarts: ["5"]).map { $0.notifications.map(\.id) },
[["1", "2"], ["3", "4"], ["5"], ["6"], ["7"], ["8"]])
XCTAssertTrue(NotificationGroup.make(from: []).isEmpty)
}
func testElapsedTimeRoundsDown() {
let now = Date(timeIntervalSince1970: 2_000_000_000)
XCTAssertEqual(
PostElapsedTime.text(since: now.addingTimeInterval(-119), now: now),
PostElapsedTime.text(since: now.addingTimeInterval(-60), now: now))
XCTAssertNotEqual(
PostElapsedTime.text(since: now.addingTimeInterval(-120), now: now),
PostElapsedTime.text(since: now.addingTimeInterval(-119), now: now))
XCTAssertEqual(
PostElapsedTime.text(since: now.addingTimeInterval(20), now: now),
PostElapsedTime.text(since: now, now: now))
}
func testElapsedTimeUsesCompactUnits() {
let now = Date(timeIntervalSince1970: 2_000_000_000)
XCTAssertEqual(PostElapsedTime.text(since: now.addingTimeInterval(-59), now: now), "59s")
XCTAssertEqual(PostElapsedTime.text(since: now.addingTimeInterval(-60), now: now), "1m")
XCTAssertEqual(PostElapsedTime.text(since: now.addingTimeInterval(-3_600), now: now), "1h")
XCTAssertEqual(PostElapsedTime.text(since: now.addingTimeInterval(-86_400), now: now), "1d")
XCTAssertEqual(PostElapsedTime.text(since: now.addingTimeInterval(-604_800), now: now), "1w")
XCTAssertEqual(PostElapsedTime.text(since: now.addingTimeInterval(-31_536_000), now: now), "1y")
}
func testElapsedTimeStaysWithinFourCharactersForExtremeDates() {
let now = Date(timeIntervalSince1970: 2_000_000_000)
let farPast = PostElapsedTime.text(since: .distantPast, now: now)
let farFuture = PostElapsedTime.text(since: .distantFuture, now: now)
XCTAssertEqual(farPast, "999y")
XCTAssertEqual(farFuture, "0s")
XCTAssertLessThanOrEqual(farPast.count, 4)
XCTAssertLessThanOrEqual(farFuture.count, 4)
}
func testProfileRevealKeyChangesWithActorDecisionAndRevision() {
let revision = UUID()
let decision = ModerationPolicy.Decision(action: .warn, reasons: [])
let key = ProfileRevealKey(actorID: "one", decision: decision, contentRevision: revision)
XCTAssertEqual(key, ProfileRevealKey(actorID: "one", decision: decision, contentRevision: revision))
XCTAssertNotEqual(key, ProfileRevealKey(actorID: "two", decision: decision, contentRevision: revision))
XCTAssertNotEqual(key, ProfileRevealKey(actorID: "one", decision: nil, contentRevision: revision))
XCTAssertNotEqual(key, ProfileRevealKey(actorID: "one", decision: decision, contentRevision: UUID()))
}
func testForegroundRetryKeepsPreferenceErrorWhileLoading() async {
let service = ModerationTestService()
service.preferenceError = URLError(.notConnectedToInternet)
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let error = model.contentPreferencesError
XCTAssertNotNil(error)
let gate = ThreadGate()
service.preferenceGate = gate
model.sceneBecameActive()
await gate.waitUntilStarted()
XCTAssertNil(model.contentPreferences)
XCTAssertEqual(model.contentPreferencesError, error)
XCTAssertTrue(model.timelinePosts.isEmpty)
await gate.release()
}
func testUnavailableLabelerBlocksContentUntilPreferencesReload() async {
let service = ModerationTestService()
service.missingLabeler = true
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
XCTAssertNil(model.contentPreferences)
XCTAssertTrue(model.contentPreferencesError?.contains("Manage your moderation preferences in Bluesky") == true)
XCTAssertTrue(model.timelinePosts.isEmpty)
service.missingLabeler = false
await model.reloadContentPreferences()
XCTAssertNil(model.contentPreferencesError)
XCTAssertNotNil(model.contentPreferences)
await model.loadFeed()
XCTAssertFalse(model.timelinePosts.isEmpty)
}
func testFailedContentPreferenceRefreshRemovesPreviousDecision() async {
let service = ModerationTestService()
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let post = model.posts[0]
XCTAssertNotNil(model.contentDecision(for: post))
service.failPreferences = true
await model.reloadContentPreferences()
XCTAssertNil(model.contentDecision(for: post))
XCTAssertNotNil(model.contentPreferencesError)
XCTAssertTrue(model.posts.isEmpty)
XCTAssertTrue(model.timelinePosts.isEmpty)
XCTAssertTrue(model.visibleNotifications.isEmpty)
}
func testUnchangedPreferenceReloadKeepsCachedContent() async {
let service = ModerationTestService()
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
await model.loadProfile(actorID: "mae")
_ = await model.loadNotifications()
await model.loadPost(id: "post-2")
let oldRevision = model.contentRevision
XCTAssertFalse(model.posts.isEmpty)
await model.reloadContentPreferences()
XCTAssertEqual(model.contentRevision, oldRevision)
XCTAssertFalse(model.posts.isEmpty)
XCTAssertFalse(model.profiles.isEmpty)
XCTAssertFalse(model.authorPosts.isEmpty)
XCTAssertFalse(model.threadContexts.isEmpty)
XCTAssertFalse(model.notifications.isEmpty)
await model.loadFeed()
XCTAssertFalse(model.timelinePosts.isEmpty)
}
func testConnectionFailureKeepsLoadedPreferencesAndContent() async {
let service = ModerationTestService()
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let ids = model.posts.map(\.id)
let revision = model.contentRevision
service.preferenceError = URLError(.notConnectedToInternet)
await model.reloadContentPreferences()
XCTAssertNotNil(model.contentPreferences)
XCTAssertNil(model.contentPreferencesError)
XCTAssertNotNil(model.contentPreferencesRefreshError)
XCTAssertEqual(model.posts.map(\.id), ids)
XCTAssertEqual(model.contentRevision, revision)
XCTAssertNil(model.errorMessage)
service.preferenceError = nil
await model.reloadContentPreferences()
XCTAssertNil(model.contentPreferencesRefreshError)
}
func testExpiredPreferencesDoNotSurviveConnectionFailure() async {
let service = ModerationTestService()
var time = Date(timeIntervalSince1970: 1_700_000_000)
let model = makeAppModel(service: service, now: { time })
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
time.addTimeInterval(3600)
service.preferenceError = URLError(.notConnectedToInternet)
await model.reloadContentPreferences()
XCTAssertNil(model.contentPreferences)
XCTAssertNotNil(model.contentPreferencesError)
XCTAssertNil(model.contentPreferencesRefreshError)
XCTAssertTrue(model.posts.isEmpty)
}
}