1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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)
}
}