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
import Observation
import XCTest
@testable import OldFella
extension AppModelTests {
func testPreparedContentRefreshesWhenExpiryTimerFires() async throws {
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.feedPage = FeedPage(posts: [post], cursor: nil)
let model = makeAppModel(service: service)
await model.signInWithDemo()
service.mutedWords = [.init(value: "hidden", expiresAt: Date().addingTimeInterval(0.1))]
await model.reloadContentPreferences()
await model.loadFeed()
XCTAssertTrue(model.timelinePosts.isEmpty)
let refreshed = expectation(description: "Expired muted word refreshes the prepared feed")
withObservationTracking {
_ = model.timelinePosts
} onChange: {
Task { @MainActor in
if model.timelinePosts.map(\.id) == [post.id] { refreshed.fulfill() }
}
}
await fulfillment(of: [refreshed], timeout: 2)
XCTAssertEqual(model.timelinePosts.map(\.id), [post.id])
}
func testPreparedContentRefreshesAtMutedWordAndLabelExpiry() async {
let service = ModerationTestService()
var time = Date(timeIntervalSince1970: 1_800_000_000)
let author = Post.Author(id: "other", name: "Other", handle: "other")
let muted = Post(
id: "muted", author: author, text: "hidden text", likeCount: 0, repostCount: 0, isLiked: false,
isReposted: false, imageURLs: [], replyIDs: [])
var labeled = Post(
id: "labeled", author: author, text: "safe text", likeCount: 0, repostCount: 0, isLiked: false,
isReposted: false, imageURLs: [], replyIDs: [])
labeled.moderationLabels = [
.init(value: "!hide", sourceDID: author.id, expiresAt: time.addingTimeInterval(120))
]
service.mutedWords = [.init(value: "hidden", expiresAt: time.addingTimeInterval(60))]
service.feedPage = FeedPage(posts: [muted, labeled], cursor: nil)
service.notificationPage = NotificationPage(
notifications: [
FeedNotification(
id: "notification", author: author, reason: .reply, indexedAt: time, isRead: false,
postID: muted.id, relatedPost: muted)
], cursor: nil)
let model = makeAppModel(service: service, now: { time })
await model.signInWithDemo()
await model.loadNotifications()
XCTAssertTrue(model.timelinePosts.isEmpty)
XCTAssertFalse(model.hasUnreadNotifications)
let labeledState = model.postViewState(for: labeled)
let decisions = ObservationProbe { labeledState.decision }
XCTAssertEqual(labeledState.decision?.action, .hide)
time.addTimeInterval(60)
model.refreshExpiredModeration()
XCTAssertEqual(model.timelinePosts.map(\.id), [muted.id])
XCTAssertTrue(model.hasUnreadNotifications)
XCTAssertEqual(model.contentDecision(for: labeled)?.action, .hide)
time.addTimeInterval(60)
model.refreshExpiredModeration()
XCTAssertEqual(model.timelinePosts.map(\.id), [muted.id, labeled.id])
XCTAssertEqual(model.contentDecision(for: labeled)?.action, .allow)
await Task.yield()
XCTAssertEqual(labeledState.decision?.action, .allow)
XCTAssertEqual(decisions.values.count, 1)
let preparations = model.contentPreparationCount
model.refreshExpiredModeration()
XCTAssertEqual(model.contentPreparationCount, preparations)
time.addTimeInterval(-120)
model.refreshExpiredModeration()
XCTAssertTrue(model.timelinePosts.isEmpty)
XCTAssertFalse(model.hasUnreadNotifications)
await model.signOut()
assertSignedOutModeration(model, post: muted)
}
func testTimelineRespectsGraphicMediaPreferenceOnPostWithMultipleLabels() async {
let author = Post.Author(id: "did:example:other", name: "Other", handle: "other")
let labels: [ModerationPolicy.Label] = [
.init(value: "porn", sourceDID: author.id),
.init(value: "graphic-media", sourceDID: author.id)
]
let post = Post(
id: "graphic", author: author, text: "", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false,
imageURLs: [URL(string: "https://example.com/image.jpg")!], replyIDs: [], moderationLabels: labels)
for setting: ModerationPolicy.LabelSetting in [.ignore, .warn, .hide] {
let service = ModerationTestService()
service.preferenceOverride = .init(
userDID: "demo", adultContentEnabled: true, labels: ["porn": .warn, "graphic-media": setting],
labelDefinitions: ModerationPolicy.builtInDefinitions)
service.feedPage = FeedPage(posts: [post], cursor: nil)
let model = makeAppModel(service: service)
await model.signIn(handle: "demo", appPassword: "password")
XCTAssertEqual(model.timelineRowIDs, setting == .hide ? [] : ["graphic"], "setting: \(setting)")
}
}
func testTimelineAdultMediaWarningsDoNotOverrideOtherWarnings() async {
let author = Post.Author(id: "did:example:other", name: "Other", handle: "other")
let image = URL(string: "https://example.com/image.jpg")!
let adult = ModerationPolicy.Label(value: "sexual", sourceDID: author.id)
let other = ModerationPolicy.Label(value: "icon-intolerant", sourceDID: author.id)
let locked = ModerationPolicy.Label(value: "locked", sourceDID: author.id)
let definition = ModerationPolicy.LabelDefinition(
value: "locked", sourceDID: author.id, defaultSetting: .warn, noOverride: true, severity: .alert,
blur: .media)
func post(_ id: String, labels: [ModerationPolicy.Label], quote: Bool) -> Post {
let quoted =
quote
? Post.Quote(
postID: "quote-\(id)", author: author, text: "quote", imageURLs: [image], moderationLabels: labels)
: nil
return Post(
id: id, author: author, text: "safe", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false,
imageURLs: quote ? [] : [image], quote: quoted, replyIDs: [], moderationLabels: quote ? [] : labels)
}
for setting: ModerationPolicy.LabelSetting in [.warn, .ignore, .hide] {
let service = ModerationTestService()
service.preferenceOverride = .init(
userDID: "demo", adultContentEnabled: true, labels: ["sexual": setting, "icon-intolerant": .warn],
labelDefinitions: ModerationPolicy.builtInDefinitions + [definition])
service.feedPage = FeedPage(
posts: [
post("adult", labels: [adult], quote: false), post("adult-quote", labels: [adult], quote: true),
post("mixed", labels: [adult, other], quote: false),
post("mixed-quote", labels: [adult, other], quote: true),
post("locked", labels: [adult, locked], quote: false),
post("locked-quote", labels: [adult, locked], quote: true)
], cursor: nil)
let model = makeAppModel(service: service)
await model.signIn(handle: "demo", appPassword: "password")
XCTAssertEqual(
model.timelineRowIDs, setting == .hide ? [] : ["adult", "adult-quote"], "setting: \(setting)")
}
}
func testTimelineOmitsHiddenAndRevealRequiredPosts() async {
let service = ModerationTestService()
let author = Post.Author(id: "did:example:other", name: "Other", handle: "other")
func post(_ id: String, label: String? = nil, media: Bool = false) -> Post {
var result = Post(
id: id, author: author, text: id, likeCount: 0, repostCount: 0, isLiked: false, isReposted: false,
imageURLs: media ? [URL(string: "https://example.com/image.jpg")!] : [], 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)
service.feedPage = FeedPage(
posts: [
post("safe"), post("hidden", label: "!hide"), post("warning", label: "!warn"),
post("media", label: "nudity", media: true), post("no-media", label: "nudity"),
post("alert", label: "scam")
], cursor: nil)
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
XCTAssertEqual(model.timelinePosts.map(\.id), ["safe", "media", "no-media", "alert"])
}
func testTimelineOmitsPostsWithHiddenOrRevealRequiredQuotes() 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) -> Post {
let labels: [ModerationPolicy.Label] = label.map { [.init(value: $0, sourceDID: author.id)] } ?? []
let quote = Post.Quote(
postID: "quote-\(id)", author: author, text: "quoted text", imageURLs: media ? [image] : [],
moderationLabels: labels)
return Post(
id: id, author: author, text: "safe", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false,
imageURLs: [], quote: quote, replyIDs: [])
}
let information = ModerationPolicy.LabelDefinition(
value: "information", sourceDID: author.id, severity: .inform, blur: .none)
service.preferenceOverride = .init(
userDID: "demo", adultContentEnabled: true, labels: ["nudity": .warn, "scam": .warn, "information": .warn],
labelDefinitions: ModerationPolicy.builtInDefinitions + [information])
let safe = post("safe")
let hidden = post("hidden", label: "!hide")
let warning = post("warning", label: "!warn")
let media = post("media", label: "nudity", media: true)
let textOnly = post("text-only", label: "nudity")
let alert = post("alert", label: "scam")
let informational = post("informational", label: "information")
service.feedPage = FeedPage(posts: [safe, hidden, textOnly, warning, alert, media, informational], cursor: nil)
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
XCTAssertEqual(model.quoteContentDecision(hidden.quote!, fallbackPost: nil)?.action, .hide)
XCTAssertTrue(model.quoteContentDecision(warning.quote!, fallbackPost: nil)?.ui(.contentList).blur == true)
XCTAssertTrue(model.quoteContentDecision(media.quote!, fallbackPost: nil)?.ui(.contentMedia).blur == true)
XCTAssertTrue(model.quoteContentDecision(alert.quote!, fallbackPost: nil)?.ui(.contentList).alert == true)
XCTAssertTrue(
model.quoteContentDecision(informational.quote!, fallbackPost: nil)?.ui(.contentList).inform == true)
XCTAssertEqual(model.timelinePosts.map(\.id), ["safe", "text-only", "alert", "media", "informational"])
}
private func assertSignedOutModeration(_ model: AppModel, post: Post) {
XCTAssertTrue(model.timelinePosts.isEmpty)
XCTAssertTrue(model.visibleNotifications.isEmpty)
XCTAssertTrue(model.notificationGroups.isEmpty)
XCTAssertFalse(model.hasUnreadNotifications)
XCTAssertNil(model.contentDecision(for: post))
}
}