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