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

129 lines
4.5 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from 'vitest';
import { Git } from '../git';
import { GitCommand } from './git';
import type { JSRuntimeFS } from '../JSRuntime';
// Mock filesystem
const createMockFS = (): JSRuntimeFS => ({
readFile: vi.fn().mockResolvedValue('mock content'),
writeFile: vi.fn().mockResolvedValue(undefined),
readdir: vi.fn().mockResolvedValue([]),
mkdir: vi.fn().mockResolvedValue(undefined),
stat: vi.fn().mockImplementation((path: string) => {
if (path.endsWith('/.git')) {
throw new Error('ENOENT: no such file or directory');
}
return Promise.resolve({
isDirectory: () => true,
isFile: () => false,
size: 0,
mtimeMs: Date.now(),
});
}),
lstat: vi.fn().mockResolvedValue({
isDirectory: () => true,
isFile: () => false,
size: 0,
mtimeMs: Date.now(),
}),
unlink: vi.fn().mockResolvedValue(undefined),
rmdir: vi.fn().mockResolvedValue(undefined),
rename: vi.fn().mockResolvedValue(undefined),
readlink: vi.fn().mockResolvedValue('mock link'),
symlink: vi.fn().mockResolvedValue(undefined),
});
describe('GitCommand', () => {
let gitCommand: GitCommand;
let mockFS: JSRuntimeFS;
const testCwd = '/test';
beforeEach(() => {
mockFS = createMockFS();
const mockNostr = {
req: vi.fn(),
query: vi.fn(),
event: vi.fn(),
group: vi.fn(),
relay: vi.fn(),
relays: new Map(),
close: vi.fn(),
} as unknown as import('@nostrify/nostrify').NPool;
const git = new Git({ fs: mockFS, nostr: mockNostr });
gitCommand = new GitCommand({ git, fs: mockFS, cwd: testCwd });
});
it('should have correct command properties', () => {
expect(gitCommand.name).toBe('git');
expect(gitCommand.description).toBe('Git version control system');
expect(gitCommand.usage).toBe('git <command> [<args>]');
});
it('should show help when no arguments provided', async () => {
const result = await gitCommand.execute([], testCwd);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('usage: git');
expect(result.stdout).toContain('These are common Git commands');
expect(result.stdout).toContain('init');
expect(result.stdout).toContain('status');
expect(result.stdout).toContain('add');
expect(result.stdout).toContain('commit');
});
it('should show help when --help flag is used', async () => {
const result = await gitCommand.execute(['--help'], testCwd);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('usage: git');
});
it('should show version when --version flag is used', async () => {
const result = await gitCommand.execute(['--version'], testCwd);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('git version');
expect(result.stdout).toContain('isomorphic-git');
});
it('should return error for unknown subcommand', async () => {
const result = await gitCommand.execute(['unknown-command'], testCwd);
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('unknown-command');
expect(result.stderr).toContain('is not a git command');
});
it('should handle status subcommand (not a git repo)', async () => {
const result = await gitCommand.execute(['status'], testCwd);
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('fatal: not a git repository');
});
it('should handle init subcommand', async () => {
const result = await gitCommand.execute(['init'], testCwd);
// With our mock filesystem, init might succeed or fail depending on isomorphic-git behavior
// The important thing is that the subcommand is recognized and executed
expect([0, 1]).toContain(result.exitCode);
if (result.exitCode === 0) {
expect(result.stdout).toContain('Initialized');
}
});
it('should register all expected subcommands', () => {
const expectedSubcommands = [
'init', 'status', 'add', 'commit', 'log', 'branch', 'checkout',
'remote', 'push', 'pull', 'clone', 'config', 'reset', 'diff', 'tag', 'show',
'stash', 'rm', 'mv', 'restore', 'switch', 'merge', 'revert', 'rev-parse', 'ls-files',
];
// We can't directly access the subcommands map, but we can test that they work
expectedSubcommands.forEach(async (subcommand) => {
const result = await gitCommand.execute([subcommand, '--help'], testCwd);
// Most subcommands will fail due to not being in a git repo, but they should be recognized
expect(result.stderr).not.toContain('is not a git command');
});
});
});