| 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.deepEqual(parseCommand('/sticker party_parrot'), { name: 'sticker', args: 'party_parrot' }); |
| 16 | assert.equal(parseCommand('hello /kick'), null); |
| 17 | assert.equal(parseCommand('//kick bob'), null); |
| 18 | assert.equal(unescapeCommand('//kick bob'), '/kick bob'); |
| 19 | assert.equal(isKnownCommand('kick'), true); |
| 20 | assert.equal(isKnownCommand('sticker'), true); |
| 21 | assert.equal(isKnownCommand('shrug'), false); |
| 22 | }); |
| 23 | |
| 24 | test('finds targets by id or the longest display name', () => { |
| 25 | assert.deepEqual(splitTarget('@carol:example.org 50', people), { user_id: '@carol:example.org', rest: '50' }); |
| 26 | assert.deepEqual(splitTarget('Alice Liddell spam', people), { user_id: '@alice:example.org', rest: 'spam' }); |
| 27 | assert.deepEqual(splitTarget('alice', people), { user_id: '@al:example.org', rest: '' }); |
| 28 | assert.ok('error' in splitTarget('Bob', people)); |
| 29 | assert.ok('error' in splitTarget('Zed', people)); |
| 30 | assert.ok('error' in splitTarget('', people)); |
| 31 | }); |
| 32 | |
| 33 | test('a leading slash word opens the command list', async () => { |
| 34 | const { commandQueryAt, matchCommands } = await import('../src/lib/commands.ts'); |
| 35 | assert.equal(commandQueryAt('/ki', 3), 'ki'); |
| 36 | assert.equal(commandQueryAt('/', 1), ''); |
| 37 | assert.equal(commandQueryAt('/kick bob', 9), null); |
| 38 | assert.equal(commandQueryAt('hi /ki', 6), null); |
| 39 | assert.deepEqual(matchCommands('un').slice(0, 3), ['unban', 'unmute', 'unignore']); |
| 40 | assert.equal(matchCommands('in')[0], 'invite'); |
| 41 | assert.ok(matchCommands('in').includes('disinvite')); |
| 42 | assert.deepEqual(matchCommands('stk'), ['sticker']); |
| 43 | assert.equal(matchCommands('avatar')[0], 'roomavatar'); |
| 44 | assert.deepEqual(matchCommands('STK'), ['sticker']); |
| 45 | assert.deepEqual(matchCommands('zz'), []); |
| 46 | assert.deepEqual(matchCommands('kk'), ['kick']); |
| 47 | assert.deepEqual(matchCommands('kkk'), []); |
| 48 | }); |