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
import Foundation
import XCTest
@testable import OldFella
final class PostLinkMappingTests: XCTestCase {
func testVideoEmbedDoesNotRepeatInlineLinkInPostOrQuote() async throws {
let text = "Watch www.youtube.com/watch?v=L0p3..."
let inlineURL = "https://www.youtube.com/watch?v=L0p39TPUWVI"
let attachedURL = inlineURL + "&list=PLOe2mO99p_MY&index=9"
let record: [String: Any] = [
"text": text,
"facets": [[
"index": ["byteStart": 6, "byteEnd": text.utf8.count],
"features": [["$type": "app.bsky.richtext.facet#link", "uri": inlineURL]]
]]
]
let author = ["did": "did:plc:author", "handle": "author.example"]
let external: [String: Any] = [
"$type": "app.bsky.embed.external#view",
"external": ["uri": attachedURL, "title": "Video", "description": ""]
]
let embed: [String: Any] = [
"$type": "app.bsky.embed.recordWithMedia#view",
"media": external,
"record": ["record": [
"uri": "at://did:plc:author/app.bsky.feed.post/quote", "author": author,
"value": record, "embeds": [external]
]]
]
let service = ATProtoBlueskyService()
let mapped = await service.mapPost([
"uri": "at://did:plc:author/app.bsky.feed.post/post", "cid": "post-cid",
"author": author, "record": record, "embed": embed
])
let post = try XCTUnwrap(mapped)
let quote = try XCTUnwrap(post.quote)
XCTAssertEqual(post.text, text)
XCTAssertEqual(quote.text, text)
XCTAssertEqual(post.facets.map(\.feature), [.link(try XCTUnwrap(URL(string: inlineURL)))])
XCTAssertEqual(quote.facets, post.facets)
XCTAssertTrue(post.links.isEmpty)
XCTAssertTrue(quote.links.isEmpty)
}
func testExternalLinksPreserveDifferentDestinations() throws {
let inline = try XCTUnwrap(URL(string: "https://www.youtube.com/watch?v=first"))
let cases: [(String, Bool)] = [
(inline.absoluteString, false),
(inline.absoluteString + "&list=playlist&index=9", false),
("https://youtube.com/watch?index=9&v=first&list=playlist", false),
("https://www.youtube.com/watch?v=second&list=playlist&index=9", true),
(inline.absoluteString + "&t=60", true),
("https://www.youtube.com/playlist?list=playlist", true),
("https://example.com/watch?v=first&list=playlist&index=9", true)
]
for (raw, isDistinct) in cases {
let url = try XCTUnwrap(URL(string: raw))
let embeds: [[String: Any]] = [["external": ["uri": raw]]]
XCTAssertEqual(
ATProtoBlueskyService.externalLinks(from: embeds, excluding: [inline]),
isDistinct ? [url] : [], raw)
XCTAssertEqual(ATProtoBlueskyService.externalLinks(from: embeds, excluding: []), [url], raw)
}
let article = try XCTUnwrap(URL(string: "https://example.com/article?page=1"))
let nextPage = try XCTUnwrap(URL(string: "https://example.com/article?page=2"))
XCTAssertEqual(
ATProtoBlueskyService.externalLinks(
from: [["external": ["uri": nextPage.absoluteString]]], excluding: [article]), [nextPage])
}
func testShortenedHTTPSLabelKeepsFullDestinationWithoutFallbackDuplicate() async throws {
let label = "https://example.com/arti..."
let url = try XCTUnwrap(URL(string: "https://example.com/articles/full?query=value"))
let service = ATProtoBlueskyService()
let mapped = await service.mapPost([
"uri": "at://did:plc:author/app.bsky.feed.post/post", "cid": "post-cid",
"author": ["did": "did:plc:author", "handle": "author.example"],
"record": [
"text": label,
"facets": [[
"index": ["byteStart": 0, "byteEnd": label.utf8.count],
"features": [["$type": "app.bsky.richtext.facet#link", "uri": url.absoluteString]]
]]
],
"embed": ["external": ["uri": url.absoluteString]]
])
let post = try XCTUnwrap(mapped)
XCTAssertEqual(post.text, label)
XCTAssertEqual(post.facets.map(\.feature), [.link(url)])
XCTAssertTrue(post.links.isEmpty)
let rendered = PostTextFacets.render(post.text, facets: post.facets)
XCTAssertEqual(String(rendered.characters), url.absoluteString)
XCTAssertEqual(rendered.link, url)
}
}