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
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 ")
}
}