atagen

malice

public
Star0
Watch0
787cdbbf0721a1b004712020969dadaa42f35e32
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git
web/test/mentions.test.tsTypescript59 lines2.5 KB
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, stripMarkdownPills } from '../src/lib/mentions.ts';
5
import { findUrls } from '../src/lib/urls.ts';
6
7
const alice = { user_id: '@alice:example.org', name: 'Alice Liddell', avatar: null };
8
const bob = { user_id: '@bob:example.org', name: 'Bob', avatar: null };
9
const carol = { user_id: '@carol:example.org', name: 'Carol', avatar: null };
10
11
test('finds the query under the caret only at a word start', () => {
12
  assert.deepEqual(mentionQueryAt('hi @al', 6), { start: 3, query: 'al' });
13
  assert.deepEqual(mentionQueryAt('@', 1), { start: 0, query: '' });
14
  assert.equal(mentionQueryAt('me@al', 5), null);
15
  assert.equal(mentionQueryAt('@al ice', 7), null);
16
});
17
18
test('ranks by name and id, skipping yourself', () => {
19
  assert.deepEqual(rankPeople([alice, bob, carol], 'lid', null), [alice]);
20
  assert.deepEqual(rankPeople([alice, bob, carol], 'bob:ex', null), [bob]);
21
  assert.deepEqual(rankPeople([alice, bob, carol], '', '@bob:example.org'), [alice, carol]);
22
});
23
24
test('keeps picked names still in the body and typed matrix ids', () => {
25
  const picked = [
26
    { user_id: alice.user_id, text: alice.name },
27
    { user_id: bob.user_id, text: bob.name }
28
  ];
29
  const mentions = resolveMentions('Alice Liddell and @carol:example.org', picked);
30
  assert.deepEqual(mentionedUserIds(mentions), ['@alice:example.org', '@carol:example.org']);
31
});
32
33
test('links mentions outside code', () => {
34
  const mentions = [{ user_id: '@a<b:example.org', text: 'A<B' }];
35
  assert.equal(
36
    codeToHtml('hey A<B `A<B`', mentions),
37
    'hey <a href="https://matrix.to/#/@a&lt;b:example.org">A&lt;B</a> <code>A&lt;B</code>'
38
  );
39
  assert.equal(codeToHtml('nobody here', mentions), null);
40
});
41
42
test('markdown pills in the plain body become names', () => {
43
  const body = 'Good Morning [lillis](https://matrix.to/#/@lillis:matrix.org),\nhi';
44
  assert.deepEqual(stripMarkdownPills(body), {
45
    text: 'Good Morning lillis,\nhi',
46
    mentions: [{ user_id: '@lillis:matrix.org', text: 'lillis' }]
47
  });
48
  assert.deepEqual(stripMarkdownPills('[x](https://matrix.to/#/@%E0%A4%A)'), {
49
    text: '[x](https://matrix.to/#/@%E0%A4%A)',
50
    mentions: []
51
  });
52
});
53
54
test('matrix.to permalinks get no link card', () => {
55
  assert.deepEqual(
56
    findUrls('https://matrix.to/#/@lillis:matrix.org and https://example.org/a').map((u) => u.href),
57
    ['https://example.org/a']
58
  );
59
});