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
import Observation
import XCTest
@testable import OldFella
extension AppModelTests {
func testCancelledReadsDoNotShowErrorsOrRemoveContent() 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)
for error: Error in [CancellationError(), URLError(.cancelled)] {
service.readError = error
service.preferenceError = error
await model.reloadContentPreferences()
await model.loadFeed()
await model.loadNextPage()
await model.loadPost(id: "post-1")
await model.loadProfile(actorID: "mae", forceRefresh: true)
XCTAssertEqual(model.posts.map(\.id), ids)
XCTAssertNil(model.errorMessage)
XCTAssertNil(model.feedErrorMessage)
XCTAssertNil(model.postErrors["post-1"])
XCTAssertNil(model.profileErrors["mae"])
XCTAssertNil(model.contentPreferencesError)
XCTAssertNil(model.contentPreferencesRefreshError)
XCTAssertFalse(model.isLoading)
XCTAssertFalse(model.isLoadingPost)
}
}
func testReadFailuresStayInlineAndKeepExistingFeed() async {
let service = ModerationTestService()
let author = Post.Author(id: "author", name: "Author", handle: "author")
let posts = (0..<20).map { index in
Post(
id: "fixture-\(index)", author: author, text: "Post \(index)", likeCount: 0, repostCount: 0,
isLiked: false, isReposted: false, imageURLs: [], quote: nil, replyIDs: [])
}
service.feedPage = FeedPage(posts: posts, cursor: "next-page")
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let ids = model.posts.map(\.id)
service.readError = URLError(.timedOut)
await model.loadFeed()
XCTAssertNotNil(model.feedErrorMessage)
XCTAssertFalse(model.retryFeedPage)
await model.loadNextPage()
XCTAssertTrue(model.retryFeedPage)
await model.loadPost(id: "post-1")
await model.loadProfile(actorID: "mae")
XCTAssertNotNil(model.postErrors["post-1"])
XCTAssertNotNil(model.profileErrors["mae"])
XCTAssertEqual(model.posts.map(\.id), ids)
XCTAssertNil(model.errorMessage)
}
func testChangedPreferencesInvalidateContent() async {
let service = ModerationTestService()
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let revision = model.contentRevision
service.mutedWords = [.init(value: "walk")]
await model.reloadContentPreferences()
XCTAssertNotEqual(model.contentRevision, revision)
XCTAssertTrue(model.posts.isEmpty)
await model.loadFeed()
XCTAssertFalse(model.timelinePosts.contains { $0.text.localizedCaseInsensitiveContains("walk") })
}
func testNotificationsKeepInformationalAuthorLabelsButHideFilteredAuthors() async {
let service = ModerationTestService()
func author(_ id: String, label: String) -> Post.Author {
Post.Author(id: id, name: id, handle: id, moderationLabels: [.init(value: label, sourceDID: id)])
}
let informational = author("information-author", label: "information")
let hidden = author("hidden-author", label: "!hide")
let blurred = author("blurred-author", label: "!warn")
let definition = ModerationPolicy.LabelDefinition(
value: "information", sourceDID: informational.id, severity: .inform, blur: .none)
service.preferenceOverride = .init(
userDID: "demo", adultContentEnabled: true, labels: ["information": .warn],
labelDefinitions: ModerationPolicy.builtInDefinitions + [definition])
let recent = Date()
service.notificationPage = NotificationPage(
notifications: [
FeedNotification(
id: "recent-like", author: informational, reason: .like, indexedAt: recent, isRead: false,
postID: "post"),
FeedNotification(
id: "recent-reply", author: informational, reason: .reply, indexedAt: recent, isRead: false,
postID: "reply"),
FeedNotification(
id: "hidden", author: hidden, reason: .follow, indexedAt: recent, isRead: false, postID: nil),
FeedNotification(
id: "blurred", author: blurred, reason: .follow, indexedAt: recent, isRead: false, postID: nil)
], cursor: nil)
let model = makeAppModel(service: service)
await model.signInWithDemo()
await model.loadNotifications()
XCTAssertEqual(model.contentDecision(author: informational)?.action, .warn)
XCTAssertEqual(model.visibleNotifications.map(\.id), ["recent-like", "recent-reply"])
XCTAssertEqual(model.notificationRows.map(\.id), ["recent-like", "recent-reply"])
XCTAssertTrue(model.hasUnreadNotifications)
}
func testPreparedContentReadsDoNotRepeatFilteringOrModeration() async throws {
let service = ModerationTestService()
service.mutedWords = [.init(value: "hidden")]
let author = Post.Author(id: "other", name: "Other", handle: "other")
let safe = Post(
id: "safe", author: author, text: "safe text", likeCount: 0, repostCount: 0, isLiked: false,
isReposted: false, imageURLs: [], replyIDs: [])
let hidden = Post(
id: "hidden", author: author, text: "hidden text", likeCount: 0, repostCount: 0, isLiked: false,
isReposted: false, imageURLs: [], replyIDs: [])
service.feedPage = FeedPage(posts: [safe, hidden], cursor: nil)
service.notificationPage = NotificationPage(
notifications: [
FeedNotification(
id: "visible", author: author, reason: .reply, indexedAt: Date(), isRead: false, postID: safe.id,
relatedPost: safe),
FeedNotification(
id: "filtered", author: author, reason: .reply, indexedAt: Date(), isRead: false, postID: hidden.id,
relatedPost: hidden)
], cursor: nil)
let model = makeAppModel(service: service)
await model.signInWithDemo()
await model.loadNotifications()
let preparations = model.contentPreparationCount
let evaluations = model.moderationEvaluationCount
for _ in 0..<100 {
XCTAssertEqual(model.timelinePosts.map(\.id), [safe.id])
XCTAssertEqual(model.visibleNotifications.map(\.id), ["visible"])
XCTAssertEqual(model.notificationGroups.map(\.id), ["visible"])
XCTAssertTrue(model.hasUnreadNotifications)
XCTAssertEqual(model.contentDecision(for: safe)?.action, .allow)
XCTAssertEqual(model.contentDecision(author: author)?.action, .allow)
}
XCTAssertEqual(model.contentPreparationCount, preparations)
XCTAssertEqual(model.moderationEvaluationCount, evaluations)
model.clearError()
model.clearModerationError()
XCTAssertEqual(model.contentPreparationCount, preparations)
XCTAssertEqual(model.moderationEvaluationCount, evaluations)
try await model.markNotificationsRead()
XCTAssertFalse(model.hasUnreadNotifications)
XCTAssertTrue(model.notificationGroups[0].notifications[0].isRead)
XCTAssertEqual(model.moderationEvaluationCount, evaluations)
}
func testPreparedContentUpdatesForMuteAndFollowChanges() async {
let service = ModerationTestService()
let author = Post.Author(id: "other", name: "Other", handle: "other")
let post = Post(
id: "post", author: author, text: "hidden text", likeCount: 0, repostCount: 0, isLiked: false,
isReposted: false, imageURLs: [], replyIDs: [])
service.mutedWords = [.init(value: "hidden", excludeFollowing: true)]
service.feedPage = FeedPage(posts: [post], cursor: nil)
service.notificationPage = NotificationPage(
notifications: [
FeedNotification(
id: "notification", author: author, reason: .reply, indexedAt: Date(), isRead: false,
postID: post.id, relatedPost: post)
], cursor: nil)
let model = makeAppModel(service: service)
await model.signInWithDemo()
await model.loadNotifications()
XCTAssertTrue(model.timelinePosts.isEmpty)
XCTAssertFalse(model.hasUnreadNotifications)
await model.toggleFollow(actorID: author.id)
XCTAssertEqual(model.timelinePosts.map(\.id), [post.id])
XCTAssertTrue(model.hasUnreadNotifications)
await model.hideAccountFromTimeline(actorID: author.id, block: false)
XCTAssertTrue(model.timelinePosts.isEmpty)
XCTAssertTrue(model.visibleNotifications.isEmpty)
XCTAssertTrue(model.notificationGroups.isEmpty)
XCTAssertFalse(model.hasUnreadNotifications)
await model.setModeration(actorID: author.id, block: false)
XCTAssertEqual(model.timelinePosts.map(\.id), [post.id])
XCTAssertTrue(model.hasUnreadNotifications)
await model.toggleFollow(actorID: author.id)
XCTAssertTrue(model.timelinePosts.isEmpty)
XCTAssertFalse(model.hasUnreadNotifications)
}
func testPreparedContentUpdatesWhenQuoteFallbackChanges() async {
let service = ModerationTestService()
let author = Post.Author(id: "other", name: "Other", handle: "other")
let quoted = Post(
id: "quoted", author: author, text: "safe text", likeCount: 0, repostCount: 0, isLiked: false,
isReposted: false, imageURLs: [], replyIDs: [])
let enclosing = Post(
id: "enclosing", author: author, text: "safe text", likeCount: 0, repostCount: 0, isLiked: false,
isReposted: false, imageURLs: [], quote: Post.Quote(postID: quoted.id), replyIDs: [])
service.mutedWords = [.init(value: "hidden")]
service.feedPage = FeedPage(posts: [enclosing, quoted], cursor: nil)
let model = makeAppModel(service: service)
await model.signInWithDemo()
XCTAssertEqual(model.timelinePosts.map(\.id), [enclosing.id, quoted.id])
let enclosingState = model.postViewState(for: enclosing)
let quotedChanges = ObservationProbe { enclosingState.quotedPost }
var hidden = quoted
hidden.text = "hidden text"
service.threadPosts = [hidden]
await model.loadPost(id: quoted.id)
await Task.yield()
XCTAssertTrue(model.timelinePosts.isEmpty)
XCTAssertEqual(enclosingState.quotedPost, hidden)
XCTAssertEqual(enclosingState.decision?.action, .hide)
XCTAssertEqual(quotedChanges.values.count, 1)
XCTAssertEqual(model.contentDecision(for: enclosing)?.action, .hide)
service.threadPosts = [quoted]
await model.loadPost(id: quoted.id)
XCTAssertEqual(model.timelinePosts.map(\.id), [enclosing.id, quoted.id])
XCTAssertEqual(model.contentDecision(for: enclosing)?.action, .allow)
XCTAssertEqual(enclosingState.quotedPost, quoted)
XCTAssertEqual(enclosingState.decision?.action, .allow)
}
}