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

geesawra.industries/old-fella

Old people's social media app

0
1
main
96 lines · 4.5 KB Raw History
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)
    }
}