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
import Observation
import XCTest
@testable import OldFella
extension AppModelTests {
func testDelayedSignOutPreventsNewModerationWork() async {
let service = ModerationTestService()
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
_ = await model.loadModeration(actorID: "mae")
service.suspendSignOut = true
let signingOut = Task { await model.signOut() }
while service.pendingSignOut == nil { await Task.yield() }
let loaded = await model.loadModeration(actorID: "alex")
await model.setModeration(actorID: "mae", block: true)
let reported = await model.submitReport(.account("mae"), reason: "reason")
await model.toggleLike(postID: "post-1")
await model.toggleRepost(postID: "post-1")
await model.toggleFollow(actorID: "mae")
let published = await model.publish(text: "A post")
XCTAssertFalse(published)
XCTAssertEqual(service.postActionCalls, 0)
XCTAssertFalse(loaded)
XCTAssertFalse(reported)
XCTAssertTrue(service.actions.isEmpty)
XCTAssertNil(service.reported)
service.pendingSignOut?.resume()
await signingOut.value
XCTAssertNil(model.account)
XCTAssertTrue(model.moderationStates.isEmpty)
XCTAssertTrue(model.moderationBusy.isEmpty)
XCTAssertNil(model.moderationError)
}
func testFailedSignOutKeepsDelayedBlockSuccess() async {
let service = ModerationTestService()
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
_ = await model.loadModeration(actorID: "mae")
service.suspendBlock = true
let write = Task { await model.setModeration(actorID: "mae", block: true) }
while service.pendingBlock == nil { await Task.yield() }
service.failSignOut = true
await model.signOut()
XCTAssertNotNil(model.account)
XCTAssertTrue(model.moderationBusy.contains("mae"))
service.pendingBlock?.resume(returning: "block-uri")
await write.value
XCTAssertEqual(model.moderationStates["mae"]?.blockURI, "block-uri")
XCTAssertFalse(model.moderationBusy.contains("mae"))
XCTAssertNil(model.uncertainModerationIDs["mae"])
}
func testFailedSignOutKeepsDelayedUnknownBlockLocked() async {
let service = ModerationTestService()
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
_ = await model.loadModeration(actorID: "mae")
service.suspendBlock = true
let write = Task { await model.setModeration(actorID: "mae", block: true) }
while service.pendingBlock == nil { await Task.yield() }
service.failSignOut = true
await model.signOut()
service.pendingBlock?.resume(throwing: ServiceError.invalidResponse)
await write.value
XCTAssertNotNil(model.account)
XCTAssertEqual(model.uncertainModerationIDs["mae"], [.block])
service.suspendBlock = false
await model.setModeration(actorID: "mae", block: true)
XCTAssertTrue(service.actions.isEmpty)
}
func testUncertainBlockCannotBeRepeatedOrClearedByStaleRead() async {
let service = ModerationTestService()
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
_ = await model.loadModeration(actorID: "mae")
service.fail = true
await model.setModeration(actorID: "mae", block: true)
XCTAssertEqual(model.uncertainModerationIDs["mae"], [.block])
service.fail = false
let loaded = await model.loadModeration(actorID: "mae")
XCTAssertTrue(loaded)
await model.setModeration(actorID: "mae", block: true)
XCTAssertTrue(service.actions.isEmpty)
await model.setModeration(actorID: "mae", block: false)
XCTAssertEqual(service.actions, ["mute:true:mae"])
XCTAssertEqual(model.uncertainModerationIDs["mae"], [.block])
}
func testRejectedActionCanBeRetried() async {
let service = ModerationTestService()
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
_ = await model.loadModeration(actorID: "mae")
service.reject = true
await model.setModeration(actorID: "mae", block: true)
XCTAssertNil(model.uncertainModerationIDs["mae"])
XCTAssertNotNil(model.moderationError)
service.reject = false
await model.setModeration(actorID: "mae", block: true)
XCTAssertEqual(service.actions, ["block:true:mae"])
}
func testRejectedReportShowsNotSent() async {
let service = ModerationTestService()
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
service.reject = true
let sent = await model.submitReport(.account("mae"), reason: "reason")
XCTAssertFalse(sent)
XCTAssertEqual(model.moderationError, "The report was not sent. Rejected")
}
func testModerationFailuresKeepStateAndReportFailure() async {
let service = ModerationTestService()
let model = makeAppModel(service: service)
await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
_ = await model.loadModeration(actorID: "mae")
let timelineIDs = model.timelinePosts.map(\.id)
service.fail = true
await model.setModeration(actorID: "mae", block: false)
XCTAssertFalse(model.moderationStates["mae"]?.isMuted ?? true)
XCTAssertEqual(model.uncertainModerationIDs["mae"], [.mute])
XCTAssertEqual(model.timelinePosts.map(\.id), timelineIDs)
XCTAssertNotNil(model.moderationError)
let sent = await model.submitReport(.post(uri: "post-1", cid: "cid"), reason: "reason")
XCTAssertFalse(sent)
XCTAssertEqual(
model.moderationError, "The report result is unknown. Sending it again can create a duplicate report.")
XCTAssertTrue(model.uncertainReports.contains(.post("post-1")))
service.fail = false
let duplicate = await model.submitReport(.post(uri: "post-1", cid: "new-cid"), reason: "reason")
XCTAssertFalse(duplicate)
XCTAssertNil(service.reported)
let otherPost = await model.submitReport(.post(uri: "post-2", cid: "other-cid"), reason: "reason")
XCTAssertTrue(otherPost)
XCTAssertEqual(service.reported, .post(uri: "post-2", cid: "other-cid"))
let success = await model.submitReport(.account("mae"), reason: "reason")
XCTAssertTrue(success)
XCTAssertEqual(service.reported, .account("mae"))
}
func testImageAttachmentKeepsAltTextAndMimeType() throws {
let image = UIGraphicsImageRenderer(size: CGSize(width: 4, height: 4)).image { context in
UIColor.systemRed.setFill()
context.fill(CGRect(x: 0, y: 0, width: 4, height: 4))
}
let source = try XCTUnwrap(image.pngData())
let attachment = ImageAttachment(data: source, altText: "A red boat", mimeType: "image/png")
let upload = try ATProtoBlueskyService.prepareImageForUpload(attachment)
XCTAssertEqual(attachment.altText, "A red boat")
XCTAssertEqual(upload.mimeType, "image/jpeg")
XCTAssertFalse(upload.data.isEmpty)
XCTAssertLessThanOrEqual(upload.data.count, 1_000_000)
}
}