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
import Foundation
import XCTest
@testable import OldFella
extension ATProtoServiceTests {
func testAuthorFeedMapsGalleryImagesAndQuotedGalleries() async throws {
let configuration = URLSessionConfiguration.ephemeral
configuration.protocolClasses = [StubURLProtocol.self]
let session = URLSession(configuration: configuration)
let author: [String: Any] = ["did": "did:plc:media", "handle": "media.example"]
let gallery = galleryFixture
func post(_ id: String, embed: [String: Any]) -> [String: Any] {
[
"uri": "at://did:plc:media/app.bsky.feed.post/\(id)", "cid": "cid-\(id)", "author": author,
"record": ["text": "", "createdAt": "2024-05-06T12:34:56Z"], "indexedAt": "2024-05-06T12:34:56Z",
"embed": embed
]
}
let quotedURI = "at://did:plc:quoted/app.bsky.feed.post/original"
let nested: [String: Any] = [
"$type": "app.bsky.embed.recordWithMedia#view",
"record": [
"$type": "app.bsky.embed.record#view",
"record": [
"$type": "app.bsky.embed.record#viewRecord", "uri": quotedURI, "cid": "quoted-cid",
"author": author, "value": ["text": "Quoted", "createdAt": "2024-05-06T12:34:56Z"],
"indexedAt": "2024-05-06T12:34:56Z", "embeds": [gallery]
]
],
"media": gallery
]
let data = try JSONSerialization.data(withJSONObject: [
"feed": [post("direct", embed: gallery), post("nested", embed: nested)].map { ["post": $0] }
])
StubURLProtocol.handler = { _ in (200, data) }
defer {
StubURLProtocol.handler = nil
session.invalidateAndCancel()
}
let service = ATProtoBlueskyService(defaults: .standard, session: session)
let posts = try await service.loadAuthorPosts(actorID: "did:plc:media", cursor: nil).posts
let first = try XCTUnwrap(URL(string: "https://cdn.example.test/one.jpg"))
let second = try XCTUnwrap(URL(string: "https://cdn.example.test/two-thumb.jpg"))
XCTAssertEqual(posts.count, 2)
XCTAssertEqual(posts[0].imageURLs, [first, second])
let thumbnails = [first: URL(string: "https://cdn.example.test/one-thumb.jpg")!, second: second]
XCTAssertEqual(posts[0].imageThumbnailURLs, thumbnails)
XCTAssertEqual(posts[0].imageDescriptions[first], "First image")
XCTAssertEqual(posts[0].imageDescriptions[second], "Second image")
XCTAssertEqual(posts[1].imageURLs, [first, second])
XCTAssertEqual(posts[1].imageThumbnailURLs, thumbnails)
XCTAssertEqual(posts[1].quote?.postID, quotedURI)
XCTAssertEqual(posts[1].quote?.imageURLs, [first, second])
XCTAssertEqual(posts[1].quote?.imageThumbnailURLs, thumbnails)
XCTAssertEqual(posts[1].quote?.imageDescriptions[first], "First image")
}
private var galleryFixture: [String: Any] {
[
"$type": "app.bsky.embed.gallery#view",
"items": [
[
"$type": "app.bsky.embed.gallery#viewImage", "fullsize": "https://cdn.example.test/one.jpg",
"thumbnail": "https://cdn.example.test/one-thumb.jpg", "alt": "First image",
"aspectRatio": ["width": 800, "height": 600]
],
[
"$type": "app.bsky.embed.gallery#viewImage", "thumbnail": "https://cdn.example.test/two-thumb.jpg",
"alt": "Second image"
],
["$type": "app.bsky.embed.gallery#viewOther", "fullsize": "https://cdn.example.test/ignored.jpg"]
]
]
}
}