(config: {
files?: Record<string, { content: string; size?: number }>
errors?: Record<string, { code?: string; message?: string }>
})
| 18 | |
| 19 | // Helper to create a mock filesystem |
| 20 | function 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 | |
| 71 | describe('getFiles', () => { |
| 72 | let isFileIgnoredSpy: ReturnType<typeof spyOn> |
no test coverage detected