chore: improve unit tests on kiro rules

This commit is contained in:
Ralph Khreish
2025-07-22 21:52:35 +03:00
parent 8c9889be1a
commit e362670aed

View File

@@ -5,7 +5,15 @@ jest.mock('fs', () => ({
readFileSync: jest.fn(),
writeFileSync: jest.fn(),
existsSync: jest.fn(),
mkdirSync: jest.fn()
mkdirSync: jest.fn(),
readdirSync: jest.fn(),
copyFileSync: jest.fn()
}));
// Mock the log function
jest.mock('../../../scripts/modules/utils.js', () => ({
log: jest.fn(),
isSilentMode: jest.fn().mockReturnValue(false)
}));
// Import modules after mocking
@@ -22,6 +30,7 @@ describe('Kiro Rule Transformer', () => {
const mockConsoleError = jest
.spyOn(console, 'error')
.mockImplementation(() => {});
jest.spyOn(console, 'log').mockImplementation(() => {});
beforeEach(() => {
jest.clearAllMocks();
@@ -209,7 +218,114 @@ Use the .mdc extension for all rule files.`;
'rules/cursor_rules.mdc': 'kiro_rules.md',
'rules/dev_workflow.mdc': 'dev_workflow.md',
'rules/self_improve.mdc': 'self_improve.md',
'rules/taskmaster.mdc': 'taskmaster.md'
'rules/taskmaster.mdc': 'taskmaster.md',
'rules/taskmaster_hooks_workflow.mdc': 'taskmaster_hooks_workflow.md'
});
});
describe('onPostConvert lifecycle hook', () => {
const mockReaddirSync = jest.spyOn(fs, 'readdirSync');
const mockCopyFileSync = jest.spyOn(fs, 'copyFileSync');
beforeEach(() => {
jest.clearAllMocks();
// Setup default mock implementation that doesn't throw
mockCopyFileSync.mockImplementation(() => {});
});
it('should copy hook files when kiro-hooks directory exists', () => {
const projectRoot = '/test/project';
const assetsDir = '/test/assets';
const hookFiles = [
'tm-test-hook1.kiro.hook',
'tm-test-hook2.kiro.hook',
'not-a-hook.txt'
];
// Mock directory existence
mockExistsSync.mockImplementation((path) => {
if (path === '/test/assets/kiro-hooks') return true;
if (path === '/test/project/.kiro/hooks') return false;
return true;
});
// Mock reading hook files
mockReaddirSync.mockReturnValue(hookFiles);
// Call the lifecycle hook
kiroProfile.onPostConvertRulesProfile(projectRoot, assetsDir);
// Verify hooks directory was created
expect(mockMkdirSync).toHaveBeenCalledWith('/test/project/.kiro/hooks', {
recursive: true
});
// Verify only .kiro.hook files were copied
expect(mockCopyFileSync).toHaveBeenCalledTimes(2);
expect(mockCopyFileSync).toHaveBeenCalledWith(
'/test/assets/kiro-hooks/tm-test-hook1.kiro.hook',
'/test/project/.kiro/hooks/tm-test-hook1.kiro.hook'
);
expect(mockCopyFileSync).toHaveBeenCalledWith(
'/test/assets/kiro-hooks/tm-test-hook2.kiro.hook',
'/test/project/.kiro/hooks/tm-test-hook2.kiro.hook'
);
});
it('should handle case when hooks directory already exists', () => {
const projectRoot = '/test/project';
const assetsDir = '/test/assets';
const hookFiles = ['tm-test-hook.kiro.hook'];
// Mock all directories exist
mockExistsSync.mockReturnValue(true);
mockReaddirSync.mockReturnValue(hookFiles);
// Call the lifecycle hook
kiroProfile.onPostConvertRulesProfile(projectRoot, assetsDir);
// Verify hooks directory was NOT created (already exists)
expect(mockMkdirSync).not.toHaveBeenCalled();
// Verify hook was copied
expect(mockCopyFileSync).toHaveBeenCalledWith(
'/test/assets/kiro-hooks/tm-test-hook.kiro.hook',
'/test/project/.kiro/hooks/tm-test-hook.kiro.hook'
);
});
it('should handle case when kiro-hooks source directory does not exist', () => {
const projectRoot = '/test/project';
const assetsDir = '/test/assets';
// Mock source directory doesn't exist
mockExistsSync.mockImplementation((path) => {
if (path === '/test/assets/kiro-hooks') return false;
return true;
});
// Call the lifecycle hook
kiroProfile.onPostConvertRulesProfile(projectRoot, assetsDir);
// Verify no files were copied
expect(mockReaddirSync).not.toHaveBeenCalled();
expect(mockCopyFileSync).not.toHaveBeenCalled();
});
it('should handle case when no hook files exist in source directory', () => {
const projectRoot = '/test/project';
const assetsDir = '/test/assets';
// Mock directory exists but has no hook files
mockExistsSync.mockReturnValue(true);
mockReaddirSync.mockReturnValue(['readme.txt', 'config.json']);
// Call the lifecycle hook
kiroProfile.onPostConvertRulesProfile(projectRoot, assetsDir);
// Verify no files were copied
expect(mockCopyFileSync).not.toHaveBeenCalled();
});
});
});