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

geesawra.industries/old-fella

Old people's social media app

0
1
main
137 lines · 6.1 KB Raw History
import Foundation
import XCTest

@testable import OldFella

extension SearchTests {
    func testPostSearchUsesTimelineReplyAndRepostRulesAcrossPages() async {
        let service = SearchTestService()
        var root = post("root")
        root.author.isFollowing = true
        var hiddenRoot = post("hidden-root")
        hiddenRoot.moderationLabels = [.init(value: "!warn", sourceDID: hiddenRoot.author.id)]
        func reply(_ id: String, rootID: String, followed: Bool) -> Post {
            var result = post(id)
            result.author.isFollowing = followed
            result.replyRootURI = rootID
            result.replyParentURI = rootID
            return result
        }
        let hiddenContext = reply("hidden-context", rootID: hiddenRoot.id, followed: true)
        let hiddenRepost = post("hidden-repost")
        let warnedAuthor = Post.Author(
            id: "warned", name: "warned", handle: "warned",
            moderationLabels: [
                .init(value: "!warn", sourceDID: "warned")
            ])
        let unfollowed = reply("unfollowed", rootID: root.id, followed: false)
        let first = reply("first", rootID: root.id, followed: true)
        let later = reply("later", rootID: root.id, followed: true)
        service.postPages = [
            "first": FeedPage(
                posts: [hiddenContext, hiddenRepost, unfollowed, first], cursor: "next",
                replyContexts: [hiddenContext.id: [hiddenRoot], unfollowed.id: [root], first.id: [root]],
                repostedBy: [hiddenRepost.id: warnedAuthor]),
            "next": FeedPage(posts: [later, post("standalone")], cursor: nil, replyContexts: [later.id: [root]])
        ]
        let suite = "SearchTests.\(UUID().uuidString)"
        let defaults = UserDefaults(suiteName: suite)!
        defer { defaults.removePersistentDomain(forName: suite) }
        let model = AppModel(service: service, feedDefaults: defaults)
        await model.signIn(handle: "demo", appPassword: "password")
        model.resetSearch(query: "posts", kind: .posts)
        await model.loadSearchPage()
        await model.loadSearchPage()

        XCTAssertEqual(model.visibleSearchPosts.map(\.id), ["first", "standalone"])
        model.induceADHD = true
        XCTAssertEqual(model.visibleSearchPosts.map(\.id), ["unfollowed", "standalone"])
    }

    func testMentionSuggestionsPreserveSearchStateAndLimitUniqueAccounts() async throws {
        let service = SearchTestService()
        service.accountPages["first"] = AccountSearchPage(accounts: [author("saved")], cursor: "next")
        let model = await signedInModel(service)
        model.resetSearch(query: "saved search", kind: .accounts)
        await model.loadSearchPage()
        service.accountPages["first"] = AccountSearchPage(
            accounts: [author("a"), author("a"), author("b"), author("c"), author("d"), author("e"), author("f")],
            cursor: "ignored"
        )

        let suggestions = try await model.mentionSuggestions(query: "  friend  ")

        XCTAssertEqual(suggestions.map(\.id), ["a", "b", "c", "d", "e"])
        XCTAssertEqual(service.accountRequests.last?.query, "friend")
        XCTAssertNil(service.accountRequests.last?.cursor)
        XCTAssertEqual(model.searchQuery, "saved search")
        XCTAssertEqual(model.searchAccounts.map(\.id), ["saved"])
        XCTAssertEqual(model.searchCursor, "next")
        XCTAssertTrue(model.hasSearched)
        XCTAssertFalse(model.isSearching)
        XCTAssertNil(model.searchError)
    }

    func testMentionSuggestionsHideAccountsWithModerationLabels() async throws {
        let service = SearchTestService()
        let hidden = Post.Author(
            id: "hidden", name: "Hidden", handle: "hidden.example",
            moderationLabels: [
                .init(value: "!hide", sourceDID: "hidden")
            ])
        service.accountPages["first"] = AccountSearchPage(accounts: [hidden, author("visible")], cursor: nil)
        let model = await signedInModel(service)

        let suggestions = try await model.mentionSuggestions(query: "friend")

        XCTAssertEqual(suggestions.map(\.id), ["visible"])
        XCTAssertFalse(model.isAccountSummaryVisible(hidden))
    }

    func testMentionSuggestionsDiscardResponseAfterSignOut() async throws {
        let service = SearchTestService()
        let started = expectation(description: "Mention lookup started")
        service.suspendedAccountQuery = "friend"
        service.accountStarted = started
        let model = await signedInModel(service)
        let lookup = Task { try await model.mentionSuggestions(query: "friend") }
        await fulfillment(of: [started], timeout: 2)
        await model.signOut()
        service.resumeAccount(with: AccountSearchPage(accounts: [author("old")], cursor: nil))

        do {
            _ = try await lookup.value
            XCTFail("A lookup from an old session must be canceled.")
        } catch is CancellationError {
        }
    }

    func testMentionSuggestionsDiscardCanceledResponse() async throws {
        let service = SearchTestService()
        let started = expectation(description: "Mention lookup started")
        service.suspendedAccountQuery = "friend"
        service.accountStarted = started
        let model = await signedInModel(service)
        let lookup = Task { try await model.mentionSuggestions(query: "friend") }
        await fulfillment(of: [started], timeout: 2)
        lookup.cancel()
        service.resumeAccount(with: AccountSearchPage(accounts: [author("old")], cursor: nil))

        do {
            _ = try await lookup.value
            XCTFail("A canceled lookup must discard its response.")
        } catch is CancellationError {
        }
    }

    func testMentionSuggestionsSkipEmptyQueriesAndSignedOutAccounts() async throws {
        let service = SearchTestService()
        let model = AppModel(service: service)
        let signedOut = try await model.mentionSuggestions(query: "friend")
        XCTAssertTrue(signedOut.isEmpty)
        await model.signIn(handle: "demo", appPassword: "password")
        let empty = try await model.mentionSuggestions(query: " \n ")
        XCTAssertTrue(empty.isEmpty)
        XCTAssertTrue(service.accountRequests.isEmpty)
    }
}