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

geesawra.industries/old-fella

Old people's social media app

0
1
main
206 lines · 10.0 KB Raw History
import Observation
import XCTest

@testable import OldFella

extension AppModelTests {
    func testProfileOnlyPostSupportsInteractionsAndSharedNavigation() async throws {
        let profileOnlyPost = Post(
            id: "profile-only-post",
            author: Post.Author(id: "mae", name: "Mae Rivers", handle: "mae.example"),
            text: "A post that is not in the Following feed.",
            likeCount: 5,
            repostCount: 2,
            isLiked: false,
            isReposted: false,
            imageURLs: [],
            replyIDs: []
        )
        let model = makeAppModel(service: ProfilePostTestService(additionalProfilePost: profileOnlyPost))
        await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
        await model.loadProfile(actorID: "mae")
        let initialTimelineIDs = model.timelinePosts.map(\.id)

        XCTAssertTrue(model.posts.contains { $0.id == profileOnlyPost.id })
        XCTAssertFalse(model.timelinePosts.contains { $0.id == profileOnlyPost.id })
        XCTAssertEqual(model.post(id: profileOnlyPost.id), profileOnlyPost)

        await model.toggleLike(postID: profileOnlyPost.id)
        await model.toggleRepost(postID: profileOnlyPost.id)

        let profilePost = try XCTUnwrap(model.posts(for: "mae").first { $0.id == profileOnlyPost.id })
        let sharedPost = try XCTUnwrap(model.post(id: profileOnlyPost.id))
        XCTAssertTrue(profilePost.isLiked)
        XCTAssertEqual(profilePost.likeCount, 6)
        XCTAssertTrue(profilePost.isReposted)
        XCTAssertEqual(profilePost.repostCount, 3)
        XCTAssertEqual(profilePost, sharedPost)

        await model.toggleLike(postID: profileOnlyPost.id)
        await model.toggleRepost(postID: profileOnlyPost.id)

        let unlikedProfilePost = try XCTUnwrap(model.posts(for: "mae").first { $0.id == profileOnlyPost.id })
        let unlikedSharedPost = try XCTUnwrap(model.post(id: profileOnlyPost.id))
        XCTAssertFalse(unlikedProfilePost.isLiked)
        XCTAssertEqual(unlikedProfilePost.likeCount, 5)
        XCTAssertFalse(unlikedProfilePost.isReposted)
        XCTAssertEqual(unlikedProfilePost.repostCount, 2)
        XCTAssertEqual(unlikedProfilePost, unlikedSharedPost)
        XCTAssertEqual(model.timelinePosts.map(\.id), initialTimelineIDs)
        XCTAssertFalse(model.timelinePosts.contains { $0.id == profileOnlyPost.id })
    }

    func testPublishingInsertsPostAndSignOutClearsFeed() async {
        let model = makeAppModel(service: MockBlueskyService())
        await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
        let success = await model.publish(text: "  A demo post  ")
        XCTAssertTrue(success)
        XCTAssertEqual(model.posts.first?.text, "A demo post")
        await model.loadProfile(actorID: "mae")
        XCTAssertNotNil(model.profileCursors["mae"])
        await model.signOut()
        XCTAssertNil(model.account)
        XCTAssertTrue(model.posts.isEmpty)
        XCTAssertTrue(model.profileCursors.isEmpty)
        XCTAssertTrue(model.loadingMoreProfileIDs.isEmpty)
    }

    func testPublishedPostUsesAccountAndKeepsSelectedImages() async {
        let service = ModerationTestService()
        let model = makeAppModel(service: service)
        await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
        let image = ImageAttachment(data: Data([1, 2, 3]), altText: "A photo")
        let success = await model.publish(text: "Hello", images: [image])
        XCTAssertTrue(success)
        XCTAssertEqual(model.posts.first?.author.name, "Demo Account")
        XCTAssertEqual(model.posts.first?.pendingImages, [image])
        await Task.yield()
        XCTAssertEqual(model.posts.first?.pendingImages, [image])
        XCTAssertNil(model.publishErrorMessage)
    }

    func testFailedPublishKeepsCurrentFeed() async {
        let model = makeAppModel(service: MockBlueskyService(failsPublishing: true))
        await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
        let initialCount = model.posts.count
        let success = await model.publish(text: "This will fail")
        XCTAssertFalse(success)
        XCTAssertEqual(model.posts.count, initialCount)
        XCTAssertNotNil(model.publishErrorMessage)
    }

    func testPublishRejectsMissingQuoteOrReplyBeforeSending() async {
        let service = ActionFailureTestService()
        let model = makeAppModel(service: service)
        await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")

        for (reply, quote) in [(nil, "missing-quote"), ("missing-reply", nil)] {
            let success = await model.publish(text: "A post", replyTo: reply, quotePostID: quote)
            XCTAssertFalse(success)
            XCTAssertEqual(model.publishErrorMessage, ServiceError.postReferenceUnavailable.localizedDescription)
            XCTAssertFalse(model.publishResultUnknown)
            XCTAssertTrue(service.publishedTexts.isEmpty)
        }
    }

    func testUnknownActionsKeepStateAndBlockRepeats() async throws {
        let service = ActionFailureTestService()
        let model = makeAppModel(service: service)
        await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
        await model.loadProfile(actorID: "mae")
        let original = try XCTUnwrap(model.post(id: "post-1"))
        let following = model.followingActorIDs.contains("mae")
        await model.toggleLike(postID: original.id)
        await model.toggleLike(postID: original.id)
        await model.toggleRepost(postID: original.id)
        XCTAssertEqual(service.likeCalls, 1)
        XCTAssertEqual(service.repostCalls, 0)
        XCTAssertEqual(model.post(id: original.id), original)
        await model.toggleRepost(postID: "post-2")
        await model.toggleRepost(postID: "post-2")
        XCTAssertEqual(service.repostCalls, 1)
        await model.toggleFollow(actorID: "mae")
        await model.toggleFollow(actorID: "mae")
        XCTAssertEqual(service.followCalls, 1)
        XCTAssertEqual(model.followingActorIDs.contains("mae"), following)
        XCTAssertEqual(model.profile(actorID: "mae")?.isFollowing, following)
    }

    func testActionErrorsKeepDetailsAndWarnAboutUnknownResult() async {
        let service = ActionFailureTestService()
        service.publishFailure = ServiceError.postTextTooLong
        let model = makeAppModel(service: service)
        await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
        await model.toggleLike(postID: "post-1")
        XCTAssertTrue(model.errorMessage?.contains(ServiceError.invalidResponse.localizedDescription) == true)
        XCTAssertTrue(model.errorMessage?.contains("may have received") == true)
        await model.toggleRepost(postID: "post-1")
        XCTAssertTrue(model.errorMessage?.contains("may have received") == true)
        await model.toggleFollow(actorID: "mae")
        XCTAssertTrue(model.errorMessage?.contains("may have received") == true)
        _ = await model.publish(text: "A post")
        XCTAssertEqual(model.publishErrorMessage, ServiceError.postTextTooLong.localizedDescription)
        XCTAssertFalse(model.publishResultUnknown)
    }

    func testRefreshDuringActionDoesNotAdjustCountsTwice() async throws {
        let service = ActionFailureTestService()
        service.failActions = false
        service.actionDelay = 100_000_000
        let model = makeAppModel(service: service)
        await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
        var refreshed = try XCTUnwrap(model.post(id: "post-1"))
        let initialLikes = refreshed.likeCount
        let initialReposts = refreshed.repostCount
        let like = Task { await model.toggleLike(postID: "post-1") }
        while !model.activePostActions.contains("post-1") { await Task.yield() }
        refreshed.isLiked = true
        refreshed.likeCount += 1
        service.refreshedPost = refreshed
        await model.loadFeed()
        await like.value
        XCTAssertEqual(model.post(id: "post-1")?.likeCount, initialLikes + 1)

        let repost = Task { await model.toggleRepost(postID: "post-1") }
        while !model.activePostActions.contains("post-1") { await Task.yield() }
        refreshed.isReposted = true
        refreshed.repostCount += 1
        service.refreshedPost = refreshed
        await model.loadFeed()
        await repost.value
        XCTAssertEqual(model.post(id: "post-1")?.repostCount, initialReposts + 1)
    }

    func testDuplicatePostActionWaitsForFirstRequest() async {
        let service = ActionFailureTestService()
        service.actionDelay = 100_000_000
        let model = makeAppModel(service: service)
        await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
        let first = Task { await model.toggleLike(postID: "post-1") }
        while !model.activePostActions.contains("post-1") { await Task.yield() }
        await model.toggleLike(postID: "post-1")
        await model.toggleRepost(postID: "post-1")
        await first.value
        XCTAssertEqual(service.likeCalls, 1)
        XCTAssertEqual(service.repostCalls, 0)
        XCTAssertFalse(model.activePostActions.contains("post-1"))
    }

    func testPublishFailureCanRetryWithSameTextAndAttachment() async {
        let service = ActionFailureTestService()
        let model = makeAppModel(service: service)
        await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
        let image = ImageAttachment(data: Data([1, 2, 3]), altText: "A red boat", mimeType: "image/jpeg")
        let initialCount = model.posts.count
        let first = await model.publish(text: "A post", images: [image])
        XCTAssertFalse(first)
        XCTAssertEqual(model.posts.count, initialCount)
        XCTAssertNotNil(model.publishErrorMessage)
        XCTAssertTrue(model.publishResultUnknown)
        let second = await model.publish(text: "A post", images: [image])
        XCTAssertFalse(second)
        XCTAssertEqual(service.publishedTexts, ["A post"])
        XCTAssertEqual(service.publishedImages.first?.first?.altText, "A red boat")
        XCTAssertEqual(service.publishedImages.first?.first?.data, image.data)
    }
}