feat: enhance commands with multi-subtask support, MCP integration, and update notifications
- Add support for comma-separated subtask IDs in remove-subtask command - Implement MCP configuration in project initialization - Add package update notification system with version comparison - Improve command documentation with boolean flag conventions - Add comprehensive error handling for unknown options - Update help text with better examples and formatting - Implement proper validation for command inputs - Add global error handling patterns with helpful user messages
This commit is contained in:
@@ -526,4 +526,59 @@ describe('Commands Module', () => {
|
||||
expect(mockExit).toHaveBeenCalledWith(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// Test the version comparison utility
|
||||
describe('Version comparison', () => {
|
||||
// Use a dynamic import for the commands module
|
||||
let compareVersions;
|
||||
|
||||
beforeAll(async () => {
|
||||
// Import the function we want to test dynamically
|
||||
const commandsModule = await import('../../scripts/modules/commands.js');
|
||||
compareVersions = commandsModule.compareVersions;
|
||||
});
|
||||
|
||||
test('compareVersions correctly compares semantic versions', () => {
|
||||
expect(compareVersions('1.0.0', '1.0.0')).toBe(0);
|
||||
expect(compareVersions('1.0.0', '1.0.1')).toBe(-1);
|
||||
expect(compareVersions('1.0.1', '1.0.0')).toBe(1);
|
||||
expect(compareVersions('1.0.0', '1.1.0')).toBe(-1);
|
||||
expect(compareVersions('1.1.0', '1.0.0')).toBe(1);
|
||||
expect(compareVersions('1.0.0', '2.0.0')).toBe(-1);
|
||||
expect(compareVersions('2.0.0', '1.0.0')).toBe(1);
|
||||
expect(compareVersions('1.0', '1.0.0')).toBe(0);
|
||||
expect(compareVersions('1.0.0.0', '1.0.0')).toBe(0);
|
||||
expect(compareVersions('1.0.0', '1.0.0.1')).toBe(-1);
|
||||
});
|
||||
});
|
||||
|
||||
// Test the update check functionality
|
||||
describe('Update check', () => {
|
||||
let displayUpgradeNotification;
|
||||
let consoleLogSpy;
|
||||
|
||||
beforeAll(async () => {
|
||||
// Import the function we want to test dynamically
|
||||
const commandsModule = await import('../../scripts/modules/commands.js');
|
||||
displayUpgradeNotification = commandsModule.displayUpgradeNotification;
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
// Spy on console.log
|
||||
consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => {});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
consoleLogSpy.mockRestore();
|
||||
});
|
||||
|
||||
test('displays upgrade notification when newer version is available', () => {
|
||||
// Test displayUpgradeNotification function
|
||||
displayUpgradeNotification('1.0.0', '1.1.0');
|
||||
expect(consoleLogSpy).toHaveBeenCalled();
|
||||
expect(consoleLogSpy.mock.calls[0][0]).toContain('Update Available!');
|
||||
expect(consoleLogSpy.mock.calls[0][0]).toContain('1.0.0');
|
||||
expect(consoleLogSpy.mock.calls[0][0]).toContain('1.1.0');
|
||||
});
|
||||
});
|
||||
@@ -143,4 +143,255 @@ describe('Windsurf Rules File Handling', () => {
|
||||
expect.any(String)
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// New test suite for MCP Configuration Handling
|
||||
describe('MCP Configuration Handling', () => {
|
||||
let tempDir;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
|
||||
// Create a temporary directory for testing
|
||||
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'task-master-test-'));
|
||||
|
||||
// Spy on fs methods
|
||||
jest.spyOn(fs, 'writeFileSync').mockImplementation(() => {});
|
||||
jest.spyOn(fs, 'readFileSync').mockImplementation((filePath) => {
|
||||
if (filePath.toString().includes('mcp.json')) {
|
||||
return JSON.stringify({
|
||||
"mcpServers": {
|
||||
"existing-server": {
|
||||
"command": "node",
|
||||
"args": ["server.js"]
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
return '{}';
|
||||
});
|
||||
jest.spyOn(fs, 'existsSync').mockImplementation((filePath) => {
|
||||
// Return true for specific paths to test different scenarios
|
||||
if (filePath.toString().includes('package.json')) {
|
||||
return true;
|
||||
}
|
||||
// Default to false for other paths
|
||||
return false;
|
||||
});
|
||||
jest.spyOn(fs, 'mkdirSync').mockImplementation(() => {});
|
||||
jest.spyOn(fs, 'copyFileSync').mockImplementation(() => {});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
// Clean up the temporary directory
|
||||
try {
|
||||
fs.rmSync(tempDir, { recursive: true, force: true });
|
||||
} catch (err) {
|
||||
console.error(`Error cleaning up: ${err.message}`);
|
||||
}
|
||||
});
|
||||
|
||||
// Test function that simulates the behavior of setupMCPConfiguration
|
||||
function mockSetupMCPConfiguration(targetDir, projectName) {
|
||||
const mcpDirPath = path.join(targetDir, '.cursor');
|
||||
const mcpJsonPath = path.join(mcpDirPath, 'mcp.json');
|
||||
|
||||
// Create .cursor directory if it doesn't exist
|
||||
if (!fs.existsSync(mcpDirPath)) {
|
||||
fs.mkdirSync(mcpDirPath, { recursive: true });
|
||||
}
|
||||
|
||||
// New MCP config to be added - references the installed package
|
||||
const newMCPServer = {
|
||||
"task-master-ai": {
|
||||
"command": "npx",
|
||||
"args": [
|
||||
"task-master-ai",
|
||||
"mcp-server"
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
// Check if mcp.json already exists
|
||||
if (fs.existsSync(mcpJsonPath)) {
|
||||
try {
|
||||
// Read existing config
|
||||
const mcpConfig = JSON.parse(fs.readFileSync(mcpJsonPath, 'utf8'));
|
||||
|
||||
// Initialize mcpServers if it doesn't exist
|
||||
if (!mcpConfig.mcpServers) {
|
||||
mcpConfig.mcpServers = {};
|
||||
}
|
||||
|
||||
// Add the taskmaster-ai server if it doesn't exist
|
||||
if (!mcpConfig.mcpServers["task-master-ai"]) {
|
||||
mcpConfig.mcpServers["task-master-ai"] = newMCPServer["task-master-ai"];
|
||||
}
|
||||
|
||||
// Write the updated configuration
|
||||
fs.writeFileSync(
|
||||
mcpJsonPath,
|
||||
JSON.stringify(mcpConfig, null, 4)
|
||||
);
|
||||
} catch (error) {
|
||||
// Create new configuration on error
|
||||
const newMCPConfig = {
|
||||
"mcpServers": newMCPServer
|
||||
};
|
||||
|
||||
fs.writeFileSync(mcpJsonPath, JSON.stringify(newMCPConfig, null, 4));
|
||||
}
|
||||
} else {
|
||||
// If mcp.json doesn't exist, create it
|
||||
const newMCPConfig = {
|
||||
"mcpServers": newMCPServer
|
||||
};
|
||||
|
||||
fs.writeFileSync(mcpJsonPath, JSON.stringify(newMCPConfig, null, 4));
|
||||
}
|
||||
}
|
||||
|
||||
test('creates mcp.json when it does not exist', () => {
|
||||
// Arrange
|
||||
const mcpJsonPath = path.join(tempDir, '.cursor', 'mcp.json');
|
||||
|
||||
// Act
|
||||
mockSetupMCPConfiguration(tempDir, 'test-project');
|
||||
|
||||
// Assert
|
||||
expect(fs.writeFileSync).toHaveBeenCalledWith(
|
||||
mcpJsonPath,
|
||||
expect.stringContaining('task-master-ai')
|
||||
);
|
||||
|
||||
// Should create a proper structure with mcpServers key
|
||||
expect(fs.writeFileSync).toHaveBeenCalledWith(
|
||||
mcpJsonPath,
|
||||
expect.stringContaining('mcpServers')
|
||||
);
|
||||
|
||||
// Should reference npx command
|
||||
expect(fs.writeFileSync).toHaveBeenCalledWith(
|
||||
mcpJsonPath,
|
||||
expect.stringContaining('npx')
|
||||
);
|
||||
});
|
||||
|
||||
test('updates existing mcp.json by adding new server', () => {
|
||||
// Arrange
|
||||
const mcpJsonPath = path.join(tempDir, '.cursor', 'mcp.json');
|
||||
|
||||
// Override the existsSync mock to simulate mcp.json exists
|
||||
fs.existsSync.mockImplementation((filePath) => {
|
||||
if (filePath.toString().includes('mcp.json')) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
// Act
|
||||
mockSetupMCPConfiguration(tempDir, 'test-project');
|
||||
|
||||
// Assert
|
||||
// Should preserve existing server
|
||||
expect(fs.writeFileSync).toHaveBeenCalledWith(
|
||||
mcpJsonPath,
|
||||
expect.stringContaining('existing-server')
|
||||
);
|
||||
|
||||
// Should add our new server
|
||||
expect(fs.writeFileSync).toHaveBeenCalledWith(
|
||||
mcpJsonPath,
|
||||
expect.stringContaining('task-master-ai')
|
||||
);
|
||||
});
|
||||
|
||||
test('handles JSON parsing errors by creating new mcp.json', () => {
|
||||
// Arrange
|
||||
const mcpJsonPath = path.join(tempDir, '.cursor', 'mcp.json');
|
||||
|
||||
// Override existsSync to say mcp.json exists
|
||||
fs.existsSync.mockImplementation((filePath) => {
|
||||
if (filePath.toString().includes('mcp.json')) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
// But make readFileSync return invalid JSON
|
||||
fs.readFileSync.mockImplementation((filePath) => {
|
||||
if (filePath.toString().includes('mcp.json')) {
|
||||
return '{invalid json';
|
||||
}
|
||||
return '{}';
|
||||
});
|
||||
|
||||
// Act
|
||||
mockSetupMCPConfiguration(tempDir, 'test-project');
|
||||
|
||||
// Assert
|
||||
// Should create a new valid JSON file with our server
|
||||
expect(fs.writeFileSync).toHaveBeenCalledWith(
|
||||
mcpJsonPath,
|
||||
expect.stringContaining('task-master-ai')
|
||||
);
|
||||
});
|
||||
|
||||
test('does not modify existing server configuration if it already exists', () => {
|
||||
// Arrange
|
||||
const mcpJsonPath = path.join(tempDir, '.cursor', 'mcp.json');
|
||||
|
||||
// Override existsSync to say mcp.json exists
|
||||
fs.existsSync.mockImplementation((filePath) => {
|
||||
if (filePath.toString().includes('mcp.json')) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
// Return JSON that already has task-master-ai
|
||||
fs.readFileSync.mockImplementation((filePath) => {
|
||||
if (filePath.toString().includes('mcp.json')) {
|
||||
return JSON.stringify({
|
||||
"mcpServers": {
|
||||
"existing-server": {
|
||||
"command": "node",
|
||||
"args": ["server.js"]
|
||||
},
|
||||
"task-master-ai": {
|
||||
"command": "custom",
|
||||
"args": ["custom-args"]
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
return '{}';
|
||||
});
|
||||
|
||||
// Spy to check what's written
|
||||
const writeFileSyncSpy = jest.spyOn(fs, 'writeFileSync');
|
||||
|
||||
// Act
|
||||
mockSetupMCPConfiguration(tempDir, 'test-project');
|
||||
|
||||
// Assert
|
||||
// Verify the written data contains the original taskmaster configuration
|
||||
const dataWritten = JSON.parse(writeFileSyncSpy.mock.calls[0][1]);
|
||||
expect(dataWritten.mcpServers["task-master-ai"].command).toBe("custom");
|
||||
expect(dataWritten.mcpServers["task-master-ai"].args).toContain("custom-args");
|
||||
});
|
||||
|
||||
test('creates the .cursor directory if it doesnt exist', () => {
|
||||
// Arrange
|
||||
const cursorDirPath = path.join(tempDir, '.cursor');
|
||||
|
||||
// Make sure it looks like the directory doesn't exist
|
||||
fs.existsSync.mockReturnValue(false);
|
||||
|
||||
// Act
|
||||
mockSetupMCPConfiguration(tempDir, 'test-project');
|
||||
|
||||
// Assert
|
||||
expect(fs.mkdirSync).toHaveBeenCalledWith(cursorDirPath, { recursive: true });
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user