test: fix findTaskById complexity report testcases

This commit is contained in:
Shrey Paharia
2025-04-19 13:24:50 +05:30
parent e045a5268c
commit dcb3f2f9f9

View File

@@ -2,24 +2,7 @@
* Task finder tests * Task finder tests
*/ */
import { jest } from '@jest/globals'; // Import after mocks are set up - No mocks needed for readComplexityReport anymore
// Set up mock before imports
const mockReadComplexityReport = jest.fn().mockReturnValue(null);
// Mock utils module to control readComplexityReport behavior
jest.mock('../../scripts/modules/utils.js', () => {
// Get the original module
const originalModule = jest.requireActual('../../scripts/modules/utils.js');
// Return a modified version
return {
...originalModule,
readComplexityReport: mockReadComplexityReport
};
});
// Import after mocks are set up
import { findTaskById } from '../../scripts/modules/utils.js'; import { findTaskById } from '../../scripts/modules/utils.js';
import { emptySampleTasks, sampleTasks } from '../fixtures/sample-tasks.js'; import { emptySampleTasks, sampleTasks } from '../fixtures/sample-tasks.js';
@@ -64,11 +47,30 @@ describe('Task Finder', () => {
const task = findTaskById(emptySampleTasks.tasks, 1); const task = findTaskById(emptySampleTasks.tasks, 1);
expect(task).toBeNull(); expect(task).toBeNull();
}); });
test('should work correctly when no complexity report is provided', () => {
// Pass null as the complexity report
const task = findTaskById(sampleTasks.tasks, 2, null);
test('should include complexity score when report exists', () => { expect(task).toBeDefined();
// call readComplexityReport expect(task.id).toBe(2);
// Set up mock implementation for this test expect(task.complexityScore).toBeUndefined();
mockReadComplexityReport.mockReturnValue({ });
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 task = findTaskById(sampleTasks.tasks, 2, complexityReport);
expect(task).toBeDefined();
expect(task.id).toBe(2);
expect(task.complexityScore).toBeUndefined();
});
test('should include complexity score when report is provided', () => {
// Define the complexity report for this test
const complexityReport = {
meta: { meta: {
generatedAt: '2023-01-01T00:00:00.000Z', generatedAt: '2023-01-01T00:00:00.000Z',
tasksAnalyzed: 3, tasksAnalyzed: 3,
@@ -94,37 +96,13 @@ describe('Task Finder', () => {
recommendedSubtasks: 4 recommendedSubtasks: 4
} }
] ]
}); };
const task = findTaskById(sampleTasks.tasks, 2); const task = findTaskById(sampleTasks.tasks, 2, complexityReport);
expect(task).toBeDefined(); expect(task).toBeDefined();
expect(task.id).toBe(2); expect(task.id).toBe(2);
expect(task.complexityScore).toBe(8); expect(task.complexityScore).toBe(8);
}); });
test('should work correctly when task has no complexity data', () => {
// Set up mock implementation for this test
mockReadComplexityReport.mockReturnValue({
complexityAnalysis: [{ taskId: 999, complexityScore: 5 }]
});
const task = findTaskById(sampleTasks.tasks, 2);
expect(task).toBeDefined();
expect(task.id).toBe(2);
expect(task.complexityScore).toBeUndefined();
});
test('should work correctly when no complexity report exists', () => {
// Set up mock implementation for this test
mockReadComplexityReport.mockReturnValue(null);
const task = findTaskById(sampleTasks.tasks, 2);
expect(task).toBeDefined();
expect(task.id).toBe(2);
expect(task.complexityScore).toBeUndefined();
});
}); });
}); });