| 1 | import assert from 'node:assert/strict'; |
| 2 | import { test } from 'node:test'; |
| 3 | import { codeToHtml } from '../src/lib/code.ts'; |
| 4 | import { mentionQueryAt, mentionedUserIds, rankPeople, resolveMentions } from '../src/lib/mentions.ts'; |
| 5 | |
| 6 | const alice = { user_id: '@alice:example.org', name: 'Alice Liddell', avatar: null }; |
| 7 | const bob = { user_id: '@bob:example.org', name: 'Bob', avatar: null }; |
| 8 | const carol = { user_id: '@carol:example.org', name: 'Carol', avatar: null }; |
| 9 | |
| 10 | test('finds the query under the caret only at a word start', () => { |
| 11 | assert.deepEqual(mentionQueryAt('hi @al', 6), { start: 3, query: 'al' }); |
| 12 | assert.deepEqual(mentionQueryAt('@', 1), { start: 0, query: '' }); |
| 13 | assert.equal(mentionQueryAt('me@al', 5), null); |
| 14 | assert.equal(mentionQueryAt('@al ice', 7), null); |
| 15 | }); |
| 16 | |
| 17 | test('ranks by name and id, skipping yourself', () => { |
| 18 | assert.deepEqual(rankPeople([alice, bob, carol], 'lid', null), [alice]); |
| 19 | assert.deepEqual(rankPeople([alice, bob, carol], 'bob:ex', null), [bob]); |
| 20 | assert.deepEqual(rankPeople([alice, bob, carol], '', '@bob:example.org'), [alice, carol]); |
| 21 | }); |
| 22 | |
| 23 | test('keeps picked names still in the body and typed matrix ids', () => { |
| 24 | const picked = [ |
| 25 | { user_id: alice.user_id, text: alice.name }, |
| 26 | { user_id: bob.user_id, text: bob.name } |
| 27 | ]; |
| 28 | const mentions = resolveMentions('Alice Liddell and @carol:example.org', picked); |
| 29 | assert.deepEqual(mentionedUserIds(mentions), ['@alice:example.org', '@carol:example.org']); |
| 30 | }); |
| 31 | |
| 32 | test('links mentions outside code', () => { |
| 33 | const mentions = [{ user_id: '@a<b:example.org', text: 'A<B' }]; |
| 34 | assert.equal( |
| 35 | codeToHtml('hey A<B `A<B`', mentions), |
| 36 | 'hey <a href="https://matrix.to/#/@a<b:example.org">A<B</a> <code>A<B</code>' |
| 37 | ); |
| 38 | assert.equal(codeToHtml('nobody here', mentions), null); |
| 39 | }); |