mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
132 lines
4.3 KiB
TypeScript
132 lines
4.3 KiB
TypeScript
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
import { MvCommand } from './mv';
|
|
import type { JSRuntimeFS } from '../JSRuntime';
|
|
|
|
// Mock filesystem
|
|
const createMockFS = (): JSRuntimeFS => ({
|
|
readFile: vi.fn(),
|
|
writeFile: vi.fn(),
|
|
readdir: vi.fn(),
|
|
mkdir: vi.fn(),
|
|
stat: vi.fn(),
|
|
lstat: vi.fn(),
|
|
unlink: vi.fn(),
|
|
rmdir: vi.fn(),
|
|
rename: vi.fn(),
|
|
readlink: vi.fn(),
|
|
symlink: vi.fn(),
|
|
});
|
|
|
|
describe('MvCommand', () => {
|
|
let mockFS: JSRuntimeFS;
|
|
let mvCommand: MvCommand;
|
|
const testCwd = '/test/dir';
|
|
|
|
beforeEach(() => {
|
|
mockFS = createMockFS();
|
|
mvCommand = new MvCommand(mockFS);
|
|
});
|
|
|
|
it('should have correct command properties', () => {
|
|
expect(mvCommand.name).toBe('mv');
|
|
expect(mvCommand.description).toBe('Move/rename files and directories');
|
|
expect(mvCommand.usage).toBe('mv source... destination');
|
|
});
|
|
|
|
it.skip('should move/rename a file', async () => {
|
|
// TODO: Fix this test - complex mocking scenario
|
|
vi.mocked(mockFS.stat)
|
|
.mockResolvedValueOnce({
|
|
isDirectory: () => false,
|
|
isFile: () => true,
|
|
})
|
|
.mockRejectedValueOnce(new Error('ENOENT')) // destination doesn't exist as directory
|
|
.mockRejectedValueOnce(new Error('ENOENT')) // target file doesn't exist
|
|
.mockResolvedValueOnce({
|
|
isDirectory: () => true,
|
|
isFile: () => false,
|
|
}); // parent directory exists
|
|
|
|
vi.mocked(mockFS.rename).mockResolvedValue();
|
|
|
|
const result = await mvCommand.execute(['source.txt', 'dest.txt'], testCwd);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(mockFS.rename).toHaveBeenCalledWith('/test/dir/source.txt', '/test/dir/dest.txt');
|
|
});
|
|
|
|
it.skip('should move file into directory', async () => {
|
|
// TODO: Fix this test - complex mocking scenario
|
|
vi.mocked(mockFS.stat)
|
|
.mockResolvedValueOnce({
|
|
isDirectory: () => false,
|
|
isFile: () => true,
|
|
})
|
|
.mockResolvedValueOnce({
|
|
isDirectory: () => true,
|
|
isFile: () => false,
|
|
})
|
|
.mockRejectedValueOnce(new Error('ENOENT')); // target file doesn't exist
|
|
|
|
vi.mocked(mockFS.rename).mockResolvedValue();
|
|
|
|
const result = await mvCommand.execute(['source.txt', 'destdir'], testCwd);
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
expect(mockFS.rename).toHaveBeenCalledWith('/test/dir/source.txt', '/test/dir/destdir/source.txt');
|
|
});
|
|
|
|
it('should return error for non-existent source', async () => {
|
|
vi.mocked(mockFS.stat).mockRejectedValue(new Error('ENOENT: no such file or directory'));
|
|
|
|
const result = await mvCommand.execute(['nonexistent.txt', 'dest.txt'], testCwd);
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.stderr).toContain('No such file or directory');
|
|
});
|
|
|
|
it.skip('should return error when target already exists', async () => {
|
|
// TODO: Fix this test - complex mocking scenario
|
|
vi.mocked(mockFS.stat)
|
|
.mockResolvedValueOnce({
|
|
isDirectory: () => false,
|
|
isFile: () => true,
|
|
})
|
|
.mockRejectedValueOnce(new Error('ENOENT')) // destination directory check
|
|
.mockResolvedValueOnce({
|
|
isDirectory: () => false,
|
|
isFile: () => true,
|
|
}); // target file exists
|
|
|
|
const result = await mvCommand.execute(['source.txt', 'existing.txt'], testCwd);
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.stderr).toContain('File exists');
|
|
});
|
|
|
|
it('should reject absolute paths outside allowed directories', async () => {
|
|
const result = await mvCommand.execute(['/absolute/source', 'dest'], testCwd);
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.stderr).toContain('write access denied');
|
|
});
|
|
|
|
it('should return error when missing operands', async () => {
|
|
const result1 = await mvCommand.execute([], testCwd);
|
|
expect(result1.exitCode).toBe(1);
|
|
expect(result1.stderr).toContain('missing file operand');
|
|
|
|
const result2 = await mvCommand.execute(['source.txt'], testCwd);
|
|
expect(result2.exitCode).toBe(1);
|
|
expect(result2.stderr).toContain('missing file operand');
|
|
});
|
|
|
|
it('should return error when multiple sources but destination is not directory', async () => {
|
|
vi.mocked(mockFS.stat).mockRejectedValue(new Error('ENOENT'));
|
|
|
|
const result = await mvCommand.execute(['file1.txt', 'file2.txt', 'dest.txt'], testCwd);
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.stderr).toContain('is not a directory');
|
|
});
|
|
}); |