Files
claude-task-master/tests/unit/dependency-manager.test.js
Parthy 04e11b5e82 feat: implement cross-tag task movement functionality (#1088)
* feat: enhance move command with cross-tag functionality

- Updated the `move` command to allow moving tasks between different tags, including options for handling dependencies.
- Added new options: `--from-tag`, `--to-tag`, `--with-dependencies`, `--ignore-dependencies`, and `--force`.
- Implemented validation for cross-tag moves and dependency checks.
- Introduced helper functions in the dependency manager for validating and resolving cross-tag dependencies.
- Added integration and unit tests to cover new functionality and edge cases.

* fix: refactor cross-tag move logic and enhance validation

- Moved the import of `moveTasksBetweenTags` to the correct location in `commands.js` for better clarity.
- Added new helper functions in `dependency-manager.js` to improve validation and error handling for cross-tag moves.
- Enhanced existing functions to ensure proper handling of task dependencies and conflicts.
- Updated tests to cover new validation scenarios and ensure robust error messaging for invalid task IDs and tags.

* fix: improve task ID handling and error messaging in cross-tag moves

- Refactored `moveTasksBetweenTags` to normalize task IDs for comparison, ensuring consistent handling of string and numeric IDs.
- Enhanced error messages for cases where source and target tags are the same but no destination is specified.
- Updated tests to validate new behavior, including handling string dependencies correctly during cross-tag moves.
- Cleaned up existing code for better readability and maintainability.

* test: add comprehensive tests for cross-tag move and dependency validation

- Introduced new test files for `move-cross-tag` and `cross-tag-dependencies` to cover various scenarios in cross-tag task movement.
- Implemented tests for handling task movement with and without dependencies, including edge cases for error handling.
- Enhanced existing tests in `fix-dependencies-command` and `move-task` to ensure robust validation of task IDs and dependencies.
- Mocked necessary modules and functions to isolate tests and improve reliability.
- Ensured coverage for both successful and failed cross-tag move operations, validating expected outcomes and error messages.

* test: refactor cross-tag move tests for better clarity and reusability

- Introduced a helper function `simulateCrossTagMove` to streamline cross-tag move test cases, reducing redundancy and improving readability.
- Updated existing tests to utilize the new helper function, ensuring consistent handling of expected messages and options.
- Enhanced test coverage for various scenarios, including handling of dependencies and flags.

* feat: add cross-tag task movement functionality

- Introduced new commands for moving tasks between different tags, enhancing project organization capabilities.
- Updated README with usage examples for cross-tag movement, including options for handling dependencies.
- Created comprehensive documentation for cross-tag task movement, detailing usage, error handling, and best practices.
- Implemented core logic for cross-tag moves, including validation for dependencies and error handling.
- Added integration and unit tests to ensure robust functionality and coverage for various scenarios, including edge cases.

* fix: enhance error handling and logging in cross-tag task movement

- Improved logging in `moveTaskCrossTagDirect` to include detailed arguments for better traceability.
- Refactored error handling to utilize structured error objects, providing clearer suggestions for resolving cross-tag dependency conflicts and subtask movement restrictions.
- Updated documentation to reflect changes in error handling and provide clearer guidance on task movement options.
- Added integration tests for cross-tag movement scenarios, ensuring robust validation of error handling and task movement logic.
- Cleaned up existing tests for clarity and reusability, enhancing overall test coverage.

* feat: enhance dependency resolution and error handling in task movement

- Added recursive dependency resolution for tasks in `moveTasksBetweenTags`, improving handling of complex task relationships.
- Introduced helper functions to find all dependencies and reverse dependencies, ensuring comprehensive coverage during task moves.
- Enhanced error messages in `validateSubtaskMove` and `displaySubtaskMoveError` for better clarity on movement restrictions.
- Updated tests to cover new functionality, including integration tests for complex cross-tag movement scenarios and edge cases.
- Refactored existing code for improved readability and maintainability, ensuring consistent handling of task IDs and dependencies.

* feat: unify dependency traversal and enhance task management utilities

- Introduced `traverseDependencies` utility for unified forward and reverse dependency traversal, improving code reusability and clarity.
- Refactored `findAllDependenciesRecursively` to leverage the new utility, streamlining dependency resolution in task management.
- Added `formatTaskIdForDisplay` helper for better task ID formatting in UI, enhancing user experience during error displays.
- Updated tests to cover new utility functions and ensure robust validation of dependency handling across various scenarios.
- Improved overall code organization and readability, ensuring consistent handling of task dependencies and IDs.

* fix: improve validation for dependency parameters in `findAllDependenciesRecursively`

- Added checks to ensure `sourceTasks` and `allTasks` are arrays, throwing errors if not, to prevent runtime issues.
- Updated documentation comment for clarity on the function's purpose and parameters.

* fix: remove `force` option from task movement parameters

- Eliminated the `force` parameter from the `moveTaskCrossTagDirect` function and related tools, simplifying the task movement logic.
- Updated documentation and tests to reflect the removal of the `force` option, ensuring clarity and consistency across the codebase.
- Adjusted related functions and tests to focus on `ignoreDependencies` as the primary control for handling dependency conflicts during task moves.

* Add cross-tag task movement functionality

- Introduced functionality for organizing tasks across different contexts by enabling cross-tag movement.
- Added `formatTaskIdForDisplay` helper to improve task ID formatting in UI error messages.
- Updated relevant tests to incorporate new functionality and ensure accurate error displays during task movements.

* Update scripts/modules/dependency-manager.js

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* refactor(dependency-manager): Fix subtask resolution and extract helper functions

1. Fix subtask finding logic (lines 1315-1330):
   - Correctly locate parent task by numeric ID
   - Search within parent's subtasks array instead of top-level tasks
   - Properly handle relative subtask references

2. Extract helper functions from getDependentTaskIds (lines 1440-1636):
   - Move findTasksThatDependOn as module-level function
   - Move taskDependsOnSource as module-level function
   - Move subtasksDependOnSource as module-level function
   - Improves readability, maintainability, and testability

Both fixes address architectural issues and improve code organization.

* refactor(dependency-manager): Enhance subtask resolution and dependency validation

- Improved subtask resolution logic to correctly find parent tasks and their subtasks, ensuring accurate identification of dependencies.
- Filtered out null/undefined dependencies before processing, enhancing robustness in dependency checks.
- Updated comments for clarity on the logic flow and purpose of changes, improving code maintainability.

* refactor(move-task): clarify destination ID description and improve skipped task handling

- Updated the description for the destination ID to clarify its usage in cross-tag moves.
- Simplified the handling of skipped tasks during multiple task movements, improving readability and logging.
- Enhanced the API result response to include detailed information about moved and skipped tasks, ensuring better feedback for users.

* refactor(commands): remove redundant tag validation logic

- Eliminated the check for identical source and target tags in the task movement logic, simplifying the code.
- This change streamlines the flow for within-tag moves, enhancing readability and maintainability.

* refactor(commands): enhance move command logic and error handling

- Introduced helper functions for better organization of cross-tag and within-tag move logic, improving code readability and maintainability.
- Enhanced error handling with structured error objects, providing clearer feedback for dependency conflicts and invalid tag combinations.
- Updated move command help output to include best practices and error resolution tips, ensuring users have comprehensive guidance during task movements.
- Streamlined task movement logic to handle multiple tasks more effectively, including detailed logging of successful and failed moves.

* test(dependency-manager): add subtasks to task structure and mock dependency traversal

- Updated `circular-dependencies.test.js` to include subtasks in task definitions, enhancing test coverage for task structures with nested dependencies.
- Mocked `traverseDependencies` in `fix-dependencies-command.test.js` to ensure consistent behavior during tests, improving reliability of dependency-related tests.

* refactor(dependency-manager): extract subtask finding logic into helper function

- Added `findSubtaskInParent` function to encapsulate subtask resolution within a parent task's subtasks array, improving code organization and readability.
- Updated `findDependencyTask` to utilize the new helper function, streamlining the logic for finding subtasks and enhancing maintainability.
- Enhanced comments for clarity on the purpose and functionality of the new subtask finding logic.

* refactor(ui): enhance subtask ID validation and improve error handling

- Added validation for subtask ID format in `formatDependenciesWithStatus` and `taskExists`, ensuring proper handling of invalid formats.
- Updated error logging in `displaySubtaskMoveError` to provide warnings for unexpected task ID formats, improving user feedback.
- Converted hints to a Set in `displayDependencyValidationHints` to ensure unique hints are displayed, enhancing clarity in the UI.

* test(cli): remove redundant timing check in complex cross-tag scenarios

- Eliminated the timing check for task completion within 5 seconds in `complex-cross-tag-scenarios.test.js`, streamlining the test logic.
- This change focuses on verifying task success without unnecessary timing constraints, enhancing test clarity and maintainability.

* test(integration): enhance task movement tests with mock file system

- Added integration tests for moving tasks within the same tag and between different tags using the actual `moveTask` and `moveTasksBetweenTags` functions.
- Implemented `mock-fs` to simulate file system interactions, improving test isolation and reliability.
- Verified task movement success and ensured proper handling of subtasks and dependencies, enhancing overall test coverage for task management functionality.
- Included error handling tests for missing tags and task IDs to ensure robustness in task movement operations.

* test(unit): add comprehensive tests for moveTaskCrossTagDirect functionality

- Introduced new test cases to verify mock functionality, ensuring that mocks for `findTasksPath` and `readJSON` are working as expected.
- Added tests for parameter validation, error handling, and function call flow, including scenarios for missing project roots and identical source/target tags.
- Enhanced coverage for ID parsing and move options, ensuring robust handling of various input conditions and improving overall test reliability.

* test(integration): skip tests for dependency conflict handling and withDependencies option

- Marked tests for handling dependency conflicts and the withDependencies option as skipped due to issues with the mock setup.
- Added TODOs to address the mock-fs setup for complex dependency scenarios, ensuring future improvements in test reliability.

* test(unit): expand cross-tag move command tests with comprehensive mocks

- Added extensive mocks for various modules to enhance the testing of the cross-tag move functionality in `move-cross-tag.test.js`.
- Implemented detailed test cases for handling cross-tag moves, including validation for missing parameters and identical source/target tags.
- Improved error handling tests to ensure robust feedback for invalid operations, enhancing overall test reliability and coverage.

* test(integration): add complex dependency scenarios to task movement tests

- Introduced new integration tests for handling complex dependency scenarios in task movement, utilizing the actual `moveTasksBetweenTags` function.
- Added tests for circular dependencies, nested dependency chains, and cross-tag dependency resolution, enhancing coverage and reliability.
- Documented limitations of the mock-fs setup for complex scenarios and provided warnings in the test output to guide future improvements.
- Skipped tests for dependency conflicts and the withDependencies option due to mock setup issues, with TODOs for resolution.

* test(unit): refactor move-cross-tag tests with focused mock system

- Simplified mocking in `move-cross-tag.test.js` by implementing a configuration-driven mock system, reducing the number of mocked modules from 20+ to 5 core functionalities.
- Introduced a reusable mock factory to streamline the creation of mocks based on configuration, enhancing maintainability and clarity.
- Added documentation for the new mock system, detailing usage examples and benefits, including reduced complexity and improved test focus.
- Implemented tests to validate the mock configuration, ensuring flexibility in enabling/disabling specific mocks.

* test(unit): clean up mocks and improve isEmpty function in fix-dependencies-command tests

- Removed the mock for `traverseDependencies` as it was unnecessary, simplifying the test setup.
- Updated the `isEmpty` function to clarify its behavior regarding null and undefined values, enhancing code readability and maintainability.

* test(unit): update traverseDependencies mock for consistency across tests

- Standardized the mock implementation of `traverseDependencies` in both `fix-dependencies-command.test.js` and `complexity-report-tag-isolation.test.js` to accept `sourceTasks`, `allTasks`, and `options` parameters, ensuring uniformity in test setups.
- This change enhances clarity and maintainability of the tests by aligning the mock behavior across different test files.

* fix(core): improve task movement error handling and ID normalization

- Wrapped task movement logic in a try-finally block to ensure console output is restored even on errors, enhancing reliability.
- Normalized source IDs to handle mixed string/number comparisons, preventing potential issues in dependency checks.
- Added tests for ID type consistency to verify that the normalization fix works correctly across various scenarios, improving test coverage and robustness.

* refactor(task-manager): restructure task movement logic for improved validation and execution

- Renamed and refactored `moveTasksBetweenTags` to streamline the task movement process into distinct phases: validation, data preparation, dependency resolution, execution, and finalization.
- Introduced `validateMove`, `prepareTaskData`, `resolveDependencies`, `executeMoveOperation`, and `finalizeMove` functions to enhance modularity and clarity.
- Updated documentation comments to reflect changes in function responsibilities and parameters.
- Added comprehensive unit tests for the new structure, ensuring robust validation and error handling across various scenarios.
- Improved handling of dependencies and task existence checks during the move operation, enhancing overall reliability.

* fix(move-task): streamline task movement logic and improve error handling

- Refactored the task movement process to enhance clarity and maintainability by replacing `forEach` with a `for...of` loop for better async handling.
- Consolidated error handling and result logging to ensure consistent feedback during task moves.
- Updated the logic for generating files only on the last move, improving performance and reducing unnecessary operations.
- Enhanced validation for skipped tasks, ensuring accurate reporting of moved and skipped tasks in the final result.

* fix(docs): update error message formatting and enhance clarity in task movement documentation

- Changed code block syntax from generic to `text` for better readability in error messages related to task movement and dependency conflicts.
- Ensured consistent formatting across all error message examples to improve user understanding of task movement restrictions and resolutions.
- Added a newline at the end of the file for proper formatting.

* Update .changeset/crazy-meals-hope.md

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* chore: improve changeset

* chore: improve changeset

* fix referenced bug in docs and remove docs

* chore: fix format

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Ralph Khreish <35776126+Crunchyman-ralph@users.noreply.github.com>
2025-08-11 18:58:51 +02:00

924 lines
24 KiB
JavaScript

/**
* Dependency Manager module tests
*/
import { jest } from '@jest/globals';
import {
validateTaskDependencies,
isCircularDependency,
removeDuplicateDependencies,
cleanupSubtaskDependencies,
ensureAtLeastOneIndependentSubtask,
validateAndFixDependencies,
canMoveWithDependencies
} from '../../scripts/modules/dependency-manager.js';
import * as utils from '../../scripts/modules/utils.js';
import { sampleTasks } from '../fixtures/sample-tasks.js';
// Mock dependencies
jest.mock('path');
jest.mock('chalk', () => ({
green: jest.fn((text) => `<green>${text}</green>`),
yellow: jest.fn((text) => `<yellow>${text}</yellow>`),
red: jest.fn((text) => `<red>${text}</red>`),
cyan: jest.fn((text) => `<cyan>${text}</cyan>`),
bold: jest.fn((text) => `<bold>${text}</bold>`)
}));
jest.mock('boxen', () => jest.fn((text) => `[boxed: ${text}]`));
jest.mock('@anthropic-ai/sdk', () => ({
Anthropic: jest.fn().mockImplementation(() => ({}))
}));
// Mock utils module
const mockTaskExists = jest.fn();
const mockFormatTaskId = jest.fn();
const mockFindCycles = jest.fn();
const mockLog = jest.fn();
const mockReadJSON = jest.fn();
const mockWriteJSON = jest.fn();
jest.mock('../../scripts/modules/utils.js', () => ({
log: mockLog,
readJSON: mockReadJSON,
writeJSON: mockWriteJSON,
taskExists: mockTaskExists,
formatTaskId: mockFormatTaskId,
findCycles: mockFindCycles
}));
jest.mock('../../scripts/modules/ui.js', () => ({
displayBanner: jest.fn()
}));
jest.mock('../../scripts/modules/task-manager.js', () => ({
generateTaskFiles: jest.fn()
}));
// Create a path for test files
const TEST_TASKS_PATH = 'tests/fixture/test-tasks.json';
describe('Dependency Manager Module', () => {
beforeEach(() => {
jest.clearAllMocks();
// Set default implementations
mockTaskExists.mockImplementation((tasks, id) => {
if (Array.isArray(tasks)) {
if (typeof id === 'string' && id.includes('.')) {
const [taskId, subtaskId] = id.split('.').map(Number);
const task = tasks.find((t) => t.id === taskId);
return (
task &&
task.subtasks &&
task.subtasks.some((st) => st.id === subtaskId)
);
}
return tasks.some(
(task) => task.id === (typeof id === 'string' ? parseInt(id, 10) : id)
);
}
return false;
});
mockFormatTaskId.mockImplementation((id) => {
if (typeof id === 'string' && id.includes('.')) {
return id;
}
return parseInt(id, 10);
});
mockFindCycles.mockImplementation((tasks) => {
// Simplified cycle detection for testing
const dependencyMap = new Map();
// Build dependency map
tasks.forEach((task) => {
if (task.dependencies) {
dependencyMap.set(task.id, task.dependencies);
}
});
const visited = new Set();
const recursionStack = new Set();
function dfs(taskId) {
visited.add(taskId);
recursionStack.add(taskId);
const dependencies = dependencyMap.get(taskId) || [];
for (const depId of dependencies) {
if (!visited.has(depId)) {
if (dfs(depId)) return true;
} else if (recursionStack.has(depId)) {
return true;
}
}
recursionStack.delete(taskId);
return false;
}
// Check for cycles starting from each unvisited node
for (const taskId of dependencyMap.keys()) {
if (!visited.has(taskId)) {
if (dfs(taskId)) return true;
}
}
return false;
});
});
describe('isCircularDependency function', () => {
test('should detect a direct circular dependency', () => {
const tasks = [
{ id: 1, dependencies: [2] },
{ id: 2, dependencies: [1] }
];
const result = isCircularDependency(tasks, 1);
expect(result).toBe(true);
});
test('should detect an indirect circular dependency', () => {
const tasks = [
{ id: 1, dependencies: [2] },
{ id: 2, dependencies: [3] },
{ id: 3, dependencies: [1] }
];
const result = isCircularDependency(tasks, 1);
expect(result).toBe(true);
});
test('should return false for non-circular dependencies', () => {
const tasks = [
{ id: 1, dependencies: [2] },
{ id: 2, dependencies: [3] },
{ id: 3, dependencies: [] }
];
const result = isCircularDependency(tasks, 1);
expect(result).toBe(false);
});
test('should handle a task with no dependencies', () => {
const tasks = [
{ id: 1, dependencies: [] },
{ id: 2, dependencies: [1] }
];
const result = isCircularDependency(tasks, 1);
expect(result).toBe(false);
});
test('should handle a task depending on itself', () => {
const tasks = [{ id: 1, dependencies: [1] }];
const result = isCircularDependency(tasks, 1);
expect(result).toBe(true);
});
test('should handle subtask dependencies correctly', () => {
const tasks = [
{
id: 1,
dependencies: [],
subtasks: [
{ id: 1, dependencies: ['1.2'] },
{ id: 2, dependencies: ['1.3'] },
{ id: 3, dependencies: ['1.1'] }
]
}
];
// This creates a circular dependency: 1.1 -> 1.2 -> 1.3 -> 1.1
const result = isCircularDependency(tasks, '1.1', ['1.3', '1.2']);
expect(result).toBe(true);
});
test('should allow non-circular subtask dependencies within same parent', () => {
const tasks = [
{
id: 1,
dependencies: [],
subtasks: [
{ id: 1, dependencies: [] },
{ id: 2, dependencies: ['1.1'] },
{ id: 3, dependencies: ['1.2'] }
]
}
];
// This is a valid dependency chain: 1.3 -> 1.2 -> 1.1
const result = isCircularDependency(tasks, '1.1', []);
expect(result).toBe(false);
});
test('should properly handle dependencies between subtasks of the same parent', () => {
const tasks = [
{
id: 1,
dependencies: [],
subtasks: [
{ id: 1, dependencies: [] },
{ id: 2, dependencies: ['1.1'] },
{ id: 3, dependencies: [] }
]
}
];
// Check if adding a dependency from subtask 1.3 to 1.2 creates a circular dependency
// This should be false as 1.3 -> 1.2 -> 1.1 is a valid chain
mockTaskExists.mockImplementation(() => true);
const result = isCircularDependency(tasks, '1.3', ['1.2']);
expect(result).toBe(false);
});
test('should correctly detect circular dependencies in subtasks of the same parent', () => {
const tasks = [
{
id: 1,
dependencies: [],
subtasks: [
{ id: 1, dependencies: ['1.3'] },
{ id: 2, dependencies: ['1.1'] },
{ id: 3, dependencies: ['1.2'] }
]
}
];
// This creates a circular dependency: 1.1 -> 1.3 -> 1.2 -> 1.1
mockTaskExists.mockImplementation(() => true);
const result = isCircularDependency(tasks, '1.2', ['1.1']);
expect(result).toBe(true);
});
});
describe('validateTaskDependencies function', () => {
test('should detect missing dependencies', () => {
const tasks = [
{ id: 1, dependencies: [99] }, // 99 doesn't exist
{ id: 2, dependencies: [1] }
];
const result = validateTaskDependencies(tasks);
expect(result.valid).toBe(false);
expect(result.issues.length).toBeGreaterThan(0);
expect(result.issues[0].type).toBe('missing');
expect(result.issues[0].taskId).toBe(1);
expect(result.issues[0].dependencyId).toBe(99);
});
test('should detect circular dependencies', () => {
const tasks = [
{ id: 1, dependencies: [2] },
{ id: 2, dependencies: [1] }
];
const result = validateTaskDependencies(tasks);
expect(result.valid).toBe(false);
expect(result.issues.some((issue) => issue.type === 'circular')).toBe(
true
);
});
test('should detect self-dependencies', () => {
const tasks = [{ id: 1, dependencies: [1] }];
const result = validateTaskDependencies(tasks);
expect(result.valid).toBe(false);
expect(
result.issues.some(
(issue) => issue.type === 'self' && issue.taskId === 1
)
).toBe(true);
});
test('should return valid for correct dependencies', () => {
const tasks = [
{ id: 1, dependencies: [] },
{ id: 2, dependencies: [1] },
{ id: 3, dependencies: [1, 2] }
];
const result = validateTaskDependencies(tasks);
expect(result.valid).toBe(true);
expect(result.issues.length).toBe(0);
});
test('should handle tasks with no dependencies property', () => {
const tasks = [
{ id: 1 }, // Missing dependencies property
{ id: 2, dependencies: [1] }
];
const result = validateTaskDependencies(tasks);
// Should be valid since a missing dependencies property is interpreted as an empty array
expect(result.valid).toBe(true);
});
test('should handle subtask dependencies correctly', () => {
const tasks = [
{
id: 1,
dependencies: [],
subtasks: [
{ id: 1, dependencies: [] },
{ id: 2, dependencies: ['1.1'] }, // Valid - depends on another subtask
{ id: 3, dependencies: ['1.2'] } // Valid - depends on another subtask
]
},
{
id: 2,
dependencies: ['1.3'], // Valid - depends on a subtask from task 1
subtasks: []
}
];
// Set up mock to handle subtask validation
mockTaskExists.mockImplementation((tasks, id) => {
if (typeof id === 'string' && id.includes('.')) {
const [taskId, subtaskId] = id.split('.').map(Number);
const task = tasks.find((t) => t.id === taskId);
return (
task &&
task.subtasks &&
task.subtasks.some((st) => st.id === subtaskId)
);
}
return tasks.some((task) => task.id === parseInt(id, 10));
});
const result = validateTaskDependencies(tasks);
expect(result.valid).toBe(true);
expect(result.issues.length).toBe(0);
});
test('should detect missing subtask dependencies', () => {
const tasks = [
{
id: 1,
dependencies: [],
subtasks: [
{ id: 1, dependencies: ['1.4'] }, // Invalid - subtask 4 doesn't exist
{ id: 2, dependencies: ['2.1'] } // Invalid - task 2 has no subtasks
]
},
{
id: 2,
dependencies: [],
subtasks: []
}
];
// Mock taskExists to correctly identify missing subtasks
mockTaskExists.mockImplementation((taskArray, depId) => {
if (typeof depId === 'string' && depId === '1.4') {
return false; // Subtask 1.4 doesn't exist
}
if (typeof depId === 'string' && depId === '2.1') {
return false; // Subtask 2.1 doesn't exist
}
return true; // All other dependencies exist
});
const result = validateTaskDependencies(tasks);
expect(result.valid).toBe(false);
expect(result.issues.length).toBeGreaterThan(0);
// Should detect missing subtask dependencies
expect(
result.issues.some(
(issue) =>
issue.type === 'missing' &&
String(issue.taskId) === '1.1' &&
String(issue.dependencyId) === '1.4'
)
).toBe(true);
});
test('should detect circular dependencies between subtasks', () => {
const tasks = [
{
id: 1,
dependencies: [],
subtasks: [
{ id: 1, dependencies: ['1.2'] },
{ id: 2, dependencies: ['1.1'] } // Creates a circular dependency with 1.1
]
}
];
// Mock isCircularDependency for subtasks
mockFindCycles.mockReturnValue(true);
const result = validateTaskDependencies(tasks);
expect(result.valid).toBe(false);
expect(result.issues.some((issue) => issue.type === 'circular')).toBe(
true
);
});
test('should properly validate dependencies between subtasks of the same parent', () => {
const tasks = [
{
id: 23,
dependencies: [],
subtasks: [
{ id: 8, dependencies: ['23.13'] },
{ id: 10, dependencies: ['23.8'] },
{ id: 13, dependencies: [] }
]
}
];
// Mock taskExists to validate the subtask dependencies
mockTaskExists.mockImplementation((taskArray, id) => {
if (typeof id === 'string') {
if (id === '23.8' || id === '23.10' || id === '23.13') {
return true;
}
}
return false;
});
const result = validateTaskDependencies(tasks);
expect(result.valid).toBe(true);
expect(result.issues.length).toBe(0);
});
});
describe('removeDuplicateDependencies function', () => {
test('should remove duplicate dependencies from tasks', () => {
const tasksData = {
tasks: [
{ id: 1, dependencies: [2, 2, 3, 3, 3] },
{ id: 2, dependencies: [3] },
{ id: 3, dependencies: [] }
]
};
const result = removeDuplicateDependencies(tasksData);
expect(result.tasks[0].dependencies).toEqual([2, 3]);
expect(result.tasks[1].dependencies).toEqual([3]);
expect(result.tasks[2].dependencies).toEqual([]);
});
test('should handle empty dependencies array', () => {
const tasksData = {
tasks: [
{ id: 1, dependencies: [] },
{ id: 2, dependencies: [1] }
]
};
const result = removeDuplicateDependencies(tasksData);
expect(result.tasks[0].dependencies).toEqual([]);
expect(result.tasks[1].dependencies).toEqual([1]);
});
test('should handle tasks with no dependencies property', () => {
const tasksData = {
tasks: [
{ id: 1 }, // No dependencies property
{ id: 2, dependencies: [1] }
]
};
const result = removeDuplicateDependencies(tasksData);
expect(result.tasks[0]).not.toHaveProperty('dependencies');
expect(result.tasks[1].dependencies).toEqual([1]);
});
});
describe('cleanupSubtaskDependencies function', () => {
test('should remove dependencies to non-existent subtasks', () => {
const tasksData = {
tasks: [
{
id: 1,
dependencies: [],
subtasks: [
{ id: 1, dependencies: [] },
{ id: 2, dependencies: [3] } // Dependency 3 doesn't exist
]
},
{
id: 2,
dependencies: ['1.2'], // Valid subtask dependency
subtasks: [
{ id: 1, dependencies: ['1.1'] } // Valid subtask dependency
]
}
]
};
const result = cleanupSubtaskDependencies(tasksData);
// Should remove the invalid dependency to subtask 3
expect(result.tasks[0].subtasks[1].dependencies).toEqual([]);
// Should keep valid dependencies
expect(result.tasks[1].dependencies).toEqual(['1.2']);
expect(result.tasks[1].subtasks[0].dependencies).toEqual(['1.1']);
});
test('should handle tasks without subtasks', () => {
const tasksData = {
tasks: [
{ id: 1, dependencies: [] },
{ id: 2, dependencies: [1] }
]
};
const result = cleanupSubtaskDependencies(tasksData);
// Should return the original data unchanged
expect(result).toEqual(tasksData);
});
});
describe('ensureAtLeastOneIndependentSubtask function', () => {
test('should clear dependencies of first subtask if none are independent', () => {
const tasksData = {
tasks: [
{
id: 1,
subtasks: [
{ id: 1, dependencies: [2] },
{ id: 2, dependencies: [1] }
]
}
]
};
const result = ensureAtLeastOneIndependentSubtask(tasksData);
expect(result).toBe(true);
expect(tasksData.tasks[0].subtasks[0].dependencies).toEqual([]);
expect(tasksData.tasks[0].subtasks[1].dependencies).toEqual([1]);
});
test('should not modify tasks if at least one subtask is independent', () => {
const tasksData = {
tasks: [
{
id: 1,
subtasks: [
{ id: 1, dependencies: [] },
{ id: 2, dependencies: [1] }
]
}
]
};
const result = ensureAtLeastOneIndependentSubtask(tasksData);
expect(result).toBe(false);
expect(tasksData.tasks[0].subtasks[0].dependencies).toEqual([]);
expect(tasksData.tasks[0].subtasks[1].dependencies).toEqual([1]);
});
test('should handle tasks without subtasks', () => {
const tasksData = {
tasks: [{ id: 1 }, { id: 2, dependencies: [1] }]
};
const result = ensureAtLeastOneIndependentSubtask(tasksData);
expect(result).toBe(false);
expect(tasksData).toEqual({
tasks: [{ id: 1 }, { id: 2, dependencies: [1] }]
});
});
test('should handle empty subtasks array', () => {
const tasksData = {
tasks: [{ id: 1, subtasks: [] }]
};
const result = ensureAtLeastOneIndependentSubtask(tasksData);
expect(result).toBe(false);
expect(tasksData).toEqual({
tasks: [{ id: 1, subtasks: [] }]
});
});
});
describe('validateAndFixDependencies function', () => {
test('should fix multiple dependency issues and return true if changes made', () => {
const tasksData = {
tasks: [
{
id: 1,
dependencies: [1, 1, 99], // Self-dependency and duplicate and invalid dependency
subtasks: [
{ id: 1, dependencies: [2, 2] }, // Duplicate dependencies
{ id: 2, dependencies: [1] }
]
},
{
id: 2,
dependencies: [1],
subtasks: [
{ id: 1, dependencies: [99] } // Invalid dependency
]
}
]
};
// Mock taskExists for validating dependencies
mockTaskExists.mockImplementation((tasks, id) => {
// Convert id to string for comparison
const idStr = String(id);
// Handle subtask references (e.g., "1.2")
if (idStr.includes('.')) {
const [parentId, subtaskId] = idStr.split('.').map(Number);
const task = tasks.find((t) => t.id === parentId);
return (
task &&
task.subtasks &&
task.subtasks.some((st) => st.id === subtaskId)
);
}
// Handle regular task references
const taskId = parseInt(idStr, 10);
return taskId === 1 || taskId === 2; // Only tasks 1 and 2 exist
});
// Make a copy for verification that original is modified
const originalData = JSON.parse(JSON.stringify(tasksData));
const result = validateAndFixDependencies(tasksData);
expect(result).toBe(true);
// Check that data has been modified
expect(tasksData).not.toEqual(originalData);
// Check specific changes
// 1. Self-dependency removed
expect(tasksData.tasks[0].dependencies).not.toContain(1);
// 2. Invalid dependency removed
expect(tasksData.tasks[0].dependencies).not.toContain(99);
// 3. Dependencies have been deduplicated
if (tasksData.tasks[0].subtasks[0].dependencies.length > 0) {
expect(tasksData.tasks[0].subtasks[0].dependencies).toEqual(
expect.arrayContaining([])
);
}
// 4. Invalid subtask dependency removed
expect(tasksData.tasks[1].subtasks[0].dependencies).toEqual([]);
// IMPORTANT: Verify no calls to writeJSON with actual tasks.json
expect(mockWriteJSON).not.toHaveBeenCalledWith(
'tasks/tasks.json',
expect.anything()
);
});
test('should return false if no changes needed', () => {
const tasksData = {
tasks: [
{
id: 1,
dependencies: [],
subtasks: [
{ id: 1, dependencies: [] }, // Already has an independent subtask
{ id: 2, dependencies: ['1.1'] }
]
},
{
id: 2,
dependencies: [1]
}
]
};
// Mock taskExists to validate all dependencies as valid
mockTaskExists.mockImplementation((tasks, id) => {
// Convert id to string for comparison
const idStr = String(id);
// Handle subtask references
if (idStr.includes('.')) {
const [parentId, subtaskId] = idStr.split('.').map(Number);
const task = tasks.find((t) => t.id === parentId);
return (
task &&
task.subtasks &&
task.subtasks.some((st) => st.id === subtaskId)
);
}
// Handle regular task references
const taskId = parseInt(idStr, 10);
return taskId === 1 || taskId === 2;
});
const originalData = JSON.parse(JSON.stringify(tasksData));
const result = validateAndFixDependencies(tasksData);
expect(result).toBe(false);
// Verify data is unchanged
expect(tasksData).toEqual(originalData);
// IMPORTANT: Verify no calls to writeJSON with actual tasks.json
expect(mockWriteJSON).not.toHaveBeenCalledWith(
'tasks/tasks.json',
expect.anything()
);
});
test('should handle invalid input', () => {
expect(validateAndFixDependencies(null)).toBe(false);
expect(validateAndFixDependencies({})).toBe(false);
expect(validateAndFixDependencies({ tasks: null })).toBe(false);
expect(validateAndFixDependencies({ tasks: 'not an array' })).toBe(false);
// IMPORTANT: Verify no calls to writeJSON with actual tasks.json
expect(mockWriteJSON).not.toHaveBeenCalledWith(
'tasks/tasks.json',
expect.anything()
);
});
test('should save changes when tasksPath is provided', () => {
const tasksData = {
tasks: [
{
id: 1,
dependencies: [1, 1], // Self-dependency and duplicate
subtasks: [
{ id: 1, dependencies: [99] } // Invalid dependency
]
}
]
};
// Mock taskExists for this specific test
mockTaskExists.mockImplementation((tasks, id) => {
// Convert id to string for comparison
const idStr = String(id);
// Handle subtask references
if (idStr.includes('.')) {
const [parentId, subtaskId] = idStr.split('.').map(Number);
const task = tasks.find((t) => t.id === parentId);
return (
task &&
task.subtasks &&
task.subtasks.some((st) => st.id === subtaskId)
);
}
// Handle regular task references
const taskId = parseInt(idStr, 10);
return taskId === 1; // Only task 1 exists
});
// Copy the original data to verify changes
const originalData = JSON.parse(JSON.stringify(tasksData));
// Call the function with our test path instead of the actual tasks.json
const result = validateAndFixDependencies(tasksData, TEST_TASKS_PATH);
// First verify that the result is true (changes were made)
expect(result).toBe(true);
// Verify the data was modified
expect(tasksData).not.toEqual(originalData);
// IMPORTANT: Verify no calls to writeJSON with actual tasks.json
expect(mockWriteJSON).not.toHaveBeenCalledWith(
'tasks/tasks.json',
expect.anything()
);
});
});
describe('canMoveWithDependencies', () => {
it('should return canMove: false when conflicts exist', () => {
const allTasks = [
{
id: 1,
tag: 'source',
dependencies: [2],
title: 'Task 1'
},
{
id: 2,
tag: 'other',
dependencies: [],
title: 'Task 2'
}
];
const result = canMoveWithDependencies('1', 'source', 'target', allTasks);
expect(result.canMove).toBe(false);
expect(result.conflicts).toBeDefined();
expect(result.conflicts.length).toBeGreaterThan(0);
expect(result.dependentTaskIds).toBeDefined();
});
it('should return canMove: true when no conflicts exist', () => {
const allTasks = [
{
id: 1,
tag: 'source',
dependencies: [],
title: 'Task 1'
},
{
id: 2,
tag: 'target',
dependencies: [],
title: 'Task 2'
}
];
const result = canMoveWithDependencies('1', 'source', 'target', allTasks);
expect(result.canMove).toBe(true);
expect(result.conflicts).toBeDefined();
expect(result.conflicts.length).toBe(0);
expect(result.dependentTaskIds).toBeDefined();
expect(result.dependentTaskIds.length).toBe(0);
});
it('should handle subtask lookup correctly', () => {
const allTasks = [
{
id: 1,
tag: 'source',
dependencies: [],
title: 'Parent Task',
subtasks: [
{
id: 1,
dependencies: [2],
title: 'Subtask 1'
}
]
},
{
id: 2,
tag: 'other',
dependencies: [],
title: 'Task 2'
}
];
const result = canMoveWithDependencies(
'1.1',
'source',
'target',
allTasks
);
expect(result.canMove).toBe(false);
expect(result.conflicts).toBeDefined();
expect(result.conflicts.length).toBeGreaterThan(0);
});
it('should return error when task not found', () => {
const allTasks = [
{
id: 1,
tag: 'source',
dependencies: [],
title: 'Task 1'
}
];
const result = canMoveWithDependencies(
'999',
'source',
'target',
allTasks
);
expect(result.canMove).toBe(false);
expect(result.error).toBe('Task not found');
expect(result.dependentTaskIds).toEqual([]);
expect(result.conflicts).toEqual([]);
});
});
});