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) } }