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