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

103 lines
3.6 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from 'vitest';
import { GitRevertCommand } from './revert';
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');
},
writeFile: vi.fn(async () => {}),
unlink: vi.fn(async () => {}),
mkdir: vi.fn(async () => {}),
} as unknown as JSRuntimeFS;
describe('GitRevertCommand', () => {
let revertCommand: GitRevertCommand;
let mockGit: Git;
beforeEach(() => {
vi.clearAllMocks();
mockGit = {
resolveRef: vi.fn().mockResolvedValue('abc123'),
readCommit: vi.fn().mockResolvedValue({
oid: 'abc123',
commit: {
message: 'Add file2',
parent: ['parent123'],
author: { name: 'Test', email: '[email protected]', timestamp: 1 },
},
}),
readTree: vi.fn().mockImplementation(({ oid }: { oid: string }) => {
if (oid === 'abc123') {
return Promise.resolve({ tree: [
{ mode: '100644', path: 'file1.txt', oid: 'blob1' },
{ mode: '100644', path: 'file2.txt', oid: 'blob2' },
] });
}
if (oid === 'parent123') {
return Promise.resolve({ tree: [
{ mode: '100644', path: 'file1.txt', oid: 'blob1' },
] });
}
return Promise.resolve({ tree: [] });
}),
readBlob: vi.fn().mockResolvedValue({
oid: 'blob1',
blob: new TextEncoder().encode('original'),
}),
add: vi.fn(async () => {}),
remove: vi.fn(async () => {}),
commit: vi.fn().mockResolvedValue('newcommit'),
currentBranch: vi.fn().mockResolvedValue('main'),
} as unknown as Git;
revertCommand = new GitRevertCommand({ git: mockGit, fs: mockFS });
});
it('should fail without commit argument', async () => {
const result = await revertCommand.execute([], '/test-repo');
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('empty commit set');
});
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 GitRevertCommand({ git: mockGit, fs: mockFSNoGit });
const result = await cmd.execute(['HEAD'], '/not-a-repo');
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('not a git repository');
});
it('should fail when reverting a root commit', async () => {
vi.mocked(mockGit.readCommit).mockResolvedValue({
oid: 'abc123',
commit: {
message: 'Root',
parent: [],
author: { name: 'Test', email: '[email protected]', timestamp: 1 },
},
} as unknown as Awaited<ReturnType<Git['readCommit']>>);
const result = await revertCommand.execute(['HEAD'], '/test-repo');
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('root commit');
});
it('should revert a commit that added a file', async () => {
const result = await revertCommand.execute(['HEAD'], '/test-repo');
expect(result.exitCode).toBe(0);
// file2.txt was added in target, so it should be removed
expect(mockGit.remove).toHaveBeenCalledWith({ dir: '/test-repo', filepath: 'file2.txt' });
expect(mockGit.commit).toHaveBeenCalled();
});
it('should support --no-commit', async () => {
const result = await revertCommand.execute(['--no-commit', 'HEAD'], '/test-repo');
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Applied revert');
expect(mockGit.commit).not.toHaveBeenCalled();
});
});