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

geesawra.industries/old-fella

Old people's social media app

0
1
main
53 lines · 2.7 KB Raw History
import Foundation
import XCTest

@testable import OldFella

@MainActor
final class PreparedPostTextTests: XCTestCase {
    func testPreparesFeedRepliesAndEmbeddedQuotesWithTheirLinks() throws {
        let url = try XCTUnwrap(URL(string: "https://example.com/full"))
        var post = makePost(id: "post", text: "See example.com")
        post.facets = [PostFacet(byteStart: 4, byteEnd: 15, feature: .link(url))]
        let reply = makePost(id: "context", text: "Earlier reply")
        post.quote = Post.Quote(postID: reply.id, text: "Embedded quote")
        let page = FeedPage(posts: [post], cursor: nil, replyContexts: [post.id: [reply]])
        let cache = PreparedPostTextCache()
        cache.insert(PreparedPostTextCache.prepare(page: page))

        let postText = try XCTUnwrap(cache.renderedText(id: post.id, text: post.text, facets: post.facets))
        XCTAssertEqual(String(postText.characters), "See https://example.com/full")
        let link = try XCTUnwrap(postText.range(of: url.absoluteString))
        XCTAssertEqual(postText[link].link, url)
        let plainText = try XCTUnwrap(postText.range(of: "See "))
        XCTAssertEqual(postText[plainText].link, PostTextFacets.openContainingPostURL)

        let replyText = try XCTUnwrap(cache.renderedText(id: reply.id, text: reply.text, facets: []))
        let quoteText = try XCTUnwrap(
            cache.renderedText(id: reply.id, text: "Embedded quote", facets: [], embeddedQuote: true))
        XCTAssertEqual(String(replyText.characters), reply.text)
        XCTAssertEqual(String(quoteText.characters), "Embedded quote")
        XCTAssertNil(cache.renderedText(id: reply.id, text: "Embedded quote", facets: []))
    }

    func testRejectsChangedSourceTextBytesAndFacets() throws {
        let text = "é e\u{301}"
        let equivalentText = "e\u{301} é"
        let url = try XCTUnwrap(URL(string: "https://example.com/full"))
        var post = makePost(id: "post", text: text)
        post.facets = [PostFacet(byteStart: 0, byteEnd: 2, feature: .link(url))]
        let cache = PreparedPostTextCache()
        cache.insert(PreparedPostTextCache.prepare(page: FeedPage(posts: [post], cursor: nil)))

        XCTAssertEqual(text, equivalentText)
        XCTAssertNotNil(cache.renderedText(id: post.id, text: text, facets: post.facets))
        XCTAssertNil(cache.renderedText(id: post.id, text: equivalentText, facets: post.facets))
        XCTAssertNil(cache.renderedText(id: post.id, text: text, facets: []))
    }

    private func makePost(id: String, text: String) -> Post {
        Post(
            id: id, author: .init(id: "reader", name: "Reader", handle: "reader.example"),
            text: text, likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyIDs: [])
    }
}