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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import Foundation
import XCTest
@testable import OldFella
extension ATProtoServiceTests {
func testAuthorFeedMapsAttachedMediaAndPreservesTextLinksAndQuotes() async throws {
let configuration = URLSessionConfiguration.ephemeral
configuration.protocolClasses = [StubURLProtocol.self]
let session = URLSession(configuration: configuration)
let fixture = MediaMappingFixture()
let responseData = try fixture.responseData
StubURLProtocol.clearRequests()
StubURLProtocol.handler = { request in
XCTAssertEqual(request.url?.host, "public.api.bsky.app")
XCTAssertEqual(request.url?.path, "/xrpc/app.bsky.feed.getAuthorFeed")
return (200, responseData)
}
defer {
StubURLProtocol.handler = nil
session.invalidateAndCancel()
}
let service = ATProtoBlueskyService(defaults: .standard, session: session)
let feed = try await service.loadAuthorPosts(actorID: "did:plc:media", cursor: nil)
let posts = Dictionary(uniqueKeysWithValues: feed.posts.map { ($0.id, $0) })
try assertExternalMedia(posts)
try assertAttachedMedia(posts)
try assertQuotedMedia(posts, fixture: fixture)
try assertTextMedia(posts, fixture: fixture)
}
private func assertExternalMedia(_ posts: [String: Post]) throws {
let direct = try XCTUnwrap(posts["at://did:plc:media/app.bsky.feed.post/direct-external"])
XCTAssertEqual(direct.externalTitle, "Article title")
XCTAssertEqual(direct.externalDescription, "Article description")
XCTAssertEqual(direct.links, [URL(string: "https://example.test/article")!])
XCTAssertEqual(
posts["at://did:plc:media/app.bsky.feed.post/text-with-external"]?.links,
[URL(string: "https://example.test/article")!])
XCTAssertEqual(posts["at://did:plc:media/app.bsky.feed.post/duplicate-external"]?.links, [])
XCTAssertEqual(posts["at://did:plc:media/app.bsky.feed.post/invalid-external"]?.links, [])
XCTAssertEqual(
posts["at://did:plc:media/app.bsky.feed.post/nested-image"]?.quote?.links,
[URL(string: "https://example.test/quoted-article")!])
let nested = try XCTUnwrap(posts["at://did:plc:media/app.bsky.feed.post/external-gif"])
XCTAssertEqual(nested.externalTitle, "GIF")
XCTAssertEqual(nested.externalDescription, "A person nods")
XCTAssertEqual(nested.quote?.externalTitle, "GIF")
XCTAssertEqual(nested.quote?.externalDescription, "Quoted GIF")
}
private func assertAttachedMedia(_ posts: [String: Post]) throws {
let imageURL = try XCTUnwrap(URL(string: "https://cdn.example.test/photo.jpg"))
let nestedImageURL = try XCTUnwrap(URL(string: "https://cdn.example.test/nested.png"))
let videoURL = try XCTUnwrap(URL(string: "https://video.example.test/playlist.m3u8"))
let nestedVideoURL = try XCTUnwrap(URL(string: "https://video.example.test/nested.m3u8"))
let attachedGIFVideoURL = try XCTUnwrap(URL(string: "https://video.example.test/animated.m3u8"))
let nestedGIFVideoURL = try XCTUnwrap(URL(string: "https://video.example.test/nested-animated.m3u8"))
let gifURL = try XCTUnwrap(URL(string: "https://cdn.example.test/loop.gif"))
let directVideoURL = try XCTUnwrap(URL(string: "https://cdn.example.test/clip.mp4"))
let siteURL = try XCTUnwrap(URL(string: "https://example.test/article"))
XCTAssertEqual(posts["at://did:plc:media/app.bsky.feed.post/attached-image"]?.imageURLs, [imageURL])
XCTAssertEqual(
posts["at://did:plc:media/app.bsky.feed.post/attached-image"]?.imageThumbnailURLs,
[imageURL: URL(string: "https://cdn.example.test/photo-thumb.jpg")!])
XCTAssertEqual(
posts["at://did:plc:media/app.bsky.feed.post/attached-image"]?.imageDescriptions[imageURL], "Photo")
XCTAssertEqual(
posts["at://did:plc:media/app.bsky.feed.post/nested-image"]?.imageDescriptions[nestedImageURL],
"Nested photo")
XCTAssertEqual(posts["at://did:plc:media/app.bsky.feed.post/attached-video"]?.videoURLs, [videoURL])
XCTAssertEqual(posts["at://did:plc:media/app.bsky.feed.post/attached-video"]?.inlineGIFVideoURLs, [])
XCTAssertEqual(posts["at://did:plc:media/app.bsky.feed.post/attached-gif-video"]?.videoURLs, [])
XCTAssertEqual(
posts["at://did:plc:media/app.bsky.feed.post/attached-gif-video"]?.inlineGIFVideoURLs, [attachedGIFVideoURL]
)
XCTAssertEqual(posts["at://did:plc:media/app.bsky.feed.post/nested-image"]?.imageURLs, [nestedImageURL])
}
private func assertQuotedMedia(_ posts: [String: Post], fixture: MediaMappingFixture) throws {
let nestedVideoURL = try XCTUnwrap(URL(string: "https://video.example.test/nested.m3u8"))
let nestedGIFVideoURL = try XCTUnwrap(URL(string: "https://video.example.test/nested-animated.m3u8"))
let quote = try XCTUnwrap(posts["at://did:plc:media/app.bsky.feed.post/nested-image"]?.quote)
XCTAssertEqual(quote.postID, fixture.quotedURI)
XCTAssertEqual(quote.author?.handle, "quoted.example")
XCTAssertEqual(quote.text, fixture.quotedText)
XCTAssertEqual(
quote.facets.map(\.feature),
[
.link(try XCTUnwrap(URL(string: "https://quoted.example"))),
.mention("did:plc:quoted"),
.tag("quote")
])
XCTAssertEqual(
quote.facets.map(\.byteStart), [fixture.quotedLinkStart, fixture.quotedMentionStart, fixture.quotedTagStart]
)
XCTAssertEqual(
quote.facets.map(\.byteEnd), [fixture.quotedLinkEnd, fixture.quotedMentionEnd, fixture.quotedTagEnd])
XCTAssertEqual(quote.createdAt, ISO8601DateFormatter().date(from: "2023-01-02T03:04:05Z"))
XCTAssertEqual(
posts["at://did:plc:media/app.bsky.feed.post/nested-image"]?.createdAt?.timeIntervalSince1970 ?? 0,
1714998896.123, accuracy: 0.001)
XCTAssertEqual(quote.imageURLs, [try XCTUnwrap(URL(string: "https://cdn.example.test/quoted.jpg"))])
XCTAssertEqual(
quote.imageThumbnailURLs,
[URL(string: "https://cdn.example.test/quoted.jpg")!: URL(string: "https://cdn.example.test/quoted-thumb.jpg")!])
XCTAssertEqual(
quote.imageDescriptions[try XCTUnwrap(URL(string: "https://cdn.example.test/quoted.jpg"))], "Quoted photo")
XCTAssertEqual(quote.inlineGIFVideoURLs, [try XCTUnwrap(URL(string: "https://video.example.test/quoted.m3u8"))])
XCTAssertEqual(posts["at://did:plc:media/app.bsky.feed.post/nested-video"]?.videoURLs, [nestedVideoURL])
XCTAssertEqual(posts["at://did:plc:media/app.bsky.feed.post/nested-video"]?.inlineGIFVideoURLs, [])
XCTAssertEqual(
posts["at://did:plc:media/app.bsky.feed.post/nested-video"]?.quote?.postID,
"at://did:plc:quoted/app.bsky.feed.post/video-quote")
XCTAssertEqual(posts["at://did:plc:media/app.bsky.feed.post/nested-gif-video"]?.videoURLs, [])
XCTAssertEqual(
posts["at://did:plc:media/app.bsky.feed.post/nested-gif-video"]?.inlineGIFVideoURLs, [nestedGIFVideoURL])
}
private func assertTextMedia(_ posts: [String: Post], fixture: MediaMappingFixture) throws {
let imageURL = try XCTUnwrap(URL(string: "https://cdn.example.test/photo.jpg"))
let gifURL = try XCTUnwrap(URL(string: "https://cdn.example.test/loop.gif"))
let directVideoURL = try XCTUnwrap(URL(string: "https://cdn.example.test/clip.mp4"))
let siteURL = try XCTUnwrap(URL(string: "https://example.test/article"))
let textPost = try XCTUnwrap(posts["at://did:plc:media/app.bsky.feed.post/text-media"])
XCTAssertEqual(textPost.imageURLs, [])
XCTAssertEqual(textPost.gifURLs, [gifURL])
let externalPost = try XCTUnwrap(posts["at://did:plc:media/app.bsky.feed.post/external-gif"])
XCTAssertEqual(externalPost.gifURLs, [URL(string: "https://static.klipy.com/example.gif?hh=498&mp4=abc")!])
XCTAssertEqual(
externalPost.imageDescriptions[URL(string: "https://static.klipy.com/example.gif?hh=498&mp4=abc")!],
"A person nods")
XCTAssertEqual(
externalPost.quote?.gifURLs,
[URL(string: "https://example.test/quoted.gif?size=1")!, URL(string: "https://example.test/facet.gif")!])
XCTAssertEqual(externalPost.quote?.postID, fixture.quotedURI)
XCTAssertEqual(textPost.videoURLs, [directVideoURL])
XCTAssertEqual(textPost.inlineGIFVideoURLs, [])
let facetLinks = Set(
textPost.facets.compactMap { facet -> URL? in
if case .link(let url) = facet.feature { return url }
return nil
})
XCTAssertEqual(Set(textPost.links).union(facetLinks), Set([gifURL, directVideoURL, siteURL]))
XCTAssertFalse(textPost.videoURLs.contains(siteURL))
XCTAssertEqual(posts["at://did:plc:media/app.bsky.feed.post/duplicate-text-image"]?.imageURLs, [imageURL])
}
}