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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
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"])
}
}