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