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
218
219
220
221
222
223
224
225
226
227
228
229
230
import Observation
import XCTest
@testable import OldFella
extension AppModelTests {
func testReplyContextDoesNotObserveUnrelatedNotificationPosts() async throws {
let service = ModerationTestService()
let author = Post.Author(id: "other", name: "Other", handle: "other", isFollowing: true)
func post(_ id: String, text: String) -> Post {
Post(
id: id, author: author, text: text, likeCount: 0, repostCount: 0,
isLiked: false, isReposted: false, imageURLs: [], replyIDs: [])
}
let parent = post("parent", text: "Original parent")
var child = post("child", text: "Reply")
child.replyRootURI = parent.id
child.replyParentURI = parent.id
let standalone = post("standalone", text: "A separate post")
let unrelated = post("notification-post", text: "An unrelated notification post")
service.feedPage = FeedPage(
posts: [child, parent, standalone], cursor: nil,
replyContexts: [child.id: [parent]])
setUnrelatedNotification(service, author: author, post: unrelated)
let model = makeAppModel(service: service)
await model.signInWithDemo()
XCTAssertEqual(model.timelineRowIDs, [child.id, standalone.id])
XCTAssertEqual(model.replyContext(for: child), [parent])
XCTAssertTrue(model.replyContext(for: standalone).isEmpty)
let originalContexts = model.replyContexts
let replyChanges = ObservationProbe { model.replyContext(for: child) }
let standaloneChanges = ObservationProbe { model.replyContext(for: standalone) }
await model.loadNotifications()
await Task.yield()
XCTAssertEqual(model.post(id: unrelated.id), unrelated)
XCTAssertEqual(model.timelineRowIDs, [child.id, standalone.id])
XCTAssertEqual(model.replyContexts, originalContexts)
XCTAssertEqual(model.replyContext(for: child), [parent])
XCTAssertTrue(model.replyContext(for: standalone).isEmpty)
XCTAssertEqual(replyChanges.invalidationCount, 0)
XCTAssertTrue(replyChanges.values.isEmpty, "An unrelated cached post must not update reply context")
XCTAssertEqual(standaloneChanges.invalidationCount, 0)
XCTAssertTrue(standaloneChanges.values.isEmpty, "An unrelated cached post must not update a standalone row")
replyChanges.reset()
standaloneChanges.reset()
var updated = parent
updated.text = "Updated parent"
service.threadPosts = [updated]
await model.loadPost(id: parent.id)
await Task.yield()
XCTAssertEqual(model.replyContext(for: child), [updated])
XCTAssertEqual(replyChanges.invalidationCount, 1)
XCTAssertEqual(replyChanges.values, [[updated]])
XCTAssertEqual(standaloneChanges.invalidationCount, 0)
XCTAssertTrue(standaloneChanges.values.isEmpty)
assertSyntheticReplyContext(model, child: child, standalone: standalone, updated: updated)
}
func testPreparedFeedRowsKeepIdentityAcrossContentUpdates() async throws {
let model = makeAppModel(service: MockBlueskyService())
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let rows = model.timelineRows
let first = try XCTUnwrap(rows.first)
XCTAssertEqual(rows.map(\.id), model.timelinePosts.map(\.id))
for row in rows { XCTAssertTrue(row === model.postViewState(for: row.post)) }
let changes = ObservationProbe { model.timelineRows.map(\.id) }
await model.toggleLike(postID: first.id)
await model.refreshFeed()
await Task.yield()
XCTAssertTrue(first.post.isLiked)
XCTAssertTrue(zip(model.timelineRows, rows).allSatisfy { $0 === $1 })
XCTAssertTrue(changes.values.isEmpty)
await model.hideAccountFromTimeline(actorID: first.post.author.id, block: false)
XCTAssertFalse(model.timelineRows.contains { $0.post.author.id == first.post.author.id })
await model.signOut()
XCTAssertTrue(model.timelineRows.isEmpty)
}
func testLikeObservesOnlyTheChangedRow() async throws {
let service = ActionFailureTestService()
service.failActions = false
service.actionDelay = 100_000_000
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let target = model.postViewState(for: try XCTUnwrap(model.post(id: "post-1")))
let other = model.postViewState(for: try XCTUnwrap(model.post(id: "post-2")))
let account = ObservationProbe { (model.account, model.isRestoringSession, model.errorMessage) }
let timeline = ObservationProbe { (model.hasUnreadNotifications, model.timelineRowIDs) }
let otherRow = ObservationProbe {
(other.post, other.isBusy, other.isUncertain, other.decision, other.quotedPost)
}
let busy = ObservationProbe { target.isBusy }
let post = ObservationProbe { target.post }
let original = target.post
let action = Task { await model.toggleLike(postID: original.id) }
while !target.isBusy { await Task.yield() }
await Task.yield()
XCTAssertEqual(busy.values, [true])
await action.value
await Task.yield()
XCTAssertEqual(busy.values, [true, false])
XCTAssertEqual(post.values.count, 1)
XCTAssertTrue(target.post.isLiked)
XCTAssertEqual(target.post.likeCount, original.likeCount + 1)
XCTAssertTrue(account.values.isEmpty)
XCTAssertTrue(timeline.values.isEmpty)
XCTAssertTrue(otherRow.values.isEmpty)
}
func testPostRowObservesModerationDecisionChanges() async throws {
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.feedPage = FeedPage(posts: [post], cursor: nil)
let model = makeAppModel(service: service)
await model.signInWithDemo()
let state = model.postViewState(for: post)
let decisions = ObservationProbe { state.decision }
service.mutedWords = [.init(value: "hidden")]
await model.reloadContentPreferences()
await Task.yield()
XCTAssertEqual(decisions.values.count, 1)
XCTAssertTrue(decisions.values.allSatisfy { $0 == nil })
XCTAssertNil(state.decision)
await model.loadFeed()
let reloaded = model.postViewState(for: post)
XCTAssertEqual(reloaded.decision?.action, .hide)
XCTAssertTrue(model.timelineRowIDs.isEmpty)
}
func testFailedLikeUpdatesOnlyItsRow() async throws {
for unknown in [false, true] {
let service = ActionFailureTestService()
service.actionDelay = 100_000_000
if !unknown { service.likeFailure = ActionNotSentError(underlying: ServiceError.invalidResponse) }
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let target = model.postViewState(for: try XCTUnwrap(model.post(id: "post-1")))
let other = model.postViewState(for: try XCTUnwrap(model.post(id: "post-2")))
let original = target.post
let busy = ObservationProbe { target.isBusy }
let uncertain = ObservationProbe { target.isUncertain }
let otherRow = ObservationProbe {
(other.post, other.isBusy, other.isUncertain, other.decision, other.quotedPost)
}
let action = Task { await model.toggleLike(postID: original.id) }
while !target.isBusy { await Task.yield() }
await Task.yield()
XCTAssertEqual(busy.values, [true])
await action.value
await Task.yield()
XCTAssertEqual(busy.values, [true, false])
XCTAssertEqual(target.post, original)
XCTAssertEqual(target.isUncertain, unknown)
XCTAssertEqual(uncertain.values, unknown ? [true] : [])
XCTAssertTrue(otherRow.values.isEmpty)
XCTAssertNotNil(model.errorMessage)
}
}
func testLikeAndRepostUpdateCounts() async {
let model = makeAppModel(service: MockBlueskyService())
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
await model.toggleLike(postID: "post-1")
await model.toggleRepost(postID: "post-1")
XCTAssertEqual(model.post(id: "post-1")?.likeCount, 19)
XCTAssertEqual(model.post(id: "post-1")?.repostCount, 4)
await model.toggleLike(postID: "post-1")
XCTAssertEqual(model.post(id: "post-1")?.likeCount, 18)
}
func testProfileInteractionsUpdateCanonicalFeedPost() async throws {
let model = makeAppModel(service: MockBlueskyService())
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
await model.loadProfile(actorID: "mae")
let initialTimelineIDs = model.timelinePosts.map(\.id)
await model.toggleLike(postID: "post-1")
await model.toggleRepost(postID: "post-1")
let likedProfilePost = try XCTUnwrap(model.posts(for: "mae").first)
let likedSharedPost = try XCTUnwrap(model.post(id: "post-1"))
XCTAssertTrue(likedProfilePost.isLiked)
XCTAssertEqual(likedProfilePost.likeCount, 19)
XCTAssertTrue(likedProfilePost.isReposted)
XCTAssertEqual(likedProfilePost.repostCount, 4)
XCTAssertEqual(likedProfilePost, likedSharedPost)
await model.toggleLike(postID: "post-1")
await model.toggleRepost(postID: "post-1")
let unlikedProfilePost = try XCTUnwrap(model.posts(for: "mae").first)
let unlikedSharedPost = try XCTUnwrap(model.post(id: "post-1"))
XCTAssertFalse(unlikedProfilePost.isLiked)
XCTAssertEqual(unlikedProfilePost.likeCount, 18)
XCTAssertFalse(unlikedProfilePost.isReposted)
XCTAssertEqual(unlikedProfilePost.repostCount, 3)
XCTAssertEqual(unlikedProfilePost, unlikedSharedPost)
XCTAssertEqual(model.timelinePosts.map(\.id), initialTimelineIDs)
}
private func setUnrelatedNotification(_ service: ModerationTestService, author: Post.Author, post: Post) {
service.notificationPage = NotificationPage(
notifications: [
FeedNotification(
id: "notification", author: author, reason: .like,
indexedAt: Date(timeIntervalSince1970: 1_800_000_000), isRead: false,
postID: post.id, relatedPost: post)
], cursor: nil)
}
private func assertSyntheticReplyContext(
_ model: AppModel, child: Post, standalone: Post, updated: Post
) {
var synthetic = child
synthetic.replyRootURI = standalone.id
synthetic.replyParentURI = standalone.id
XCTAssertEqual(model.replyContext(for: synthetic), [standalone])
XCTAssertEqual(model.replyContext(for: child), [updated])
}
}