import Observation import XCTest @testable import OldFella extension AppModelTests { func testTimelineOmitsPostsWithHiddenOrRevealRequiredReplyContext() async { let service = ModerationTestService() let author = Post.Author(id: "other", name: "Other", handle: "other") let image = URL(string: "https://example.com/image.jpg")! func post(_ id: String, label: String? = nil, media: Bool = false, quote: Post.Quote? = nil) -> Post { var result = Post( id: id, author: author, text: "safe", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: media ? [image] : [], quote: quote, replyIDs: []) if let label { result.moderationLabels = [.init(value: label, sourceDID: author.id)] } return result } service.preferenceOverride = .init( userDID: "demo", adultContentEnabled: true, labels: ["nudity": .warn, "scam": .warn], labelDefinitions: ModerationPolicy.builtInDefinitions) let posts = ["safe", "hidden", "text-only", "warning", "alert", "media", "quoted-warning"].map { id in var reply = post(id) reply.replyRootURI = id == "hidden" ? "first-context" : "\(id == "text-only" ? "text" : id == "quoted-warning" ? "quoted" : id)-context" reply.replyParentURI = id == "hidden" ? "hidden-context" : reply.replyRootURI return reply } let warningQuote = Post.Quote( postID: "context-quote", author: author, text: "quoted text", moderationLabels: [.init(value: "!warn", sourceDID: author.id)]) service.feedPage = FeedPage( posts: posts, cursor: nil, replyContexts: [ "safe": [post("safe-context")], "hidden": [post("first-context"), post("hidden-context", label: "!hide")], "text-only": [post("text-context", label: "nudity")], "warning": [post("warning-context", label: "!warn")], "alert": [post("alert-context", label: "scam")], "media": [post("media-context", label: "nudity", media: true)], "quoted-warning": [post("quoted-context", quote: warningQuote)] ]) let model = makeAppModel(service: service) model.induceADHD = true defer { model.induceADHD = false } await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") XCTAssertEqual(model.timelinePosts.map(\.id), ["safe", "text-only", "alert", "media"]) } func testRepostAuthorMustPassContentPreferences() async { let model = makeAppModel(service: MockBlueskyService()) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") let author = Post.Author( id: "did:example:other", name: "Other", handle: "other", moderationLabels: [.init(value: "porn", sourceDID: "did:example:other")]) XCTAssertFalse(model.isRepostAuthorVisible(author)) await model.signOut() XCTAssertFalse(model.isRepostAuthorVisible(author)) } func testContentDecisionUsesPostInputsAndQuoteInputs() async { let model = makeAppModel(service: MockBlueskyService()) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") let author = Post.Author( id: "did:example:other", name: "Other", handle: "other", moderationLabels: [.init(value: "porn", sourceDID: "did:example:other")]) let post = Post( id: "post", author: author, text: "text", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyIDs: []) XCTAssertEqual(model.contentDecision(for: post)?.action, .hide) let quote = Post.Quote(postID: "post", author: author, text: "text") XCTAssertTrue(model.isQuoteHidden(quote, fallbackPost: nil)) await model.signOut() XCTAssertNil(model.contentDecision(for: post)) } func testMutedWordsApplyToEmbeddedText() async { let service = ModerationTestService() service.mutedWords = [.init(value: "spoiler")] let model = makeAppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") let author = Post.Author(id: "other", name: "Other", handle: "other") let imageURL = URL(string: "https://example.com/image")! var post = Post( id: "post", author: author, text: "safe", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyIDs: []) XCTAssertEqual(model.contentDecision(for: post)?.action, .allow) post.quote = .init(postID: "quote", author: author, text: "spoiler") XCTAssertTrue(model.contentDecision(for: post)?.ui(.contentList).filter == true) post.quote = .init(postID: "quote", author: author, imageDescriptions: [imageURL: "spoiler"]) XCTAssertTrue(model.contentDecision(for: post)?.ui(.contentList).filter == true) post.quote = nil post.externalTitle = "spoiler" XCTAssertTrue(model.contentDecision(for: post)?.ui(.contentList).filter == true) post.externalTitle = nil post.externalDescription = "spoiler" XCTAssertTrue(model.contentDecision(for: post)?.ui(.contentList).filter == true) post.externalDescription = nil post.quote = .init(postID: "quote", author: author, externalTitle: "spoiler") XCTAssertTrue(model.contentDecision(for: post)?.ui(.contentList).filter == true) post.quote = .init(postID: "quote", author: author, externalDescription: "spoiler") XCTAssertTrue(model.contentDecision(for: post)?.ui(.contentList).filter == true) for quote in [ Post.Quote(postID: "quote", author: author, text: "spoiler"), Post.Quote(postID: "quote", author: author, imageDescriptions: [imageURL: "spoiler"]), Post.Quote(postID: "quote", author: author, externalTitle: "spoiler"), Post.Quote(postID: "quote", author: author, externalDescription: "spoiler") ] { XCTAssertTrue(model.isQuoteHidden(quote, fallbackPost: nil)) } } func testQuoteUsesItsOwnAuthorAndContentForMutedWords() async { let service = ModerationTestService() service.mutedWords = [.init(value: "spoiler", excludeFollowing: true)] let model = makeAppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") let followed = Post.Author(id: "followed", name: "Followed", handle: "followed", isFollowing: true) let other = Post.Author(id: "other", name: "Other", handle: "other") var post = Post( id: "post", author: followed, text: "safe", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyIDs: []) post.quote = .init(postID: "quote", author: other, text: "spoiler") XCTAssertEqual(model.contentDecision(for: post)?.action, .hide) post = Post( id: "post", author: other, text: "safe", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], quote: .init(postID: "quote", author: followed, text: "spoiler"), replyIDs: []) XCTAssertEqual(model.contentDecision(for: post)?.action, .allow) post.quote = .init(postID: "quote", author: other, text: "safe", tags: ["spoiler"]) XCTAssertEqual(model.contentDecision(for: post)?.action, .hide) post.quote = .init(postID: "quote", author: other, text: "prespoilerpost", languages: ["ja"]) XCTAssertEqual(model.contentDecision(for: post)?.action, .hide) post.quote?.languages = ["en"] XCTAssertEqual(model.contentDecision(for: post)?.action, .allow) } func testUnfollowOverridesCachedAuthorsAndQuotes() async { let service = ModerationTestService() service.mutedWords = [.init(value: "spoiler", excludeFollowing: true)] let author = Post.Author(id: "followed", name: "Followed", handle: "followed", isFollowing: true) let post = Post( id: "cached", author: author, text: "spoiler", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyIDs: []) let quote = Post.Quote(postID: "quoted", author: author, text: "spoiler") service.threadPosts = [post] service.notificationPage = NotificationPage( notifications: [ .init( id: "notification", author: author, reason: .mention, indexedAt: .now, isRead: false, postID: post.id, relatedPost: post) ], cursor: nil) let model = makeAppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") await model.loadPost(id: post.id) _ = await model.loadNotifications() await model.toggleFollow(actorID: author.id) XCTAssertEqual(model.contentDecision(for: post)?.action, .allow) XCTAssertEqual(model.visibleNotifications.count, 1) await model.toggleFollow(actorID: author.id) XCTAssertEqual(model.contentDecision(for: model.post(id: post.id)!)?.action, .hide) XCTAssertEqual(model.quoteContentDecision(quote, fallbackPost: nil)?.action, .hide) XCTAssertTrue(model.visibleNotifications.isEmpty) await model.toggleFollow(actorID: author.id) XCTAssertEqual(model.contentDecision(for: post)?.action, .allow) XCTAssertEqual(model.visibleNotifications.count, 1) } func testFollowResultDoesNotCrossSessions() async { let service = ModerationTestService() service.mutedWords = [.init(value: "spoiler", excludeFollowing: true)] let author = Post.Author(id: "followed", name: "Followed", handle: "followed", isFollowing: true) let model = makeAppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") await model.toggleFollow(actorID: author.id) service.suspendFollow = true let pending = Task { await model.toggleFollow(actorID: author.id) } while service.pendingFollow == nil { await Task.yield() } await model.signOut() await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") service.pendingFollow?.resume(returning: nil) await pending.value XCTAssertEqual(model.contentDecision(author: author, text: "spoiler")?.action, .allow) XCTAssertFalse(model.followingActorIDs.contains(author.id)) } func testQuoteLabelsDoNotHideEnclosingPostAndWarningsCanBeShown() async { let model = makeAppModel(service: MockBlueskyService()) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") let author = Post.Author(id: "other", name: "Other", handle: "other") let warning = Post.Quote( postID: "quote", author: author, text: "text", moderationLabels: [.init(value: "!warn", sourceDID: "other")] ) let post = Post( id: "post", author: author, text: "safe", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], quote: warning, replyIDs: []) XCTAssertEqual(model.contentDecision(for: post)?.action, .allow) XCTAssertEqual(model.quoteContentDecision(warning, fallbackPost: nil)?.action, .warn) XCTAssertFalse(model.isQuoteHidden(warning, fallbackPost: nil)) var hidden = warning hidden.moderationLabels = [.init(value: "!hide", sourceDID: "other")] XCTAssertTrue(model.isQuoteHidden(hidden, fallbackPost: nil)) } func testMutedThreadRepliesAreFiltered() async { let service = ModerationTestService() service.mutedWords = [.init(value: "spoiler")] let author = Post.Author(id: "other", name: "Other", handle: "other") let root = Post( id: "root", author: author, text: "safe", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyIDs: ["hidden", "visible"]) let hidden = Post( id: "hidden", author: author, text: "spoiler", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyParentURI: "root", replyIDs: []) let visible = Post( id: "visible", author: author, text: "safe", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyParentURI: "root", replyIDs: []) service.threadPosts = [root, hidden, visible] let model = makeAppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") await model.loadPost(id: "root") XCTAssertEqual(model.directReplies(to: root).map(\.id), ["visible"]) XCTAssertEqual(model.conversationReplies(to: root).map(\.id), ["visible"]) XCTAssertEqual(model.contentDecision(for: hidden)?.action, .hide) } }