import Foundation import XCTest @testable import OldFella final class ComposerMentionTests: XCTestCase { func testQueryUsesCursorAndReplacesWholeHandle() throws { let text = "Hello @friend.example and @other.example" let caret = ("Hello @fri" as NSString).length let mention = try XCTUnwrap(ComposerMention.active(in: text, selection: NSRange(location: caret, length: 0))) XCTAssertEqual(mention.query, "fri") let result = try XCTUnwrap(mention.inserting(handle: "alice.bsky.social", in: text)) XCTAssertEqual(result.text, "Hello @alice.bsky.social and @other.example") XCTAssertEqual(result.selection.location, "Hello @alice.bsky.social".utf16.count) } func testUnicodePrefixAndInsertionAtEnd() throws { let text = "🌤️ café @ma" let mention = try XCTUnwrap( ComposerMention.active(in: text, selection: NSRange(location: text.utf16.count, length: 0))) let result = try XCTUnwrap(mention.inserting(handle: "mae.example", in: text)) XCTAssertEqual(result.text, "🌤️ café @mae.example ") XCTAssertEqual(result.selection.location, result.text.utf16.count) } func testDoesNotSuggestForEmailsLinksSelectionsOrEmptyHandle() { for text in [ "mail@example.com", "café@example.com", "https://example.com/@friend", "https://@friend", "Hello @", "Hello @friend " ] { XCTAssertNil( ComposerMention.active(in: text, selection: NSRange(location: text.utf16.count, length: 0)), text) } XCTAssertNil(ComposerMention.active(in: "@friend", selection: NSRange(location: 2, length: 1))) XCTAssertNil(ComposerMention.active(in: "@friend", selection: NSRange(location: 99, length: 0))) } func testInsertionPreservesFollowingPunctuation() throws { let text = "Hello (@ma), welcome" let mention = try XCTUnwrap( ComposerMention.active(in: text, selection: NSRange(location: "Hello (@ma".utf16.count, length: 0))) XCTAssertEqual(mention.inserting(handle: "mae.example", in: text)?.text, "Hello (@mae.example), welcome") } func testSecondMentionAndNewline() throws { let text = "@first.example\n@al" let mention = try XCTUnwrap( ComposerMention.active(in: text, selection: NSRange(location: text.utf16.count, length: 0))) XCTAssertEqual(mention.query, "al") XCTAssertEqual(mention.inserting(handle: "alex.example", in: text)?.text, "@first.example\n@alex.example ") } }