mock fs for transformer tests
This commit is contained in:
@@ -1,37 +1,42 @@
|
||||
import { jest } from '@jest/globals';
|
||||
|
||||
// Mock fs module before importing anything that uses it
|
||||
jest.mock('fs', () => ({
|
||||
readFileSync: jest.fn(),
|
||||
writeFileSync: jest.fn(),
|
||||
existsSync: jest.fn(),
|
||||
mkdirSync: jest.fn()
|
||||
}));
|
||||
|
||||
// Import modules after mocking
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { dirname } from 'path';
|
||||
import {
|
||||
convertAllRulesToProfileRules,
|
||||
convertRuleToProfileRule,
|
||||
getRulesProfile
|
||||
} from '../../../src/utils/rule-transformer.js';
|
||||
import { convertRuleToProfileRule } from '../../../src/utils/rule-transformer.js';
|
||||
import { cursorProfile } from '../../../scripts/profiles/cursor.js';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(__filename);
|
||||
|
||||
describe('Cursor Rule Transformer', () => {
|
||||
const testDir = path.join(__dirname, 'temp-test-dir');
|
||||
// Set up spies on the mocked modules
|
||||
const mockReadFileSync = jest.spyOn(fs, 'readFileSync');
|
||||
const mockWriteFileSync = jest.spyOn(fs, 'writeFileSync');
|
||||
const mockExistsSync = jest.spyOn(fs, 'existsSync');
|
||||
const mockMkdirSync = jest.spyOn(fs, 'mkdirSync');
|
||||
const mockConsoleError = jest
|
||||
.spyOn(console, 'error')
|
||||
.mockImplementation(() => {});
|
||||
|
||||
beforeAll(() => {
|
||||
// Create test directory
|
||||
if (!fs.existsSync(testDir)) {
|
||||
fs.mkdirSync(testDir, { recursive: true });
|
||||
}
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
// Setup default mocks
|
||||
mockReadFileSync.mockReturnValue('');
|
||||
mockWriteFileSync.mockImplementation(() => {});
|
||||
mockExistsSync.mockReturnValue(true);
|
||||
mockMkdirSync.mockImplementation(() => {});
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
// Clean up test directory
|
||||
if (fs.existsSync(testDir)) {
|
||||
fs.rmSync(testDir, { recursive: true, force: true });
|
||||
}
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('should correctly convert basic terms', () => {
|
||||
// Create a test Cursor rule file with basic terms
|
||||
const testCursorRule = path.join(testDir, 'basic-terms.mdc');
|
||||
const testContent = `---
|
||||
description: Test Cursor rule for basic terms
|
||||
globs: **/*
|
||||
@@ -41,26 +46,35 @@ alwaysApply: true
|
||||
This is a Cursor rule that references cursor.so and uses the word Cursor multiple times.
|
||||
Also has references to .mdc files.`;
|
||||
|
||||
fs.writeFileSync(testCursorRule, testContent);
|
||||
// Mock file read to return our test content
|
||||
mockReadFileSync.mockReturnValue(testContent);
|
||||
|
||||
// Convert it
|
||||
const testCursorOut = path.join(testDir, 'basic-terms.mdc');
|
||||
convertRuleToProfileRule(testCursorRule, testCursorOut, cursorProfile);
|
||||
// Call the actual function
|
||||
const result = convertRuleToProfileRule(
|
||||
'source.mdc',
|
||||
'target.mdc',
|
||||
cursorProfile
|
||||
);
|
||||
|
||||
// Read the converted file
|
||||
const convertedContent = fs.readFileSync(testCursorOut, 'utf8');
|
||||
// Verify the function succeeded
|
||||
expect(result).toBe(true);
|
||||
|
||||
// Verify transformations (should preserve Cursor branding and references)
|
||||
expect(convertedContent).toContain('Cursor rule');
|
||||
expect(convertedContent).toContain('cursor.so');
|
||||
expect(convertedContent).toContain('.mdc');
|
||||
expect(convertedContent).not.toContain('roocode.com');
|
||||
expect(convertedContent).not.toContain('windsurf.com');
|
||||
// Verify file operations were called correctly
|
||||
expect(mockReadFileSync).toHaveBeenCalledWith('source.mdc', 'utf8');
|
||||
expect(mockWriteFileSync).toHaveBeenCalledTimes(1);
|
||||
|
||||
// Get the transformed content that was written
|
||||
const writeCall = mockWriteFileSync.mock.calls[0];
|
||||
const transformedContent = writeCall[1];
|
||||
|
||||
// Verify transformations (Cursor profile should keep everything the same)
|
||||
expect(transformedContent).toContain('Cursor');
|
||||
expect(transformedContent).toContain('cursor.so');
|
||||
expect(transformedContent).toContain('.mdc');
|
||||
expect(transformedContent).toContain('Cursor rule');
|
||||
});
|
||||
|
||||
it('should correctly convert tool references', () => {
|
||||
// Create a test Cursor rule file with tool references
|
||||
const testCursorRule = path.join(testDir, 'tool-refs.mdc');
|
||||
const testContent = `---
|
||||
description: Test Cursor rule for tool references
|
||||
globs: **/*
|
||||
@@ -72,27 +86,31 @@ alwaysApply: true
|
||||
- run_command executes terminal commands
|
||||
- use_mcp connects to external services`;
|
||||
|
||||
fs.writeFileSync(testCursorRule, testContent);
|
||||
// Mock file read to return our test content
|
||||
mockReadFileSync.mockReturnValue(testContent);
|
||||
|
||||
// Convert it
|
||||
const testCursorOut = path.join(testDir, 'tool-refs.mdc');
|
||||
convertRuleToProfileRule(testCursorRule, testCursorOut, cursorProfile);
|
||||
// Call the actual function
|
||||
const result = convertRuleToProfileRule(
|
||||
'source.mdc',
|
||||
'target.mdc',
|
||||
cursorProfile
|
||||
);
|
||||
|
||||
// Read the converted file
|
||||
const convertedContent = fs.readFileSync(testCursorOut, 'utf8');
|
||||
// Verify the function succeeded
|
||||
expect(result).toBe(true);
|
||||
|
||||
// Verify transformations (should preserve Cursor tool references)
|
||||
expect(convertedContent).toContain('search tool');
|
||||
expect(convertedContent).toContain('edit_file tool');
|
||||
expect(convertedContent).toContain('run_command');
|
||||
expect(convertedContent).toContain('use_mcp');
|
||||
expect(convertedContent).not.toContain('apply_diff');
|
||||
expect(convertedContent).not.toContain('search_files');
|
||||
// Get the transformed content that was written
|
||||
const writeCall = mockWriteFileSync.mock.calls[0];
|
||||
const transformedContent = writeCall[1];
|
||||
|
||||
// Verify transformations (Cursor uses standard tool names, so no transformation)
|
||||
expect(transformedContent).toContain('search tool');
|
||||
expect(transformedContent).toContain('edit_file tool');
|
||||
expect(transformedContent).toContain('run_command');
|
||||
expect(transformedContent).toContain('use_mcp');
|
||||
});
|
||||
|
||||
it('should correctly update file references', () => {
|
||||
// Create a test Cursor rule file with file references
|
||||
const testCursorRule = path.join(testDir, 'file-refs.mdc');
|
||||
const testContent = `---
|
||||
description: Test Cursor rule for file references
|
||||
globs: **/*
|
||||
@@ -102,19 +120,97 @@ alwaysApply: true
|
||||
This references [dev_workflow.mdc](mdc:.cursor/rules/dev_workflow.mdc) and
|
||||
[taskmaster.mdc](mdc:.cursor/rules/taskmaster.mdc).`;
|
||||
|
||||
fs.writeFileSync(testCursorRule, testContent);
|
||||
// Mock file read to return our test content
|
||||
mockReadFileSync.mockReturnValue(testContent);
|
||||
|
||||
// Convert it
|
||||
const testCursorOut = path.join(testDir, 'file-refs.mdc');
|
||||
convertRuleToProfileRule(testCursorRule, testCursorOut, cursorProfile);
|
||||
// Call the actual function
|
||||
const result = convertRuleToProfileRule(
|
||||
'source.mdc',
|
||||
'target.mdc',
|
||||
cursorProfile
|
||||
);
|
||||
|
||||
// Read the converted file
|
||||
const convertedContent = fs.readFileSync(testCursorOut, 'utf8');
|
||||
// Verify the function succeeded
|
||||
expect(result).toBe(true);
|
||||
|
||||
// Verify transformations (should preserve Cursor file references)
|
||||
expect(convertedContent).toContain('(mdc:.cursor/rules/dev_workflow.mdc)');
|
||||
expect(convertedContent).toContain('(mdc:.cursor/rules/taskmaster.mdc)');
|
||||
expect(convertedContent).not.toContain('(mdc:.roo/rules/');
|
||||
expect(convertedContent).not.toContain('(mdc:.windsurf/rules/');
|
||||
// Get the transformed content that was written
|
||||
const writeCall = mockWriteFileSync.mock.calls[0];
|
||||
const transformedContent = writeCall[1];
|
||||
|
||||
// Verify transformations (Cursor should keep the same references)
|
||||
expect(transformedContent).toContain(
|
||||
'(mdc:.cursor/rules/dev_workflow.mdc)'
|
||||
);
|
||||
expect(transformedContent).toContain('(mdc:.cursor/rules/taskmaster.mdc)');
|
||||
});
|
||||
|
||||
it('should handle file read errors', () => {
|
||||
// Mock file read to throw an error
|
||||
mockReadFileSync.mockImplementation(() => {
|
||||
throw new Error('File not found');
|
||||
});
|
||||
|
||||
// Call the actual function
|
||||
const result = convertRuleToProfileRule(
|
||||
'nonexistent.mdc',
|
||||
'target.mdc',
|
||||
cursorProfile
|
||||
);
|
||||
|
||||
// Verify the function failed gracefully
|
||||
expect(result).toBe(false);
|
||||
|
||||
// Verify writeFileSync was not called
|
||||
expect(mockWriteFileSync).not.toHaveBeenCalled();
|
||||
|
||||
// Verify error was logged
|
||||
expect(mockConsoleError).toHaveBeenCalledWith(
|
||||
'Error converting rule file: File not found'
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle file write errors', () => {
|
||||
const testContent = 'test content';
|
||||
mockReadFileSync.mockReturnValue(testContent);
|
||||
|
||||
// Mock file write to throw an error
|
||||
mockWriteFileSync.mockImplementation(() => {
|
||||
throw new Error('Permission denied');
|
||||
});
|
||||
|
||||
// Call the actual function
|
||||
const result = convertRuleToProfileRule(
|
||||
'source.mdc',
|
||||
'target.mdc',
|
||||
cursorProfile
|
||||
);
|
||||
|
||||
// Verify the function failed gracefully
|
||||
expect(result).toBe(false);
|
||||
|
||||
// Verify error was logged
|
||||
expect(mockConsoleError).toHaveBeenCalledWith(
|
||||
'Error converting rule file: Permission denied'
|
||||
);
|
||||
});
|
||||
|
||||
it('should create target directory if it does not exist', () => {
|
||||
const testContent = 'test content';
|
||||
mockReadFileSync.mockReturnValue(testContent);
|
||||
|
||||
// Mock directory doesn't exist initially
|
||||
mockExistsSync.mockReturnValue(false);
|
||||
|
||||
// Call the actual function
|
||||
convertRuleToProfileRule(
|
||||
'source.mdc',
|
||||
'some/deep/path/target.mdc',
|
||||
cursorProfile
|
||||
);
|
||||
|
||||
// Verify directory creation was called
|
||||
expect(mockMkdirSync).toHaveBeenCalledWith('some/deep/path', {
|
||||
recursive: true
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user