import Foundation import XCTest @testable import OldFella @MainActor final class SearchTests: XCTestCase { func author(_ id: String) -> Post.Author { Post.Author(id: id, name: id, handle: id) } func post(_ id: String) -> Post { Post( id: id, author: author("author"), text: id, likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], quote: nil, replyIDs: []) } func signedInModel(_ service: SearchTestService) async -> AppModel { let model = AppModel(service: service) await model.signIn(handle: "demo", appPassword: "password") return model } func testOwnPostsFilterResetsPagesAndForwardsAccountOnEveryPage() async { let service = SearchTestService() service.postPages = [ "first": FeedPage(posts: [post("first")], cursor: "next"), "next": FeedPage(posts: [post("second")], cursor: nil) ] let model = await signedInModel(service) model.resetSearch(query: "birds", kind: .posts) await model.loadSearchPage() model.resetSearch(query: "birds", kind: .posts, ownPostsOnly: true) XCTAssertTrue(model.searchPosts.isEmpty) XCTAssertNil(model.searchCursor) await model.loadSearchPage() await model.loadSearchPage() XCTAssertEqual(service.postRequests.map(\.cursor), [nil, nil, "next"]) XCTAssertEqual(service.postRequests.map(\.authorID), [nil, model.account?.id, model.account?.id]) model.resetSearch(query: "birds", kind: .posts) XCTAssertFalse(model.searchOwnPostsOnly) await model.loadSearchPage() XCTAssertNil(service.postRequests.last?.authorID) } func testAccountPagesForwardCursorRemoveDuplicatesAndStopAtCycle() async { let service = SearchTestService() service.accountPages = [ "first": AccountSearchPage(accounts: [author("a"), author("a")], cursor: "one"), "one": AccountSearchPage(accounts: [author("a"), author("b")], cursor: "two"), "two": AccountSearchPage(accounts: [author("c")], cursor: "one") ] let model = await signedInModel(service) model.resetSearch(query: " people ", kind: .accounts) await model.loadSearchPage() await model.loadSearchPage() await model.loadSearchPage() await model.loadSearchPage() XCTAssertEqual(service.accountRequests.map(\.query), ["people", "people", "people"]) XCTAssertEqual(service.accountRequests.map(\.cursor), [nil, "one", "two"]) XCTAssertEqual(model.searchAccounts.map(\.id), ["a", "b", "c"]) XCTAssertNil(model.searchCursor) XCTAssertTrue(model.hasSearched) } func testPostPagesForwardCursorRemoveDuplicatesAndStopAtEnd() async { let service = SearchTestService() service.postPages = [ "first": FeedPage(posts: [post("a"), post("a")], cursor: "next"), "next": FeedPage(posts: [post("a"), post("b")], cursor: nil) ] let model = await signedInModel(service) model.resetSearch(query: "posts", kind: .posts) await model.loadSearchPage() await model.loadSearchPage() await model.loadSearchPage() XCTAssertEqual(service.postRequests.map(\.cursor), [nil, "next"]) XCTAssertEqual(service.postRequests.map(\.query), ["posts", "posts"]) XCTAssertEqual(model.searchPosts.map(\.id), ["a", "b"]) XCTAssertNil(model.searchCursor) } func testFailedNextPagePreservesRowsAndCursorForRetry() async { let service = SearchTestService() service.accountPages = [ "first": AccountSearchPage(accounts: [author("a")], cursor: "next"), "next": AccountSearchPage(accounts: [author("b")], cursor: nil) ] let model = await signedInModel(service) model.resetSearch(query: "people", kind: .accounts) await model.loadSearchPage() service.failAccountCursor = "next" await model.loadSearchPage() XCTAssertEqual(model.searchAccounts.map(\.id), ["a"]) XCTAssertEqual(model.searchCursor, "next") XCTAssertNotNil(model.searchError) service.failAccountCursor = nil await model.loadSearchPage() XCTAssertEqual(service.accountRequests.map(\.cursor), [nil, "next", "next"]) XCTAssertEqual(model.searchAccounts.map(\.id), ["a", "b"]) XCTAssertNil(model.searchCursor) XCTAssertNil(model.searchError) } func testQueryAndKindResetRowsCursorAndSearchState() async { let service = SearchTestService() service.accountPages["first"] = AccountSearchPage(accounts: [author("a")], cursor: "next") service.postPages["first"] = FeedPage(posts: [post("p")], cursor: "next") let model = await signedInModel(service) model.resetSearch(query: "first", kind: .accounts) await model.loadSearchPage() model.resetSearch(query: "second", kind: .accounts) XCTAssertEqual(model.searchQuery, "second") XCTAssertTrue(model.searchAccounts.isEmpty) XCTAssertNil(model.searchCursor) XCTAssertFalse(model.hasSearched) await model.loadSearchPage() model.resetSearch(query: "second", kind: .posts) XCTAssertEqual(model.searchKind, .posts) XCTAssertTrue(model.searchAccounts.isEmpty) XCTAssertTrue(model.searchPosts.isEmpty) XCTAssertNil(model.searchCursor) XCTAssertFalse(model.hasSearched) await model.loadSearchPage() XCTAssertEqual(model.searchPosts.map(\.id), ["p"]) XCTAssertEqual(service.postRequests.map(\.cursor), [nil]) model.resetSearch(query: "third", kind: .posts) XCTAssertTrue(model.searchPosts.isEmpty) XCTAssertNil(model.searchCursor) } func testOldResponseIsDiscardedAfterQueryAndKindChange() async { let service = SearchTestService() let started = expectation(description: "Old search started") service.suspendedAccountQuery = "old" service.accountStarted = started service.postPages["first"] = FeedPage(posts: [post("new")], cursor: nil) let model = await signedInModel(service) model.resetSearch(query: "old", kind: .accounts) let old = Task { await model.loadSearchPage() } await fulfillment(of: [started], timeout: 2) model.resetSearch(query: "new", kind: .posts) await model.loadSearchPage() service.resumeAccount(with: AccountSearchPage(accounts: [author("old")], cursor: "old-next")) await old.value XCTAssertEqual(model.searchQuery, "new") XCTAssertEqual(model.searchKind, .posts) XCTAssertTrue(model.searchAccounts.isEmpty) XCTAssertEqual(model.searchPosts.map(\.id), ["new"]) XCTAssertNil(model.searchCursor) XCTAssertFalse(model.isSearching) } func testConcurrentPageRequestsAreSuppressed() async { let service = SearchTestService() let started = expectation(description: "Search started") service.suspendedAccountQuery = "people" service.accountStarted = started let model = await signedInModel(service) model.resetSearch(query: "people", kind: .accounts) let first = Task { await model.loadSearchPage() } await fulfillment(of: [started], timeout: 2) await model.loadSearchPage() XCTAssertEqual(service.accountRequests.count, 1) service.resumeAccount(with: AccountSearchPage(accounts: [author("a")], cursor: nil)) await first.value XCTAssertEqual(model.searchAccounts.map(\.id), ["a"]) } func testMutedPostIsNotVisibleInSearchResults() async { let service = SearchTestService() service.mutedWords = [.init(value: "muted")] service.postPages["first"] = FeedPage(posts: [post("visible"), post("muted")], cursor: nil) let model = await signedInModel(service) model.resetSearch(query: "posts", kind: .posts) await model.loadSearchPage() XCTAssertEqual(model.searchPosts.map(\.id), ["visible", "muted"]) XCTAssertEqual(model.visibleSearchPosts.map(\.id), ["visible"]) } func testPostSearchUsesTimelineModerationForPostsAndQuotes() async { let service = SearchTestService() let image = URL(string: "https://example.com/image.jpg")! let postAuthor = author("other") func labeledPost(_ id: String, label: String, media: Bool = false, quoted: Bool = false) -> Post { var result = post(id) result.author = postAuthor let labels = [ModerationPolicy.Label(value: label, sourceDID: postAuthor.id)] if quoted { result.quote = Post.Quote( postID: "quote-\(id)", author: postAuthor, text: "quote", imageURLs: media ? [image] : [], moderationLabels: labels) } else { result.moderationLabels = labels result.imageURLs = media ? [image] : [] } return result } service.preferenceOverride = .init( userDID: "demo", adultContentEnabled: true, labels: ["nudity": .warn, "icon-intolerant": .warn], labelDefinitions: ModerationPolicy.builtInDefinitions) service.postPages["first"] = FeedPage( posts: [ post("safe"), labeledPost("hidden", label: "!hide"), labeledPost("warning", label: "!warn"), labeledPost("quote-warning", label: "!warn", quoted: true), labeledPost("media-warning", label: "icon-intolerant", media: true), labeledPost("quote-media-warning", label: "icon-intolerant", media: true, quoted: true), labeledPost("adult-media", label: "nudity", media: true), labeledPost("adult-quote-media", label: "nudity", media: true, quoted: true) ], cursor: nil) let model = await signedInModel(service) model.resetSearch(query: "posts", kind: .posts) await model.loadSearchPage() XCTAssertEqual(model.visibleSearchPosts.map(\.id), ["safe", "adult-media", "adult-quote-media"]) } }