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
226
import Observation
import XCTest
@testable import OldFella
extension AppModelTests {
func testTimelineOmitsPostsWithHiddenOrRevealRequiredReplyContext() async {
let service = ModerationTestService()
let author = Post.Author(id: "other", name: "Other", handle: "other")
let image = URL(string: "https://example.com/image.jpg")!
func post(_ id: String, label: String? = nil, media: Bool = false, quote: Post.Quote? = nil) -> Post {
var result = Post(
id: id, author: author, text: "safe", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false,
imageURLs: media ? [image] : [], quote: quote, replyIDs: [])
if let label { result.moderationLabels = [.init(value: label, sourceDID: author.id)] }
return result
}
service.preferenceOverride = .init(
userDID: "demo", adultContentEnabled: true, labels: ["nudity": .warn, "scam": .warn],
labelDefinitions: ModerationPolicy.builtInDefinitions)
let posts = ["safe", "hidden", "text-only", "warning", "alert", "media", "quoted-warning"].map { id in
var reply = post(id)
reply.replyRootURI =
id == "hidden"
? "first-context" : "\(id == "text-only" ? "text" : id == "quoted-warning" ? "quoted" : id)-context"
reply.replyParentURI = id == "hidden" ? "hidden-context" : reply.replyRootURI
return reply
}
let warningQuote = Post.Quote(
postID: "context-quote", author: author, text: "quoted text",
moderationLabels: [.init(value: "!warn", sourceDID: author.id)])
service.feedPage = FeedPage(
posts: posts, cursor: nil,
replyContexts: [
"safe": [post("safe-context")],
"hidden": [post("first-context"), post("hidden-context", label: "!hide")],
"text-only": [post("text-context", label: "nudity")],
"warning": [post("warning-context", label: "!warn")],
"alert": [post("alert-context", label: "scam")],
"media": [post("media-context", label: "nudity", media: true)],
"quoted-warning": [post("quoted-context", quote: warningQuote)]
])
let model = makeAppModel(service: service)
model.induceADHD = true
defer { model.induceADHD = false }
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
XCTAssertEqual(model.timelinePosts.map(\.id), ["safe", "text-only", "alert", "media"])
}
func testRepostAuthorMustPassContentPreferences() async {
let model = makeAppModel(service: MockBlueskyService())
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let author = Post.Author(
id: "did:example:other", name: "Other", handle: "other",
moderationLabels: [.init(value: "porn", sourceDID: "did:example:other")])
XCTAssertFalse(model.isRepostAuthorVisible(author))
await model.signOut()
XCTAssertFalse(model.isRepostAuthorVisible(author))
}
func testContentDecisionUsesPostInputsAndQuoteInputs() async {
let model = makeAppModel(service: MockBlueskyService())
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let author = Post.Author(
id: "did:example:other", name: "Other", handle: "other",
moderationLabels: [.init(value: "porn", sourceDID: "did:example:other")])
let post = Post(
id: "post", author: author, text: "text", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false,
imageURLs: [], replyIDs: [])
XCTAssertEqual(model.contentDecision(for: post)?.action, .hide)
let quote = Post.Quote(postID: "post", author: author, text: "text")
XCTAssertTrue(model.isQuoteHidden(quote, fallbackPost: nil))
await model.signOut()
XCTAssertNil(model.contentDecision(for: post))
}
func testMutedWordsApplyToEmbeddedText() async {
let service = ModerationTestService()
service.mutedWords = [.init(value: "spoiler")]
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let author = Post.Author(id: "other", name: "Other", handle: "other")
let imageURL = URL(string: "https://example.com/image")!
var post = Post(
id: "post", author: author, text: "safe", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false,
imageURLs: [], replyIDs: [])
XCTAssertEqual(model.contentDecision(for: post)?.action, .allow)
post.quote = .init(postID: "quote", author: author, text: "spoiler")
XCTAssertTrue(model.contentDecision(for: post)?.ui(.contentList).filter == true)
post.quote = .init(postID: "quote", author: author, imageDescriptions: [imageURL: "spoiler"])
XCTAssertTrue(model.contentDecision(for: post)?.ui(.contentList).filter == true)
post.quote = nil
post.externalTitle = "spoiler"
XCTAssertTrue(model.contentDecision(for: post)?.ui(.contentList).filter == true)
post.externalTitle = nil
post.externalDescription = "spoiler"
XCTAssertTrue(model.contentDecision(for: post)?.ui(.contentList).filter == true)
post.externalDescription = nil
post.quote = .init(postID: "quote", author: author, externalTitle: "spoiler")
XCTAssertTrue(model.contentDecision(for: post)?.ui(.contentList).filter == true)
post.quote = .init(postID: "quote", author: author, externalDescription: "spoiler")
XCTAssertTrue(model.contentDecision(for: post)?.ui(.contentList).filter == true)
for quote in [
Post.Quote(postID: "quote", author: author, text: "spoiler"),
Post.Quote(postID: "quote", author: author, imageDescriptions: [imageURL: "spoiler"]),
Post.Quote(postID: "quote", author: author, externalTitle: "spoiler"),
Post.Quote(postID: "quote", author: author, externalDescription: "spoiler")
] {
XCTAssertTrue(model.isQuoteHidden(quote, fallbackPost: nil))
}
}
func testQuoteUsesItsOwnAuthorAndContentForMutedWords() async {
let service = ModerationTestService()
service.mutedWords = [.init(value: "spoiler", excludeFollowing: true)]
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let followed = Post.Author(id: "followed", name: "Followed", handle: "followed", isFollowing: true)
let other = Post.Author(id: "other", name: "Other", handle: "other")
var post = Post(
id: "post", author: followed, text: "safe", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false,
imageURLs: [], replyIDs: [])
post.quote = .init(postID: "quote", author: other, text: "spoiler")
XCTAssertEqual(model.contentDecision(for: post)?.action, .hide)
post = Post(
id: "post", author: other, text: "safe", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false,
imageURLs: [], quote: .init(postID: "quote", author: followed, text: "spoiler"), replyIDs: [])
XCTAssertEqual(model.contentDecision(for: post)?.action, .allow)
post.quote = .init(postID: "quote", author: other, text: "safe", tags: ["spoiler"])
XCTAssertEqual(model.contentDecision(for: post)?.action, .hide)
post.quote = .init(postID: "quote", author: other, text: "prespoilerpost", languages: ["ja"])
XCTAssertEqual(model.contentDecision(for: post)?.action, .hide)
post.quote?.languages = ["en"]
XCTAssertEqual(model.contentDecision(for: post)?.action, .allow)
}
func testUnfollowOverridesCachedAuthorsAndQuotes() async {
let service = ModerationTestService()
service.mutedWords = [.init(value: "spoiler", excludeFollowing: true)]
let author = Post.Author(id: "followed", name: "Followed", handle: "followed", isFollowing: true)
let post = Post(
id: "cached", author: author, text: "spoiler", likeCount: 0, repostCount: 0, isLiked: false,
isReposted: false, imageURLs: [], replyIDs: [])
let quote = Post.Quote(postID: "quoted", author: author, text: "spoiler")
service.threadPosts = [post]
service.notificationPage = NotificationPage(
notifications: [
.init(
id: "notification", author: author, reason: .mention, indexedAt: .now, isRead: false,
postID: post.id, relatedPost: post)
], cursor: nil)
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
await model.loadPost(id: post.id)
_ = await model.loadNotifications()
await model.toggleFollow(actorID: author.id)
XCTAssertEqual(model.contentDecision(for: post)?.action, .allow)
XCTAssertEqual(model.visibleNotifications.count, 1)
await model.toggleFollow(actorID: author.id)
XCTAssertEqual(model.contentDecision(for: model.post(id: post.id)!)?.action, .hide)
XCTAssertEqual(model.quoteContentDecision(quote, fallbackPost: nil)?.action, .hide)
XCTAssertTrue(model.visibleNotifications.isEmpty)
await model.toggleFollow(actorID: author.id)
XCTAssertEqual(model.contentDecision(for: post)?.action, .allow)
XCTAssertEqual(model.visibleNotifications.count, 1)
}
func testFollowResultDoesNotCrossSessions() async {
let service = ModerationTestService()
service.mutedWords = [.init(value: "spoiler", excludeFollowing: true)]
let author = Post.Author(id: "followed", name: "Followed", handle: "followed", isFollowing: true)
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
await model.toggleFollow(actorID: author.id)
service.suspendFollow = true
let pending = Task { await model.toggleFollow(actorID: author.id) }
while service.pendingFollow == nil { await Task.yield() }
await model.signOut()
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
service.pendingFollow?.resume(returning: nil)
await pending.value
XCTAssertEqual(model.contentDecision(author: author, text: "spoiler")?.action, .allow)
XCTAssertFalse(model.followingActorIDs.contains(author.id))
}
func testQuoteLabelsDoNotHideEnclosingPostAndWarningsCanBeShown() async {
let model = makeAppModel(service: MockBlueskyService())
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let author = Post.Author(id: "other", name: "Other", handle: "other")
let warning = Post.Quote(
postID: "quote", author: author, text: "text", moderationLabels: [.init(value: "!warn", sourceDID: "other")]
)
let post = Post(
id: "post", author: author, text: "safe", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false,
imageURLs: [], quote: warning, replyIDs: [])
XCTAssertEqual(model.contentDecision(for: post)?.action, .allow)
XCTAssertEqual(model.quoteContentDecision(warning, fallbackPost: nil)?.action, .warn)
XCTAssertFalse(model.isQuoteHidden(warning, fallbackPost: nil))
var hidden = warning
hidden.moderationLabels = [.init(value: "!hide", sourceDID: "other")]
XCTAssertTrue(model.isQuoteHidden(hidden, fallbackPost: nil))
}
func testMutedThreadRepliesAreFiltered() async {
let service = ModerationTestService()
service.mutedWords = [.init(value: "spoiler")]
let author = Post.Author(id: "other", name: "Other", handle: "other")
let root = Post(
id: "root", author: author, text: "safe", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false,
imageURLs: [], replyIDs: ["hidden", "visible"])
let hidden = Post(
id: "hidden", author: author, text: "spoiler", likeCount: 0, repostCount: 0, isLiked: false,
isReposted: false, imageURLs: [], replyParentURI: "root", replyIDs: [])
let visible = Post(
id: "visible", author: author, text: "safe", likeCount: 0, repostCount: 0, isLiked: false,
isReposted: false, imageURLs: [], replyParentURI: "root", replyIDs: [])
service.threadPosts = [root, hidden, visible]
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
await model.loadPost(id: "root")
XCTAssertEqual(model.directReplies(to: root).map(\.id), ["visible"])
XCTAssertEqual(model.conversationReplies(to: root).map(\.id), ["visible"])
XCTAssertEqual(model.contentDecision(for: hidden)?.action, .hide)
}
}