atagen

malice

public
Star0
Watch0
d8624c214c044f548c0a9b7cbcae6d081c88cca8
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git

Blame: web/test/mentions.test.ts

cf23dbf4e77catagen2026-10-23 06:36 UTC1
import assert from 'node:assert/strict';
cf23dbf4e77catagen2026-10-23 06:36 UTC2
import { test } from 'node:test';
6d6a4c34bdc1atagen2026-10-24 06:51 UTC3
import { formatBody } from '../src/lib/markdown.ts';
705b1e1062c2atagen2026-10-23 09:05 UTC4
import { mentionQueryAt, mentionedUserIds, rankPeople, resolveMentions, stripMarkdownPills } from '../src/lib/mentions.ts';
705b1e1062c2atagen2026-10-23 09:05 UTC5
import { findUrls } from '../src/lib/urls.ts';
cf23dbf4e77catagen2026-10-23 06:36 UTC6
cf23dbf4e77catagen2026-10-23 06:36 UTC7
const alice = { user_id: '@alice:example.org', name: 'Alice Liddell', avatar: null };
cf23dbf4e77catagen2026-10-23 06:36 UTC8
const bob = { user_id: '@bob:example.org', name: 'Bob', avatar: null };
cf23dbf4e77catagen2026-10-23 06:36 UTC9
const carol = { user_id: '@carol:example.org', name: 'Carol', avatar: null };
cf23dbf4e77catagen2026-10-23 06:36 UTC10
cf23dbf4e77catagen2026-10-23 06:36 UTC11
test('finds the query under the caret only at a word start', () => {
cf23dbf4e77catagen2026-10-23 06:36 UTC12
  assert.deepEqual(mentionQueryAt('hi @al', 6), { start: 3, query: 'al' });
cf23dbf4e77catagen2026-10-23 06:36 UTC13
  assert.deepEqual(mentionQueryAt('@', 1), { start: 0, query: '' });
cf23dbf4e77catagen2026-10-23 06:36 UTC14
  assert.equal(mentionQueryAt('me@al', 5), null);
cf23dbf4e77catagen2026-10-23 06:36 UTC15
  assert.equal(mentionQueryAt('@al ice', 7), null);
cf23dbf4e77catagen2026-10-23 06:36 UTC16
});
cf23dbf4e77catagen2026-10-23 06:36 UTC17
cf23dbf4e77catagen2026-10-23 06:36 UTC18
test('ranks by name and id, skipping yourself', () => {
cf23dbf4e77catagen2026-10-23 06:36 UTC19
  assert.deepEqual(rankPeople([alice, bob, carol], 'lid', null), [alice]);
cf23dbf4e77catagen2026-10-23 06:36 UTC20
  assert.deepEqual(rankPeople([alice, bob, carol], 'bob:ex', null), [bob]);
cf23dbf4e77catagen2026-10-23 06:36 UTC21
  assert.deepEqual(rankPeople([alice, bob, carol], '', '@bob:example.org'), [alice, carol]);
cf23dbf4e77catagen2026-10-23 06:36 UTC22
});
cf23dbf4e77catagen2026-10-23 06:36 UTC23
cf23dbf4e77catagen2026-10-23 06:36 UTC24
test('keeps picked names still in the body and typed matrix ids', () => {
cf23dbf4e77catagen2026-10-23 06:36 UTC25
  const picked = [
cf23dbf4e77catagen2026-10-23 06:36 UTC26
    { user_id: alice.user_id, text: alice.name },
cf23dbf4e77catagen2026-10-23 06:36 UTC27
    { user_id: bob.user_id, text: bob.name }
cf23dbf4e77catagen2026-10-23 06:36 UTC28
  ];
cf23dbf4e77catagen2026-10-23 06:36 UTC29
  const mentions = resolveMentions('Alice Liddell and @carol:example.org', picked);
cf23dbf4e77catagen2026-10-23 06:36 UTC30
  assert.deepEqual(mentionedUserIds(mentions), ['@alice:example.org', '@carol:example.org']);
cf23dbf4e77catagen2026-10-23 06:36 UTC31
});
cf23dbf4e77catagen2026-10-23 06:36 UTC32
cf23dbf4e77catagen2026-10-23 06:36 UTC33
test('links mentions outside code', () => {
cf23dbf4e77catagen2026-10-23 06:36 UTC34
  const mentions = [{ user_id: '@a<b:example.org', text: 'A<B' }];
cf23dbf4e77catagen2026-10-23 06:36 UTC35
  assert.equal(
6d6a4c34bdc1atagen2026-10-24 06:51 UTC36
    formatBody('hey A<B `A<B`', mentions, true),
6d6a4c34bdc1atagen2026-10-24 06:51 UTC37
    '<p>hey <a href="https://matrix.to/#/@a&lt;b:example.org">A&lt;B</a> <code>A&lt;B</code></p>'
cf23dbf4e77catagen2026-10-23 06:36 UTC38
  );
6d6a4c34bdc1atagen2026-10-24 06:51 UTC39
  assert.equal(formatBody('hey A<B', mentions, false), 'hey <a href="https://matrix.to/#/@a&lt;b:example.org">A&lt;B</a>');
6d6a4c34bdc1atagen2026-10-24 06:51 UTC40
  assert.equal(formatBody('nobody here', mentions, true), null);
cf23dbf4e77catagen2026-10-23 06:36 UTC41
});
705b1e1062c2atagen2026-10-23 09:05 UTC42
705b1e1062c2atagen2026-10-23 09:05 UTC43
test('markdown pills in the plain body become names', () => {
705b1e1062c2atagen2026-10-23 09:05 UTC44
  const body = 'Good Morning [lillis](https://matrix.to/#/@lillis:matrix.org),\nhi';
705b1e1062c2atagen2026-10-23 09:05 UTC45
  assert.deepEqual(stripMarkdownPills(body), {
705b1e1062c2atagen2026-10-23 09:05 UTC46
    text: 'Good Morning lillis,\nhi',
705b1e1062c2atagen2026-10-23 09:05 UTC47
    mentions: [{ user_id: '@lillis:matrix.org', text: 'lillis' }]
705b1e1062c2atagen2026-10-23 09:05 UTC48
  });
705b1e1062c2atagen2026-10-23 09:05 UTC49
  assert.deepEqual(stripMarkdownPills('[x](https://matrix.to/#/@%E0%A4%A)'), {
705b1e1062c2atagen2026-10-23 09:05 UTC50
    text: '[x](https://matrix.to/#/@%E0%A4%A)',
705b1e1062c2atagen2026-10-23 09:05 UTC51
    mentions: []
705b1e1062c2atagen2026-10-23 09:05 UTC52
  });
705b1e1062c2atagen2026-10-23 09:05 UTC53
});
705b1e1062c2atagen2026-10-23 09:05 UTC54
705b1e1062c2atagen2026-10-23 09:05 UTC55
test('matrix.to permalinks get no link card', () => {
705b1e1062c2atagen2026-10-23 09:05 UTC56
  assert.deepEqual(
705b1e1062c2atagen2026-10-23 09:05 UTC57
    findUrls('https://matrix.to/#/@lillis:matrix.org and https://example.org/a').map((u) => u.href),
705b1e1062c2atagen2026-10-23 09:05 UTC58
    ['https://example.org/a']
705b1e1062c2atagen2026-10-23 09:05 UTC59
  );
705b1e1062c2atagen2026-10-23 09:05 UTC60
});