~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/old-fella

Old people's social media app

0
1
main
72 lines · 3.5 KB Raw History
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)
    }
}