UNPKG

safe-commander-mcp

Version:

A secure MCP server for executing whitelisted development commands with comprehensive security controls and resource limits

90 lines (89 loc) 5.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const command_registry_1 = require("../../src/commands/command-registry"); describe('Command Registry', () => { describe('COMMAND_CATEGORIES', () => { test('should have defined command categories', () => { expect(command_registry_1.COMMAND_CATEGORIES).toBeDefined(); expect(command_registry_1.COMMAND_CATEGORIES.PACKAGE_MANAGERS).toContain('npm'); expect(command_registry_1.COMMAND_CATEGORIES.VERSION_CONTROL).toContain('git'); expect(command_registry_1.COMMAND_CATEGORIES.FILE_OPERATIONS).toContain('ls'); }); }); describe('getAllowedCommands', () => { test('should return array of allowed commands', () => { const commands = (0, command_registry_1.getAllowedCommands)(); expect(Array.isArray(commands)).toBe(true); expect(commands.length).toBeGreaterThan(0); expect(commands).toContain('npm'); expect(commands).toContain('git'); }); test('should return a copy of the commands array', () => { const commands1 = (0, command_registry_1.getAllowedCommands)(); const commands2 = (0, command_registry_1.getAllowedCommands)(); expect(commands1).not.toBe(commands2); // Different array instances expect(commands1).toEqual(commands2); // Same content }); }); describe('getCommandsByCategory', () => { test('should return commands for valid categories', () => { const packageCommands = (0, command_registry_1.getCommandsByCategory)('PACKAGE_MANAGERS'); const versionControlCommands = (0, command_registry_1.getCommandsByCategory)('VERSION_CONTROL'); expect(Array.isArray(packageCommands)).toBe(true); expect(Array.isArray(versionControlCommands)).toBe(true); // These should only include commands that are both in the category AND allowed expect(packageCommands.every(cmd => (0, command_registry_1.getAllowedCommands)().includes(cmd))).toBe(true); expect(versionControlCommands.every(cmd => (0, command_registry_1.getAllowedCommands)().includes(cmd))).toBe(true); }); test('should filter by allowed commands', () => { // If npm is in both categories and allowed commands, it should appear const allowedCommands = (0, command_registry_1.getAllowedCommands)(); const packageCommands = (0, command_registry_1.getCommandsByCategory)('PACKAGE_MANAGERS'); if (allowedCommands.includes('npm')) { expect(packageCommands).toContain('npm'); } }); }); describe('getCommandCategory', () => { test('should return correct category for known commands', () => { expect((0, command_registry_1.getCommandCategory)('npm')).toBe('PACKAGE_MANAGERS'); expect((0, command_registry_1.getCommandCategory)('git')).toBe('VERSION_CONTROL'); expect((0, command_registry_1.getCommandCategory)('ls')).toBe('FILE_OPERATIONS'); }); test('should return OTHER for unknown commands', () => { expect((0, command_registry_1.getCommandCategory)('unknown_command')).toBe('OTHER'); expect((0, command_registry_1.getCommandCategory)('nonexistent')).toBe('OTHER'); }); test('should handle empty strings', () => { expect((0, command_registry_1.getCommandCategory)('')).toBe('OTHER'); }); }); describe('isCommandAllowed', () => { test('should return true for allowed commands', () => { const allowedCommands = (0, command_registry_1.getAllowedCommands)(); allowedCommands.forEach(command => { expect((0, command_registry_1.isCommandAllowed)(command)).toBe(true); }); }); test('should return false for disallowed commands', () => { expect((0, command_registry_1.isCommandAllowed)('rm')).toBe(false); expect((0, command_registry_1.isCommandAllowed)('sudo')).toBe(false); expect((0, command_registry_1.isCommandAllowed)('unknown')).toBe(false); }); }); describe('getCommandDescription', () => { test('should return a string description', () => { const description = (0, command_registry_1.getCommandDescription)(); expect(typeof description).toBe('string'); expect(description.length).toBeGreaterThan(0); }); test('should include allowed commands in description', () => { const description = (0, command_registry_1.getCommandDescription)(); const allowedCommands = (0, command_registry_1.getAllowedCommands)(); // Description should mention some of the allowed commands allowedCommands.forEach(command => { expect(description).toContain(command); }); }); }); });