atagen

malice

public
Star0
Watch0
77eddb6b710ffe2b884b8bdf7b0611041f7358e6
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git
web/test/mentions.test.tsTypescript60 lines2.6 KB
1
import assert from 'node:assert/strict';
2
import { test } from 'node:test';
3
import { formatBody } from '../src/lib/markdown.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
    formatBody('hey A<B `A<B`', mentions, true),
37
    '<p>hey <a href="https://matrix.to/#/@a&lt;b:example.org">A&lt;B</a> <code>A&lt;B</code></p>'
38
  );
39
  assert.equal(formatBody('hey A<B', mentions, false), 'hey <a href="https://matrix.to/#/@a&lt;b:example.org">A&lt;B</a>');
40
  assert.equal(formatBody('nobody here', mentions, true), null);
41
});
42
43
test('markdown pills in the plain body become names', () => {
44
  const body = 'Good Morning [lillis](https://matrix.to/#/@lillis:matrix.org),\nhi';
45
  assert.deepEqual(stripMarkdownPills(body), {
46
    text: 'Good Morning lillis,\nhi',
47
    mentions: [{ user_id: '@lillis:matrix.org', text: 'lillis' }]
48
  });
49
  assert.deepEqual(stripMarkdownPills('[x](https://matrix.to/#/@%E0%A4%A)'), {
50
    text: '[x](https://matrix.to/#/@%E0%A4%A)',
51
    mentions: []
52
  });
53
});
54
55
test('matrix.to permalinks get no link card', () => {
56
  assert.deepEqual(
57
    findUrls('https://matrix.to/#/@lillis:matrix.org and https://example.org/a').map((u) => u.href),
58
    ['https://example.org/a']
59
  );
60
});