claude-task-master/tests/unit/commands.test.js

915 lines
26 KiB
JavaScript
Raw Normal View History

feat: Enhance Task Master CLI with Testing Framework, Perplexity AI Integration, and Refactored Core Logic This commit introduces significant enhancements and refactoring to the Task Master CLI, focusing on improved testing, integration with Perplexity AI for research-backed task updates, and core logic refactoring for better maintainability and functionality. **Testing Infrastructure Setup:** - Implemented Jest as the primary testing framework, setting up a comprehensive testing environment. - Added new test scripts to including , , and for streamlined testing workflows. - Integrated necessary devDependencies for testing, such as , , , , and , to support unit, integration, and end-to-end testing. **Dependency Updates:** - Updated and to reflect the latest dependency versions, ensuring project stability and access to the newest features and security patches. - Upgraded to version 0.9.16 and usage: openai [-h] [-v] [-b API_BASE] [-k API_KEY] [-p PROXY [PROXY ...]] [-o ORGANIZATION] [-t {openai,azure}] [--api-version API_VERSION] [--azure-endpoint AZURE_ENDPOINT] [--azure-ad-token AZURE_AD_TOKEN] [-V] {api,tools,migrate,grit} ... positional arguments: {api,tools,migrate,grit} api Direct API calls tools Client side tools for convenience options: -h, --help show this help message and exit -v, --verbose Set verbosity. -b, --api-base API_BASE What API base url to use. -k, --api-key API_KEY What API key to use. -p, --proxy PROXY [PROXY ...] What proxy to use. -o, --organization ORGANIZATION Which organization to run as (will use your default organization if not specified) -t, --api-type {openai,azure} The backend API to call, must be `openai` or `azure` --api-version API_VERSION The Azure API version, e.g. 'https://learn.microsoft.com/en-us/azure/ai- services/openai/reference#rest-api-versioning' --azure-endpoint AZURE_ENDPOINT The Azure endpoint, e.g. 'https://endpoint.openai.azure.com' --azure-ad-token AZURE_AD_TOKEN A token from Azure Active Directory, https://www.microsoft.com/en- us/security/business/identity-access/microsoft-entra- id -V, --version show program's version number and exit to 4.89.0. - Added dependency (version 2.3.0) and updated related dependencies to their latest versions. **Perplexity AI Integration for Research-Backed Updates:** - Introduced an option to leverage Perplexity AI for task updates, enabling research-backed enhancements to task details. - Implemented logic to initialize a Perplexity AI client if the environment variable is available. - Modified the function to accept a parameter, allowing dynamic selection between Perplexity AI and Claude AI for task updates based on API key availability and user preference. - Enhanced to handle responses from Perplexity AI and update tasks accordingly, including improved error handling and logging for robust operation. **Core Logic Refactoring and Improvements:** - Refactored the function to utilize task IDs instead of dependency IDs, ensuring consistency and clarity in dependency management. - Implemented a new function to rigorously check for both circular dependencies and self-dependencies within tasks, improving task relationship integrity. - Enhanced UI elements in : - Refactored to incorporate icons for different task statuses and utilize a object for color mapping, improving visual representation of task status. - Updated to display colored complexity scores with emojis, providing a more intuitive and visually appealing representation of task complexity. - Refactored the task data structure creation and validation process: - Updated the JSON Schema for to reflect a more streamlined and efficient task structure. - Implemented Task Model Classes for better data modeling and type safety. - Improved File System Operations for task data management. - Developed robust Validation Functions and an Error Handling System to ensure data integrity and application stability. **Testing Guidelines Implementation:** - Implemented guidelines for writing testable code when developing new features, promoting a test-driven development approach. - Added testing requirements and best practices for unit, integration, and edge case testing to ensure comprehensive test coverage. - Updated the development workflow to mandate writing tests before proceeding with configuration and documentation updates, reinforcing the importance of testing throughout the development lifecycle. This commit collectively enhances the Task Master CLI's reliability, functionality, and developer experience through improved testing practices, AI-powered research capabilities, and a more robust and maintainable codebase.
2025-03-24 13:28:08 -04:00
/**
* Commands module tests
*/
import { jest } from '@jest/globals';
import { sampleTasks, emptySampleTasks } from '../../tests/fixtures/sample-tasks.js';
feat: Enhance Task Master CLI with Testing Framework, Perplexity AI Integration, and Refactored Core Logic This commit introduces significant enhancements and refactoring to the Task Master CLI, focusing on improved testing, integration with Perplexity AI for research-backed task updates, and core logic refactoring for better maintainability and functionality. **Testing Infrastructure Setup:** - Implemented Jest as the primary testing framework, setting up a comprehensive testing environment. - Added new test scripts to including , , and for streamlined testing workflows. - Integrated necessary devDependencies for testing, such as , , , , and , to support unit, integration, and end-to-end testing. **Dependency Updates:** - Updated and to reflect the latest dependency versions, ensuring project stability and access to the newest features and security patches. - Upgraded to version 0.9.16 and usage: openai [-h] [-v] [-b API_BASE] [-k API_KEY] [-p PROXY [PROXY ...]] [-o ORGANIZATION] [-t {openai,azure}] [--api-version API_VERSION] [--azure-endpoint AZURE_ENDPOINT] [--azure-ad-token AZURE_AD_TOKEN] [-V] {api,tools,migrate,grit} ... positional arguments: {api,tools,migrate,grit} api Direct API calls tools Client side tools for convenience options: -h, --help show this help message and exit -v, --verbose Set verbosity. -b, --api-base API_BASE What API base url to use. -k, --api-key API_KEY What API key to use. -p, --proxy PROXY [PROXY ...] What proxy to use. -o, --organization ORGANIZATION Which organization to run as (will use your default organization if not specified) -t, --api-type {openai,azure} The backend API to call, must be `openai` or `azure` --api-version API_VERSION The Azure API version, e.g. 'https://learn.microsoft.com/en-us/azure/ai- services/openai/reference#rest-api-versioning' --azure-endpoint AZURE_ENDPOINT The Azure endpoint, e.g. 'https://endpoint.openai.azure.com' --azure-ad-token AZURE_AD_TOKEN A token from Azure Active Directory, https://www.microsoft.com/en- us/security/business/identity-access/microsoft-entra- id -V, --version show program's version number and exit to 4.89.0. - Added dependency (version 2.3.0) and updated related dependencies to their latest versions. **Perplexity AI Integration for Research-Backed Updates:** - Introduced an option to leverage Perplexity AI for task updates, enabling research-backed enhancements to task details. - Implemented logic to initialize a Perplexity AI client if the environment variable is available. - Modified the function to accept a parameter, allowing dynamic selection between Perplexity AI and Claude AI for task updates based on API key availability and user preference. - Enhanced to handle responses from Perplexity AI and update tasks accordingly, including improved error handling and logging for robust operation. **Core Logic Refactoring and Improvements:** - Refactored the function to utilize task IDs instead of dependency IDs, ensuring consistency and clarity in dependency management. - Implemented a new function to rigorously check for both circular dependencies and self-dependencies within tasks, improving task relationship integrity. - Enhanced UI elements in : - Refactored to incorporate icons for different task statuses and utilize a object for color mapping, improving visual representation of task status. - Updated to display colored complexity scores with emojis, providing a more intuitive and visually appealing representation of task complexity. - Refactored the task data structure creation and validation process: - Updated the JSON Schema for to reflect a more streamlined and efficient task structure. - Implemented Task Model Classes for better data modeling and type safety. - Improved File System Operations for task data management. - Developed robust Validation Functions and an Error Handling System to ensure data integrity and application stability. **Testing Guidelines Implementation:** - Implemented guidelines for writing testable code when developing new features, promoting a test-driven development approach. - Added testing requirements and best practices for unit, integration, and edge case testing to ensure comprehensive test coverage. - Updated the development workflow to mandate writing tests before proceeding with configuration and documentation updates, reinforcing the importance of testing throughout the development lifecycle. This commit collectively enhances the Task Master CLI's reliability, functionality, and developer experience through improved testing practices, AI-powered research capabilities, and a more robust and maintainable codebase.
2025-03-24 13:28:08 -04:00
// Mock functions that need jest.fn methods
const mockParsePRD = jest.fn().mockResolvedValue(undefined);
const mockUpdateTaskById = jest.fn().mockResolvedValue({
2025-04-09 00:25:27 +02:00
id: 2,
title: 'Updated Task',
description: 'Updated description'
});
const mockDisplayBanner = jest.fn();
const mockDisplayHelp = jest.fn();
const mockLog = jest.fn();
// Mock modules first
jest.mock('fs', () => ({
2025-04-09 00:25:27 +02:00
existsSync: jest.fn(),
readFileSync: jest.fn()
}));
jest.mock('path', () => ({
2025-04-09 00:25:27 +02:00
join: jest.fn((dir, file) => `${dir}/${file}`)
}));
jest.mock('chalk', () => ({
2025-04-09 00:25:27 +02:00
red: jest.fn((text) => text),
blue: jest.fn((text) => text),
green: jest.fn((text) => text),
yellow: jest.fn((text) => text),
white: jest.fn((text) => ({
bold: jest.fn((text) => text)
})),
reset: jest.fn((text) => text)
}));
feat: Enhance Task Master CLI with Testing Framework, Perplexity AI Integration, and Refactored Core Logic This commit introduces significant enhancements and refactoring to the Task Master CLI, focusing on improved testing, integration with Perplexity AI for research-backed task updates, and core logic refactoring for better maintainability and functionality. **Testing Infrastructure Setup:** - Implemented Jest as the primary testing framework, setting up a comprehensive testing environment. - Added new test scripts to including , , and for streamlined testing workflows. - Integrated necessary devDependencies for testing, such as , , , , and , to support unit, integration, and end-to-end testing. **Dependency Updates:** - Updated and to reflect the latest dependency versions, ensuring project stability and access to the newest features and security patches. - Upgraded to version 0.9.16 and usage: openai [-h] [-v] [-b API_BASE] [-k API_KEY] [-p PROXY [PROXY ...]] [-o ORGANIZATION] [-t {openai,azure}] [--api-version API_VERSION] [--azure-endpoint AZURE_ENDPOINT] [--azure-ad-token AZURE_AD_TOKEN] [-V] {api,tools,migrate,grit} ... positional arguments: {api,tools,migrate,grit} api Direct API calls tools Client side tools for convenience options: -h, --help show this help message and exit -v, --verbose Set verbosity. -b, --api-base API_BASE What API base url to use. -k, --api-key API_KEY What API key to use. -p, --proxy PROXY [PROXY ...] What proxy to use. -o, --organization ORGANIZATION Which organization to run as (will use your default organization if not specified) -t, --api-type {openai,azure} The backend API to call, must be `openai` or `azure` --api-version API_VERSION The Azure API version, e.g. 'https://learn.microsoft.com/en-us/azure/ai- services/openai/reference#rest-api-versioning' --azure-endpoint AZURE_ENDPOINT The Azure endpoint, e.g. 'https://endpoint.openai.azure.com' --azure-ad-token AZURE_AD_TOKEN A token from Azure Active Directory, https://www.microsoft.com/en- us/security/business/identity-access/microsoft-entra- id -V, --version show program's version number and exit to 4.89.0. - Added dependency (version 2.3.0) and updated related dependencies to their latest versions. **Perplexity AI Integration for Research-Backed Updates:** - Introduced an option to leverage Perplexity AI for task updates, enabling research-backed enhancements to task details. - Implemented logic to initialize a Perplexity AI client if the environment variable is available. - Modified the function to accept a parameter, allowing dynamic selection between Perplexity AI and Claude AI for task updates based on API key availability and user preference. - Enhanced to handle responses from Perplexity AI and update tasks accordingly, including improved error handling and logging for robust operation. **Core Logic Refactoring and Improvements:** - Refactored the function to utilize task IDs instead of dependency IDs, ensuring consistency and clarity in dependency management. - Implemented a new function to rigorously check for both circular dependencies and self-dependencies within tasks, improving task relationship integrity. - Enhanced UI elements in : - Refactored to incorporate icons for different task statuses and utilize a object for color mapping, improving visual representation of task status. - Updated to display colored complexity scores with emojis, providing a more intuitive and visually appealing representation of task complexity. - Refactored the task data structure creation and validation process: - Updated the JSON Schema for to reflect a more streamlined and efficient task structure. - Implemented Task Model Classes for better data modeling and type safety. - Improved File System Operations for task data management. - Developed robust Validation Functions and an Error Handling System to ensure data integrity and application stability. **Testing Guidelines Implementation:** - Implemented guidelines for writing testable code when developing new features, promoting a test-driven development approach. - Added testing requirements and best practices for unit, integration, and edge case testing to ensure comprehensive test coverage. - Updated the development workflow to mandate writing tests before proceeding with configuration and documentation updates, reinforcing the importance of testing throughout the development lifecycle. This commit collectively enhances the Task Master CLI's reliability, functionality, and developer experience through improved testing practices, AI-powered research capabilities, and a more robust and maintainable codebase.
2025-03-24 13:28:08 -04:00
jest.mock('../../scripts/modules/ui.js', () => ({
2025-04-09 00:25:27 +02:00
displayBanner: mockDisplayBanner,
displayHelp: mockDisplayHelp
}));
jest.mock('../../scripts/modules/task-manager.js', () => ({
2025-04-09 00:25:27 +02:00
parsePRD: mockParsePRD,
updateTaskById: mockUpdateTaskById
feat: Enhance Task Master CLI with Testing Framework, Perplexity AI Integration, and Refactored Core Logic This commit introduces significant enhancements and refactoring to the Task Master CLI, focusing on improved testing, integration with Perplexity AI for research-backed task updates, and core logic refactoring for better maintainability and functionality. **Testing Infrastructure Setup:** - Implemented Jest as the primary testing framework, setting up a comprehensive testing environment. - Added new test scripts to including , , and for streamlined testing workflows. - Integrated necessary devDependencies for testing, such as , , , , and , to support unit, integration, and end-to-end testing. **Dependency Updates:** - Updated and to reflect the latest dependency versions, ensuring project stability and access to the newest features and security patches. - Upgraded to version 0.9.16 and usage: openai [-h] [-v] [-b API_BASE] [-k API_KEY] [-p PROXY [PROXY ...]] [-o ORGANIZATION] [-t {openai,azure}] [--api-version API_VERSION] [--azure-endpoint AZURE_ENDPOINT] [--azure-ad-token AZURE_AD_TOKEN] [-V] {api,tools,migrate,grit} ... positional arguments: {api,tools,migrate,grit} api Direct API calls tools Client side tools for convenience options: -h, --help show this help message and exit -v, --verbose Set verbosity. -b, --api-base API_BASE What API base url to use. -k, --api-key API_KEY What API key to use. -p, --proxy PROXY [PROXY ...] What proxy to use. -o, --organization ORGANIZATION Which organization to run as (will use your default organization if not specified) -t, --api-type {openai,azure} The backend API to call, must be `openai` or `azure` --api-version API_VERSION The Azure API version, e.g. 'https://learn.microsoft.com/en-us/azure/ai- services/openai/reference#rest-api-versioning' --azure-endpoint AZURE_ENDPOINT The Azure endpoint, e.g. 'https://endpoint.openai.azure.com' --azure-ad-token AZURE_AD_TOKEN A token from Azure Active Directory, https://www.microsoft.com/en- us/security/business/identity-access/microsoft-entra- id -V, --version show program's version number and exit to 4.89.0. - Added dependency (version 2.3.0) and updated related dependencies to their latest versions. **Perplexity AI Integration for Research-Backed Updates:** - Introduced an option to leverage Perplexity AI for task updates, enabling research-backed enhancements to task details. - Implemented logic to initialize a Perplexity AI client if the environment variable is available. - Modified the function to accept a parameter, allowing dynamic selection between Perplexity AI and Claude AI for task updates based on API key availability and user preference. - Enhanced to handle responses from Perplexity AI and update tasks accordingly, including improved error handling and logging for robust operation. **Core Logic Refactoring and Improvements:** - Refactored the function to utilize task IDs instead of dependency IDs, ensuring consistency and clarity in dependency management. - Implemented a new function to rigorously check for both circular dependencies and self-dependencies within tasks, improving task relationship integrity. - Enhanced UI elements in : - Refactored to incorporate icons for different task statuses and utilize a object for color mapping, improving visual representation of task status. - Updated to display colored complexity scores with emojis, providing a more intuitive and visually appealing representation of task complexity. - Refactored the task data structure creation and validation process: - Updated the JSON Schema for to reflect a more streamlined and efficient task structure. - Implemented Task Model Classes for better data modeling and type safety. - Improved File System Operations for task data management. - Developed robust Validation Functions and an Error Handling System to ensure data integrity and application stability. **Testing Guidelines Implementation:** - Implemented guidelines for writing testable code when developing new features, promoting a test-driven development approach. - Added testing requirements and best practices for unit, integration, and edge case testing to ensure comprehensive test coverage. - Updated the development workflow to mandate writing tests before proceeding with configuration and documentation updates, reinforcing the importance of testing throughout the development lifecycle. This commit collectively enhances the Task Master CLI's reliability, functionality, and developer experience through improved testing practices, AI-powered research capabilities, and a more robust and maintainable codebase.
2025-03-24 13:28:08 -04:00
}));
// Add this function before the mock of utils.js
/**
* Convert camelCase to kebab-case
* @param {string} str - String to convert
* @returns {string} kebab-case version of the input
*/
const toKebabCase = (str) => {
2025-04-09 00:25:27 +02:00
return str
.replace(/([a-z0-9])([A-Z])/g, '$1-$2')
.toLowerCase()
.replace(/^-/, ''); // Remove leading hyphen if present
};
/**
* Detect camelCase flags in command arguments
* @param {string[]} args - Command line arguments to check
* @returns {Array<{original: string, kebabCase: string}>} - List of flags that should be converted
*/
function detectCamelCaseFlags(args) {
2025-04-09 00:25:27 +02:00
const camelCaseFlags = [];
for (const arg of args) {
if (arg.startsWith('--')) {
const flagName = arg.split('=')[0].slice(2); // Remove -- and anything after =
// Skip if it's a single word (no hyphens) or already in kebab-case
if (!flagName.includes('-')) {
// Check for camelCase pattern (lowercase followed by uppercase)
if (/[a-z][A-Z]/.test(flagName)) {
const kebabVersion = toKebabCase(flagName);
if (kebabVersion !== flagName) {
camelCaseFlags.push({
original: flagName,
kebabCase: kebabVersion
});
}
}
}
}
}
return camelCaseFlags;
}
// Then update the utils.js mock to include these functions
feat: Enhance Task Master CLI with Testing Framework, Perplexity AI Integration, and Refactored Core Logic This commit introduces significant enhancements and refactoring to the Task Master CLI, focusing on improved testing, integration with Perplexity AI for research-backed task updates, and core logic refactoring for better maintainability and functionality. **Testing Infrastructure Setup:** - Implemented Jest as the primary testing framework, setting up a comprehensive testing environment. - Added new test scripts to including , , and for streamlined testing workflows. - Integrated necessary devDependencies for testing, such as , , , , and , to support unit, integration, and end-to-end testing. **Dependency Updates:** - Updated and to reflect the latest dependency versions, ensuring project stability and access to the newest features and security patches. - Upgraded to version 0.9.16 and usage: openai [-h] [-v] [-b API_BASE] [-k API_KEY] [-p PROXY [PROXY ...]] [-o ORGANIZATION] [-t {openai,azure}] [--api-version API_VERSION] [--azure-endpoint AZURE_ENDPOINT] [--azure-ad-token AZURE_AD_TOKEN] [-V] {api,tools,migrate,grit} ... positional arguments: {api,tools,migrate,grit} api Direct API calls tools Client side tools for convenience options: -h, --help show this help message and exit -v, --verbose Set verbosity. -b, --api-base API_BASE What API base url to use. -k, --api-key API_KEY What API key to use. -p, --proxy PROXY [PROXY ...] What proxy to use. -o, --organization ORGANIZATION Which organization to run as (will use your default organization if not specified) -t, --api-type {openai,azure} The backend API to call, must be `openai` or `azure` --api-version API_VERSION The Azure API version, e.g. 'https://learn.microsoft.com/en-us/azure/ai- services/openai/reference#rest-api-versioning' --azure-endpoint AZURE_ENDPOINT The Azure endpoint, e.g. 'https://endpoint.openai.azure.com' --azure-ad-token AZURE_AD_TOKEN A token from Azure Active Directory, https://www.microsoft.com/en- us/security/business/identity-access/microsoft-entra- id -V, --version show program's version number and exit to 4.89.0. - Added dependency (version 2.3.0) and updated related dependencies to their latest versions. **Perplexity AI Integration for Research-Backed Updates:** - Introduced an option to leverage Perplexity AI for task updates, enabling research-backed enhancements to task details. - Implemented logic to initialize a Perplexity AI client if the environment variable is available. - Modified the function to accept a parameter, allowing dynamic selection between Perplexity AI and Claude AI for task updates based on API key availability and user preference. - Enhanced to handle responses from Perplexity AI and update tasks accordingly, including improved error handling and logging for robust operation. **Core Logic Refactoring and Improvements:** - Refactored the function to utilize task IDs instead of dependency IDs, ensuring consistency and clarity in dependency management. - Implemented a new function to rigorously check for both circular dependencies and self-dependencies within tasks, improving task relationship integrity. - Enhanced UI elements in : - Refactored to incorporate icons for different task statuses and utilize a object for color mapping, improving visual representation of task status. - Updated to display colored complexity scores with emojis, providing a more intuitive and visually appealing representation of task complexity. - Refactored the task data structure creation and validation process: - Updated the JSON Schema for to reflect a more streamlined and efficient task structure. - Implemented Task Model Classes for better data modeling and type safety. - Improved File System Operations for task data management. - Developed robust Validation Functions and an Error Handling System to ensure data integrity and application stability. **Testing Guidelines Implementation:** - Implemented guidelines for writing testable code when developing new features, promoting a test-driven development approach. - Added testing requirements and best practices for unit, integration, and edge case testing to ensure comprehensive test coverage. - Updated the development workflow to mandate writing tests before proceeding with configuration and documentation updates, reinforcing the importance of testing throughout the development lifecycle. This commit collectively enhances the Task Master CLI's reliability, functionality, and developer experience through improved testing practices, AI-powered research capabilities, and a more robust and maintainable codebase.
2025-03-24 13:28:08 -04:00
jest.mock('../../scripts/modules/utils.js', () => ({
2025-04-09 00:25:27 +02:00
CONFIG: {
projectVersion: '1.5.0'
},
log: mockLog,
toKebabCase: toKebabCase,
detectCamelCaseFlags: detectCamelCaseFlags
feat: Enhance Task Master CLI with Testing Framework, Perplexity AI Integration, and Refactored Core Logic This commit introduces significant enhancements and refactoring to the Task Master CLI, focusing on improved testing, integration with Perplexity AI for research-backed task updates, and core logic refactoring for better maintainability and functionality. **Testing Infrastructure Setup:** - Implemented Jest as the primary testing framework, setting up a comprehensive testing environment. - Added new test scripts to including , , and for streamlined testing workflows. - Integrated necessary devDependencies for testing, such as , , , , and , to support unit, integration, and end-to-end testing. **Dependency Updates:** - Updated and to reflect the latest dependency versions, ensuring project stability and access to the newest features and security patches. - Upgraded to version 0.9.16 and usage: openai [-h] [-v] [-b API_BASE] [-k API_KEY] [-p PROXY [PROXY ...]] [-o ORGANIZATION] [-t {openai,azure}] [--api-version API_VERSION] [--azure-endpoint AZURE_ENDPOINT] [--azure-ad-token AZURE_AD_TOKEN] [-V] {api,tools,migrate,grit} ... positional arguments: {api,tools,migrate,grit} api Direct API calls tools Client side tools for convenience options: -h, --help show this help message and exit -v, --verbose Set verbosity. -b, --api-base API_BASE What API base url to use. -k, --api-key API_KEY What API key to use. -p, --proxy PROXY [PROXY ...] What proxy to use. -o, --organization ORGANIZATION Which organization to run as (will use your default organization if not specified) -t, --api-type {openai,azure} The backend API to call, must be `openai` or `azure` --api-version API_VERSION The Azure API version, e.g. 'https://learn.microsoft.com/en-us/azure/ai- services/openai/reference#rest-api-versioning' --azure-endpoint AZURE_ENDPOINT The Azure endpoint, e.g. 'https://endpoint.openai.azure.com' --azure-ad-token AZURE_AD_TOKEN A token from Azure Active Directory, https://www.microsoft.com/en- us/security/business/identity-access/microsoft-entra- id -V, --version show program's version number and exit to 4.89.0. - Added dependency (version 2.3.0) and updated related dependencies to their latest versions. **Perplexity AI Integration for Research-Backed Updates:** - Introduced an option to leverage Perplexity AI for task updates, enabling research-backed enhancements to task details. - Implemented logic to initialize a Perplexity AI client if the environment variable is available. - Modified the function to accept a parameter, allowing dynamic selection between Perplexity AI and Claude AI for task updates based on API key availability and user preference. - Enhanced to handle responses from Perplexity AI and update tasks accordingly, including improved error handling and logging for robust operation. **Core Logic Refactoring and Improvements:** - Refactored the function to utilize task IDs instead of dependency IDs, ensuring consistency and clarity in dependency management. - Implemented a new function to rigorously check for both circular dependencies and self-dependencies within tasks, improving task relationship integrity. - Enhanced UI elements in : - Refactored to incorporate icons for different task statuses and utilize a object for color mapping, improving visual representation of task status. - Updated to display colored complexity scores with emojis, providing a more intuitive and visually appealing representation of task complexity. - Refactored the task data structure creation and validation process: - Updated the JSON Schema for to reflect a more streamlined and efficient task structure. - Implemented Task Model Classes for better data modeling and type safety. - Improved File System Operations for task data management. - Developed robust Validation Functions and an Error Handling System to ensure data integrity and application stability. **Testing Guidelines Implementation:** - Implemented guidelines for writing testable code when developing new features, promoting a test-driven development approach. - Added testing requirements and best practices for unit, integration, and edge case testing to ensure comprehensive test coverage. - Updated the development workflow to mandate writing tests before proceeding with configuration and documentation updates, reinforcing the importance of testing throughout the development lifecycle. This commit collectively enhances the Task Master CLI's reliability, functionality, and developer experience through improved testing practices, AI-powered research capabilities, and a more robust and maintainable codebase.
2025-03-24 13:28:08 -04:00
}));
// Import all modules after mocking
feat: Enhance Task Master CLI with Testing Framework, Perplexity AI Integration, and Refactored Core Logic This commit introduces significant enhancements and refactoring to the Task Master CLI, focusing on improved testing, integration with Perplexity AI for research-backed task updates, and core logic refactoring for better maintainability and functionality. **Testing Infrastructure Setup:** - Implemented Jest as the primary testing framework, setting up a comprehensive testing environment. - Added new test scripts to including , , and for streamlined testing workflows. - Integrated necessary devDependencies for testing, such as , , , , and , to support unit, integration, and end-to-end testing. **Dependency Updates:** - Updated and to reflect the latest dependency versions, ensuring project stability and access to the newest features and security patches. - Upgraded to version 0.9.16 and usage: openai [-h] [-v] [-b API_BASE] [-k API_KEY] [-p PROXY [PROXY ...]] [-o ORGANIZATION] [-t {openai,azure}] [--api-version API_VERSION] [--azure-endpoint AZURE_ENDPOINT] [--azure-ad-token AZURE_AD_TOKEN] [-V] {api,tools,migrate,grit} ... positional arguments: {api,tools,migrate,grit} api Direct API calls tools Client side tools for convenience options: -h, --help show this help message and exit -v, --verbose Set verbosity. -b, --api-base API_BASE What API base url to use. -k, --api-key API_KEY What API key to use. -p, --proxy PROXY [PROXY ...] What proxy to use. -o, --organization ORGANIZATION Which organization to run as (will use your default organization if not specified) -t, --api-type {openai,azure} The backend API to call, must be `openai` or `azure` --api-version API_VERSION The Azure API version, e.g. 'https://learn.microsoft.com/en-us/azure/ai- services/openai/reference#rest-api-versioning' --azure-endpoint AZURE_ENDPOINT The Azure endpoint, e.g. 'https://endpoint.openai.azure.com' --azure-ad-token AZURE_AD_TOKEN A token from Azure Active Directory, https://www.microsoft.com/en- us/security/business/identity-access/microsoft-entra- id -V, --version show program's version number and exit to 4.89.0. - Added dependency (version 2.3.0) and updated related dependencies to their latest versions. **Perplexity AI Integration for Research-Backed Updates:** - Introduced an option to leverage Perplexity AI for task updates, enabling research-backed enhancements to task details. - Implemented logic to initialize a Perplexity AI client if the environment variable is available. - Modified the function to accept a parameter, allowing dynamic selection between Perplexity AI and Claude AI for task updates based on API key availability and user preference. - Enhanced to handle responses from Perplexity AI and update tasks accordingly, including improved error handling and logging for robust operation. **Core Logic Refactoring and Improvements:** - Refactored the function to utilize task IDs instead of dependency IDs, ensuring consistency and clarity in dependency management. - Implemented a new function to rigorously check for both circular dependencies and self-dependencies within tasks, improving task relationship integrity. - Enhanced UI elements in : - Refactored to incorporate icons for different task statuses and utilize a object for color mapping, improving visual representation of task status. - Updated to display colored complexity scores with emojis, providing a more intuitive and visually appealing representation of task complexity. - Refactored the task data structure creation and validation process: - Updated the JSON Schema for to reflect a more streamlined and efficient task structure. - Implemented Task Model Classes for better data modeling and type safety. - Improved File System Operations for task data management. - Developed robust Validation Functions and an Error Handling System to ensure data integrity and application stability. **Testing Guidelines Implementation:** - Implemented guidelines for writing testable code when developing new features, promoting a test-driven development approach. - Added testing requirements and best practices for unit, integration, and edge case testing to ensure comprehensive test coverage. - Updated the development workflow to mandate writing tests before proceeding with configuration and documentation updates, reinforcing the importance of testing throughout the development lifecycle. This commit collectively enhances the Task Master CLI's reliability, functionality, and developer experience through improved testing practices, AI-powered research capabilities, and a more robust and maintainable codebase.
2025-03-24 13:28:08 -04:00
import fs from 'fs';
import path from 'path';
import chalk from 'chalk';
import { setupCLI } from '../../scripts/modules/commands.js';
feat: Enhance Task Master CLI with Testing Framework, Perplexity AI Integration, and Refactored Core Logic This commit introduces significant enhancements and refactoring to the Task Master CLI, focusing on improved testing, integration with Perplexity AI for research-backed task updates, and core logic refactoring for better maintainability and functionality. **Testing Infrastructure Setup:** - Implemented Jest as the primary testing framework, setting up a comprehensive testing environment. - Added new test scripts to including , , and for streamlined testing workflows. - Integrated necessary devDependencies for testing, such as , , , , and , to support unit, integration, and end-to-end testing. **Dependency Updates:** - Updated and to reflect the latest dependency versions, ensuring project stability and access to the newest features and security patches. - Upgraded to version 0.9.16 and usage: openai [-h] [-v] [-b API_BASE] [-k API_KEY] [-p PROXY [PROXY ...]] [-o ORGANIZATION] [-t {openai,azure}] [--api-version API_VERSION] [--azure-endpoint AZURE_ENDPOINT] [--azure-ad-token AZURE_AD_TOKEN] [-V] {api,tools,migrate,grit} ... positional arguments: {api,tools,migrate,grit} api Direct API calls tools Client side tools for convenience options: -h, --help show this help message and exit -v, --verbose Set verbosity. -b, --api-base API_BASE What API base url to use. -k, --api-key API_KEY What API key to use. -p, --proxy PROXY [PROXY ...] What proxy to use. -o, --organization ORGANIZATION Which organization to run as (will use your default organization if not specified) -t, --api-type {openai,azure} The backend API to call, must be `openai` or `azure` --api-version API_VERSION The Azure API version, e.g. 'https://learn.microsoft.com/en-us/azure/ai- services/openai/reference#rest-api-versioning' --azure-endpoint AZURE_ENDPOINT The Azure endpoint, e.g. 'https://endpoint.openai.azure.com' --azure-ad-token AZURE_AD_TOKEN A token from Azure Active Directory, https://www.microsoft.com/en- us/security/business/identity-access/microsoft-entra- id -V, --version show program's version number and exit to 4.89.0. - Added dependency (version 2.3.0) and updated related dependencies to their latest versions. **Perplexity AI Integration for Research-Backed Updates:** - Introduced an option to leverage Perplexity AI for task updates, enabling research-backed enhancements to task details. - Implemented logic to initialize a Perplexity AI client if the environment variable is available. - Modified the function to accept a parameter, allowing dynamic selection between Perplexity AI and Claude AI for task updates based on API key availability and user preference. - Enhanced to handle responses from Perplexity AI and update tasks accordingly, including improved error handling and logging for robust operation. **Core Logic Refactoring and Improvements:** - Refactored the function to utilize task IDs instead of dependency IDs, ensuring consistency and clarity in dependency management. - Implemented a new function to rigorously check for both circular dependencies and self-dependencies within tasks, improving task relationship integrity. - Enhanced UI elements in : - Refactored to incorporate icons for different task statuses and utilize a object for color mapping, improving visual representation of task status. - Updated to display colored complexity scores with emojis, providing a more intuitive and visually appealing representation of task complexity. - Refactored the task data structure creation and validation process: - Updated the JSON Schema for to reflect a more streamlined and efficient task structure. - Implemented Task Model Classes for better data modeling and type safety. - Improved File System Operations for task data management. - Developed robust Validation Functions and an Error Handling System to ensure data integrity and application stability. **Testing Guidelines Implementation:** - Implemented guidelines for writing testable code when developing new features, promoting a test-driven development approach. - Added testing requirements and best practices for unit, integration, and edge case testing to ensure comprehensive test coverage. - Updated the development workflow to mandate writing tests before proceeding with configuration and documentation updates, reinforcing the importance of testing throughout the development lifecycle. This commit collectively enhances the Task Master CLI's reliability, functionality, and developer experience through improved testing practices, AI-powered research capabilities, and a more robust and maintainable codebase.
2025-03-24 13:28:08 -04:00
// We'll use a simplified, direct test approach instead of Commander mocking
feat: Enhance Task Master CLI with Testing Framework, Perplexity AI Integration, and Refactored Core Logic This commit introduces significant enhancements and refactoring to the Task Master CLI, focusing on improved testing, integration with Perplexity AI for research-backed task updates, and core logic refactoring for better maintainability and functionality. **Testing Infrastructure Setup:** - Implemented Jest as the primary testing framework, setting up a comprehensive testing environment. - Added new test scripts to including , , and for streamlined testing workflows. - Integrated necessary devDependencies for testing, such as , , , , and , to support unit, integration, and end-to-end testing. **Dependency Updates:** - Updated and to reflect the latest dependency versions, ensuring project stability and access to the newest features and security patches. - Upgraded to version 0.9.16 and usage: openai [-h] [-v] [-b API_BASE] [-k API_KEY] [-p PROXY [PROXY ...]] [-o ORGANIZATION] [-t {openai,azure}] [--api-version API_VERSION] [--azure-endpoint AZURE_ENDPOINT] [--azure-ad-token AZURE_AD_TOKEN] [-V] {api,tools,migrate,grit} ... positional arguments: {api,tools,migrate,grit} api Direct API calls tools Client side tools for convenience options: -h, --help show this help message and exit -v, --verbose Set verbosity. -b, --api-base API_BASE What API base url to use. -k, --api-key API_KEY What API key to use. -p, --proxy PROXY [PROXY ...] What proxy to use. -o, --organization ORGANIZATION Which organization to run as (will use your default organization if not specified) -t, --api-type {openai,azure} The backend API to call, must be `openai` or `azure` --api-version API_VERSION The Azure API version, e.g. 'https://learn.microsoft.com/en-us/azure/ai- services/openai/reference#rest-api-versioning' --azure-endpoint AZURE_ENDPOINT The Azure endpoint, e.g. 'https://endpoint.openai.azure.com' --azure-ad-token AZURE_AD_TOKEN A token from Azure Active Directory, https://www.microsoft.com/en- us/security/business/identity-access/microsoft-entra- id -V, --version show program's version number and exit to 4.89.0. - Added dependency (version 2.3.0) and updated related dependencies to their latest versions. **Perplexity AI Integration for Research-Backed Updates:** - Introduced an option to leverage Perplexity AI for task updates, enabling research-backed enhancements to task details. - Implemented logic to initialize a Perplexity AI client if the environment variable is available. - Modified the function to accept a parameter, allowing dynamic selection between Perplexity AI and Claude AI for task updates based on API key availability and user preference. - Enhanced to handle responses from Perplexity AI and update tasks accordingly, including improved error handling and logging for robust operation. **Core Logic Refactoring and Improvements:** - Refactored the function to utilize task IDs instead of dependency IDs, ensuring consistency and clarity in dependency management. - Implemented a new function to rigorously check for both circular dependencies and self-dependencies within tasks, improving task relationship integrity. - Enhanced UI elements in : - Refactored to incorporate icons for different task statuses and utilize a object for color mapping, improving visual representation of task status. - Updated to display colored complexity scores with emojis, providing a more intuitive and visually appealing representation of task complexity. - Refactored the task data structure creation and validation process: - Updated the JSON Schema for to reflect a more streamlined and efficient task structure. - Implemented Task Model Classes for better data modeling and type safety. - Improved File System Operations for task data management. - Developed robust Validation Functions and an Error Handling System to ensure data integrity and application stability. **Testing Guidelines Implementation:** - Implemented guidelines for writing testable code when developing new features, promoting a test-driven development approach. - Added testing requirements and best practices for unit, integration, and edge case testing to ensure comprehensive test coverage. - Updated the development workflow to mandate writing tests before proceeding with configuration and documentation updates, reinforcing the importance of testing throughout the development lifecycle. This commit collectively enhances the Task Master CLI's reliability, functionality, and developer experience through improved testing practices, AI-powered research capabilities, and a more robust and maintainable codebase.
2025-03-24 13:28:08 -04:00
describe('Commands Module', () => {
2025-04-09 00:25:27 +02:00
// Set up spies on the mocked modules
const mockExistsSync = jest.spyOn(fs, 'existsSync');
const mockReadFileSync = jest.spyOn(fs, 'readFileSync');
const mockJoin = jest.spyOn(path, 'join');
const mockConsoleLog = jest
.spyOn(console, 'log')
.mockImplementation(() => {});
const mockConsoleError = jest
.spyOn(console, 'error')
.mockImplementation(() => {});
const mockExit = jest.spyOn(process, 'exit').mockImplementation(() => {});
beforeEach(() => {
jest.clearAllMocks();
mockExistsSync.mockReturnValue(true);
});
afterAll(() => {
jest.restoreAllMocks();
});
describe('setupCLI function', () => {
test('should return Commander program instance', () => {
const program = setupCLI();
expect(program).toBeDefined();
expect(program.name()).toBe('dev');
});
test('should read version from package.json when available', () => {
mockExistsSync.mockReturnValue(true);
mockReadFileSync.mockReturnValue('{"version": "1.0.0"}');
mockJoin.mockReturnValue('package.json');
const program = setupCLI();
const version = program._version();
expect(mockReadFileSync).toHaveBeenCalledWith('package.json', 'utf8');
expect(version).toBe('1.0.0');
});
test('should use default version when package.json is not available', () => {
mockExistsSync.mockReturnValue(false);
const program = setupCLI();
const version = program._version();
expect(mockReadFileSync).not.toHaveBeenCalled();
expect(version).toBe('1.5.0');
});
test('should use default version when package.json reading throws an error', () => {
mockExistsSync.mockReturnValue(true);
mockReadFileSync.mockImplementation(() => {
throw new Error('Invalid JSON');
});
const program = setupCLI();
const version = program._version();
expect(mockReadFileSync).toHaveBeenCalled();
expect(version).toBe('1.5.0');
});
});
describe('Kebab Case Validation', () => {
test('should detect camelCase flags correctly', () => {
const args = ['node', 'task-master', '--camelCase', '--kebab-case'];
const camelCaseFlags = args.filter(
(arg) =>
arg.startsWith('--') && /[A-Z]/.test(arg) && !arg.includes('-[A-Z]')
);
expect(camelCaseFlags).toContain('--camelCase');
expect(camelCaseFlags).not.toContain('--kebab-case');
});
test('should accept kebab-case flags correctly', () => {
const args = ['node', 'task-master', '--kebab-case'];
const camelCaseFlags = args.filter(
(arg) =>
arg.startsWith('--') && /[A-Z]/.test(arg) && !arg.includes('-[A-Z]')
);
expect(camelCaseFlags).toHaveLength(0);
});
});
describe('parse-prd command', () => {
// Since mocking Commander is complex, we'll test the action handler directly
// Recreate the action handler logic based on commands.js
async function parsePrdAction(file, options) {
// Use input option if file argument not provided
const inputFile = file || options.input;
const defaultPrdPath = 'scripts/prd.txt';
// If no input file specified, check for default PRD location
if (!inputFile) {
if (fs.existsSync(defaultPrdPath)) {
console.log(chalk.blue(`Using default PRD file: ${defaultPrdPath}`));
const numTasks = parseInt(options.numTasks, 10);
const outputPath = options.output;
console.log(chalk.blue(`Generating ${numTasks} tasks...`));
await mockParsePRD(defaultPrdPath, outputPath, numTasks);
return;
}
console.log(
chalk.yellow(
'No PRD file specified and default PRD file not found at scripts/prd.txt.'
)
);
return;
}
const numTasks = parseInt(options.numTasks, 10);
const outputPath = options.output;
console.log(chalk.blue(`Parsing PRD file: ${inputFile}`));
console.log(chalk.blue(`Generating ${numTasks} tasks...`));
await mockParsePRD(inputFile, outputPath, numTasks);
}
beforeEach(() => {
// Reset the parsePRD mock
mockParsePRD.mockClear();
});
test('should use default PRD path when no arguments provided', async () => {
// Arrange
mockExistsSync.mockReturnValue(true);
// Act - call the handler directly with the right params
await parsePrdAction(undefined, {
numTasks: '10',
output: 'tasks/tasks.json'
});
// Assert
expect(mockExistsSync).toHaveBeenCalledWith('scripts/prd.txt');
expect(mockConsoleLog).toHaveBeenCalledWith(
expect.stringContaining('Using default PRD file')
);
expect(mockParsePRD).toHaveBeenCalledWith(
'scripts/prd.txt',
'tasks/tasks.json',
10 // Default value from command definition
);
});
test('should display help when no arguments and no default PRD exists', async () => {
// Arrange
mockExistsSync.mockReturnValue(false);
// Act - call the handler directly with the right params
await parsePrdAction(undefined, {
numTasks: '10',
output: 'tasks/tasks.json'
});
// Assert
expect(mockConsoleLog).toHaveBeenCalledWith(
expect.stringContaining('No PRD file specified')
);
expect(mockParsePRD).not.toHaveBeenCalled();
});
test('should use explicitly provided file path', async () => {
// Arrange
const testFile = 'test/prd.txt';
// Act - call the handler directly with the right params
await parsePrdAction(testFile, {
numTasks: '10',
output: 'tasks/tasks.json'
});
// Assert
expect(mockConsoleLog).toHaveBeenCalledWith(
expect.stringContaining(`Parsing PRD file: ${testFile}`)
);
expect(mockParsePRD).toHaveBeenCalledWith(
testFile,
'tasks/tasks.json',
10
);
expect(mockExistsSync).not.toHaveBeenCalledWith('scripts/prd.txt');
});
test('should use file path from input option when provided', async () => {
// Arrange
const testFile = 'test/prd.txt';
// Act - call the handler directly with the right params
await parsePrdAction(undefined, {
input: testFile,
numTasks: '10',
output: 'tasks/tasks.json'
});
// Assert
expect(mockConsoleLog).toHaveBeenCalledWith(
expect.stringContaining(`Parsing PRD file: ${testFile}`)
);
expect(mockParsePRD).toHaveBeenCalledWith(
testFile,
'tasks/tasks.json',
10
);
expect(mockExistsSync).not.toHaveBeenCalledWith('scripts/prd.txt');
});
test('should respect numTasks and output options', async () => {
// Arrange
const testFile = 'test/prd.txt';
const outputFile = 'custom/output.json';
const numTasks = 15;
// Act - call the handler directly with the right params
await parsePrdAction(testFile, {
numTasks: numTasks.toString(),
output: outputFile
});
// Assert
expect(mockParsePRD).toHaveBeenCalledWith(testFile, outputFile, numTasks);
});
});
describe('updateTask command', () => {
// Since mocking Commander is complex, we'll test the action handler directly
// Recreate the action handler logic based on commands.js
async function updateTaskAction(options) {
try {
const tasksPath = options.file;
// Validate required parameters
if (!options.id) {
console.error(chalk.red('Error: --id parameter is required'));
console.log(
chalk.yellow(
'Usage example: task-master update-task --id=23 --prompt="Update with new information"'
)
);
process.exit(1);
return; // Add early return to prevent calling updateTaskById
}
// Parse the task ID and validate it's a number
const taskId = parseInt(options.id, 10);
if (isNaN(taskId) || taskId <= 0) {
console.error(
chalk.red(
`Error: Invalid task ID: ${options.id}. Task ID must be a positive integer.`
)
);
console.log(
chalk.yellow(
'Usage example: task-master update-task --id=23 --prompt="Update with new information"'
)
);
process.exit(1);
return; // Add early return to prevent calling updateTaskById
}
if (!options.prompt) {
console.error(
chalk.red(
'Error: --prompt parameter is required. Please provide information about the changes.'
)
);
console.log(
chalk.yellow(
'Usage example: task-master update-task --id=23 --prompt="Update with new information"'
)
);
process.exit(1);
return; // Add early return to prevent calling updateTaskById
}
const prompt = options.prompt;
const useResearch = options.research || false;
// Validate tasks file exists
if (!fs.existsSync(tasksPath)) {
console.error(
chalk.red(`Error: Tasks file not found at path: ${tasksPath}`)
);
if (tasksPath === 'tasks/tasks.json') {
console.log(
chalk.yellow(
'Hint: Run task-master init or task-master parse-prd to create tasks.json first'
)
);
} else {
console.log(
chalk.yellow(
`Hint: Check if the file path is correct: ${tasksPath}`
)
);
}
process.exit(1);
return; // Add early return to prevent calling updateTaskById
}
console.log(
chalk.blue(`Updating task ${taskId} with prompt: "${prompt}"`)
);
console.log(chalk.blue(`Tasks file: ${tasksPath}`));
if (useResearch) {
// Verify Perplexity API key exists if using research
if (!process.env.PERPLEXITY_API_KEY) {
console.log(
chalk.yellow(
'Warning: PERPLEXITY_API_KEY environment variable is missing. Research-backed updates will not be available.'
)
);
console.log(
chalk.yellow('Falling back to Claude AI for task update.')
);
} else {
console.log(
chalk.blue('Using Perplexity AI for research-backed task update')
);
}
}
const result = await mockUpdateTaskById(
tasksPath,
taskId,
prompt,
useResearch
);
// If the task wasn't updated (e.g., if it was already marked as done)
if (!result) {
console.log(
chalk.yellow(
'\nTask update was not completed. Review the messages above for details.'
)
);
}
} catch (error) {
console.error(chalk.red(`Error: ${error.message}`));
// Provide more helpful error messages for common issues
if (
error.message.includes('task') &&
error.message.includes('not found')
) {
console.log(chalk.yellow('\nTo fix this issue:'));
console.log(
' 1. Run task-master list to see all available task IDs'
);
console.log(' 2. Use a valid task ID with the --id parameter');
} else if (error.message.includes('API key')) {
console.log(
chalk.yellow(
'\nThis error is related to API keys. Check your environment variables.'
)
);
}
if (true) {
// CONFIG.debug
console.error(error);
}
process.exit(1);
}
}
beforeEach(() => {
// Reset all mocks
jest.clearAllMocks();
// Set up spy for existsSync (already mocked in the outer scope)
mockExistsSync.mockReturnValue(true);
});
test('should validate required parameters - missing ID', async () => {
// Set up the command options without ID
const options = {
file: 'test-tasks.json',
prompt: 'Update the task'
};
// Call the action directly
await updateTaskAction(options);
// Verify validation error
expect(mockConsoleError).toHaveBeenCalledWith(
expect.stringContaining('--id parameter is required')
);
expect(mockExit).toHaveBeenCalledWith(1);
expect(mockUpdateTaskById).not.toHaveBeenCalled();
});
test('should validate required parameters - invalid ID', async () => {
// Set up the command options with invalid ID
const options = {
file: 'test-tasks.json',
id: 'not-a-number',
prompt: 'Update the task'
};
// Call the action directly
await updateTaskAction(options);
// Verify validation error
expect(mockConsoleError).toHaveBeenCalledWith(
expect.stringContaining('Invalid task ID')
);
expect(mockExit).toHaveBeenCalledWith(1);
expect(mockUpdateTaskById).not.toHaveBeenCalled();
});
test('should validate required parameters - missing prompt', async () => {
// Set up the command options without prompt
const options = {
file: 'test-tasks.json',
id: '2'
};
// Call the action directly
await updateTaskAction(options);
// Verify validation error
expect(mockConsoleError).toHaveBeenCalledWith(
expect.stringContaining('--prompt parameter is required')
);
expect(mockExit).toHaveBeenCalledWith(1);
expect(mockUpdateTaskById).not.toHaveBeenCalled();
});
test('should validate tasks file exists', async () => {
// Mock file not existing
mockExistsSync.mockReturnValue(false);
// Set up the command options
const options = {
file: 'missing-tasks.json',
id: '2',
prompt: 'Update the task'
};
// Call the action directly
await updateTaskAction(options);
// Verify validation error
expect(mockConsoleError).toHaveBeenCalledWith(
expect.stringContaining('Tasks file not found')
);
expect(mockExit).toHaveBeenCalledWith(1);
expect(mockUpdateTaskById).not.toHaveBeenCalled();
});
test('should call updateTaskById with correct parameters', async () => {
// Set up the command options
const options = {
file: 'test-tasks.json',
id: '2',
prompt: 'Update the task',
research: true
};
// Mock perplexity API key
process.env.PERPLEXITY_API_KEY = 'dummy-key';
// Call the action directly
await updateTaskAction(options);
// Verify updateTaskById was called with correct parameters
expect(mockUpdateTaskById).toHaveBeenCalledWith(
'test-tasks.json',
2,
'Update the task',
true
);
// Verify console output
expect(mockConsoleLog).toHaveBeenCalledWith(
expect.stringContaining('Updating task 2')
);
expect(mockConsoleLog).toHaveBeenCalledWith(
expect.stringContaining('Using Perplexity AI')
);
// Clean up
delete process.env.PERPLEXITY_API_KEY;
});
test('should handle null result from updateTaskById', async () => {
// Mock updateTaskById returning null (e.g., task already completed)
mockUpdateTaskById.mockResolvedValueOnce(null);
// Set up the command options
const options = {
file: 'test-tasks.json',
id: '2',
prompt: 'Update the task'
};
// Call the action directly
await updateTaskAction(options);
// Verify updateTaskById was called
expect(mockUpdateTaskById).toHaveBeenCalled();
// Verify console output for null result
expect(mockConsoleLog).toHaveBeenCalledWith(
expect.stringContaining('Task update was not completed')
);
});
test('should handle errors from updateTaskById', async () => {
// Mock updateTaskById throwing an error
mockUpdateTaskById.mockRejectedValueOnce(new Error('Task update failed'));
// Set up the command options
const options = {
file: 'test-tasks.json',
id: '2',
prompt: 'Update the task'
};
// Call the action directly
await updateTaskAction(options);
// Verify error handling
expect(mockConsoleError).toHaveBeenCalledWith(
expect.stringContaining('Error: Task update failed')
);
expect(mockExit).toHaveBeenCalledWith(1);
});
});
// Add test for add-task command
describe('add-task command', () => {
let mockTaskManager;
let addTaskCommand;
let addTaskAction;
let mockFs;
// Import the sample tasks fixtures
beforeEach(async () => {
// Mock fs module to return sample tasks
mockFs = {
existsSync: jest.fn().mockReturnValue(true),
readFileSync: jest.fn().mockReturnValue(JSON.stringify(sampleTasks))
};
// Create a mock task manager with an addTask function that resolves to taskId 5
mockTaskManager = {
addTask: jest.fn().mockImplementation((file, prompt, dependencies, priority, session, research, generateFiles, manualTaskData) => {
// Return the next ID after the last one in sample tasks
const newId = sampleTasks.tasks.length + 1;
return Promise.resolve(newId.toString());
})
};
// Create a simplified version of the add-task action function for testing
addTaskAction = async (cmd, options) => {
options = options || {}; // Ensure options is not undefined
const isManualCreation = options.title && options.description;
// Get prompt directly or from p shorthand
const prompt = options.prompt || options.p;
// Validate that either prompt or title+description are provided
if (!prompt && !isManualCreation) {
throw new Error('Either --prompt or both --title and --description must be provided');
}
// Prepare dependencies if provided
let dependencies = [];
if (options.dependencies) {
dependencies = options.dependencies.split(',').map(id => id.trim());
}
// Create manual task data if title and description are provided
let manualTaskData = null;
if (isManualCreation) {
manualTaskData = {
title: options.title,
description: options.description,
details: options.details || '',
testStrategy: options.testStrategy || ''
};
}
// Call addTask with the right parameters
return await mockTaskManager.addTask(
options.file || 'tasks/tasks.json',
prompt,
dependencies,
options.priority || 'medium',
{ session: process.env },
options.research || options.r || false,
null,
manualTaskData
);
};
});
test('should throw error if no prompt or manual task data provided', async () => {
// Call without required params
const options = { file: 'tasks/tasks.json' };
await expect(async () => {
await addTaskAction(undefined, options);
}).rejects.toThrow('Either --prompt or both --title and --description must be provided');
});
test('should handle short-hand flag -p for prompt', async () => {
// Use -p as prompt short-hand
const options = {
p: 'Create a login component',
file: 'tasks/tasks.json'
};
await addTaskAction(undefined, options);
// Check that task manager was called with correct arguments
expect(mockTaskManager.addTask).toHaveBeenCalledWith(
expect.any(String), // File path
'Create a login component', // Prompt
[], // Dependencies
'medium', // Default priority
{ session: process.env },
false, // Research flag
null, // Generate files parameter
null // Manual task data
);
});
test('should handle short-hand flag -r for research', async () => {
const options = {
prompt: 'Create authentication system',
r: true,
file: 'tasks/tasks.json'
};
await addTaskAction(undefined, options);
// Check that task manager was called with correct research flag
expect(mockTaskManager.addTask).toHaveBeenCalledWith(
expect.any(String),
'Create authentication system',
[],
'medium',
{ session: process.env },
true, // Research flag should be true
null, // Generate files parameter
null // Manual task data
);
});
test('should handle manual task creation with title and description', async () => {
const options = {
title: 'Login Component',
description: 'Create a reusable login form',
details: 'Implementation details here',
file: 'tasks/tasks.json'
};
await addTaskAction(undefined, options);
// Check that task manager was called with correct manual task data
expect(mockTaskManager.addTask).toHaveBeenCalledWith(
expect.any(String),
undefined, // No prompt for manual creation
[],
'medium',
{ session: process.env },
false,
null, // Generate files parameter
{ // Manual task data
title: 'Login Component',
description: 'Create a reusable login form',
details: 'Implementation details here',
testStrategy: ''
}
);
});
test('should handle dependencies parameter', async () => {
const options = {
prompt: 'Create user settings page',
dependencies: '1, 3, 5', // Dependencies with spaces
file: 'tasks/tasks.json'
};
await addTaskAction(undefined, options);
// Check that dependencies are parsed correctly
expect(mockTaskManager.addTask).toHaveBeenCalledWith(
expect.any(String),
'Create user settings page',
['1', '3', '5'], // Should trim whitespace from dependencies
'medium',
{ session: process.env },
false,
null, // Generate files parameter
null // Manual task data
);
});
test('should handle priority parameter', async () => {
const options = {
prompt: 'Create navigation menu',
priority: 'high',
file: 'tasks/tasks.json'
};
await addTaskAction(undefined, options);
// Check that priority is passed correctly
expect(mockTaskManager.addTask).toHaveBeenCalledWith(
expect.any(String),
'Create navigation menu',
[],
'high', // Should use the provided priority
{ session: process.env },
false,
null, // Generate files parameter
null // Manual task data
);
});
test('should use default values for optional parameters', async () => {
const options = {
prompt: 'Basic task',
file: 'tasks/tasks.json'
};
await addTaskAction(undefined, options);
// Check that default values are used
expect(mockTaskManager.addTask).toHaveBeenCalledWith(
expect.any(String),
'Basic task',
[], // Empty dependencies array by default
'medium', // Default priority is medium
{ session: process.env },
false, // Research is false by default
null, // Generate files parameter
null // Manual task data
);
});
});
});
// Test the version comparison utility
describe('Version comparison', () => {
2025-04-09 00:25:27 +02:00
// 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', () => {
2025-04-09 00:25:27 +02:00
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');
});
});