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
import Observation
import XCTest
@testable import OldFella
@MainActor
final class ActionFailureTestService: BlueskyService {
let base = MockBlueskyService()
var likeCalls = 0
var actionDelay: UInt64 = 0
var failActions = true
var refreshedPost: Post?
var publishFailure: Error = ServiceError.invalidResponse
var likeFailure: Error = ServiceError.invalidResponse
var followFailure: Error = ServiceError.invalidResponse
var publishDelay: UInt64 = 0
var followDelay: UInt64 = 0
var repostCalls = 0
var followCalls = 0
var publishedTexts: [String] = []
var publishedImages: [[ImageAttachment]] = []
func restoreAccount() async throws -> Account? { try await base.restoreAccount() }
func loadModerationPreferences() async throws -> ModerationPolicy.Preferences {
try await base.loadModerationPreferences()
}
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 {
let page = try await base.loadFollowingFeed(cursor: cursor)
guard let refreshedPost else { return page }
return FeedPage(
posts: page.posts.map { $0.id == refreshedPost.id ? refreshedPost : $0 }, cursor: page.cursor,
replyContexts: page.replyContexts, repostedBy: page.repostedBy)
}
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? {
likeCalls += 1
if actionDelay > 0 { try await Task.sleep(nanoseconds: actionDelay) }
if failActions && likeCalls == 1 { throw likeFailure }
return try await base.toggleLike(post: post, liked: liked)
}
func toggleRepost(post: Post, reposted: Bool) async throws -> String? {
repostCalls += 1
if actionDelay > 0 { try await Task.sleep(nanoseconds: actionDelay) }
if failActions && repostCalls == 1 { throw ServiceError.invalidResponse }
return try await base.toggleRepost(post: post, reposted: reposted)
}
func setFollowing(_ following: Bool, actorID: String) async throws -> String? {
followCalls += 1
if followDelay > 0 { try await Task.sleep(nanoseconds: followDelay) }
if failActions && followCalls == 1 { throw followFailure }
return try await base.setFollowing(following, actorID: actorID)
}
func publish(text: String, replyTo: Post?, quotePost: Post?, images: [ImageAttachment]) async throws -> Post {
publishedTexts.append(text)
publishedImages.append(images)
if publishDelay > 0 { try await Task.sleep(nanoseconds: publishDelay) }
if failActions && publishedTexts.count == 1 { throw publishFailure }
return try await base.publish(text: text, replyTo: replyTo, quotePost: quotePost, images: images)
}
}