Files
shakespeare/src/lib/commands/git/mv.test.ts
T
Alex Gleason b3fddf6cb3 Audit and overhaul virtual shell's git command
Fix critical bugs, add extensive missing flags, and add 8 new subcommands
to bring the browser-side git command closer to parity with real git.

Critical bug fixes:
- git commit: file-count classification used impossible stageStatus=1
  comparisons, always reporting 0 files changed
- git status --porcelain: untracked files could clobber staged indicators
- git add: directory walker skipped all dotfiles (including .gitignore
  itself), ignoring real git's .gitignore-based filtering
- git pull: untracked files incorrectly blocked pulls
- git reset: target regex rejected valid refs like branch names
- git diff: path filter used substring matching; --cached read working
  directory instead of index; <commit> and <commit>..<commit> forms were
  stubbed/unimplemented; used fake JS hashes instead of git.hashBlob

Implemented git reset --soft (was returning "not implemented").

New flags added across existing subcommands:
- commit: --signoff, --no-verify, -F/--file, --author, -C/--reuse-message,
  multiple -m support
- add: -u/--update, -f/--force, -n/--dry-run, absolute paths inside repo
- log: --stat, -p/--patch, --name-only, --name-status, --author, --grep,
  --since, --until, --no-merges, --reverse, --follow, custom
  --pretty=format:, proper timezone in output
- push: -u/--set-upstream (now wired to config), --tags, --delete,
  --dry-run, refspec (local:remote)
- fetch: --all, --tags, --prune now actually passed through
- branch: -a/--all, -r/--remotes, -m/-M (rename), --show-current, -v
- checkout: -B, -f/--force, commit-hash support (detached HEAD)
- config: --unset, --get-all, --add, --local, proper --list that reads
  the whole config file
- tag: real annotated tags via git.annotatedTag, -m, -f, -n, glob patterns
- clone: -b/--branch, -o/--origin, --bare
- init: -b/--initial-branch
- show: --stat, --name-only, --name-status
- diff: --stat, --name-only, --name-status
- remote: set-url, get-url, rename, show subcommands

New subcommands:
- git rm: remove files from index (and optionally worktree)
- git mv: rename/move files with index updates
- git restore: modern replacement for checkout of files
- git switch: modern replacement for branch checkout
- git merge: wrapper over git.merge (including --abort, --ff-only)
- git revert: create an inverse commit
- git rev-parse: resolve refs, --short, --abbrev-ref, --show-toplevel,
  --git-dir, HEAD~N / HEAD^N
- git ls-files: list tracked/modified/deleted/untracked files

All existing tests updated where behavior intentionally changed; new
subcommands have dedicated test suites. Final suite: 1392 passing.
2026-04-27 00:42:10 -05:00

92 lines
3.5 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from 'vitest';
import { GitMvCommand } from './mv';
import type { Git } from '../../git';
import type { JSRuntimeFS } from '../../JSRuntime';
const createMockFS = () => {
const files = new Map<string, string>();
const dirs = new Set(['/test-repo', '/test-repo/dir']);
files.set('/test-repo/file1.txt', 'contents');
return {
stat: vi.fn(async (path: string) => {
if (path.endsWith('/.git') || dirs.has(path)) {
return { isDirectory: () => true, isFile: () => false };
}
if (files.has(path)) {
return { isDirectory: () => false, isFile: () => true };
}
throw new Error('ENOENT');
}),
rename: vi.fn(async (src: string, dst: string) => {
const content = files.get(src);
if (content === undefined) throw new Error('ENOENT');
files.delete(src);
files.set(dst, content);
}),
mkdir: vi.fn(async (path: string) => { dirs.add(path); }),
readFile: vi.fn(async (path: string) => files.get(path) || ''),
_files: files,
} as unknown as JSRuntimeFS & { _files: Map<string, string> };
};
describe('GitMvCommand', () => {
let mvCommand: GitMvCommand;
let mockGit: Git;
let mockFS: JSRuntimeFS & { _files: Map<string, string> };
beforeEach(() => {
mockFS = createMockFS() as JSRuntimeFS & { _files: Map<string, string> };
mockGit = {
remove: vi.fn(async () => {}),
add: vi.fn(async () => {}),
} as unknown as Git;
mvCommand = new GitMvCommand({ git: mockGit, fs: mockFS });
});
it('should fail without arguments', async () => {
const result = await mvCommand.execute([], '/test-repo');
expect(result.exitCode).toBe(1);
});
it('should rename a file', async () => {
const result = await mvCommand.execute(['file1.txt', 'file2.txt'], '/test-repo');
expect(result.exitCode).toBe(0);
expect(mockFS.rename).toHaveBeenCalledWith('/test-repo/file1.txt', '/test-repo/file2.txt');
expect(mockGit.remove).toHaveBeenCalledWith({ dir: '/test-repo', filepath: 'file1.txt' });
expect(mockGit.add).toHaveBeenCalledWith({ dir: '/test-repo', filepath: 'file2.txt' });
});
it('should fail when source does not exist', async () => {
const result = await mvCommand.execute(['nonexistent.txt', 'new.txt'], '/test-repo');
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('bad source');
});
it('should fail when destination exists without --force', async () => {
mockFS._files.set('/test-repo/file2.txt', 'existing');
const result = await mvCommand.execute(['file1.txt', 'file2.txt'], '/test-repo');
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('destination exists');
});
it('should overwrite with --force', async () => {
mockFS._files.set('/test-repo/file2.txt', 'existing');
const result = await mvCommand.execute(['-f', 'file1.txt', 'file2.txt'], '/test-repo');
expect(result.exitCode).toBe(0);
});
it('should support --dry-run', async () => {
const result = await mvCommand.execute(['-n', 'file1.txt', 'file2.txt'], '/test-repo');
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Renaming');
expect(mockFS.rename).not.toHaveBeenCalled();
});
it('should move a file into a directory', async () => {
const result = await mvCommand.execute(['file1.txt', 'dir'], '/test-repo');
expect(result.exitCode).toBe(0);
expect(mockFS.rename).toHaveBeenCalledWith('/test-repo/file1.txt', '/test-repo/dir/file1.txt');
});
});