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
import Foundation
import UIKit
extension MockBlueskyService {
func loadThread(uri: String) async throws -> ThreadResult {
let posts = try await loadPost(uri: uri)
guard uri == "post-1", ProcessInfo.processInfo.arguments.contains("-ui-testing-numbered-thread"),
var root = posts.first
else { return ThreadResult(posts: posts, mutedPostIDs: []) }
var positions: [String: ThreadPosition] = [root.id: ThreadPosition(index: 1, count: 4)].compactMapValues { $0 }
var parts: [Post] = []
root.replyCount = 2
var parentID = root.id
for index in 2...4 {
let id = "numbered-post-\(index)"
let part = Post(
id: id, author: root.author, text: "Part \(index) of the thread.", likeCount: 0, repostCount: 0,
isLiked: false, isReposted: false, imageURLs: [], replyRootURI: root.id, replyParentURI: parentID,
replyIDs: [], replyCount: index < 4 ? 1 : 0)
positions[id] = ThreadPosition(index: index, count: 4)
parts.append(part)
parentID = id
}
return ThreadResult(posts: [root] + posts.dropFirst() + parts, mutedPostIDs: [], positions: positions)
}
func loadPost(uri: String) async throws -> [Post] {
if threadResponseDelayNanoseconds > 0 {
try await Task.sleep(nanoseconds: threadResponseDelayNanoseconds)
}
if uri == "thread-selected" { return selectedThreadFixture() }
if uri == "reply-post-1" { return replyThreadFixture() }
let fixture = usesRichScrollFixture ? richScrollFixture : nil
let allPosts = fixture?.lookupPosts.map(withInteractions) ?? firstPage
guard let selected = allPosts.first(where: { $0.id == uri || uri.hasSuffix("/\($0.id)") }) else { return [] }
if let context = fixture?.replyContexts[selected.id] {
return (context + [selected]).map(withInteractions)
}
let byID = Dictionary(allPosts.map { ($0.id, $0) }, uniquingKeysWith: { first, _ in first })
var ancestors: [Post] = []
var nextID = selected.replyParentURI
var visited: Set<String> = [selected.id]
while let id = nextID, visited.insert(id).inserted, let ancestor = byID[id] {
ancestors.append(ancestor)
nextID = ancestor.replyParentURI
}
ancestors.reverse()
var descendants: [Post] = []
var queue = [selected.id]
var descendantIDs: Set<String> = [selected.id]
while !queue.isEmpty {
let parentID = queue.removeFirst()
var children = allPosts.filter { candidate in
candidate.replyParentURI == parentID
|| (candidate.replyParentURI == nil && byID[parentID]?.replyIDs.contains(candidate.id) == true)
}
if parentID == selected.id, !threadReplyOrder.isEmpty {
let order = Dictionary(
uniqueKeysWithValues: threadReplyOrder.enumerated().map { ($0.element, $0.offset) })
children.sort { order[$0.id, default: Int.max] < order[$1.id, default: Int.max] }
}
for child in children where descendantIDs.insert(child.id).inserted {
descendants.append(child)
queue.append(child.id)
}
}
var result: [Post] = []
for item in ancestors + [selected] + descendants where !result.contains(where: { $0.id == item.id }) {
result.append(item)
}
return result
}
private func selectedThreadFixture() -> [Post] {
let root = Post(
id: "thread-root", author: authors[0], text: "Conversation root with its full post text.", likeCount: 0,
repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyIDs: ["thread-level-one"])
let levelOne = Post(
id: "thread-level-one", author: authors[1], text: "First reply in a longer conversation.", likeCount: 0,
repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyRootURI: root.id,
replyParentURI: root.id, replyIDs: ["thread-level-two"])
let levelTwo = Post(
id: "thread-level-two", author: authors[2], text: "Second reply in a longer conversation.",
likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyRootURI: root.id,
replyParentURI: levelOne.id, replyIDs: ["thread-selected"])
let selected = Post(
id: "thread-selected", author: authors[0], text: "The selected post in a longer conversation.",
likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyRootURI: root.id,
replyParentURI: levelTwo.id, replyIDs: ["thread-direct", "thread-other-direct"])
let direct = Post(
id: "thread-direct", author: authors[1], text: "A direct reply to the selected post.", likeCount: 0,
repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyRootURI: root.id,
replyParentURI: selected.id, replyIDs: ["thread-nested"])
let otherDirect = Post(
id: "thread-other-direct", author: authors[2], text: "Another direct reply to the selected post.",
likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyRootURI: root.id,
replyParentURI: selected.id, replyIDs: [])
let nested = Post(
id: "thread-nested", author: authors[0], text: "A nested reply that is not shown in this view.",
likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyRootURI: root.id,
replyParentURI: direct.id, replyIDs: [])
return [root, levelOne, levelTwo, selected, direct, nested, otherDirect].map(withInteractions)
}
private func replyThreadFixture() -> [Post] {
let parent = firstPage[0]
let reply = Post(
id: "reply-post-1",
author: authors[1],
text: "Thanks for the thoughtful reply.",
likeCount: 0,
repostCount: 0,
isLiked: false,
isReposted: false,
imageURLs: [],
replyRootURI: parent.id,
replyParentURI: parent.id,
replyIDs: []
)
return [parent, reply].map(withInteractions)
}
}