atagen

malice

public
Star0
Watch0
f7069be9f61e155439aa890dcaaec24f44671d44
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git
web/test/commands.test.tsTypescript38 lines1.9 KB
1
import assert from 'node:assert/strict';
2
import { test } from 'node:test';
3
import { isKnownCommand, parseCommand, splitTarget, unescapeCommand } from '../src/lib/commands.ts';
4
5
const people = [
6
  { user_id: '@alice:example.org', name: 'Alice Liddell', avatar: null },
7
  { user_id: '@al:example.org', name: 'Alice', avatar: null },
8
  { user_id: '@bob:example.org', name: 'Bob', avatar: null },
9
  { user_id: '@bob2:example.org', name: 'Bob', avatar: null }
10
];
11
12
test('parses a leading command and leaves text alone', () => {
13
  assert.deepEqual(parseCommand('/kick  Bob  being rude '), { name: 'kick', args: 'Bob  being rude' });
14
  assert.deepEqual(parseCommand('/leave'), { name: 'leave', args: '' });
15
  assert.equal(parseCommand('hello /kick'), null);
16
  assert.equal(parseCommand('//kick bob'), null);
17
  assert.equal(unescapeCommand('//kick bob'), '/kick bob');
18
  assert.equal(isKnownCommand('kick'), true);
19
  assert.equal(isKnownCommand('shrug'), false);
20
});
21
22
test('finds targets by id or the longest display name', () => {
23
  assert.deepEqual(splitTarget('@carol:example.org 50', people), { user_id: '@carol:example.org', rest: '50' });
24
  assert.deepEqual(splitTarget('Alice Liddell spam', people), { user_id: '@alice:example.org', rest: 'spam' });
25
  assert.deepEqual(splitTarget('alice', people), { user_id: '@al:example.org', rest: '' });
26
  assert.ok('error' in splitTarget('Bob', people));
27
  assert.ok('error' in splitTarget('Zed', people));
28
  assert.ok('error' in splitTarget('', people));
29
});
30
31
test('a leading slash word opens the command list', async () => {
32
  const { commandQueryAt, matchCommands } = await import('../src/lib/commands.ts');
33
  assert.equal(commandQueryAt('/ki', 3), 'ki');
34
  assert.equal(commandQueryAt('/', 1), '');
35
  assert.equal(commandQueryAt('/kick bob', 9), null);
36
  assert.equal(commandQueryAt('hi /ki', 6), null);
37
  assert.deepEqual(matchCommands('un'), ['unban', 'unmute', 'unignore']);
38
});