import { describe, it, expect, beforeEach, vi } from 'vitest'; import { ProjectsManager } from './ProjectsManager'; import { JSRuntimeFS } from './JSRuntime'; import { Git } from './git'; import type { NPool } from '@nostrify/nostrify'; const createMockNostr = (): NPool => ({ req: vi.fn(), query: vi.fn(), event: vi.fn(), group: vi.fn(), relay: vi.fn(), relays: new Map(), close: vi.fn(), }) as unknown as NPool; // Mock filesystem for testing class MockFS implements JSRuntimeFS { private files: Map = new Map(); private dirs: Set = new Set(); async readFile(path: string): Promise; async readFile(path: string, encoding: "utf8"): Promise; async readFile(path: string, encoding: string): Promise; async readFile(path: string, encoding?: string): Promise { const content = this.files.get(path); if (content === undefined) { const error = new Error(`ENOENT: no such file or directory, open '${path}'`) as NodeJS.ErrnoException; error.code = 'ENOENT'; throw error; } if (encoding === 'utf8') { if (typeof content === 'string') { return content; } // Convert Uint8Array to string return new TextDecoder('utf-8').decode(content as Uint8Array); } if (typeof content === 'string') { // Convert string to Uint8Array return new TextEncoder().encode(content); } return content as Uint8Array; } async writeFile(path: string, data: string | Uint8Array): Promise { this.files.set(path, data); // Ensure parent directory exists const dir = path.split('/').slice(0, -1).join('/'); if (dir) { this.dirs.add(dir); } } // @ts-expect-error I don't know how to type this properly async readdir(path: string, options?: { withFileTypes?: boolean }): Promise { if (!this.dirs.has(path)) { const error = new Error(`ENOENT: no such file or directory, scandir '${path}'`) as NodeJS.ErrnoException; error.code = 'ENOENT'; throw error; } const entries: string[] = []; // Find subdirectories for (const dir of this.dirs) { if (dir.startsWith(path + '/') && !dir.slice(path.length + 1).includes('/')) { entries.push(dir.slice(path.length + 1)); } } // Find files for (const file of this.files.keys()) { if (file.startsWith(path + '/') && !file.slice(path.length + 1).includes('/')) { entries.push(file.slice(path.length + 1)); } } const uniqueEntries = [...new Set(entries)]; if (options && options.withFileTypes) { // Provide a minimal DirectoryEntry mock return uniqueEntries.map(name => { const fullPath = path.endsWith('/') ? path + name : path + '/' + name; const isDir = this.dirs.has(fullPath); return { name, isDirectory: () => isDir, isFile: () => !isDir, } as unknown as DirectoryEntry; }); } return uniqueEntries; } async mkdir(path: string): Promise { this.dirs.add(path); } async stat(path: string): Promise<{ isDirectory(): boolean; isFile(): boolean; mtimeMs?: number }> { if (this.dirs.has(path)) { return { isDirectory: () => true, isFile: () => false, mtimeMs: Date.now(), }; } if (this.files.has(path)) { return { isDirectory: () => false, isFile: () => true, mtimeMs: Date.now(), }; } const error = new Error(`ENOENT: no such file or directory, stat '${path}'`) as NodeJS.ErrnoException; error.code = 'ENOENT'; throw error; } async unlink(path: string): Promise { this.files.delete(path); } async rmdir(path: string): Promise { this.dirs.delete(path); } async rename(oldPath: string, newPath: string): Promise { // Handle both files and directories if (this.files.has(oldPath)) { const content = this.files.get(oldPath); this.files.delete(oldPath); this.files.set(newPath, content!); } if (this.dirs.has(oldPath)) { this.dirs.delete(oldPath); this.dirs.add(newPath); // Also move all subdirectories and files const itemsToMove: Array<{ oldPath: string; newPath: string; isDir: boolean }> = []; // Collect directories to move for (const dir of this.dirs) { if (dir.startsWith(oldPath + '/')) { const relativePath = dir.slice(oldPath.length); itemsToMove.push({ oldPath: dir, newPath: newPath + relativePath, isDir: true }); } } // Collect files to move for (const file of this.files.keys()) { if (file.startsWith(oldPath + '/')) { const relativePath = file.slice(oldPath.length); itemsToMove.push({ oldPath: file, newPath: newPath + relativePath, isDir: false }); } } // Move all items for (const item of itemsToMove) { if (item.isDir) { this.dirs.delete(item.oldPath); this.dirs.add(item.newPath); } else { const content = this.files.get(item.oldPath); this.files.delete(item.oldPath); this.files.set(item.newPath, content!); } } } } async lstat(path: string): Promise<{ isDirectory(): boolean; isFile(): boolean; mtimeMs?: number }> { return this.stat(path); } } describe('ProjectsManager', () => { let fs: JSRuntimeFS; let git: Git; let nostr: NPool; let projectsManager: ProjectsManager; beforeEach(() => { fs = new MockFS() as unknown as JSRuntimeFS; nostr = createMockNostr(); git = new Git({ fs, nostr }); projectsManager = new ProjectsManager({ fs, git }); }); describe('getProjects', () => { it('should detect any directory as a project', async () => { await projectsManager.init(); // Create any directory await fs.mkdir('/projects/cloned-repo'); const projects = await projectsManager.getProjects(); expect(projects).toHaveLength(1); expect(projects[0].name).toBe('cloned-repo'); // Preserve original directory name expect(projects[0].id).toBe('cloned-repo'); }); it('should detect all directories as projects', async () => { await projectsManager.init(); // Create various directories await fs.mkdir('/projects/my-project'); await fs.mkdir('/projects/another-project'); await fs.mkdir('/projects/empty-folder'); const projects = await projectsManager.getProjects(); expect(projects).toHaveLength(3); const projectNames = projects.map(p => p.name).sort(); expect(projectNames).toEqual(['another-project', 'empty-folder', 'my-project']); }); it('should preserve original project names', async () => { await projectsManager.init(); // Create projects with various naming patterns await fs.mkdir('/projects/my-awesome-project'); await fs.mkdir('/projects/my-awesome-project/.git'); await fs.writeFile('/projects/my-awesome-project/package.json', '{}'); await fs.mkdir('/projects/simple-name'); await fs.mkdir('/projects/simple-name/.git'); await fs.writeFile('/projects/simple-name/package.json', '{}'); const projects = await projectsManager.getProjects(); expect(projects).toHaveLength(2); const awesomeProject = projects.find(p => p.id === 'my-awesome-project'); const simpleProject = projects.find(p => p.id === 'simple-name'); expect(awesomeProject?.name).toBe('my-awesome-project'); expect(simpleProject?.name).toBe('simple-name'); }); }); describe('getProject', () => { it('should get any directory as a project', async () => { await projectsManager.init(); // Create any directory await fs.mkdir('/projects/cloned-repo'); const project = await projectsManager.getProject('cloned-repo'); expect(project).not.toBeNull(); expect(project?.name).toBe('cloned-repo'); expect(project?.id).toBe('cloned-repo'); }); it('should return null for non-existent projects', async () => { await projectsManager.init(); const project = await projectsManager.getProject('non-existent'); expect(project).toBeNull(); }); }); describe('renameProject', () => { it('should rename a project directory', async () => { await projectsManager.init(); // Create a project await fs.mkdir('/projects/old-name'); await fs.writeFile('/projects/old-name/package.json', '{}'); // Verify the project exists const originalProject = await projectsManager.getProject('old-name'); expect(originalProject).not.toBeNull(); // Rename the project const renamedProject = await projectsManager.renameProject('old-name', 'new-name'); // Verify the renamed project expect(renamedProject.id).toBe('new-name'); expect(renamedProject.name).toBe('new-name'); expect(renamedProject.path).toBe('/projects/new-name'); // Verify old project no longer exists const oldProject = await projectsManager.getProject('old-name'); expect(oldProject).toBeNull(); // Verify new project exists const newProject = await projectsManager.getProject('new-name'); expect(newProject).not.toBeNull(); expect(newProject?.id).toBe('new-name'); }); it('should throw error when old project does not exist', async () => { await projectsManager.init(); await expect(projectsManager.renameProject('non-existent', 'new-name')) .rejects.toThrow('Project with ID "non-existent" does not exist'); }); it('should throw error when new project name already exists', async () => { await projectsManager.init(); // Create two projects await fs.mkdir('/projects/project-1'); await fs.mkdir('/projects/project-2'); await expect(projectsManager.renameProject('project-1', 'project-2')) .rejects.toThrow('Project with ID "project-2" already exists'); }); it('should validate and normalize project names', async () => { await projectsManager.init(); // Create a project await fs.mkdir('/projects/old-name'); // Try to rename with invalid characters await expect(projectsManager.renameProject('old-name', 'New Name With Spaces')) .rejects.toThrow('Project name must contain only lowercase letters, numbers, and hyphens'); // Try to rename with empty name await expect(projectsManager.renameProject('old-name', '')) .rejects.toThrow('Project name cannot be empty'); }); }); });