| 16 | * Create a local mock file operations interface |
| 17 | */ |
| 18 | export const createLocalFileApi = (): FileOperations & { |
| 19 | getStore: () => Map<string, FileNode>; |
| 20 | } => { |
| 21 | const store = new Map<string, FileNode>(); |
| 22 | |
| 23 | // Initialize root node |
| 24 | store.set('root', { |
| 25 | id: 'root', |
| 26 | name: '', |
| 27 | path: '/', |
| 28 | type: 'folder', |
| 29 | parentId: null, |
| 30 | children: [], |
| 31 | metadata: { |
| 32 | createdAt: Date.now(), |
| 33 | updatedAt: Date.now(), |
| 34 | }, |
| 35 | }); |
| 36 | |
| 37 | const findNodeByPath = (path: string): FileNode | undefined => { |
| 38 | for (const node of store.values()) { |
| 39 | if (node.path === path) { |
| 40 | return node; |
| 41 | } |
| 42 | } |
| 43 | return undefined; |
| 44 | }; |
| 45 | |
| 46 | const listFiles = async (path = '/'): Promise<FileNode[]> => { |
| 47 | const normalizedPath = normalizePath(path); |
| 48 | const parentNode = findNodeByPath(normalizedPath); |
| 49 | |
| 50 | if (!parentNode) { |
| 51 | return []; |
| 52 | } |
| 53 | |
| 54 | if (parentNode.type !== 'folder') { |
| 55 | return [parentNode]; |
| 56 | } |
| 57 | |
| 58 | const children: FileNode[] = []; |
| 59 | for (const childId of parentNode.children || []) { |
| 60 | const child = store.get(childId); |
| 61 | if (child) { |
| 62 | children.push(child); |
| 63 | } |
| 64 | } |
| 65 | return children; |
| 66 | }; |
| 67 | |
| 68 | const readFile = async (path: string): Promise<ReadFileResult> => { |
| 69 | const normalizedPath = normalizePath(path); |
| 70 | const node = findNodeByPath(normalizedPath); |
| 71 | |
| 72 | if (!node) { |
| 73 | throw new Error(`File not found: ${path}`); |
| 74 | } |
| 75 | |