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

geesawra.industries/old-fella

Old people's social media app

0
1
main
86 lines · 3.9 KB Raw History
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
    }
}