* refactor(context): Standardize tag and projectRoot handling across all task tools This commit unifies context management by adopting a boundary-first resolution strategy. All task-scoped tools now resolve `tag` and `projectRoot` at their entry point and forward these values to the underlying direct functions. This approach centralizes context logic, ensuring consistent behavior and enhanced flexibility in multi-tag environments. * fix(tag): Clean up tag handling in task functions and sync process This commit refines the handling of the `tag` parameter across multiple functions, ensuring consistent context management. The `tag` is now passed more efficiently in `listTasksDirect`, `setTaskStatusDirect`, and `syncTasksToReadme`, improving clarity and reducing redundancy. Additionally, a TODO comment has been added in `sync-readme.js` to address future tag support enhancements. * feat(tag): Implement Boundary-First Tag Resolution for consistent tag handling This commit introduces Boundary-First Tag Resolution in the task manager, ensuring consistent and deterministic tag handling across CLI and MCP. This change resolves potential race conditions and improves the reliability of tag-specific operations. Additionally, the `expandTask` function has been updated to use the resolved tag when writing JSON, enhancing data integrity during task updates. * chore(biome): formatting * fix(expand-task): Update writeJSON call to use tag instead of resolvedTag * fix(commands): Enhance complexity report path resolution and task initialization `resolveComplexityReportPath` function to streamline output path generation based on tag context and user-defined output. - Improved clarity and maintainability of command handling by centralizing path resolution logic. * Fix: unknown currentTag * fix(task-manager): Update generateTaskFiles calls to include tag and projectRoot parameters This commit modifies the `moveTask` and `updateSubtaskById` functions to pass the `tag` and `projectRoot` parameters to the `generateTaskFiles` function. This ensures that task files are generated with the correct context when requested, enhancing consistency in task management operations. * fix(commands): Refactor tag handling and complexity report path resolution This commit updates the `registerCommands` function to utilize `taskMaster.getCurrentTag()` for consistent tag retrieval across command actions. It also enhances the initialization of `TaskMaster` by passing the tag directly, improving clarity and maintainability. The complexity report path resolution is streamlined to ensure correct file naming based on the current tag context. * fix(task-master): Update complexity report path expectations in tests This commit modifies the `initTaskMaster` test to expect a valid string for the complexity report path, ensuring it matches the expected file naming convention. This change enhances test reliability by verifying the correct output format when the path is generated. * fix(set-task-status): Enhance logging and tag resolution in task status updates This commit improves the logging output in the `registerSetTaskStatusTool` function to include the tag context when setting task statuses. It also updates the tag handling by resolving the tag using the `resolveTag` utility, ensuring that the correct tag is used when updating task statuses. Additionally, the `setTaskStatus` function is modified to remove the tag parameter from the `readJSON` and `writeJSON` calls, streamlining the data handling process. * fix(commands, expand-task, task-manager): Add complexity report option and enhance path handling This commit introduces a new `--complexity-report` option in the `registerCommands` function, allowing users to specify a custom path for the complexity report. The `expandTask` function is updated to accept the `complexityReportPath` from the context, ensuring it is utilized correctly during task expansion. Additionally, the `setTaskStatus` function now includes the `tag` parameter in the `readJSON` and `writeJSON` calls, improving task status updates with proper context. The `initTaskMaster` function is also modified to create parent directories for output paths, enhancing file handling robustness. * fix(expand-task): Add complexityReportPath to context for task expansion tests This commit updates the test for the `expandTask` function by adding the `complexityReportPath` to the context object. This change ensures that the complexity report path is correctly utilized in the test, aligning with recent enhancements to complexity report handling in the task manager. * chore: implement suggested changes * fix(parse-prd): Clarify tag parameter description for task organization Updated the documentation for the `tag` parameter in the `parse-prd.js` file to provide a clearer context on its purpose for organizing tasks into separate task lists. * Fix Inconsistent tag resolution pattern. * fix: Enhance complexity report path handling with tag support This commit updates various functions to incorporate the `tag` parameter when resolving complexity report paths. The `expandTaskDirect`, `resolveComplexityReportPath`, and related tools now utilize the current tag context, improving consistency in task management. Additionally, the complexity report path is now correctly passed through the context in the `expand-task` and `set-task-status` tools, ensuring accurate report retrieval based on the active tag. * Updated the JSDoc for the `tag` parameter in the `show-task.js` file. * Remove redundant comment on tag parameter in readJSON call * Remove unused import for getTagAwareFilePath * Add missed complexityReportPath to args for task expansion * fix(tests): Enhance research tests with tag-aware functionality This commit updates the `research.test.js` file to improve the testing of the `performResearch` function by incorporating tag-aware functionality. Key changes include mocking the `findProjectRoot` to return a valid path, enhancing the `ContextGatherer` and `FuzzyTaskSearch` mocks, and adding comprehensive tests for tag parameter handling in various scenarios. The tests now cover passing different tag values, ensuring correct behavior when tags are provided, undefined, or null, and validating the integration of tags in task discovery and context gathering processes. * Remove unused import for * fix: Refactor complexity report path handling and improve argument destructuring This commit enhances the `expandTaskDirect` function by improving the destructuring of arguments for better readability. It also updates the `analyze.js` and `analyze-task-complexity.js` files to utilize the new `resolveComplexityReportOutputPath` function, ensuring tag-aware resolution of output paths. Additionally, logging has been added to provide clarity on the report path being used. * test: Add complexity report tag isolation tests and improve path handling This commit introduces a new test file for complexity report tag isolation, ensuring that different tags maintain separate complexity reports. It enhances the existing tests in `analyze-task-complexity.test.js` by updating expectations to use `expect.stringContaining` for file paths, improving robustness against path changes. The new tests cover various scenarios, including path resolution and report generation for both master and feature tags, ensuring no cross-tag contamination occurs. * Update scripts/modules/task-manager/list-tasks.js Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update scripts/modules/task-manager/list-tasks.js Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * test(complexity-report): Fix tag slugification in filename expectations - Update mocks to use slugifyTagForFilePath for cross-platform compatibility - Replace raw tag values with slugified versions in expected filenames - Fix test expecting 'feature/user-auth-v2' to expect 'feature-user-auth-v2' - Align test with actual filename generation logic that sanitizes special chars --------- Co-authored-by: Ralph Khreish <35776126+Crunchyman-ralph@users.noreply.github.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
442 lines
13 KiB
JavaScript
442 lines
13 KiB
JavaScript
/**
|
|
* Tests for task-master.js initTaskMaster function
|
|
*/
|
|
|
|
import { jest } from '@jest/globals';
|
|
import path from 'path';
|
|
import fs from 'fs';
|
|
import os from 'os';
|
|
import { initTaskMaster, TaskMaster } from '../../src/task-master.js';
|
|
import {
|
|
TASKMASTER_DIR,
|
|
TASKMASTER_TASKS_FILE,
|
|
LEGACY_CONFIG_FILE,
|
|
TASKMASTER_CONFIG_FILE,
|
|
LEGACY_TASKS_FILE
|
|
} from '../../src/constants/paths.js';
|
|
|
|
// Mock the console to prevent noise during tests
|
|
jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
describe('initTaskMaster', () => {
|
|
let tempDir;
|
|
let originalCwd;
|
|
|
|
beforeEach(() => {
|
|
// Create a temporary directory for testing
|
|
tempDir = fs.realpathSync(
|
|
fs.mkdtempSync(path.join(os.tmpdir(), 'taskmaster-test-'))
|
|
);
|
|
originalCwd = process.cwd();
|
|
|
|
// Clear all mocks
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
afterEach(() => {
|
|
// Restore original working directory
|
|
process.chdir(originalCwd);
|
|
|
|
// Clean up temporary directory
|
|
if (fs.existsSync(tempDir)) {
|
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
describe('Project root detection', () => {
|
|
test('should find project root when .taskmaster directory exists', () => {
|
|
// Arrange - Create .taskmaster directory in temp dir
|
|
const taskMasterDir = path.join(tempDir, TASKMASTER_DIR);
|
|
fs.mkdirSync(taskMasterDir, { recursive: true });
|
|
|
|
// Change to temp directory
|
|
process.chdir(tempDir);
|
|
|
|
// Act
|
|
const taskMaster = initTaskMaster({});
|
|
|
|
// Assert
|
|
expect(taskMaster.getProjectRoot()).toBe(tempDir);
|
|
expect(taskMaster).toBeInstanceOf(TaskMaster);
|
|
});
|
|
|
|
test('should find project root when legacy config file exists', () => {
|
|
// Arrange - Create legacy config file in temp dir
|
|
const legacyConfigPath = path.join(tempDir, LEGACY_CONFIG_FILE);
|
|
fs.writeFileSync(legacyConfigPath, '{}');
|
|
|
|
// Change to temp directory
|
|
process.chdir(tempDir);
|
|
|
|
// Act
|
|
const taskMaster = initTaskMaster({});
|
|
|
|
// Assert
|
|
expect(taskMaster.getProjectRoot()).toBe(tempDir);
|
|
});
|
|
|
|
test('should find project root from subdirectory', () => {
|
|
// Arrange - Create .taskmaster directory in temp dir
|
|
const taskMasterDir = path.join(tempDir, TASKMASTER_DIR);
|
|
fs.mkdirSync(taskMasterDir, { recursive: true });
|
|
|
|
// Create a subdirectory and change to it
|
|
const srcDir = path.join(tempDir, 'src');
|
|
fs.mkdirSync(srcDir, { recursive: true });
|
|
process.chdir(srcDir);
|
|
|
|
// Act
|
|
const taskMaster = initTaskMaster({});
|
|
|
|
// Assert
|
|
expect(taskMaster.getProjectRoot()).toBe(tempDir);
|
|
});
|
|
|
|
test('should find project root from deeply nested subdirectory', () => {
|
|
// Arrange - Create .taskmaster directory in temp dir
|
|
const taskMasterDir = path.join(tempDir, TASKMASTER_DIR);
|
|
fs.mkdirSync(taskMasterDir, { recursive: true });
|
|
|
|
// Create deeply nested subdirectory and change to it
|
|
const deepDir = path.join(tempDir, 'src', 'components', 'ui');
|
|
fs.mkdirSync(deepDir, { recursive: true });
|
|
process.chdir(deepDir);
|
|
|
|
// Act
|
|
const taskMaster = initTaskMaster({});
|
|
|
|
// Assert
|
|
expect(taskMaster.getProjectRoot()).toBe(tempDir);
|
|
});
|
|
|
|
test('should throw error when no project markers found', () => {
|
|
// Arrange - Empty temp directory, no project markers
|
|
process.chdir(tempDir);
|
|
|
|
// Act & Assert
|
|
expect(() => {
|
|
initTaskMaster({});
|
|
}).toThrow(
|
|
'Unable to find project root. No project markers found. Run "init" command first.'
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('Project root override validation', () => {
|
|
test('should accept valid project root override with .taskmaster directory', () => {
|
|
// Arrange - Create .taskmaster directory in temp dir
|
|
const taskMasterDir = path.join(tempDir, TASKMASTER_DIR);
|
|
fs.mkdirSync(taskMasterDir, { recursive: true });
|
|
|
|
// Act
|
|
const taskMaster = initTaskMaster({ projectRoot: tempDir });
|
|
|
|
// Assert
|
|
expect(taskMaster.getProjectRoot()).toBe(tempDir);
|
|
});
|
|
|
|
test('should accept valid project root override with legacy config', () => {
|
|
// Arrange - Create legacy config file in temp dir
|
|
const legacyConfigPath = path.join(tempDir, LEGACY_CONFIG_FILE);
|
|
fs.writeFileSync(legacyConfigPath, '{}');
|
|
|
|
// Act
|
|
const taskMaster = initTaskMaster({ projectRoot: tempDir });
|
|
|
|
// Assert
|
|
expect(taskMaster.getProjectRoot()).toBe(tempDir);
|
|
});
|
|
|
|
test('should throw error when project root override does not exist', () => {
|
|
// Arrange - Non-existent path
|
|
const nonExistentPath = path.join(tempDir, 'does-not-exist');
|
|
|
|
// Act & Assert
|
|
expect(() => {
|
|
initTaskMaster({ projectRoot: nonExistentPath });
|
|
}).toThrow(
|
|
`Project root override path does not exist: ${nonExistentPath}`
|
|
);
|
|
});
|
|
|
|
test('should throw error when project root override has no project markers', () => {
|
|
// Arrange - Empty temp directory (no project markers)
|
|
|
|
// Act & Assert
|
|
expect(() => {
|
|
initTaskMaster({ projectRoot: tempDir });
|
|
}).toThrow(
|
|
`Project root override is not a valid taskmaster project: ${tempDir}`
|
|
);
|
|
});
|
|
|
|
test('should resolve relative project root override', () => {
|
|
// Arrange - Create .taskmaster directory in temp dir
|
|
const taskMasterDir = path.join(tempDir, TASKMASTER_DIR);
|
|
fs.mkdirSync(taskMasterDir, { recursive: true });
|
|
|
|
// Create subdirectory and change to it
|
|
const srcDir = path.join(tempDir, 'src');
|
|
fs.mkdirSync(srcDir, { recursive: true });
|
|
process.chdir(srcDir);
|
|
|
|
// Act - Use relative path '../' to go back to project root
|
|
const taskMaster = initTaskMaster({ projectRoot: '../' });
|
|
|
|
// Assert
|
|
expect(taskMaster.getProjectRoot()).toBe(tempDir);
|
|
});
|
|
});
|
|
|
|
describe('Path resolution with boolean logic', () => {
|
|
let taskMasterDir, tasksPath, configPath, statePath;
|
|
|
|
beforeEach(() => {
|
|
// Setup a valid project structure
|
|
taskMasterDir = path.join(tempDir, TASKMASTER_DIR);
|
|
fs.mkdirSync(taskMasterDir, { recursive: true });
|
|
|
|
tasksPath = path.join(tempDir, TASKMASTER_TASKS_FILE);
|
|
fs.mkdirSync(path.dirname(tasksPath), { recursive: true });
|
|
fs.writeFileSync(tasksPath, '[]');
|
|
|
|
configPath = path.join(tempDir, TASKMASTER_CONFIG_FILE);
|
|
fs.writeFileSync(configPath, '{}');
|
|
|
|
statePath = path.join(taskMasterDir, 'state.json');
|
|
fs.writeFileSync(statePath, '{}');
|
|
|
|
process.chdir(tempDir);
|
|
});
|
|
|
|
test('should return paths when required (true) and files exist', () => {
|
|
// Act
|
|
const taskMaster = initTaskMaster({
|
|
tasksPath: true,
|
|
configPath: true,
|
|
statePath: true
|
|
});
|
|
|
|
// Assert
|
|
expect(taskMaster.getTasksPath()).toBe(tasksPath);
|
|
expect(taskMaster.getConfigPath()).toBe(configPath);
|
|
expect(taskMaster.getStatePath()).toBe(statePath);
|
|
});
|
|
|
|
test('should throw error when required (true) files do not exist', () => {
|
|
// Arrange - Remove tasks file
|
|
fs.unlinkSync(tasksPath);
|
|
|
|
// Act & Assert
|
|
expect(() => {
|
|
initTaskMaster({ tasksPath: true });
|
|
}).toThrow(
|
|
'Required tasks file not found. Searched: .taskmaster/tasks/tasks.json, tasks/tasks.json'
|
|
);
|
|
});
|
|
|
|
test('should return null when optional (false/undefined) files do not exist', () => {
|
|
// Arrange - Remove tasks file
|
|
fs.unlinkSync(tasksPath);
|
|
|
|
// Act
|
|
const taskMaster = initTaskMaster({
|
|
tasksPath: false
|
|
});
|
|
|
|
// Assert
|
|
expect(taskMaster.getTasksPath()).toBeNull();
|
|
});
|
|
|
|
test('should return default paths when optional files not specified in overrides', () => {
|
|
// Arrange - Remove all optional files
|
|
fs.unlinkSync(tasksPath);
|
|
fs.unlinkSync(configPath);
|
|
fs.unlinkSync(statePath);
|
|
|
|
// Act - Don't specify any optional paths
|
|
const taskMaster = initTaskMaster({});
|
|
|
|
// Assert - Should return absolute paths with default locations
|
|
expect(taskMaster.getTasksPath()).toBe(
|
|
path.join(tempDir, TASKMASTER_TASKS_FILE)
|
|
);
|
|
expect(taskMaster.getConfigPath()).toBe(
|
|
path.join(tempDir, TASKMASTER_CONFIG_FILE)
|
|
);
|
|
expect(taskMaster.getStatePath()).toBe(
|
|
path.join(tempDir, TASKMASTER_DIR, 'state.json')
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('String path overrides', () => {
|
|
let taskMasterDir;
|
|
|
|
beforeEach(() => {
|
|
taskMasterDir = path.join(tempDir, TASKMASTER_DIR);
|
|
fs.mkdirSync(taskMasterDir, { recursive: true });
|
|
process.chdir(tempDir);
|
|
});
|
|
|
|
test('should accept valid absolute path override', () => {
|
|
// Arrange - Create custom tasks file
|
|
const customTasksPath = path.join(tempDir, 'custom-tasks.json');
|
|
fs.writeFileSync(customTasksPath, '[]');
|
|
|
|
// Act
|
|
const taskMaster = initTaskMaster({
|
|
tasksPath: customTasksPath
|
|
});
|
|
|
|
// Assert
|
|
expect(taskMaster.getTasksPath()).toBe(customTasksPath);
|
|
});
|
|
|
|
test('should accept valid relative path override', () => {
|
|
// Arrange - Create custom tasks file
|
|
const customTasksPath = path.join(tempDir, 'custom-tasks.json');
|
|
fs.writeFileSync(customTasksPath, '[]');
|
|
|
|
// Act
|
|
const taskMaster = initTaskMaster({
|
|
tasksPath: './custom-tasks.json'
|
|
});
|
|
|
|
// Assert
|
|
expect(taskMaster.getTasksPath()).toBe(customTasksPath);
|
|
});
|
|
|
|
test('should throw error when string path override does not exist', () => {
|
|
// Arrange - Non-existent file path
|
|
const nonExistentPath = path.join(tempDir, 'does-not-exist.json');
|
|
|
|
// Act & Assert
|
|
expect(() => {
|
|
initTaskMaster({ tasksPath: nonExistentPath });
|
|
}).toThrow(`tasks file override path does not exist: ${nonExistentPath}`);
|
|
});
|
|
});
|
|
|
|
describe('Legacy file support', () => {
|
|
beforeEach(() => {
|
|
// Setup basic project structure
|
|
const taskMasterDir = path.join(tempDir, TASKMASTER_DIR);
|
|
fs.mkdirSync(taskMasterDir, { recursive: true });
|
|
process.chdir(tempDir);
|
|
});
|
|
|
|
test('should find legacy tasks file when new format does not exist', () => {
|
|
// Arrange - Create legacy tasks file
|
|
const legacyTasksDir = path.join(tempDir, 'tasks');
|
|
fs.mkdirSync(legacyTasksDir, { recursive: true });
|
|
const legacyTasksPath = path.join(tempDir, LEGACY_TASKS_FILE);
|
|
fs.writeFileSync(legacyTasksPath, '[]');
|
|
|
|
// Act
|
|
const taskMaster = initTaskMaster({ tasksPath: true });
|
|
|
|
// Assert
|
|
expect(taskMaster.getTasksPath()).toBe(legacyTasksPath);
|
|
});
|
|
|
|
test('should prefer new format over legacy when both exist', () => {
|
|
// Arrange - Create both new and legacy files
|
|
const newTasksPath = path.join(tempDir, TASKMASTER_TASKS_FILE);
|
|
fs.mkdirSync(path.dirname(newTasksPath), { recursive: true });
|
|
fs.writeFileSync(newTasksPath, '[]');
|
|
|
|
const legacyTasksDir = path.join(tempDir, 'tasks');
|
|
fs.mkdirSync(legacyTasksDir, { recursive: true });
|
|
const legacyTasksPath = path.join(tempDir, LEGACY_TASKS_FILE);
|
|
fs.writeFileSync(legacyTasksPath, '[]');
|
|
|
|
// Act
|
|
const taskMaster = initTaskMaster({ tasksPath: true });
|
|
|
|
// Assert
|
|
expect(taskMaster.getTasksPath()).toBe(newTasksPath);
|
|
});
|
|
|
|
test('should find legacy config file when new format does not exist', () => {
|
|
// Arrange - Create legacy config file
|
|
const legacyConfigPath = path.join(tempDir, LEGACY_CONFIG_FILE);
|
|
fs.writeFileSync(legacyConfigPath, '{}');
|
|
|
|
// Act
|
|
const taskMaster = initTaskMaster({ configPath: true });
|
|
|
|
// Assert
|
|
expect(taskMaster.getConfigPath()).toBe(legacyConfigPath);
|
|
});
|
|
});
|
|
|
|
describe('TaskMaster class methods', () => {
|
|
test('should return all paths via getAllPaths method', () => {
|
|
// Arrange - Setup project with all files
|
|
const taskMasterDir = path.join(tempDir, TASKMASTER_DIR);
|
|
fs.mkdirSync(taskMasterDir, { recursive: true });
|
|
|
|
const tasksPath = path.join(tempDir, TASKMASTER_TASKS_FILE);
|
|
fs.mkdirSync(path.dirname(tasksPath), { recursive: true });
|
|
fs.writeFileSync(tasksPath, '[]');
|
|
|
|
const configPath = path.join(tempDir, TASKMASTER_CONFIG_FILE);
|
|
fs.writeFileSync(configPath, '{}');
|
|
|
|
process.chdir(tempDir);
|
|
|
|
// Act
|
|
const taskMaster = initTaskMaster({
|
|
tasksPath: true,
|
|
configPath: true
|
|
});
|
|
|
|
// Assert
|
|
const allPaths = taskMaster.getAllPaths();
|
|
expect(allPaths).toEqual(
|
|
expect.objectContaining({
|
|
projectRoot: tempDir,
|
|
taskMasterDir: taskMasterDir,
|
|
tasksPath: tasksPath,
|
|
configPath: configPath
|
|
})
|
|
);
|
|
|
|
// Verify paths object is frozen
|
|
expect(() => {
|
|
allPaths.projectRoot = '/different/path';
|
|
}).toThrow();
|
|
});
|
|
|
|
test('should return correct individual paths', () => {
|
|
// Arrange
|
|
const taskMasterDir = path.join(tempDir, TASKMASTER_DIR);
|
|
fs.mkdirSync(taskMasterDir, { recursive: true });
|
|
process.chdir(tempDir);
|
|
|
|
// Act
|
|
const taskMaster = initTaskMaster({});
|
|
|
|
// Assert
|
|
expect(taskMaster.getProjectRoot()).toBe(tempDir);
|
|
expect(taskMaster.getTaskMasterDir()).toBe(taskMasterDir);
|
|
// Default paths are always set for tasks, config, and state
|
|
expect(taskMaster.getTasksPath()).toBe(
|
|
path.join(tempDir, TASKMASTER_TASKS_FILE)
|
|
);
|
|
expect(taskMaster.getConfigPath()).toBe(
|
|
path.join(tempDir, TASKMASTER_CONFIG_FILE)
|
|
);
|
|
expect(taskMaster.getStatePath()).toBe(
|
|
path.join(taskMasterDir, 'state.json')
|
|
);
|
|
// PRD and complexity report paths are undefined when not provided
|
|
expect(typeof taskMaster.getComplexityReportPath()).toBe('string');
|
|
expect(taskMaster.getComplexityReportPath()).toMatch(
|
|
/task-complexity-report\.json$/
|
|
);
|
|
});
|
|
});
|
|
});
|