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

geesawra.industries/old-fella

Old people's social media app

0
1
main
51 lines · 2.4 KB Raw History
import Foundation
import XCTest

@testable import OldFella

final class ComposerTextTests: XCTestCase {
    func testCandidatesUseUTF8OffsetsAfterUnicode() throws {
        let text = "🌍 café @alice.example #日本語 https://example.com"
        let candidates = ComposerText.facetCandidates(in: text)
        XCTAssertEqual(candidates.map(\.kind), [.mention, .tag, .link])
        XCTAssertEqual(candidates.map(\.value), ["alice.example", "日本語", "https://example.com"])
        let expected = ["@alice.example", "#日本語", "https://example.com"]
        for (candidate, substring) in zip(candidates, expected) {
            let range = try XCTUnwrap(text.range(of: substring))
            XCTAssertEqual(candidate.byteStart, text[..<range.lowerBound].utf8.count)
            XCTAssertEqual(candidate.byteEnd, text[..<range.upperBound].utf8.count)
            XCTAssertEqual(
                String(bytes: Array(text.utf8)[candidate.byteStart..<candidate.byteEnd], encoding: .utf8), substring)
        }
    }

    func testURLSuppressesOverlappingMentionAndTag() {
        let text = "https://example.com/@alice.example#swift #outside"
        let candidates = ComposerText.facetCandidates(in: text)
        XCTAssertEqual(candidates.map(\.kind), [.link, .tag])
        XCTAssertEqual(candidates.map(\.value), ["https://example.com/@alice.example#swift", "outside"])
    }

    func testURLExcludesTrailingPunctuation() {
        let text = "See (https://example.com/path), https://other.example/?!"
        let candidates = ComposerText.facetCandidates(in: text)
        XCTAssertEqual(candidates.map(\.value), ["https://example.com/path", "https://other.example/"])
        for candidate in candidates {
            XCTAssertEqual(candidate.byteEnd - candidate.byteStart, candidate.value.utf8.count)
        }
    }

    func testEmailAndPartialHandleDoNotBecomeMentions() {
        let text = "alice@example.com @partial @full.example"
        let candidates = ComposerText.facetCandidates(in: text)
        XCTAssertEqual(candidates.map(\.kind), [.mention])
        XCTAssertEqual(candidates.map(\.value), ["full.example"])
    }

    func testRepeatedCandidatesKeepTheirOwnOffsets() {
        let text = "#swift #swift"
        let candidates = ComposerText.facetCandidates(in: text)
        XCTAssertEqual(candidates.map(\.byteStart), [0, 7])
        XCTAssertEqual(candidates.map(\.byteEnd), [6, 13])
    }
}