MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / createMockFs

Function createMockFs

sdk/src/__tests__/read-files.test.ts:20–69  ·  view source on GitHub ↗
(config: {
  files?: Record<string, { content: string; size?: number }>
  errors?: Record<string, { code?: string; message?: string }>
})

Source from the content-addressed store, hash-verified

18
19// Helper to create a mock filesystem
20function createMockFs(config: {
21 files?: Record<string, { content: string; size?: number }>
22 errors?: Record<string, { code?: string; message?: string }>
23}): CodebuffFileSystem {
24 const { files = {}, errors = {} } = config
25
26 return {
27 readFile: async (filePath: PathLike) => {
28 const pathStr = String(filePath)
29 if (errors[pathStr]) {
30 throw createNodeError(
31 errors[pathStr].message || 'Unknown error',
32 errors[pathStr].code || 'UNKNOWN',
33 )
34 }
35 if (files[pathStr]) {
36 return files[pathStr].content
37 }
38 throw createNodeError(
39 `ENOENT: no such file or directory: ${pathStr}`,
40 'ENOENT',
41 )
42 },
43 stat: async (filePath: PathLike) => {
44 const pathStr = String(filePath)
45 if (errors[pathStr]) {
46 throw createNodeError(
47 errors[pathStr].message || 'Unknown error',
48 errors[pathStr].code || 'UNKNOWN',
49 )
50 }
51 if (files[pathStr]) {
52 return {
53 size: files[pathStr].size ?? files[pathStr].content.length,
54 isDirectory: () => false,
55 isFile: () => true,
56 atimeMs: Date.now(),
57 mtimeMs: Date.now(),
58 }
59 }
60 throw createNodeError(
61 `ENOENT: no such file or directory: ${pathStr}`,
62 'ENOENT',
63 )
64 },
65 readdir: async () => [],
66 mkdir: async () => undefined,
67 writeFile: async () => undefined,
68 } as unknown as CodebuffFileSystem
69}
70
71describe('getFiles', () => {
72 let isFileIgnoredSpy: ReturnType<typeof spyOn>

Callers 1

read-files.test.tsFile · 0.70

Calls 1

createNodeErrorFunction · 0.90

Tested by

no test coverage detected