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

geesawra.industries/old-fella

Old people's social media app

0
1
main
150 lines · 9.0 KB Raw History
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])
    }
}