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
231
import Observation
import XCTest
@testable import OldFella
extension AppModelTests {
func testFollowingAndProfileRepostAttributionRemainInSeparateMaps() async {
let repostAuthor = Post.Author(id: "alex", name: "Alex Kim", handle: "alex.example")
let service = ProfilePostTestService(
initialRepostedBy: ["post-1": repostAuthor], followingRepostedBy: ["post-1": repostAuthor])
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
XCTAssertEqual(model.repostedBy["post-1"], repostAuthor)
await model.loadProfile(actorID: "mae")
XCTAssertEqual(model.profileRepostedBy["mae"]?["post-1"], repostAuthor)
XCTAssertNil(model.profileRepostedBy["alex"]?["post-1"])
await model.signOut()
XCTAssertTrue(model.repostedBy.isEmpty)
XCTAssertTrue(model.profileRepostedBy.isEmpty)
}
func testProfilePagesMergeReplyContextsAndActorScopedRepostAttribution() async {
let root = Post(
id: "profile-reply-root",
author: Post.Author(id: "alex", name: "Alex Kim", handle: "alex.example"),
text: "The start of this conversation.",
likeCount: 0,
repostCount: 0,
isLiked: false,
isReposted: false,
imageURLs: [],
replyIDs: []
)
var reply = Post(
id: "profile-reply",
author: Post.Author(id: "mae", name: "Mae Rivers", handle: "mae.example"),
text: "A reply from the profile.",
likeCount: 0,
repostCount: 0,
isLiked: false,
isReposted: false,
imageURLs: [],
replyIDs: []
)
reply.replyRootURI = root.id
reply.replyParentURI = root.id
let initialReposter = Post.Author(id: "alex", name: "Alex Kim", handle: "alex.example")
let laterReposter = Post.Author(id: "sam", name: "Sam Lee", handle: "sam.example")
let service = ProfilePostTestService(
additionalProfilePost: reply,
profileReplyContexts: [reply.id: [root]],
initialRepostedBy: ["post-1": initialReposter],
laterRepostedBy: ["profile-post-more": laterReposter]
)
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
await model.loadProfile(actorID: "mae")
XCTAssertEqual(model.replyContext(for: reply).map(\.id), [root.id])
XCTAssertEqual(model.profileRepostedBy["mae"]?["post-1"], initialReposter)
await model.loadNextProfilePage(actorID: "mae")
XCTAssertEqual(model.profileRepostedBy["mae"]?["profile-post-more"], laterReposter)
XCTAssertNil(model.profileRepostedBy["alex"]?["post-1"])
}
func testProfilePaginationUsesCursorAndAppendsUniquePostsInServerOrder() async {
let model = makeAppModel(service: MockBlueskyService())
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let timelineIDs = model.timelinePosts.map(\.id)
await model.loadProfile(actorID: "mae")
XCTAssertEqual(model.posts(for: "mae").map(\.id), ["post-1"])
XCTAssertEqual(model.profileCursors["mae"], "author-page-2-mae")
XCTAssertFalse(model.posts.contains { $0.id == "profile-post-more" })
XCTAssertEqual(model.timelinePosts.map(\.id), timelineIDs)
await model.loadNextProfilePage(actorID: "mae")
XCTAssertEqual(model.posts(for: "mae").map(\.id), ["post-1", "profile-post-more"])
XCTAssertEqual(model.post(id: "profile-post-more")?.text, "Another post from Mae.")
XCTAssertNil(model.profileCursors["mae"])
XCTAssertEqual(model.timelinePosts.map(\.id), timelineIDs)
XCTAssertFalse(model.timelinePosts.contains { $0.id == "profile-post-more" })
}
func testProfilePaginationFailureKeepsCursorAndExistingPostsForRetry() async {
let service = MockBlueskyService(failsAuthorFeedLoadAtCall: 2)
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
await model.loadProfile(actorID: "mae")
let initialPosts = model.posts(for: "mae")
let cursor = model.profileCursors["mae"]
await model.loadNextProfilePage(actorID: "mae")
XCTAssertEqual(model.posts(for: "mae"), initialPosts)
XCTAssertEqual(model.profileCursors["mae"], cursor)
XCTAssertNil(model.errorMessage)
XCTAssertNotNil(model.profileErrors["mae"])
await model.loadNextProfilePage(actorID: "mae")
XCTAssertEqual(model.posts(for: "mae").map(\.id), ["post-1", "profile-post-more"])
XCTAssertNil(model.profileCursors["mae"])
XCTAssertNil(model.errorMessage)
XCTAssertEqual(service.authorFeedCursors, [nil, cursor, cursor])
}
func testProfilePaginationIsIndependentAndGuardsDuplicateRequests() async {
let service = MockBlueskyService(authorFeedResponseDelayNanoseconds: 80_000_000)
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
await model.loadProfile(actorID: "mae")
await model.loadProfile(actorID: "alex")
let alexCursor = model.profileCursors["alex"]
let firstLoad = Task { await model.loadNextProfilePage(actorID: "mae") }
while !model.loadingMoreProfileIDs.contains("mae") {
await Task.yield()
}
await model.loadNextProfilePage(actorID: "mae")
await firstLoad.value
XCTAssertEqual(service.authorFeedActorIDs, ["mae", "alex", "mae"])
XCTAssertEqual(service.authorFeedCursors, [nil, nil, "author-page-2-mae"])
XCTAssertEqual(model.posts(for: "alex").map(\.id), ["post-2"])
XCTAssertEqual(model.profileCursors["alex"], alexCursor)
XCTAssertEqual(model.posts(for: "mae").map(\.id), ["post-1", "profile-post-more"])
}
func testProfilePaginationKeepsCanonicalSharedPostState() async throws {
let model = makeAppModel(service: MockBlueskyService())
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
await model.loadProfile(actorID: "mae")
await model.loadNextProfilePage(actorID: "mae")
await model.toggleLike(postID: "profile-post-more")
let profilePost = try XCTUnwrap(model.posts(for: "mae").last)
let sharedPost = try XCTUnwrap(model.post(id: "profile-post-more"))
XCTAssertTrue(profilePost.isLiked)
XCTAssertEqual(profilePost.likeCount, 1)
XCTAssertEqual(profilePost, sharedPost)
}
func testModerationUpdatesFeedOnlyAfterSuccess() async {
let service = ModerationTestService()
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
_ = await model.loadModeration(actorID: "mae")
await model.setModeration(actorID: "mae", block: false)
XCTAssertEqual(service.actions, ["mute:true:mae"])
XCTAssertEqual(model.timelinePosts.map(\.id), ["post-3"])
await model.setModeration(actorID: "mae", block: false)
XCTAssertEqual(model.timelinePosts.map(\.id), ["post-1", "post-3"])
await model.setModeration(actorID: "mae", block: true)
XCTAssertTrue(model.moderationStates["mae"]?.isBlocked == true)
await model.setModeration(actorID: "mae", block: true)
XCTAssertFalse(model.moderationStates["mae"]?.isBlocked ?? true)
XCTAssertEqual(service.actions.suffix(2), ["block:true:mae", "block:false:mae"])
}
func testUndoModerationReloadsAnEmptyCachedProfile() async {
for block in [false, true] {
let service = ModerationTestService()
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
_ = await model.loadModeration(actorID: "mae")
await model.setModeration(actorID: "mae", block: block)
service.emptyAuthorPosts = true
await model.loadProfile(actorID: "mae")
XCTAssertEqual(model.authorPosts["mae"]?.count, 0)
service.emptyAuthorPosts = false
await model.setModeration(actorID: "mae", block: block)
XCTAssertFalse(model.isHidden("mae"))
XCTAssertEqual(service.authorPostLoads, 2)
XCTAssertFalse(model.posts(for: "mae").isEmpty)
}
}
func testQuoteVisibilityChecksEmbeddedAndFallbackAuthorsAndUndo() async throws {
let service = ModerationTestService()
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let fallback = try XCTUnwrap(model.post(id: "post-1"))
let quote = Post.Quote(postID: fallback.id, author: fallback.author, text: fallback.text)
let missingAuthor = Post.Quote(postID: fallback.id)
_ = await model.loadModeration(actorID: fallback.author.id)
for block in [false, true] {
await model.setModeration(actorID: fallback.author.id, block: block)
XCTAssertTrue(model.isQuoteHidden(quote, fallbackPost: nil))
XCTAssertTrue(model.isQuoteHidden(missingAuthor, fallbackPost: fallback))
XCTAssertTrue(model.isQuoteHidden(quote, fallbackPost: fallback))
await model.setModeration(actorID: fallback.author.id, block: block)
XCTAssertFalse(model.isQuoteHidden(quote, fallbackPost: fallback))
}
}
func testModerationRefreshAndNotificationFiltering() async {
let service = ModerationTestService()
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
await model.loadNotifications()
await model.loadNextNotificationsPage()
_ = await model.loadModeration(actorID: "mae")
await model.setModeration(actorID: "mae", block: false)
XCTAssertFalse(
model.visibleNotifications.contains { $0.author.id == "mae" || $0.parentPost?.author.id == "mae" })
service.state = ModerationState()
_ = await model.loadModeration(actorID: "mae")
XCTAssertFalse(model.isHidden("mae"))
XCTAssertEqual(model.visibleNotifications.count, 7)
}
func testModerationResultFromSignedOutSessionIsIgnored() async {
let service = ModerationTestService()
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
service.suspendLoad = true
let pending = Task { await model.loadModeration(actorID: "mae") }
while service.pendingLoad == nil { await Task.yield() }
await model.signOut()
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
service.pendingLoad?.resume(returning: ModerationState(isMuted: true))
_ = await pending.value
XCTAssertNil(model.moderationStates["mae"])
XCTAssertFalse(model.isHidden("mae"))
}
}