|
| 1 | +// Mock file system data |
| 2 | +const mockFiles = new Map() |
| 3 | +const mockDirectories = new Set() |
| 4 | + |
| 5 | +// Initialize base test directories |
| 6 | +const baseTestDirs = [ |
| 7 | + "/mock", |
| 8 | + "/mock/extension", |
| 9 | + "/mock/extension/path", |
| 10 | + "/mock/storage", |
| 11 | + "/mock/storage/path", |
| 12 | + "/mock/settings", |
| 13 | + "/mock/settings/path", |
| 14 | + "/mock/mcp", |
| 15 | + "/mock/mcp/path", |
| 16 | + "/test", |
| 17 | + "/test/path", |
| 18 | + "/test/storage", |
| 19 | + "/test/storage/path", |
| 20 | + "/test/storage/path/settings", |
| 21 | + "/test/extension", |
| 22 | + "/test/extension/path", |
| 23 | + "/test/global-storage", |
| 24 | + "/test/log/path", |
| 25 | +] |
| 26 | + |
| 27 | +// Helper function to format instructions |
| 28 | +const formatInstructions = (sections: string[]): string => { |
| 29 | + const joinedSections = sections.filter(Boolean).join("\n\n") |
| 30 | + return joinedSections |
| 31 | + ? ` |
| 32 | +==== |
| 33 | +
|
| 34 | +USER'S CUSTOM INSTRUCTIONS |
| 35 | +
|
| 36 | +The following additional instructions are provided by the user, and should be followed to the best of your ability without interfering with the TOOL USE guidelines. |
| 37 | +
|
| 38 | +${joinedSections}` |
| 39 | + : "" |
| 40 | +} |
| 41 | + |
| 42 | +// Helper function to format rule content |
| 43 | +const formatRuleContent = (ruleFile: string, content: string): string => { |
| 44 | + return `Rules:\n# Rules from ${ruleFile}:\n${content}` |
| 45 | +} |
| 46 | + |
| 47 | +type RuleFiles = { |
| 48 | + ".clinerules-code": string |
| 49 | + ".clinerules-ask": string |
| 50 | + ".clinerules-architect": string |
| 51 | + ".clinerules-test": string |
| 52 | + ".clinerules-review": string |
| 53 | + ".clinerules": string |
| 54 | +} |
| 55 | + |
| 56 | +// Helper function to ensure directory exists |
| 57 | +const ensureDirectoryExists = (path: string) => { |
| 58 | + const parts = path.split("/") |
| 59 | + let currentPath = "" |
| 60 | + for (const part of parts) { |
| 61 | + if (!part) continue |
| 62 | + currentPath += "/" + part |
| 63 | + mockDirectories.add(currentPath) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +const mockFs = { |
| 68 | + readFile: jest.fn().mockImplementation(async (filePath: string, encoding?: string) => { |
| 69 | + // Return stored content if it exists |
| 70 | + if (mockFiles.has(filePath)) { |
| 71 | + return mockFiles.get(filePath) |
| 72 | + } |
| 73 | + |
| 74 | + // Handle rule files |
| 75 | + const ruleFiles: RuleFiles = { |
| 76 | + ".clinerules-code": "# Code Mode Rules\n1. Code specific rule", |
| 77 | + ".clinerules-ask": "# Ask Mode Rules\n1. Ask specific rule", |
| 78 | + ".clinerules-architect": "# Architect Mode Rules\n1. Architect specific rule", |
| 79 | + ".clinerules-test": |
| 80 | + "# Test Engineer Rules\n1. Always write tests first\n2. Get approval before modifying non-test code", |
| 81 | + ".clinerules-review": |
| 82 | + "# Code Reviewer Rules\n1. Provide specific examples in feedback\n2. Focus on maintainability and best practices", |
| 83 | + ".clinerules": "# Test Rules\n1. First rule\n2. Second rule", |
| 84 | + } |
| 85 | + |
| 86 | + // Check for exact file name match |
| 87 | + const fileName = filePath.split("/").pop() |
| 88 | + if (fileName && fileName in ruleFiles) { |
| 89 | + return ruleFiles[fileName as keyof RuleFiles] |
| 90 | + } |
| 91 | + |
| 92 | + // Check for file name in path |
| 93 | + for (const [ruleFile, content] of Object.entries(ruleFiles)) { |
| 94 | + if (filePath.includes(ruleFile)) { |
| 95 | + return content |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Handle file not found |
| 100 | + const error = new Error(`ENOENT: no such file or directory, open '${filePath}'`) |
| 101 | + ;(error as any).code = "ENOENT" |
| 102 | + throw error |
| 103 | + }), |
| 104 | + |
| 105 | + writeFile: jest.fn().mockImplementation(async (path: string, content: string) => { |
| 106 | + // Ensure parent directory exists |
| 107 | + const parentDir = path.split("/").slice(0, -1).join("/") |
| 108 | + ensureDirectoryExists(parentDir) |
| 109 | + mockFiles.set(path, content) |
| 110 | + return Promise.resolve() |
| 111 | + }), |
| 112 | + |
| 113 | + mkdir: jest.fn().mockImplementation(async (path: string, options?: { recursive?: boolean }) => { |
| 114 | + // Always handle recursive creation |
| 115 | + const parts = path.split("/") |
| 116 | + let currentPath = "" |
| 117 | + |
| 118 | + // For recursive or test/mock paths, create all parent directories |
| 119 | + if (options?.recursive || path.startsWith("/test") || path.startsWith("/mock")) { |
| 120 | + for (const part of parts) { |
| 121 | + if (!part) continue |
| 122 | + currentPath += "/" + part |
| 123 | + mockDirectories.add(currentPath) |
| 124 | + } |
| 125 | + return Promise.resolve() |
| 126 | + } |
| 127 | + |
| 128 | + // For non-recursive paths, verify parent exists |
| 129 | + for (let i = 0; i < parts.length - 1; i++) { |
| 130 | + if (!parts[i]) continue |
| 131 | + currentPath += "/" + parts[i] |
| 132 | + if (!mockDirectories.has(currentPath)) { |
| 133 | + const error = new Error(`ENOENT: no such file or directory, mkdir '${path}'`) |
| 134 | + ;(error as any).code = "ENOENT" |
| 135 | + throw error |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + // Add the final directory |
| 140 | + currentPath += "/" + parts[parts.length - 1] |
| 141 | + mockDirectories.add(currentPath) |
| 142 | + return Promise.resolve() |
| 143 | + return Promise.resolve() |
| 144 | + }), |
| 145 | + |
| 146 | + access: jest.fn().mockImplementation(async (path: string) => { |
| 147 | + // Check if the path exists in either files or directories |
| 148 | + if (mockFiles.has(path) || mockDirectories.has(path) || path.startsWith("/test")) { |
| 149 | + return Promise.resolve() |
| 150 | + } |
| 151 | + const error = new Error(`ENOENT: no such file or directory, access '${path}'`) |
| 152 | + ;(error as any).code = "ENOENT" |
| 153 | + throw error |
| 154 | + }), |
| 155 | + |
| 156 | + constants: jest.requireActual("fs").constants, |
| 157 | + |
| 158 | + // Expose mock data for test assertions |
| 159 | + _mockFiles: mockFiles, |
| 160 | + _mockDirectories: mockDirectories, |
| 161 | + |
| 162 | + // Helper to set up initial mock data |
| 163 | + _setInitialMockData: () => { |
| 164 | + // Set up default MCP settings |
| 165 | + mockFiles.set( |
| 166 | + "/mock/settings/path/cline_mcp_settings.json", |
| 167 | + JSON.stringify({ |
| 168 | + mcpServers: { |
| 169 | + "test-server": { |
| 170 | + command: "node", |
| 171 | + args: ["test.js"], |
| 172 | + disabled: false, |
| 173 | + alwaysAllow: ["existing-tool"], |
| 174 | + }, |
| 175 | + }, |
| 176 | + }), |
| 177 | + ) |
| 178 | + |
| 179 | + // Ensure all base directories exist |
| 180 | + baseTestDirs.forEach((dir) => { |
| 181 | + const parts = dir.split("/") |
| 182 | + let currentPath = "" |
| 183 | + for (const part of parts) { |
| 184 | + if (!part) continue |
| 185 | + currentPath += "/" + part |
| 186 | + mockDirectories.add(currentPath) |
| 187 | + } |
| 188 | + }) |
| 189 | + }, |
| 190 | +} |
| 191 | + |
| 192 | +// Initialize mock data |
| 193 | +mockFs._setInitialMockData() |
| 194 | + |
| 195 | +module.exports = mockFs |
0 commit comments