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
import Foundation
import XCTest
@testable import OldFella
final class SearchTestService: BlueskyService {
private let base = MockBlueskyService()
var accountPages: [String: AccountSearchPage] = [:]
var postPages: [String: FeedPage] = [:]
var accountRequests: [(query: String, cursor: String?)] = []
struct PostRequest {
let query: String
let cursor: String?
let authorID: String?
}
var postRequests: [PostRequest] = []
var failAccountCursor: String?
var mutedWords: [ModerationPolicy.MutedWord] = []
var preferenceOverride: ModerationPolicy.Preferences?
var suspendedAccountQuery: String?
var accountStarted: XCTestExpectation?
private var pendingAccount: CheckedContinuation<AccountSearchPage, Error>?
func searchAccounts(query: String, cursor: String?) async throws -> AccountSearchPage {
accountRequests.append((query, cursor))
if query == suspendedAccountQuery {
return try await withCheckedThrowingContinuation { continuation in
pendingAccount = continuation
accountStarted?.fulfill()
}
}
if cursor != nil && cursor == failAccountCursor { throw URLError(.notConnectedToInternet) }
guard let page = accountPages[cursor ?? "first"] else { throw ServiceError.invalidResponse }
return page
}
func resumeAccount(with page: AccountSearchPage) {
let pending = pendingAccount
pendingAccount = nil
pending?.resume(returning: page)
}
func searchPosts(query: String, cursor: String?, authorID: String?) async throws -> FeedPage {
postRequests.append(PostRequest(query: query, cursor: cursor, authorID: authorID))
guard let page = postPages[cursor ?? "first"] else { throw ServiceError.invalidResponse }
return page
}
func restoreAccount() async throws -> Account? { nil }
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 { try await base.signOut() }
func loadFollowingFeed(cursor: String?) async throws -> FeedPage {
try await base.loadFollowingFeed(cursor: cursor)
}
func loadNotifications(cursor: String?) async throws -> NotificationPage {
try await base.loadNotifications(cursor: cursor)
}
func markNotificationsRead(seenAt: Date) async throws { try await base.markNotificationsRead(seenAt: seenAt) }
func loadPost(uri: String) async throws -> [Post] { try await base.loadPost(uri: uri) }
func loadProfile(actorID: String) async throws -> Profile? { try await base.loadProfile(actorID: actorID) }
func loadAuthorPosts(actorID: String, cursor: String?) async throws -> FeedPage {
try await base.loadAuthorPosts(actorID: actorID, cursor: cursor)
}
func toggleLike(post: Post, liked: Bool) async throws -> String? {
try await base.toggleLike(post: post, liked: liked)
}
func toggleRepost(post: Post, reposted: Bool) async throws -> String? {
try await base.toggleRepost(post: post, reposted: reposted)
}
func publish(text: String, replyTo: Post?, quotePost: Post?, images: [ImageAttachment]) async throws -> Post {
try await base.publish(text: text, replyTo: replyTo, quotePost: quotePost, images: images)
}
func setFollowing(_ following: Bool, actorID: String) async throws -> String? {
try await base.setFollowing(following, actorID: actorID)
}
func loadModerationPreferences() async throws -> ModerationPolicy.Preferences {
var preferences: ModerationPolicy.Preferences
if let preferenceOverride {
preferences = preferenceOverride
} else {
preferences = try await base.loadModerationPreferences()
}
preferences.mutedWords = mutedWords
return preferences
}
}