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