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
import Observation
import XCTest
@testable import OldFella
extension AppModelTests {
func testProfileOnlyPostSupportsInteractionsAndSharedNavigation() async throws {
let profileOnlyPost = Post(
id: "profile-only-post",
author: Post.Author(id: "mae", name: "Mae Rivers", handle: "mae.example"),
text: "A post that is not in the Following feed.",
likeCount: 5,
repostCount: 2,
isLiked: false,
isReposted: false,
imageURLs: [],
replyIDs: []
)
let model = makeAppModel(service: ProfilePostTestService(additionalProfilePost: profileOnlyPost))
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
await model.loadProfile(actorID: "mae")
let initialTimelineIDs = model.timelinePosts.map(\.id)
XCTAssertTrue(model.posts.contains { $0.id == profileOnlyPost.id })
XCTAssertFalse(model.timelinePosts.contains { $0.id == profileOnlyPost.id })
XCTAssertEqual(model.post(id: profileOnlyPost.id), profileOnlyPost)
await model.toggleLike(postID: profileOnlyPost.id)
await model.toggleRepost(postID: profileOnlyPost.id)
let profilePost = try XCTUnwrap(model.posts(for: "mae").first { $0.id == profileOnlyPost.id })
let sharedPost = try XCTUnwrap(model.post(id: profileOnlyPost.id))
XCTAssertTrue(profilePost.isLiked)
XCTAssertEqual(profilePost.likeCount, 6)
XCTAssertTrue(profilePost.isReposted)
XCTAssertEqual(profilePost.repostCount, 3)
XCTAssertEqual(profilePost, sharedPost)
await model.toggleLike(postID: profileOnlyPost.id)
await model.toggleRepost(postID: profileOnlyPost.id)
let unlikedProfilePost = try XCTUnwrap(model.posts(for: "mae").first { $0.id == profileOnlyPost.id })
let unlikedSharedPost = try XCTUnwrap(model.post(id: profileOnlyPost.id))
XCTAssertFalse(unlikedProfilePost.isLiked)
XCTAssertEqual(unlikedProfilePost.likeCount, 5)
XCTAssertFalse(unlikedProfilePost.isReposted)
XCTAssertEqual(unlikedProfilePost.repostCount, 2)
XCTAssertEqual(unlikedProfilePost, unlikedSharedPost)
XCTAssertEqual(model.timelinePosts.map(\.id), initialTimelineIDs)
XCTAssertFalse(model.timelinePosts.contains { $0.id == profileOnlyPost.id })
}
func testPublishingInsertsPostAndSignOutClearsFeed() async {
let model = makeAppModel(service: MockBlueskyService())
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let success = await model.publish(text: " A demo post ")
XCTAssertTrue(success)
XCTAssertEqual(model.posts.first?.text, "A demo post")
await model.loadProfile(actorID: "mae")
XCTAssertNotNil(model.profileCursors["mae"])
await model.signOut()
XCTAssertNil(model.account)
XCTAssertTrue(model.posts.isEmpty)
XCTAssertTrue(model.profileCursors.isEmpty)
XCTAssertTrue(model.loadingMoreProfileIDs.isEmpty)
}
func testPublishedPostUsesAccountAndKeepsSelectedImages() async {
let service = ModerationTestService()
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let image = ImageAttachment(data: Data([1, 2, 3]), altText: "A photo")
let success = await model.publish(text: "Hello", images: [image])
XCTAssertTrue(success)
XCTAssertEqual(model.posts.first?.author.name, "Demo Account")
XCTAssertEqual(model.posts.first?.pendingImages, [image])
await Task.yield()
XCTAssertEqual(model.posts.first?.pendingImages, [image])
XCTAssertNil(model.publishErrorMessage)
}
func testFailedPublishKeepsCurrentFeed() async {
let model = makeAppModel(service: MockBlueskyService(failsPublishing: true))
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let initialCount = model.posts.count
let success = await model.publish(text: "This will fail")
XCTAssertFalse(success)
XCTAssertEqual(model.posts.count, initialCount)
XCTAssertNotNil(model.publishErrorMessage)
}
func testPublishRejectsMissingQuoteOrReplyBeforeSending() async {
let service = ActionFailureTestService()
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
for (reply, quote) in [(nil, "missing-quote"), ("missing-reply", nil)] {
let success = await model.publish(text: "A post", replyTo: reply, quotePostID: quote)
XCTAssertFalse(success)
XCTAssertEqual(model.publishErrorMessage, ServiceError.postReferenceUnavailable.localizedDescription)
XCTAssertFalse(model.publishResultUnknown)
XCTAssertTrue(service.publishedTexts.isEmpty)
}
}
func testUnknownActionsKeepStateAndBlockRepeats() async throws {
let service = ActionFailureTestService()
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
await model.loadProfile(actorID: "mae")
let original = try XCTUnwrap(model.post(id: "post-1"))
let following = model.followingActorIDs.contains("mae")
await model.toggleLike(postID: original.id)
await model.toggleLike(postID: original.id)
await model.toggleRepost(postID: original.id)
XCTAssertEqual(service.likeCalls, 1)
XCTAssertEqual(service.repostCalls, 0)
XCTAssertEqual(model.post(id: original.id), original)
await model.toggleRepost(postID: "post-2")
await model.toggleRepost(postID: "post-2")
XCTAssertEqual(service.repostCalls, 1)
await model.toggleFollow(actorID: "mae")
await model.toggleFollow(actorID: "mae")
XCTAssertEqual(service.followCalls, 1)
XCTAssertEqual(model.followingActorIDs.contains("mae"), following)
XCTAssertEqual(model.profile(actorID: "mae")?.isFollowing, following)
}
func testActionErrorsKeepDetailsAndWarnAboutUnknownResult() async {
let service = ActionFailureTestService()
service.publishFailure = ServiceError.postTextTooLong
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
await model.toggleLike(postID: "post-1")
XCTAssertTrue(model.errorMessage?.contains(ServiceError.invalidResponse.localizedDescription) == true)
XCTAssertTrue(model.errorMessage?.contains("may have received") == true)
await model.toggleRepost(postID: "post-1")
XCTAssertTrue(model.errorMessage?.contains("may have received") == true)
await model.toggleFollow(actorID: "mae")
XCTAssertTrue(model.errorMessage?.contains("may have received") == true)
_ = await model.publish(text: "A post")
XCTAssertEqual(model.publishErrorMessage, ServiceError.postTextTooLong.localizedDescription)
XCTAssertFalse(model.publishResultUnknown)
}
func testRefreshDuringActionDoesNotAdjustCountsTwice() 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")
var refreshed = try XCTUnwrap(model.post(id: "post-1"))
let initialLikes = refreshed.likeCount
let initialReposts = refreshed.repostCount
let like = Task { await model.toggleLike(postID: "post-1") }
while !model.activePostActions.contains("post-1") { await Task.yield() }
refreshed.isLiked = true
refreshed.likeCount += 1
service.refreshedPost = refreshed
await model.loadFeed()
await like.value
XCTAssertEqual(model.post(id: "post-1")?.likeCount, initialLikes + 1)
let repost = Task { await model.toggleRepost(postID: "post-1") }
while !model.activePostActions.contains("post-1") { await Task.yield() }
refreshed.isReposted = true
refreshed.repostCount += 1
service.refreshedPost = refreshed
await model.loadFeed()
await repost.value
XCTAssertEqual(model.post(id: "post-1")?.repostCount, initialReposts + 1)
}
func testDuplicatePostActionWaitsForFirstRequest() async {
let service = ActionFailureTestService()
service.actionDelay = 100_000_000
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let first = Task { await model.toggleLike(postID: "post-1") }
while !model.activePostActions.contains("post-1") { await Task.yield() }
await model.toggleLike(postID: "post-1")
await model.toggleRepost(postID: "post-1")
await first.value
XCTAssertEqual(service.likeCalls, 1)
XCTAssertEqual(service.repostCalls, 0)
XCTAssertFalse(model.activePostActions.contains("post-1"))
}
func testPublishFailureCanRetryWithSameTextAndAttachment() async {
let service = ActionFailureTestService()
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
let image = ImageAttachment(data: Data([1, 2, 3]), altText: "A red boat", mimeType: "image/jpeg")
let initialCount = model.posts.count
let first = await model.publish(text: "A post", images: [image])
XCTAssertFalse(first)
XCTAssertEqual(model.posts.count, initialCount)
XCTAssertNotNil(model.publishErrorMessage)
XCTAssertTrue(model.publishResultUnknown)
let second = await model.publish(text: "A post", images: [image])
XCTAssertFalse(second)
XCTAssertEqual(service.publishedTexts, ["A post"])
XCTAssertEqual(service.publishedImages.first?.first?.altText, "A red boat")
XCTAssertEqual(service.publishedImages.first?.first?.data, image.data)
}
}