| 1 | import assert from 'node:assert/strict'; |
| 2 | import { test } from 'node:test'; |
| 3 | import { completeShortcode, emoteQueryAt, rankEmotes, resolveEmotes } from '../src/lib/emotes.ts'; |
| 4 | import { formatBody } from '../src/lib/markdown.ts'; |
| 5 | |
| 6 | const sob = { emoji: '😭', label: 'loudly crying face', tags: [], shortcodes: ['sob'] }; |
| 7 | const smile = { emoji: '😄', label: 'grinning face with smiling eyes', tags: [], shortcodes: ['smile'] }; |
| 8 | const blob = { shortcode: 'blob_sob', mxc: 'mxc://example.org/blob' }; |
| 9 | |
| 10 | test('opens the emote query after two shortcode characters at a word start', () => { |
| 11 | assert.deepEqual(emoteQueryAt('so :so', 6), { start: 3, query: 'so' }); |
| 12 | assert.equal(emoteQueryAt('so :s', 5), null); |
| 13 | assert.equal(emoteQueryAt('re:so', 5), null); |
| 14 | assert.equal(emoteQueryAt('12:30', 5), null); |
| 15 | assert.equal(emoteQueryAt(':so b', 5), null); |
| 16 | }); |
| 17 | |
| 18 | test('ranks prefix matches ahead of word and substring matches', () => { |
| 19 | const names = rankEmotes('sob', [blob], [sob, smile]).map((c) => c.shortcode); |
| 20 | assert.deepEqual(names, ['sob', 'blob_sob']); |
| 21 | }); |
| 22 | |
| 23 | test('swaps a closed emoji shortcode for the emoji', () => { |
| 24 | assert.deepEqual(completeShortcode('ugh :sob:', 9, [sob]), { text: 'ugh 😭', caret: 6 }); |
| 25 | assert.equal(completeShortcode('ugh :blob_sob:', 14, [sob]), null); |
| 26 | assert.equal(completeShortcode('a:sob:', 6, [sob]), null); |
| 27 | }); |
| 28 | |
| 29 | test('sends used custom emotes as emoticon images', () => { |
| 30 | const emotes = resolveEmotes('hi :blob_sob: and :nope:', [blob]); |
| 31 | assert.deepEqual(emotes, [blob]); |
| 32 | assert.equal( |
| 33 | formatBody('hi :blob_sob:', [], false, emotes), |
| 34 | 'hi <img data-mx-emoticon src="mxc://example.org/blob" alt=":blob_sob:" title=":blob_sob:" height="32">' |
| 35 | ); |
| 36 | assert.match(formatBody('**hi** :blob_sob:', [], true, emotes) ?? '', /<strong>hi<\/strong> <img data-mx-emoticon/); |
| 37 | assert.equal(formatBody('`:blob_sob:`', [], true, emotes), '<p><code>:blob_sob:</code></p>'); |
| 38 | }); |