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