Files
shakespeare/src/lib/commands/ls.test.ts
T
Alex Gleason 376a982449 Overhaul shell commands for POSIX compliance and AI-agent ergonomics
Each built-in shell command has been audited and rewritten to support the
options AI agents (and humans) typically reach for, close critical behavior
gaps, and align with POSIX/GNU conventions:

- Shared argument parser (utils.ts) handles combined short flags, long
  options with '=VALUE' or space-separated values, the '--' sentinel,
  numeric shortcuts like 'head -5', and '-' for stdin.
- cat, ls, cp, mv, rm, mkdir, touch: added missing common flags; removed
  the legacy 'absolute paths are not supported' and 'reading from stdin
  is not supported' barriers where they made agent use awkward.
- mv now overwrites destinations by default (POSIX) and supports -n.
- touch updates mtime on existing files (by rewriting content) and
  accepts -c/-a/-m/-h/-r/-d/-t options.
- find: new expression engine with -iname, -path, -regex, -type,
  -empty, -size, -maxdepth, -mindepth, -not, -or, -print/-print0,
  -delete, -quit.
- grep: -v, -c, -l, -L, -w, -x, -o, -q, -E, -F, -G, -H, -h, -A/-B/-C
  context, --include/--exclude, multiple -e, -f scriptfile.
- head/tail: -c bytes, -NUM shorthand, negative/positive line counts,
  proper multi-file headers.
- sort: -k keys, -t separator, -o output, -f/-b/-d/-s, -h/-V/-M modes.
- uniq: -i, -f, -s, -w, -D, and the [input [output]] form.
- wc: -m characters and -L longest line; -c now counts bytes (UTF-8).
- cut: open-ended ranges (3-, -5), -s, --complement, --output-delimiter.
- tr: -c/-s/-t, [:class:] character classes, escape sequences.
- sed: real multi-command parser supporting ';', addresses (N, $,
  /re/, N,M), '!' negation, proper -n quiet mode, y///, and -i[SUFFIX].
- echo: -n, -e, -E with standard backslash escapes.
- pwd/cd: -L/-P, plus cd -, cd ~, cd $HOME with OLDPWD tracking.
- diff: proper LCS algorithm producing correct normal and unified
  output; adds -q, -s, -N, -i, -w, -b, -B.
- env: -i, -u, NAME=VAL assignments; reads the live shell environment.
- date: -u, -d, -R, -I, and a full GNU-subset format specifier set.

Tests for each command were updated to reflect the corrected behavior
(e.g. absolute paths and stdin are now accepted, POSIX error messages).
All 1334 tests pass; typecheck and production build succeed.
2026-04-26 23:58:13 -05:00

149 lines
4.5 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from 'vitest';
import { LsCommand } from './ls';
import type { JSRuntimeFS } from '../JSRuntime';
// Mock filesystem
const createMockFS = (): JSRuntimeFS => ({
readFile: vi.fn(),
writeFile: vi.fn(),
readdir: vi.fn(),
mkdir: vi.fn(),
stat: vi.fn(),
lstat: vi.fn(),
unlink: vi.fn(),
rmdir: vi.fn(),
rename: vi.fn(),
readlink: vi.fn(),
symlink: vi.fn(),
});
describe('LsCommand', () => {
let mockFS: JSRuntimeFS;
let lsCommand: LsCommand;
const testCwd = '/test/dir';
beforeEach(() => {
mockFS = createMockFS();
lsCommand = new LsCommand(mockFS);
});
it('should have correct command properties', () => {
expect(lsCommand.name).toBe('ls');
expect(lsCommand.description).toBe('List directory contents');
expect(lsCommand.usage).toBe('ls [-aAldrRt1FhSn] [--] [file...]');
});
it('should list current directory when no arguments', async () => {
vi.mocked(mockFS.stat).mockResolvedValue({
isDirectory: () => true,
isFile: () => false,
});
vi.mocked(mockFS.readdir).mockResolvedValue([
{ name: 'file1.txt', isDirectory: () => false, isFile: () => true },
{ name: 'dir1', isDirectory: () => true, isFile: () => false },
]);
const result = await lsCommand.execute([], testCwd);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('dir1/');
expect(result.stdout).toContain('file1.txt');
});
it('should show file info when targeting a file', async () => {
vi.mocked(mockFS.stat).mockResolvedValue({
isDirectory: () => false,
isFile: () => true,
size: 1024,
});
const result = await lsCommand.execute(['test.txt'], testCwd);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('test.txt');
});
it('should show long format with -l flag', async () => {
vi.mocked(mockFS.stat)
.mockResolvedValueOnce({
isDirectory: () => true,
isFile: () => false,
})
.mockResolvedValue({
isDirectory: () => false,
isFile: () => true,
size: 1024,
mtimeMs: Date.now(),
});
vi.mocked(mockFS.readdir).mockResolvedValue([
{ name: 'file1.txt', isDirectory: () => false, isFile: () => true },
]);
const result = await lsCommand.execute(['-l'], testCwd);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('-rw-r--r--');
expect(result.stdout).toContain('1024');
expect(result.stdout).toContain('file1.txt');
});
it('should show hidden files with -a flag', async () => {
vi.mocked(mockFS.stat).mockResolvedValue({
isDirectory: () => true,
isFile: () => false,
});
vi.mocked(mockFS.readdir).mockResolvedValue([
{ name: '.hidden', isDirectory: () => false, isFile: () => true },
{ name: 'visible.txt', isDirectory: () => false, isFile: () => true },
]);
const result = await lsCommand.execute(['-a'], testCwd);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('.hidden');
expect(result.stdout).toContain('visible.txt');
});
it('should not show hidden files by default', async () => {
vi.mocked(mockFS.stat).mockResolvedValue({
isDirectory: () => true,
isFile: () => false,
});
vi.mocked(mockFS.readdir).mockResolvedValue([
{ name: '.hidden', isDirectory: () => false, isFile: () => true },
{ name: 'visible.txt', isDirectory: () => false, isFile: () => true },
]);
const result = await lsCommand.execute([], testCwd);
expect(result.exitCode).toBe(0);
expect(result.stdout).not.toContain('.hidden');
expect(result.stdout).toContain('visible.txt');
});
it('should return error for non-existent path', async () => {
vi.mocked(mockFS.stat).mockRejectedValue(new Error('ENOENT: no such file or directory'));
const result = await lsCommand.execute(['nonexistent'], testCwd);
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('No such file or directory');
});
it('should handle absolute paths', async () => {
vi.mocked(mockFS.stat).mockResolvedValue({
isDirectory: () => true,
isFile: () => false,
});
vi.mocked(mockFS.readdir).mockResolvedValue([
{ name: 'file1.txt', isDirectory: () => false, isFile: () => true },
]);
const result = await lsCommand.execute(['/absolute/path'], testCwd);
expect(result.exitCode).toBe(0);
expect(mockFS.stat).toHaveBeenCalledWith('/absolute/path');
expect(result.stdout).toContain('file1.txt');
});
});