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

geesawra.industries/old-fella

Old people's social media app

0
1
main
92 lines · 4.0 KB Raw History
import XCTest

@testable import OldFella

@MainActor
final class PostLoadTests: XCTestCase {
    func testConcurrentThreadLoadSurvivesFirstCallerCancellation() async throws {
        let suite = "PostLoadTests-\(UUID().uuidString)"
        let defaults = try XCTUnwrap(UserDefaults(suiteName: suite))
        defer { defaults.removePersistentDomain(forName: suite) }
        let started = expectation(description: "Thread request started")
        let service = SharedThreadTestService(started: started)
        let model = AppModel(service: service, feedDefaults: defaults, composerOnly: true)
        await model.signIn(handle: "demo", appPassword: "test-only")

        let first = Task { await model.loadPost(id: "post-1") }
        await fulfillment(of: [started], timeout: 2)
        let secondStarted = expectation(description: "Second caller started")
        let second = Task {
            secondStarted.fulfill()
            await model.loadPost(id: "post-1")
        }
        await fulfillment(of: [secondStarted], timeout: 2)
        first.cancel()
        let posts = try await service.base.loadPost(uri: "post-1")
        service.finish(ThreadResult(posts: posts, mutedPostIDs: []))
        await first.value
        await second.value

        XCTAssertEqual(service.threadRequestCount, 1)
        XCTAssertNotNil(model.threadContexts["post-1"])
        XCTAssertNotNil(model.post(id: "post-1"))
        XCTAssertFalse(model.loadingPostIDs.contains("post-1"))
        XCTAssertFalse(model.failedPostIDs.contains("post-1"))
    }
}

@MainActor
private final class SharedThreadTestService: BlueskyService {
    let base = MockBlueskyService()
    private let started: XCTestExpectation
    private var result: CheckedContinuation<ThreadResult, Never>?
    private(set) var threadRequestCount = 0

    init(started: XCTestExpectation) { self.started = started }

    func loadThread(uri: String) async throws -> ThreadResult {
        threadRequestCount += 1
        return await withCheckedContinuation {
            result = $0
            started.fulfill()
        }
    }

    func finish(_ thread: ThreadResult) {
        result?.resume(returning: thread)
        result = nil
    }

    func restoreAccount() async throws -> Account? { try await base.restoreAccount() }
    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 loadModerationPreferences() async throws -> ModerationPolicy.Preferences {
        try await base.loadModerationPreferences()
    }
    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)
    }
}