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

geesawra.industries/old-fella

Old people's social media app

0
1
main
221 lines · 11.3 KB Raw History
import Observation
import XCTest

@testable import OldFella

extension AppModelTests {
    func testCancelledReadsDoNotShowErrorsOrRemoveContent() async {
        let service = ModerationTestService()
        let model = makeAppModel(service: service)
        await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
        let ids = model.posts.map(\.id)
        for error: Error in [CancellationError(), URLError(.cancelled)] {
            service.readError = error
            service.preferenceError = error
            await model.reloadContentPreferences()
            await model.loadFeed()
            await model.loadNextPage()
            await model.loadPost(id: "post-1")
            await model.loadProfile(actorID: "mae", forceRefresh: true)
            XCTAssertEqual(model.posts.map(\.id), ids)
            XCTAssertNil(model.errorMessage)
            XCTAssertNil(model.feedErrorMessage)
            XCTAssertNil(model.postErrors["post-1"])
            XCTAssertNil(model.profileErrors["mae"])
            XCTAssertNil(model.contentPreferencesError)
            XCTAssertNil(model.contentPreferencesRefreshError)
            XCTAssertFalse(model.isLoading)
            XCTAssertFalse(model.isLoadingPost)
        }
    }

    func testReadFailuresStayInlineAndKeepExistingFeed() async {
        let service = ModerationTestService()
        let author = Post.Author(id: "author", name: "Author", handle: "author")
        let posts = (0..<20).map { index in
            Post(
                id: "fixture-\(index)", author: author, text: "Post \(index)", likeCount: 0, repostCount: 0,
                isLiked: false, isReposted: false, imageURLs: [], quote: nil, replyIDs: [])
        }
        service.feedPage = FeedPage(posts: posts, cursor: "next-page")
        let model = makeAppModel(service: service)
        await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
        let ids = model.posts.map(\.id)
        service.readError = URLError(.timedOut)
        await model.loadFeed()
        XCTAssertNotNil(model.feedErrorMessage)
        XCTAssertFalse(model.retryFeedPage)
        await model.loadNextPage()
        XCTAssertTrue(model.retryFeedPage)
        await model.loadPost(id: "post-1")
        await model.loadProfile(actorID: "mae")
        XCTAssertNotNil(model.postErrors["post-1"])
        XCTAssertNotNil(model.profileErrors["mae"])
        XCTAssertEqual(model.posts.map(\.id), ids)
        XCTAssertNil(model.errorMessage)
    }

    func testChangedPreferencesInvalidateContent() async {
        let service = ModerationTestService()
        let model = makeAppModel(service: service)
        await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
        let revision = model.contentRevision
        service.mutedWords = [.init(value: "walk")]
        await model.reloadContentPreferences()
        XCTAssertNotEqual(model.contentRevision, revision)
        XCTAssertTrue(model.posts.isEmpty)
        await model.loadFeed()
        XCTAssertFalse(model.timelinePosts.contains { $0.text.localizedCaseInsensitiveContains("walk") })
    }

    func testNotificationsKeepInformationalAuthorLabelsButHideFilteredAuthors() async {
        let service = ModerationTestService()
        func author(_ id: String, label: String) -> Post.Author {
            Post.Author(id: id, name: id, handle: id, moderationLabels: [.init(value: label, sourceDID: id)])
        }
        let informational = author("information-author", label: "information")
        let hidden = author("hidden-author", label: "!hide")
        let blurred = author("blurred-author", label: "!warn")
        let definition = ModerationPolicy.LabelDefinition(
            value: "information", sourceDID: informational.id, severity: .inform, blur: .none)
        service.preferenceOverride = .init(
            userDID: "demo", adultContentEnabled: true, labels: ["information": .warn],
            labelDefinitions: ModerationPolicy.builtInDefinitions + [definition])
        let recent = Date()
        service.notificationPage = NotificationPage(
            notifications: [
                FeedNotification(
                    id: "recent-like", author: informational, reason: .like, indexedAt: recent, isRead: false,
                    postID: "post"),
                FeedNotification(
                    id: "recent-reply", author: informational, reason: .reply, indexedAt: recent, isRead: false,
                    postID: "reply"),
                FeedNotification(
                    id: "hidden", author: hidden, reason: .follow, indexedAt: recent, isRead: false, postID: nil),
                FeedNotification(
                    id: "blurred", author: blurred, reason: .follow, indexedAt: recent, isRead: false, postID: nil)
            ], cursor: nil)
        let model = makeAppModel(service: service)
        await model.signInWithDemo()
        await model.loadNotifications()

        XCTAssertEqual(model.contentDecision(author: informational)?.action, .warn)
        XCTAssertEqual(model.visibleNotifications.map(\.id), ["recent-like", "recent-reply"])
        XCTAssertEqual(model.notificationRows.map(\.id), ["recent-like", "recent-reply"])
        XCTAssertTrue(model.hasUnreadNotifications)
    }

    func testPreparedContentReadsDoNotRepeatFilteringOrModeration() async throws {
        let service = ModerationTestService()
        service.mutedWords = [.init(value: "hidden")]
        let author = Post.Author(id: "other", name: "Other", handle: "other")
        let safe = Post(
            id: "safe", author: author, text: "safe text", likeCount: 0, repostCount: 0, isLiked: false,
            isReposted: false, imageURLs: [], replyIDs: [])
        let hidden = Post(
            id: "hidden", author: author, text: "hidden text", likeCount: 0, repostCount: 0, isLiked: false,
            isReposted: false, imageURLs: [], replyIDs: [])
        service.feedPage = FeedPage(posts: [safe, hidden], cursor: nil)
        service.notificationPage = NotificationPage(
            notifications: [
                FeedNotification(
                    id: "visible", author: author, reason: .reply, indexedAt: Date(), isRead: false, postID: safe.id,
                    relatedPost: safe),
                FeedNotification(
                    id: "filtered", author: author, reason: .reply, indexedAt: Date(), isRead: false, postID: hidden.id,
                    relatedPost: hidden)
            ], cursor: nil)
        let model = makeAppModel(service: service)
        await model.signInWithDemo()
        await model.loadNotifications()
        let preparations = model.contentPreparationCount
        let evaluations = model.moderationEvaluationCount
        for _ in 0..<100 {
            XCTAssertEqual(model.timelinePosts.map(\.id), [safe.id])
            XCTAssertEqual(model.visibleNotifications.map(\.id), ["visible"])
            XCTAssertEqual(model.notificationGroups.map(\.id), ["visible"])
            XCTAssertTrue(model.hasUnreadNotifications)
            XCTAssertEqual(model.contentDecision(for: safe)?.action, .allow)
            XCTAssertEqual(model.contentDecision(author: author)?.action, .allow)
        }
        XCTAssertEqual(model.contentPreparationCount, preparations)
        XCTAssertEqual(model.moderationEvaluationCount, evaluations)
        model.clearError()
        model.clearModerationError()
        XCTAssertEqual(model.contentPreparationCount, preparations)
        XCTAssertEqual(model.moderationEvaluationCount, evaluations)
        try await model.markNotificationsRead()
        XCTAssertFalse(model.hasUnreadNotifications)
        XCTAssertTrue(model.notificationGroups[0].notifications[0].isRead)
        XCTAssertEqual(model.moderationEvaluationCount, evaluations)
    }

    func testPreparedContentUpdatesForMuteAndFollowChanges() async {
        let service = ModerationTestService()
        let author = Post.Author(id: "other", name: "Other", handle: "other")
        let post = Post(
            id: "post", author: author, text: "hidden text", likeCount: 0, repostCount: 0, isLiked: false,
            isReposted: false, imageURLs: [], replyIDs: [])
        service.mutedWords = [.init(value: "hidden", excludeFollowing: true)]
        service.feedPage = FeedPage(posts: [post], cursor: nil)
        service.notificationPage = NotificationPage(
            notifications: [
                FeedNotification(
                    id: "notification", author: author, reason: .reply, indexedAt: Date(), isRead: false,
                    postID: post.id, relatedPost: post)
            ], cursor: nil)
        let model = makeAppModel(service: service)
        await model.signInWithDemo()
        await model.loadNotifications()
        XCTAssertTrue(model.timelinePosts.isEmpty)
        XCTAssertFalse(model.hasUnreadNotifications)
        await model.toggleFollow(actorID: author.id)
        XCTAssertEqual(model.timelinePosts.map(\.id), [post.id])
        XCTAssertTrue(model.hasUnreadNotifications)
        await model.hideAccountFromTimeline(actorID: author.id, block: false)
        XCTAssertTrue(model.timelinePosts.isEmpty)
        XCTAssertTrue(model.visibleNotifications.isEmpty)
        XCTAssertTrue(model.notificationGroups.isEmpty)
        XCTAssertFalse(model.hasUnreadNotifications)
        await model.setModeration(actorID: author.id, block: false)
        XCTAssertEqual(model.timelinePosts.map(\.id), [post.id])
        XCTAssertTrue(model.hasUnreadNotifications)
        await model.toggleFollow(actorID: author.id)
        XCTAssertTrue(model.timelinePosts.isEmpty)
        XCTAssertFalse(model.hasUnreadNotifications)
    }

    func testPreparedContentUpdatesWhenQuoteFallbackChanges() async {
        let service = ModerationTestService()
        let author = Post.Author(id: "other", name: "Other", handle: "other")
        let quoted = Post(
            id: "quoted", author: author, text: "safe text", likeCount: 0, repostCount: 0, isLiked: false,
            isReposted: false, imageURLs: [], replyIDs: [])
        let enclosing = Post(
            id: "enclosing", author: author, text: "safe text", likeCount: 0, repostCount: 0, isLiked: false,
            isReposted: false, imageURLs: [], quote: Post.Quote(postID: quoted.id), replyIDs: [])
        service.mutedWords = [.init(value: "hidden")]
        service.feedPage = FeedPage(posts: [enclosing, quoted], cursor: nil)
        let model = makeAppModel(service: service)
        await model.signInWithDemo()
        XCTAssertEqual(model.timelinePosts.map(\.id), [enclosing.id, quoted.id])
        let enclosingState = model.postViewState(for: enclosing)
        let quotedChanges = ObservationProbe { enclosingState.quotedPost }
        var hidden = quoted
        hidden.text = "hidden text"
        service.threadPosts = [hidden]
        await model.loadPost(id: quoted.id)
        await Task.yield()
        XCTAssertTrue(model.timelinePosts.isEmpty)
        XCTAssertEqual(enclosingState.quotedPost, hidden)
        XCTAssertEqual(enclosingState.decision?.action, .hide)
        XCTAssertEqual(quotedChanges.values.count, 1)
        XCTAssertEqual(model.contentDecision(for: enclosing)?.action, .hide)
        service.threadPosts = [quoted]
        await model.loadPost(id: quoted.id)
        XCTAssertEqual(model.timelinePosts.map(\.id), [enclosing.id, quoted.id])
        XCTAssertEqual(model.contentDecision(for: enclosing)?.action, .allow)
        XCTAssertEqual(enclosingState.quotedPost, quoted)
        XCTAssertEqual(enclosingState.decision?.action, .allow)
    }
}