~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/old-fella

Old people's social media app

0
1
main
140 lines · 7.2 KB Raw History
import Foundation
import UIKit

extension MockBlueskyService {
    var usesRichScrollFixture: Bool {
        ProcessInfo.processInfo.arguments.contains("-ui-testing-rich-scroll")
    }

    static let richScrollImageBase64: String = {
        let format = UIGraphicsImageRendererFormat()
        format.scale = 1
        format.opaque = true
        return UIGraphicsImageRenderer(size: CGSize(width: 1024, height: 768), format: format)
            .image { context in
                for index in 0..<16 {
                    UIColor(red: 0.15 + CGFloat(index) / 32, green: 0.4, blue: 0.75, alpha: 1).setFill()
                    context.cgContext.fill(CGRect(x: CGFloat(index) * 64, y: 0, width: 64, height: 768))
                }
                UIColor(white: 1, alpha: 0.8).setFill()
                context.cgContext.fillEllipse(in: CGRect(x: 560, y: 120, width: 280, height: 280))
            }
            .pngData()!.base64EncodedString()
    }()

    func richScrollPost(index: Int, part: String, author: Post.Author, text: String) -> Post {
        Post(
            id: "at://\(author.handle)/app.bsky.feed.post/scroll-\(index)-\(part)", author: author,
            text: text, likeCount: index * 3, repostCount: index % 9,
            isLiked: index % 5 == 0, isReposted: index % 7 == 0, imageURLs: [], replyIDs: [],
            replyCount: index % 6, createdAt: Date(timeIntervalSince1970: 1_794_384_000 - Double(index * 67)))
    }

    struct RichScrollFixture {
        let posts: [Post]
        let replyContexts: [String: [Post]]
        let lookupPosts: [Post]
    }

    var richScrollFixture: RichScrollFixture {
        let fixtureAuthors = authors.map { original in
            var author = original
            author.isFollowing = true
            return author
        }
        let paragraphs = [
            "A quiet morning by the river.",
            "The clouds changed shape while we walked along the water. The path was almost empty.",
            "A small detail from the walk. The light reached the trees and the water at the same time.",
            "Three things from this morning: a clear sky, a warm cup of tea, and a long walk.\nA good start to the day."
        ]
        var posts: [Post] = []
        var contexts: [String: [Post]] = [:]
        var lookupPosts: [Post] = []
        let linkHost = ProcessInfo.processInfo.environment["OLDFELLA_UI_TEST_LINK_HOST"] ?? "example.com"
        func imageURL(index: Int, part: String) -> URL {
            URL(string: "data:image/png;name=rich-scroll-\(index)-\(part);base64,\(Self.richScrollImageBase64)")!
        }
        func quoteContext(_ post: Post) -> Post.Quote {
            Post.Quote(
                postID: post.id, author: post.author, text: post.text, facets: post.facets,
                links: post.links, imageURLs: post.imageURLs, createdAt: post.createdAt,
                imageDescriptions: post.imageDescriptions)
        }
        for index in 0..<48 {
            let author = fixtureAuthors[index % fixtureAuthors.count]
            let otherAuthor = fixtureAuthors[(index + 1) % fixtureAuthors.count]
            let mention = "@\(otherAuthor.handle)"
            let link = URL(string: "https://\(linkHost)/article/\(index)")!
            let text =
                "Post \(index + 1). \(paragraphs[index % paragraphs.count]) "
                + "\(mention) shared \(link.absoluteString) #scrolling"
            var post = richScrollPost(index: index, part: "main", author: author, text: text)
            post.facets = [
                Self.mockFacet(in: text, matching: mention, feature: .mention(otherAuthor.id)),
                Self.mockFacet(in: text, matching: link.absoluteString, feature: .link(link)),
                Self.mockFacet(in: text, matching: "#scrolling", feature: .tag("scrolling"))
            ]
            if index % 3 == 0 { post.links = [URL(string: "https://\(linkHost)/read/\(index)")!] }
            if index % 4 == 1 {
                post.imageURLs = (0..<(index % 8 == 5 ? 4 : 1)).map { imageURL(index: index, part: "image-\($0)") }
                post.imageDescriptions = Dictionary(
                    uniqueKeysWithValues: post.imageURLs.map {
                        ($0, "Colored bands and a white circle.")
                    })
            }
            var quotedPost: Post?
            if index % 4 == 2 || index % 8 == 7 {
                let quotedText = "A quoted post for row \(index + 1). \(mention) shared \(link.absoluteString)."
                var quote = richScrollPost(index: index, part: "quote", author: otherAuthor, text: quotedText)
                quote.facets = [
                    Self.mockFacet(in: quotedText, matching: mention, feature: .mention(otherAuthor.id)),
                    Self.mockFacet(in: quotedText, matching: link.absoluteString, feature: .link(link))
                ]
                if index % 8 == 2 {
                    let image = imageURL(index: index, part: "quote")
                    quote.imageURLs = [image]
                    quote.imageDescriptions[image] = "Colored bands and a white circle."
                }
                quotedPost = quote
                lookupPosts.append(quote)
                if index % 4 == 2 { post.quote = quoteContext(quote) }
            }
            if index % 4 == 3 {
                var root = richScrollPost(
                    index: index, part: "root", author: otherAuthor,
                    text: "Thread root for row \(index + 1). \(paragraphs[index % paragraphs.count])"
                )
                var parent = richScrollPost(
                    index: index, part: "parent", author: fixtureAuthors[(index + 2) % fixtureAuthors.count],
                    text: "A later reply for row \(index + 1). \(mention) shared \(link.absoluteString)."
                )
                root.createdAt = post.createdAt?.addingTimeInterval(-600)
                parent.createdAt = post.createdAt?.addingTimeInterval(-120)
                parent.replyRootURI = root.id
                parent.replyParentURI =
                    index % 8 == 7
                    ? "at://\(otherAuthor.handle)/app.bsky.feed.post/scroll-\(index)-missing" : root.id
                parent.facets = [
                    Self.mockFacet(in: parent.text, matching: mention, feature: .mention(otherAuthor.id)),
                    Self.mockFacet(in: parent.text, matching: link.absoluteString, feature: .link(link))
                ]
                if let quote = quotedPost { parent.quote = quoteContext(quote) }
                post.replyRootURI = root.id
                post.replyParentURI = parent.id
                contexts[post.id] = [root, parent]
                lookupPosts.append(contentsOf: [root, parent])
            }
            posts.append(post)
        }
        return RichScrollFixture(posts: posts, replyContexts: contexts, lookupPosts: posts + lookupPosts)
    }

    func richScrollFeedPage(authorID: String? = nil) -> FeedPage {
        let fixture = richScrollFixture
        let posts = fixture.posts.filter { authorID == nil || $0.author.id == authorID }.map(withInteractions)
        let ids = Set(posts.map(\.id))
        let contexts = fixture.replyContexts.filter { ids.contains($0.key) }.mapValues { $0.map(withInteractions) }
        return FeedPage(posts: posts, cursor: nil, replyContexts: contexts)
    }
}