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
import Observation
import XCTest
@testable import OldFella
final class ModerationTestService: BlueskyService {
private let base = MockBlueskyService()
var fail = false
var failPreferences = false
var preferenceGate: ThreadGate?
var preferenceCalls = 0
var restoredAccount: Account?
var restoreGate: ThreadGate?
var subscribedFeedLoads = 0
var preferenceError: Error?
var readError: Error?
var missingLabeler = false
var mutedWords: [ModerationPolicy.MutedWord] = []
var threadPosts: [Post]?
var feedPage: FeedPage?
var nextFeedPage: FeedPage?
var feedPages: [String: FeedPage] = [:]
var feedRequests: [String] = []
var failingFeedCursor: String?
var customFeedPages: [String: FeedPage] = [:]
var customFeedRequests: [String] = []
var customFeedGateCursor: String?
var customFeedGate: ThreadGate?
var preferenceOverride: ModerationPolicy.Preferences?
var notificationPage: NotificationPage?
var suspendFollow = false
var pendingFollow: CheckedContinuation<String?, Error>?
var reject = false
var actions: [String] = []
var reported: ReportSubject?
var state = ModerationState()
var suspendLoad = false
var pendingLoad: CheckedContinuation<ModerationState, Never>?
var suspendSignOut = false
var pendingSignOut: CheckedContinuation<Void, Never>?
var failSignOut = false
var suspendBlock = false
var pendingBlock: CheckedContinuation<String?, Error>?
var postActionCalls = 0
var emptyAuthorPosts = false
var authorPostLoads = 0
func restoreAccount() async throws -> Account? { restoredAccount }
func restoreAccount(onLocalAccount: @MainActor @Sendable (String) -> Void) async throws -> Account? {
if let restoredAccount { await onLocalAccount(restoredAccount.id) }
if let restoreGate { await restoreGate.wait() }
return restoredAccount
}
func loadModerationPreferences() async throws -> ModerationPolicy.Preferences {
preferenceCalls += 1
if let preferenceGate { await preferenceGate.wait() }
if let preferenceError { throw preferenceError }
if missingLabeler { throw ServiceError.labelDefinitionsUnavailable }
if failPreferences { throw ServiceError.invalidResponse }
if let preferenceOverride { return preferenceOverride }
var preferences = try await base.loadModerationPreferences()
preferences.mutedWords = mutedWords
return preferences
}
func signIn(handle: String, appPassword: String, authFactorToken: String?) async throws -> Account {
try await base.signIn(handle: handle, appPassword: appPassword, authFactorToken: authFactorToken)
}
func signOut() async throws {
if suspendSignOut { await withCheckedContinuation { pendingSignOut = $0 } }
if failSignOut { throw ServiceError.invalidResponse }
}
func loadFollowingFeed(cursor: String?) async throws -> FeedPage {
let key = cursor ?? "first"
feedRequests.append(key)
if cursor != nil && cursor == failingFeedCursor { throw URLError(.notConnectedToInternet) }
if let page = feedPages[key] { return page }
if let readError { throw readError }
if cursor != nil, let nextFeedPage { return nextFeedPage }
if let feedPage { return feedPage }
return try await base.loadFollowingFeed(cursor: cursor)
}
func suggestAccounts(query: String) async throws -> [Post.Author] {
try await base.suggestAccounts(query: query)
}
func loadSubscribedFeeds() async throws -> [CustomFeed] {
subscribedFeedLoads += 1
return try await base.loadSubscribedFeeds()
}
func loadCustomFeed(uri: String, cursor: String?) async throws -> FeedPage {
let key = cursor ?? "first"
customFeedRequests.append(key)
if key == customFeedGateCursor, let customFeedGate { await customFeedGate.wait() }
if let page = customFeedPages[key] { return page }
return try await base.loadCustomFeed(uri: uri, cursor: cursor)
}
func loadNotifications(cursor: String?) async throws -> NotificationPage {
if let notificationPage { return notificationPage }
return try await base.loadNotifications(cursor: cursor)
}
func markNotificationsRead(seenAt: Date) async throws {}
func loadPost(uri: String) async throws -> [Post] {
if let readError { throw readError }
if let threadPosts { return threadPosts }
return try await base.loadPost(uri: uri)
}
func loadProfile(actorID: String) async throws -> Profile? {
if let readError { throw readError }
return try await base.loadProfile(actorID: actorID)
}
func loadAuthorPosts(actorID: String, cursor: String?) async throws -> FeedPage {
if let readError { throw readError }
authorPostLoads += 1
if emptyAuthorPosts { return FeedPage(posts: [], cursor: nil) }
return try await base.loadAuthorPosts(actorID: actorID, cursor: cursor)
}
func toggleLike(post: Post, liked: Bool) async throws -> String? {
postActionCalls += 1
return nil
}
func toggleRepost(post: Post, reposted: Bool) async throws -> String? {
postActionCalls += 1
return nil
}
func publish(text: String, replyTo: Post?, quotePost: Post?, images: [ImageAttachment]) async throws -> Post {
postActionCalls += 1
return try await base.publish(text: text, replyTo: replyTo, quotePost: quotePost, images: images)
}
func setFollowing(_ following: Bool, actorID: String) async throws -> String? {
postActionCalls += 1
if suspendFollow { return try await withCheckedThrowingContinuation { pendingFollow = $0 } }
return nil
}
func moderationState(actorID: String) async throws -> ModerationState {
if suspendLoad { return await withCheckedContinuation { pendingLoad = $0 } }
return state
}
func setMuted(_ muted: Bool, actorID: String) async throws {
if fail { throw ServiceError.invalidResponse }
actions.append("mute:\(muted):\(actorID)")
}
func setBlocked(_ blocked: Bool, actorID: String, blockURI: String?) async throws -> String? {
if suspendBlock { return try await withCheckedThrowingContinuation { pendingBlock = $0 } }
if reject { throw ModerationWriteError.rejected("Rejected") }
if fail { throw ServiceError.invalidResponse }
actions.append("block:\(blocked):\(actorID)")
return blocked ? "block-uri" : nil
}
func report(_ subject: ReportSubject, reason: String) async throws {
if reject { throw ModerationWriteError.rejected("Rejected") }
if fail { throw ServiceError.invalidResponse }
reported = subject
}
}