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

194 lines
6.5 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
/**
* Task finder tests
*/
// Import after mocks are set up - No mocks needed for readComplexityReport anymore
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 { findTaskById } from '../../scripts/modules/utils.js';
import { emptySampleTasks, sampleTasks } from '../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
describe('Task Finder', () => {
2025-04-09 00:25:27 +02:00
describe('findTaskById function', () => {
test('should find a task by numeric ID', () => {
const result = findTaskById(sampleTasks.tasks, 2);
expect(result.task).toBeDefined();
expect(result.task.id).toBe(2);
expect(result.task.title).toBe('Create Core Functionality');
expect(result.originalSubtaskCount).toBeNull();
2025-04-09 00:25:27 +02:00
});
test('should find a task by string ID', () => {
const result = findTaskById(sampleTasks.tasks, '2');
expect(result.task).toBeDefined();
expect(result.task.id).toBe(2);
expect(result.originalSubtaskCount).toBeNull();
2025-04-09 00:25:27 +02:00
});
fix: normalize task IDs to numbers on load to fix comparison issues (#1063) * fix: normalize task IDs to numbers on load to fix comparison issues When tasks.json contains string IDs (e.g., "5" instead of 5), task lookups fail because the code uses parseInt() and strict equality (===) for comparisons. This fix normalizes all task and subtask IDs to numbers when loading the JSON, ensuring consistent comparisons throughout the codebase without requiring changes to multiple comparison locations. Fixes task not found errors when using string IDs in tasks.json. * Added test * Don't mess up formatting * Fix formatting once and for all * Update scripts/modules/utils.js Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update scripts/modules/utils.js Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update scripts/modules/utils.js Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * fix: normalize task IDs to numbers on load to fix comparison issues - Added normalizeTaskIds function to convert string IDs to numbers - Applied normalization in readJSON for all code paths - Fixed set-task-status, add-task, and move-task to normalize IDs when working with raw data - Exported normalizeTaskIds function for use in other modules - Added test case for string ID normalization 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * Simplified implementation * refactor: normalize IDs once when loading JSON instead of scattered calls - Normalize all tags' data when creating _rawTaggedData in readJSON - Add support for handling malformed dotted subtask IDs (e.g., "5.1" -> 1) - Remove redundant normalizeTaskIds calls from set-task-status, add-task, and move-task - Add comprehensive test for mixed ID formats (string IDs and dotted notation) - Cleaner, more maintainable solution that normalizes IDs at load time 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * chore: run format to resolve CI issues --------- Co-authored-by: Carl Mercier <carl@carlmercier.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Claude <noreply@anthropic.com>
2025-08-01 01:06:51 +03:00
test('should find tasks when JSON contains string IDs (normalized to numbers)', () => {
// Simulate tasks loaded from JSON with string IDs and mixed subtask notations
const tasksWithStringIds = [
{ id: '1', title: 'First Task' },
{
id: '2',
title: 'Second Task',
subtasks: [
{ id: '1', title: 'Subtask One' },
{ id: '2.2', title: 'Subtask Two (with dotted notation)' } // Testing dotted notation
]
},
{
id: '5',
title: 'Fifth Task',
subtasks: [
{ id: '5.1', title: 'Subtask with dotted ID' }, // Should normalize to 1
{ id: '3', title: 'Subtask with simple ID' } // Should stay as 3
]
}
];
// The readJSON function should normalize these IDs to numbers
// For this test, we'll manually normalize them to simulate what happens
tasksWithStringIds.forEach((task) => {
task.id = parseInt(task.id, 10);
if (task.subtasks) {
task.subtasks.forEach((subtask) => {
// Handle dotted notation like "5.1" -> extract the subtask part
if (typeof subtask.id === 'string' && subtask.id.includes('.')) {
const parts = subtask.id.split('.');
subtask.id = parseInt(parts[parts.length - 1], 10);
} else {
subtask.id = parseInt(subtask.id, 10);
}
});
}
});
// Test finding tasks by numeric ID
const result1 = findTaskById(tasksWithStringIds, 5);
expect(result1.task).toBeDefined();
expect(result1.task.id).toBe(5);
expect(result1.task.title).toBe('Fifth Task');
// Test finding tasks by string ID
const result2 = findTaskById(tasksWithStringIds, '5');
expect(result2.task).toBeDefined();
expect(result2.task.id).toBe(5);
// Test finding subtasks with normalized IDs
const result3 = findTaskById(tasksWithStringIds, '2.1');
expect(result3.task).toBeDefined();
expect(result3.task.id).toBe(1);
expect(result3.task.title).toBe('Subtask One');
expect(result3.task.isSubtask).toBe(true);
// Test subtask that was originally "2.2" (should be normalized to 2)
const result4 = findTaskById(tasksWithStringIds, '2.2');
expect(result4.task).toBeDefined();
expect(result4.task.id).toBe(2);
expect(result4.task.title).toBe('Subtask Two (with dotted notation)');
// Test subtask that was originally "5.1" (should be normalized to 1)
const result5 = findTaskById(tasksWithStringIds, '5.1');
expect(result5.task).toBeDefined();
expect(result5.task.id).toBe(1);
expect(result5.task.title).toBe('Subtask with dotted ID');
// Test subtask that was originally "3" (should stay as 3)
const result6 = findTaskById(tasksWithStringIds, '5.3');
expect(result6.task).toBeDefined();
expect(result6.task.id).toBe(3);
expect(result6.task.title).toBe('Subtask with simple ID');
});
2025-04-09 00:25:27 +02:00
test('should find a subtask using dot notation', () => {
const result = findTaskById(sampleTasks.tasks, '3.1');
expect(result.task).toBeDefined();
expect(result.task.id).toBe(1);
expect(result.task.title).toBe('Create Header Component');
expect(result.task.isSubtask).toBe(true);
expect(result.task.parentTask.id).toBe(3);
expect(result.originalSubtaskCount).toBeNull();
2025-04-09 00:25:27 +02:00
});
test('should return null for non-existent task ID', () => {
const result = findTaskById(sampleTasks.tasks, 99);
expect(result.task).toBeNull();
expect(result.originalSubtaskCount).toBeNull();
2025-04-09 00:25:27 +02:00
});
test('should return null for non-existent subtask ID', () => {
const result = findTaskById(sampleTasks.tasks, '3.99');
expect(result.task).toBeNull();
expect(result.originalSubtaskCount).toBeNull();
2025-04-09 00:25:27 +02:00
});
test('should return null for non-existent parent task ID in subtask notation', () => {
const result = findTaskById(sampleTasks.tasks, '99.1');
expect(result.task).toBeNull();
expect(result.originalSubtaskCount).toBeNull();
2025-04-09 00:25:27 +02:00
});
test('should return null when tasks array is empty', () => {
const result = findTaskById(emptySampleTasks.tasks, 1);
expect(result.task).toBeNull();
expect(result.originalSubtaskCount).toBeNull();
2025-04-09 00:25:27 +02:00
});
test('should work correctly when no complexity report is provided', () => {
// Pass null as the complexity report
const result = findTaskById(sampleTasks.tasks, 2, null);
expect(result.task).toBeDefined();
expect(result.task.id).toBe(2);
expect(result.task.complexityScore).toBeUndefined();
});
test('should work correctly when task has no complexity data in the provided report', () => {
// Define a complexity report that doesn't include task 2
const complexityReport = {
complexityAnalysis: [{ taskId: 999, complexityScore: 5 }]
};
const result = findTaskById(sampleTasks.tasks, 2, complexityReport);
expect(result.task).toBeDefined();
expect(result.task.id).toBe(2);
expect(result.task.complexityScore).toBeUndefined();
});
test('should include complexity score when report is provided', () => {
// Define the complexity report for this test
const complexityReport = {
meta: {
generatedAt: '2023-01-01T00:00:00.000Z',
tasksAnalyzed: 3,
thresholdScore: 5
},
complexityAnalysis: [
{
taskId: 1,
taskTitle: 'Initialize Project',
complexityScore: 3,
recommendedSubtasks: 2
},
{
taskId: 2,
taskTitle: 'Create Core Functionality',
complexityScore: 8,
recommendedSubtasks: 5
},
{
taskId: 3,
taskTitle: 'Implement UI Components',
complexityScore: 6,
recommendedSubtasks: 4
}
]
};
const result = findTaskById(sampleTasks.tasks, 2, complexityReport);
expect(result.task).toBeDefined();
expect(result.task.id).toBe(2);
expect(result.task.complexityScore).toBe(8);
});
2025-04-09 00:25:27 +02:00
});
});