import XCTest @testable import OldFella final class TimelinePolicyTests: XCTestCase { private func post(_ id: String, author: String = "followed", root: String? = nil, parent: String? = nil) -> Post { Post( id: id, author: .init(id: author, name: author, handle: author), text: "safe", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyRootURI: root, replyParentURI: parent, quote: nil, replyIDs: []) } func testRepliesUseKnownConnectionsAndCollapseAfterFiltering() { let root = post("root") let parent = post("parent") let excluded = post("excluded", author: "other", root: root.id, parent: parent.id) let first = post("first", root: root.id, parent: parent.id) let later = post("later", root: root.id, parent: root.id) let missing = post("missing", root: "absent", parent: "absent") let standalone = post("standalone", author: "other") let posts = [excluded, first, root, later, missing, standalone] let contexts = [excluded.id: [root, parent], first.id: [root, parent], later.id: [root]] func filtered(_ include: Bool) -> [String] { TimelinePolicy.filter( posts, contexts: contexts, reposts: [:], includeUnfollowedReplies: include, visibility: .init( canShow: { _ in true }, canShowRepostAuthor: { _ in true }, follows: { $0.id == "followed" }) ).map(\.id) } XCTAssertEqual(filtered(false), [first.id, missing.id, standalone.id]) XCTAssertEqual(filtered(true), [excluded.id, missing.id, standalone.id]) } func testFollowedReplyCanIncludeUnfollowedParentThroughFollowedRoot() { let root = post("root", author: "michael") let parent = post("parent", author: "miscforma", root: root.id, parent: "gap") let reply = post("reply", author: "bikepedantic", root: root.id, parent: parent.id) let result = TimelinePolicy.filter( [reply], contexts: [reply.id: [root, parent]], reposts: [:], includeUnfollowedReplies: false, visibility: .init( canShow: { _ in true }, canShowRepostAuthor: { _ in true }, follows: { ["michael", "bikepedantic"].contains($0.id) }) ) XCTAssertEqual(result.map(\.id), [reply.id]) } func testNormalModeRequiresFollowedReplyAndAnotherConnectionUnlessSelfThread() { let root = post("root", author: "stranger") let reply = post("reply", root: root.id, parent: root.id) func filtered(_ candidate: Post, include: Bool = false) -> [Post] { TimelinePolicy.filter( [candidate], contexts: [candidate.id: [root]], reposts: [:], includeUnfollowedReplies: include, visibility: .init( canShow: { _ in true }, canShowRepostAuthor: { _ in true }, follows: { $0.id == "followed" }) ) } XCTAssertTrue(filtered(reply).isEmpty) XCTAssertEqual(filtered(reply, include: true).map(\.id), [reply.id]) var connected = reply connected.replyGrandparentAuthor = .init( id: "followed-other", name: "Other", handle: "other", isFollowing: true) let result = TimelinePolicy.filter( [connected], contexts: [reply.id: [root]], reposts: [:], includeUnfollowedReplies: false, visibility: .init( canShow: { _ in true }, canShowRepostAuthor: { _ in true }, follows: { $0.id == "followed" || $0.isFollowing }) ) XCTAssertEqual(result.map(\.id), [reply.id]) var unfollowed = connected unfollowed.author = root.author XCTAssertTrue(filtered(unfollowed).isEmpty) } func testVisibilityChecksRelatedPostsAndRepostAuthorsBeforeCollapsing() { let root = post("root") let hiddenReply = post("hiddenReply", root: root.id, parent: root.id) let hiddenRepost = post("hiddenRepost", root: root.id, parent: root.id) let visibleReply = post("visibleReply", root: root.id, parent: root.id) let blockedAuthor = Post.Author(id: "blocked", name: "blocked", handle: "blocked") let result = TimelinePolicy.filter( [hiddenReply, hiddenRepost, visibleReply], contexts: [hiddenReply.id: [root], hiddenRepost.id: [root], visibleReply.id: [root]], reposts: [hiddenRepost.id: blockedAuthor], includeUnfollowedReplies: true, visibility: .init( canShow: { $0.id != hiddenReply.id }, canShowRepostAuthor: { $0?.id != blockedAuthor.id }, follows: { _ in true }) ) XCTAssertEqual(result.map(\.id), [visibleReply.id]) let hiddenContext = TimelinePolicy.filter( [visibleReply], contexts: [visibleReply.id: [root]], reposts: [:], includeUnfollowedReplies: true, visibility: .init( canShow: { $0.id != root.id }, canShowRepostAuthor: { _ in true }, follows: { _ in true }) ) XCTAssertTrue(hiddenContext.isEmpty) } func testContextTakesPrecedenceOverFeedAndSelfThreadsQualify() { let feedRoot = post("root", author: "followed") let contextRoot = post("root", author: "other") let reply = post("reply", root: feedRoot.id, parent: feedRoot.id) let selfReply = post("self", root: "self", parent: "self") let result = TimelinePolicy.filter( [reply, selfReply, feedRoot], contexts: [reply.id: [contextRoot]], reposts: [:], includeUnfollowedReplies: false, visibility: .init( canShow: { _ in true }, canShowRepostAuthor: { _ in true }, follows: { $0.id == "followed" }) ) XCTAssertEqual(result.map(\.id), [selfReply.id, feedRoot.id]) } }