Files
shakespeare/src/lib/commands/git/switch.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

83 lines
3.0 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from 'vitest';
import { GitSwitchCommand } from './switch';
import type { Git } from '../../git';
import type { JSRuntimeFS } from '../../JSRuntime';
const mockFS = {
stat: async (path: string) => {
if (path.endsWith('/.git')) return { isDirectory: () => true };
throw new Error('ENOENT');
},
} as unknown as JSRuntimeFS;
describe('GitSwitchCommand', () => {
let switchCommand: GitSwitchCommand;
let mockGit: Git;
beforeEach(() => {
mockGit = {
listBranches: vi.fn().mockResolvedValue(['main', 'other']),
currentBranch: vi.fn().mockResolvedValue('main'),
checkout: vi.fn(async () => {}),
branch: vi.fn(async () => {}),
deleteBranch: vi.fn(async () => {}),
resolveRef: vi.fn().mockResolvedValue('abc123'),
statusMatrix: vi.fn().mockResolvedValue([]),
} as unknown as Git;
switchCommand = new GitSwitchCommand({ git: mockGit, fs: mockFS });
});
it('should create and switch with -c', async () => {
vi.mocked(mockGit.listBranches).mockResolvedValue(['main']);
const result = await switchCommand.execute(['-c', 'feature'], '/test-repo');
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain("Switched to a new branch 'feature'");
expect(mockGit.branch).toHaveBeenCalled();
});
it('should switch to existing branch', async () => {
const result = await switchCommand.execute(['other'], '/test-repo');
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain("Switched to branch 'other'");
});
it('should fail for non-existent branch', async () => {
const result = await switchCommand.execute(['nonexistent'], '/test-repo');
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('invalid reference');
});
it('should say already on branch', async () => {
const result = await switchCommand.execute(['main'], '/test-repo');
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain("Already on 'main'");
});
it('should fail when not in a git repo', async () => {
const mockFSNoGit = {
stat: async () => { throw new Error('ENOENT'); },
} as unknown as JSRuntimeFS;
const cmd = new GitSwitchCommand({ git: mockGit, fs: mockFSNoGit });
const result = await cmd.execute(['main'], '/not-a-repo');
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('not a git repository');
});
it('should block switch with uncommitted changes', async () => {
vi.mocked(mockGit.statusMatrix).mockResolvedValue([
['file.txt', 1, 2, 1],
]);
const result = await switchCommand.execute(['other'], '/test-repo');
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('local changes');
});
it('should force switch with -f', async () => {
vi.mocked(mockGit.statusMatrix).mockResolvedValue([
['file.txt', 1, 2, 1],
]);
const result = await switchCommand.execute(['-f', 'other'], '/test-repo');
expect(result.exitCode).toBe(0);
});
});