|
| 1 | +import fs from 'fs/promises' |
| 2 | +import path from 'path' |
| 3 | +import os from 'os' |
| 4 | +import { addCustomInstructions } from '../system' |
| 5 | + |
| 6 | +// Mock external dependencies |
| 7 | +jest.mock('os-name', () => () => 'macOS') |
| 8 | +jest.mock('default-shell', () => '/bin/zsh') |
| 9 | +jest.mock('os', () => ({ |
| 10 | + homedir: () => '/Users/test', |
| 11 | + ...jest.requireActual('os') |
| 12 | +})) |
| 13 | + |
| 14 | +describe('system.ts', () => { |
| 15 | + let tempDir: string |
| 16 | + |
| 17 | + beforeEach(async () => { |
| 18 | + // Create a temporary directory for test files |
| 19 | + tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'cline-test-')) |
| 20 | + }) |
| 21 | + |
| 22 | + afterEach(async () => { |
| 23 | + // Clean up temporary directory after each test |
| 24 | + await fs.rm(tempDir, { recursive: true, force: true }) |
| 25 | + }) |
| 26 | + |
| 27 | + describe('addCustomInstructions', () => { |
| 28 | + it('should include content from .clinerules and .cursorrules if present', async () => { |
| 29 | + // Create test rule files |
| 30 | + await fs.writeFile(path.join(tempDir, '.clinerules'), 'Always write tests\nUse TypeScript') |
| 31 | + await fs.writeFile(path.join(tempDir, '.cursorrules'), 'Format code before committing') |
| 32 | + |
| 33 | + const customInstructions = 'Base instructions' |
| 34 | + const result = await addCustomInstructions(customInstructions, tempDir) |
| 35 | + |
| 36 | + // Verify all instructions are included |
| 37 | + expect(result).toContain('Base instructions') |
| 38 | + expect(result).toContain('Always write tests') |
| 39 | + expect(result).toContain('Use TypeScript') |
| 40 | + expect(result).toContain('Format code before committing') |
| 41 | + expect(result).toContain('Rules from .clinerules:') |
| 42 | + expect(result).toContain('Rules from .cursorrules:') |
| 43 | + }) |
| 44 | + |
| 45 | + it('should handle missing rule files gracefully', async () => { |
| 46 | + const customInstructions = 'Base instructions' |
| 47 | + const result = await addCustomInstructions(customInstructions, tempDir) |
| 48 | + |
| 49 | + // Should only contain base instructions |
| 50 | + expect(result).toContain('Base instructions') |
| 51 | + expect(result).not.toContain('Rules from') |
| 52 | + }) |
| 53 | + |
| 54 | + it('should handle empty rule files', async () => { |
| 55 | + // Create empty rule files |
| 56 | + await fs.writeFile(path.join(tempDir, '.clinerules'), '') |
| 57 | + await fs.writeFile(path.join(tempDir, '.cursorrules'), '') |
| 58 | + |
| 59 | + const customInstructions = 'Base instructions' |
| 60 | + const result = await addCustomInstructions(customInstructions, tempDir) |
| 61 | + |
| 62 | + // Should only contain base instructions |
| 63 | + expect(result).toContain('Base instructions') |
| 64 | + expect(result).not.toContain('Rules from') |
| 65 | + }) |
| 66 | + |
| 67 | + it('should handle whitespace-only rule files', async () => { |
| 68 | + // Create rule files with only whitespace |
| 69 | + await fs.writeFile(path.join(tempDir, '.clinerules'), ' \n \t ') |
| 70 | + await fs.writeFile(path.join(tempDir, '.cursorrules'), ' \n ') |
| 71 | + |
| 72 | + const customInstructions = 'Base instructions' |
| 73 | + const result = await addCustomInstructions(customInstructions, tempDir) |
| 74 | + |
| 75 | + // Should only contain base instructions |
| 76 | + expect(result).toContain('Base instructions') |
| 77 | + expect(result).not.toContain('Rules from') |
| 78 | + }) |
| 79 | + |
| 80 | + it('should handle one rule file present and one missing', async () => { |
| 81 | + // Create only .clinerules |
| 82 | + await fs.writeFile(path.join(tempDir, '.clinerules'), 'Always write tests') |
| 83 | + |
| 84 | + const customInstructions = 'Base instructions' |
| 85 | + const result = await addCustomInstructions(customInstructions, tempDir) |
| 86 | + |
| 87 | + // Should contain base instructions and .clinerules content |
| 88 | + expect(result).toContain('Base instructions') |
| 89 | + expect(result).toContain('Always write tests') |
| 90 | + expect(result).toContain('Rules from .clinerules:') |
| 91 | + expect(result).not.toContain('Rules from .cursorrules:') |
| 92 | + }) |
| 93 | + |
| 94 | + it('should handle empty custom instructions with rule files', async () => { |
| 95 | + await fs.writeFile(path.join(tempDir, '.clinerules'), 'Always write tests') |
| 96 | + await fs.writeFile(path.join(tempDir, '.cursorrules'), 'Format code before committing') |
| 97 | + |
| 98 | + const result = await addCustomInstructions('', tempDir) |
| 99 | + |
| 100 | + // Should contain rule file content even with empty custom instructions |
| 101 | + expect(result).toContain('Always write tests') |
| 102 | + expect(result).toContain('Format code before committing') |
| 103 | + expect(result).toContain('Rules from .clinerules:') |
| 104 | + expect(result).toContain('Rules from .cursorrules:') |
| 105 | + }) |
| 106 | + |
| 107 | + it('should return empty string when no instructions or rules exist', async () => { |
| 108 | + const result = await addCustomInstructions('', tempDir) |
| 109 | + expect(result).toBe('') |
| 110 | + }) |
| 111 | + }) |
| 112 | +}) |
0 commit comments