import { APP_LOCALES } from '../../core/i18n/locale'; import { COMMAND_IDS, COMMANDS, normalizeCommandInput, parseCommand, suggestCommands, type CommandId, } from './commands'; describe('command parsing', () => { it('collapses whitespace and lowercases input', () => { expect(normalizeCommandInput(' Services AI ')).toBe('services ai'); expect(normalizeCommandInput('HELP')).toBe('help'); }); it('resolves every CommandId from its canonical input and aliases in both locales', () => { for (const command of COMMANDS) { const canonical = parseCommand(command.input); expect(canonical).toEqual({ kind: 'command', definition: command }); for (const locale of APP_LOCALES) { for (const alias of command.aliases[locale]) { expect(parseCommand(alias)).toEqual({ kind: 'command', definition: command }); } } } expect(COMMANDS.map((command) => command.id)).toEqual([...COMMAND_IDS]); }); it('treats hostile and prototype inputs as unknown without side effects', () => { const prototypeNames = Object.getOwnPropertyNames(Object.prototype); const hostile = [ 'rm -rf /', 'eval(1+1)', 'new Function()', '', '__proto__', 'constructor', 'toString', 'hasOwnProperty', ]; for (const input of hostile) { expect(parseCommand(input)).toEqual({ kind: 'unknown', input }); } expect(parseCommand('')).toEqual({ kind: 'empty' }); expect(parseCommand(' ')).toEqual({ kind: 'empty' }); expect(Object.getOwnPropertyNames(Object.prototype)).toEqual(prototypeNames); expect(Object.prototype).not.toHaveProperty('polluted'); }); it('returns an equal result for the same input across repeated calls', () => { const samples = ['help', 'services ai', ' ', 'unknown-token', '__proto__']; for (const sample of samples) { expect(parseCommand(sample)).toEqual(parseCommand(sample)); } }); it('suggests commands in a stable COMMAND_IDS order and filters by prefix', () => { const emptyDe = suggestCommands('', 'de').map((command) => command.id); const emptyEn = suggestCommands('', 'en').map((command) => command.id); const expectedIds: CommandId[] = [...COMMAND_IDS]; expect(emptyDe).toEqual(expectedIds); expect(emptyEn).toEqual(expectedIds); expect(suggestCommands('c', 'en').map((command) => command.id)).toEqual([ 'cv', 'contact', 'clear', 'close', ]); expect(suggestCommands('lei', 'de').map((command) => command.id)).toEqual(['servicesAi']); expect(suggestCommands('c', 'de')).toEqual(suggestCommands('c', 'de')); }); });