Files
hideck/web/tests/commandInput.test.ts
T

29 lines
1.6 KiB
TypeScript

import assert from 'node:assert/strict'
import test from 'node:test'
import type { CommandDefinition } from '../src/types/commands'
import { buildDangerousCommand, commandSuggestions, commandTemplate } from '../src/utils/commandInput'
const definitions: CommandDefinition[] = [
{ name: 'send', usage: '/send [设备]', summary: '发送', dangerous: false, async: true, device_argument: true },
{ name: 'sms', usage: '/sms [设备]', summary: '短信', dangerous: false, async: false, device_argument: true },
{ name: 'list', usage: '/list', summary: '列表', dangerous: false, async: false, device_argument: false }
]
test('filters slash command suggestions before the first argument', () => {
assert.deepEqual(commandSuggestions('/s', definitions).map((item) => item.name), ['send', 'sms'])
assert.deepEqual(commandSuggestions('/s wwan0', definitions), [])
assert.deepEqual(commandSuggestions('s', definitions), [])
})
test('creates a command template that is ready for required arguments', () => {
assert.equal(commandTemplate(definitions[0]), '/send ')
assert.equal(commandTemplate(definitions[2]), '/list')
})
test('builds dangerous quick actions only with complete explicit arguments', () => {
assert.equal(buildDangerousCommand({ name: 'rotate', device: 'wwan0' }), '/rotate wwan0')
assert.equal(buildDangerousCommand({ name: 'switch', device: 'wwan0', target: '2' }), '/switch wwan0 2')
assert.equal(buildDangerousCommand({ name: 'vocall', device: 'wwan0', phone: '888', duration: 15 }), '/vocall wwan0 888 15')
assert.throws(() => buildDangerousCommand({ name: 'switch', device: 'wwan0' }), /Profile/)
})