import Foundation import XCTest @testable import OldFella final class ComposerEmojiTests: XCTestCase { func testShortcodeAtCaretReplacesWholeToken() throws { let text = "Look :eyes: here" let token = try XCTUnwrap(ComposerEmoji.active( in: text, selection: NSRange(location: "Look :ey".utf16.count, length: 0))) XCTAssertEqual(token.query, "ey") XCTAssertEqual(token.range, NSRange(location: 5, length: 6)) let result = try XCTUnwrap(token.inserting(emoji: "👀", in: text)) XCTAssertEqual(result.text, "Look 👀 here") XCTAssertEqual(result.selection, NSRange(location: "Look 👀".utf16.count, length: 0)) } func testOpenAndClosedShortcodes() throws { for text in [":eyes", ":eyes:"] { let token = try XCTUnwrap(ComposerEmoji.active( in: text, selection: NSRange(location: text.utf16.count, length: 0))) XCTAssertEqual(token.query, "eyes") XCTAssertEqual(token.inserting(emoji: "👀", in: text)?.text, "👀 ") } } func testUnicodePrefixAndMultiScalarEmojiKeepCaretAfterInsertion() throws { let text = "🌤️ café :heart" let token = try XCTUnwrap(ComposerEmoji.active( in: text, selection: NSRange(location: text.utf16.count, length: 0))) let result = try XCTUnwrap(token.inserting(emoji: "❤️", in: text)) XCTAssertEqual(result.text, "🌤️ café ❤️ ") XCTAssertEqual(result.selection, NSRange(location: result.text.utf16.count, length: 0)) } func testInsertionPreservesPunctuationAndWhitespace() throws { let text = "Hello (:wave), friend" let token = try XCTUnwrap(ComposerEmoji.active( in: text, selection: NSRange(location: "Hello (:wave".utf16.count, length: 0))) XCTAssertEqual(token.inserting(emoji: "👋", in: text)?.text, "Hello (👋), friend") } func testSecondTokenAfterNewline() throws { let text = ":wave\n:rocket" let token = try XCTUnwrap(ComposerEmoji.active( in: text, selection: NSRange(location: text.utf16.count, length: 0))) XCTAssertEqual(token.query, "rocket") XCTAssertEqual(token.inserting(emoji: "🚀", in: text)?.text, ":wave\n🚀 ") } func testNumericAndUnderscoreAliases() throws { for query in ["+1", "-1", "100", "heart_eyes", "thinking_face", "heart-eyes"] { let text = ":" + query let token = try XCTUnwrap(ComposerEmoji.active( in: text, selection: NSRange(location: text.utf16.count, length: 0))) XCTAssertEqual(token.query, query) } } func testDoesNotSuggestForLinksTimesEmoticonsOrWordColons() { for text in [ "https://example.com/:eyes", "https://example.com?q=:eyes", "https://:eyes", "mailto:eyes", "12:30", "word:eyes", "café:eyes", "@friend:eyes", "a/:eyes", ":", ":)", ":-)", ": eyes", ":eyes ", ":eyes\n" ] { XCTAssertNil(ComposerEmoji.active( in: text, selection: NSRange(location: text.utf16.count, length: 0)), text) } } func testSelectionAndInvalidRangesDoNotSuggestOrReplace() { for selection in [ NSRange(location: 2, length: 1), NSRange(location: 0, length: 0), NSRange(location: 1, length: 0), NSRange(location: 99, length: 0), NSRange(location: NSNotFound, length: 0) ] { XCTAssertNil(ComposerEmoji.active(in: ":eyes", selection: selection)) } let token = ComposerEmoji(range: NSRange(location: 99, length: 3), query: "eyes") XCTAssertNil(token.inserting(emoji: "👀", in: ":eyes")) } func testExactShortcodeRanksBeforeRelatedNames() { let suggestions = EmojiCatalog.suggestions(for: "eyes") XCTAssertEqual(suggestions.first?.emoji, "👀") XCTAssertEqual(suggestions.first?.shortcode, "eyes") XCTAssertTrue(suggestions.dropFirst().contains { $0.shortcode.contains("eyes") }) XCTAssertEqual(EmojiCatalog.suggestions(for: "fire").first?.emoji, "🔥") } func testPrefixRanksBeforeWordMatches() throws { let suggestions = EmojiCatalog.suggestions(for: "eye", limit: 100) let eyes = try XCTUnwrap(suggestions.firstIndex { $0.emoji == "👀" }) let heartEyes = try XCTUnwrap(suggestions.firstIndex { $0.emoji == "😍" }) XCTAssertLessThan(eyes, heartEyes) XCTAssertEqual(suggestions.first?.shortcode, "eye") } func testCommonAliasesAndCaseInsensitiveQueries() { for (query, emoji) in [ ("+1", "👍"), ("thumbsup", "👍"), ("thumbs_up", "👍"), ("-1", "👎"), ("thumbsdown", "👎"), ("joy", "😂"), ("tada", "🎉"), ("thinking_face", "🤔"), ("thinking", "🤔"), ("heart", "❤️"), ("smile", "😄"), ("laughing", "😆"), ("sob", "😭"), ("EYES", "👀"), ("heart-eyes", "😍"), ("coffee", "☕️") ] { XCTAssertEqual(EmojiCatalog.suggestions(for: query).first?.emoji, emoji, query) } } func testCatalogCoversNamesBeyondCommonAliases() { for (query, emoji) in [ ("butterfly", "🦋"), ("avocado", "🥑"), ("guitar", "🎸"), ("switzerland", "🇨🇭") ] { XCTAssertEqual(EmojiCatalog.suggestions(for: query).first?.emoji, emoji, query) } } func testLimitsUnknownQueriesAndDeduplicatedResults() { XCTAssertTrue(EmojiCatalog.suggestions(for: "").isEmpty) XCTAssertTrue(EmojiCatalog.suggestions(for: "not_an_emoji_shortcode").isEmpty) XCTAssertTrue(EmojiCatalog.suggestions(for: "eyes", limit: 0).isEmpty) XCTAssertTrue(EmojiCatalog.suggestions(for: "eyes", limit: -1).isEmpty) XCTAssertEqual(EmojiCatalog.suggestions(for: "face", limit: 3).count, 3) let suggestions = EmojiCatalog.suggestions(for: "thumb", limit: 100) XCTAssertEqual(suggestions.filter { $0.emoji == "👍" }.count, 1) XCTAssertEqual(Set(suggestions.map(\.id)).count, suggestions.count) XCTAssertEqual(suggestions, EmojiCatalog.suggestions(for: "thumb", limit: 100)) } }