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

211 lines
7.7 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from 'vitest';
import { MockFS } from '@/test/MockFS';
import { Git } from '@/lib/git';
import { GitLogCommand } from './log';
import type { NPool, NostrEvent } from '@nostrify/nostrify';
const createMockNostr = (): NPool => {
const events: NostrEvent[] = [];
return {
req: vi.fn(),
query: vi.fn(async () => events),
event: vi.fn(async (event: NostrEvent) => { events.push(event); }),
group: vi.fn().mockReturnValue({ query: vi.fn(async () => events) }),
relay: vi.fn(),
relays: new Map(),
close: vi.fn(),
} as unknown as NPool;
};
describe('GitLogCommand', () => {
let fs: MockFS;
let git: Git;
let logCommand: GitLogCommand;
beforeEach(async () => {
fs = new MockFS();
git = new Git({
fs,
nostr: createMockNostr(),
});
logCommand = new GitLogCommand({ git, fs });
// Initialize a git repository
await git.init({ dir: '/test-repo' });
// Configure git
await git.setConfig({ dir: '/test-repo', path: 'user.name', value: 'Test User' });
await git.setConfig({ dir: '/test-repo', path: 'user.email', value: '[email protected]' });
// Create some commits
await fs.writeFile('/test-repo/file1.txt', 'content 1');
await git.add({ dir: '/test-repo', filepath: 'file1.txt' });
await git.commit({
dir: '/test-repo',
message: 'First commit',
});
await fs.writeFile('/test-repo/file2.txt', 'content 2');
await git.add({ dir: '/test-repo', filepath: 'file2.txt' });
await git.commit({
dir: '/test-repo',
message: 'Second commit',
});
await fs.writeFile('/test-repo/file3.txt', 'content 3');
await git.add({ dir: '/test-repo', filepath: 'file3.txt' });
await git.commit({
dir: '/test-repo',
message: 'Third commit',
});
});
it('should show all commits by default (no limit)', async () => {
const result = await logCommand.execute([], '/test-repo');
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('First commit');
expect(result.stdout).toContain('Second commit');
expect(result.stdout).toContain('Third commit');
});
it('should limit commits with -n flag', async () => {
const result = await logCommand.execute(['-n', '1'], '/test-repo');
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Third commit');
expect(result.stdout).not.toContain('Second commit');
expect(result.stdout).not.toContain('First commit');
});
it('should limit commits with -n= syntax', async () => {
const result = await logCommand.execute(['-n=2'], '/test-repo');
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Third commit');
expect(result.stdout).toContain('Second commit');
expect(result.stdout).not.toContain('First commit');
});
it('should limit commits with --max-count flag', async () => {
const result = await logCommand.execute(['--max-count', '2'], '/test-repo');
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Third commit');
expect(result.stdout).toContain('Second commit');
expect(result.stdout).not.toContain('First commit');
});
it('should limit commits with -<number> syntax', async () => {
const result = await logCommand.execute(['-2'], '/test-repo');
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Third commit');
expect(result.stdout).toContain('Second commit');
expect(result.stdout).not.toContain('First commit');
});
it('should format with --oneline', async () => {
const result = await logCommand.execute(['--oneline'], '/test-repo');
expect(result.exitCode).toBe(0);
// Oneline format should have short hash and message on same line
const lines = result.stdout.trim().split('\n');
expect(lines.length).toBe(3);
expect(lines[0]).toMatch(/^[0-9a-f]{7} Third commit$/);
expect(lines[1]).toMatch(/^[0-9a-f]{7} Second commit$/);
expect(lines[2]).toMatch(/^[0-9a-f]{7} First commit$/);
});
it('should format with --format=oneline', async () => {
const result = await logCommand.execute(['--format=oneline'], '/test-repo');
expect(result.exitCode).toBe(0);
const lines = result.stdout.trim().split('\n');
expect(lines.length).toBe(3);
expect(lines[0]).toMatch(/^[0-9a-f]{7} Third commit$/);
});
it('should format with --format=short', async () => {
const result = await logCommand.execute(['--format=short'], '/test-repo');
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('commit');
expect(result.stdout).toContain('Author:');
expect(result.stdout).not.toContain('Date:'); // Short format doesn't include date
});
it('should format with --format=full', async () => {
const result = await logCommand.execute(['--format=full'], '/test-repo');
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('commit');
expect(result.stdout).toContain('Author:');
expect(result.stdout).toContain('Date:');
});
it('should format with --format=fuller', async () => {
const result = await logCommand.execute(['--format=fuller'], '/test-repo');
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('commit');
expect(result.stdout).toContain('Author:');
expect(result.stdout).toContain('AuthorDate:');
});
it('should format with --format=raw', async () => {
const result = await logCommand.execute(['--format=raw'], '/test-repo');
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('commit');
expect(result.stdout).toContain('author');
expect(result.stdout).toMatch(/author .+ <.+> \d+ [+-]\d{4}/);
});
it('should show graph with --graph', async () => {
const result = await logCommand.execute(['--graph', '--oneline'], '/test-repo');
expect(result.exitCode).toBe(0);
const lines = result.stdout.trim().split('\n');
expect(lines[0]).toMatch(/^\* [0-9a-f]{7} Third commit$/);
});
it('should combine --graph with full format', async () => {
const result = await logCommand.execute(['--graph'], '/test-repo');
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('* commit');
});
it('should error when not in a git repository', async () => {
const result = await logCommand.execute([], '/not-a-repo');
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('not a git repository');
});
it('should show more than 50 commits when no limit is specified', async () => {
// Create 60 commits
for (let i = 4; i <= 60; i++) {
await fs.writeFile(`/test-repo/file${i}.txt`, `content ${i}`);
await git.add({ dir: '/test-repo', filepath: `file${i}.txt` });
await git.commit({
dir: '/test-repo',
message: `Commit ${i}`,
});
}
const result = await logCommand.execute(['--oneline'], '/test-repo');
expect(result.exitCode).toBe(0);
const lines = result.stdout.trim().split('\n');
expect(lines.length).toBe(60); // Should show all 60 commits, not just 50
expect(result.stdout).toContain('Commit 60');
expect(result.stdout).toContain('Commit 1');
});
it('should respect limit even with many commits', async () => {
// Create 60 commits
for (let i = 4; i <= 60; i++) {
await fs.writeFile(`/test-repo/file${i}.txt`, `content ${i}`);
await git.add({ dir: '/test-repo', filepath: `file${i}.txt` });
await git.commit({
dir: '/test-repo',
message: `Commit ${i}`,
});
}
const result = await logCommand.execute(['-10', '--oneline'], '/test-repo');
expect(result.exitCode).toBe(0);
const lines = result.stdout.trim().split('\n');
expect(lines.length).toBe(10); // Should show only 10 commits
expect(result.stdout).toContain('Commit 60');
expect(result.stdout).not.toContain('Commit 1');
});
});