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

147 lines
5.4 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { CutCommand } from './cut';
import type { JSRuntimeFS } from '../JSRuntime';
describe('CutCommand', () => {
const mockFS = {
stat: vi.fn(),
readFile: vi.fn(),
} as unknown as JSRuntimeFS;
const cutCommand = new CutCommand(mockFS);
beforeEach(() => {
vi.clearAllMocks();
});
it('should extract characters by position', async () => {
vi.mocked(mockFS.stat).mockResolvedValue({ isDirectory: () => false, isFile: () => true });
vi.mocked(mockFS.readFile).mockResolvedValue('hello world\nfoo\n');
const result = await cutCommand.execute(['-c', '1,3,5', 'test.txt'], '/test');
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe('hlo\nfo\n');
expect(result.stderr).toBe('');
});
it('should extract character ranges', async () => {
vi.mocked(mockFS.stat).mockResolvedValue({ isDirectory: () => false, isFile: () => true });
vi.mocked(mockFS.readFile).mockResolvedValue('hello world\n');
const result = await cutCommand.execute(['-c', '1-5', 'test.txt'], '/test');
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe('hello\n');
expect(result.stderr).toBe('');
});
it('should extract fields with default tab delimiter', async () => {
vi.mocked(mockFS.stat).mockResolvedValue({ isDirectory: () => false, isFile: () => true });
vi.mocked(mockFS.readFile).mockResolvedValue('name\tage\tcity\nJohn\t25\tNY\n');
const result = await cutCommand.execute(['-f', '1,3', 'test.txt'], '/test');
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe('name\tcity\nJohn\tNY\n');
expect(result.stderr).toBe('');
});
it('should extract fields with custom delimiter', async () => {
vi.mocked(mockFS.stat).mockResolvedValue({ isDirectory: () => false, isFile: () => true });
vi.mocked(mockFS.readFile).mockResolvedValue('name,age,city\nJohn,25,NY\n');
const result = await cutCommand.execute(['-f', '1,3', '-d', ',', 'test.txt'], '/test');
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe('name,city\nJohn,NY\n');
expect(result.stderr).toBe('');
});
it('should extract field ranges', async () => {
vi.mocked(mockFS.stat).mockResolvedValue({ isDirectory: () => false, isFile: () => true });
vi.mocked(mockFS.readFile).mockResolvedValue('a,b,c,d,e\n1,2,3,4,5\n');
const result = await cutCommand.execute(['-f', '2-4', '-d', ',', 'test.txt'], '/test');
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe('b,c,d\n2,3,4\n');
expect(result.stderr).toBe('');
});
it('should handle combined character options', async () => {
vi.mocked(mockFS.stat).mockResolvedValue({ isDirectory: () => false, isFile: () => true });
vi.mocked(mockFS.readFile).mockResolvedValue('hello world\n');
const result = await cutCommand.execute(['-c1,3,5-7', 'test.txt'], '/test');
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe('hlo w\n');
expect(result.stderr).toBe('');
});
it('should handle combined field options', async () => {
vi.mocked(mockFS.stat).mockResolvedValue({ isDirectory: () => false, isFile: () => true });
vi.mocked(mockFS.readFile).mockResolvedValue('a,b,c,d,e\n');
const result = await cutCommand.execute(['-f1,3-5', '-d,', 'test.txt'], '/test');
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe('a,c,d,e\n');
expect(result.stderr).toBe('');
});
it('should handle multiple files', async () => {
vi.mocked(mockFS.stat).mockResolvedValue({ isDirectory: () => false, isFile: () => true });
vi.mocked(mockFS.readFile)
.mockResolvedValueOnce('hello\n')
.mockResolvedValueOnce('world\n');
const result = await cutCommand.execute(['-c', '1-3', 'file1.txt', 'file2.txt'], '/test');
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe('hel\nwor\n');
expect(result.stderr).toBe('');
});
it('should error when no list specified', async () => {
const result = await cutCommand.execute(['test.txt'], '/test');
expect(result.exitCode).not.toBe(0);
expect(result.stderr).toContain('you must specify a list');
});
it('should error for non-existent file', async () => {
vi.mocked(mockFS.stat).mockRejectedValue(new Error('ENOENT: no such file'));
const result = await cutCommand.execute(['-c', '1', 'nonexistent.txt'], '/test');
expect(result.exitCode).not.toBe(0);
expect(result.stderr).toContain('No such file or directory');
});
it('should error for directory', async () => {
vi.mocked(mockFS.stat).mockResolvedValue({ isDirectory: () => true, isFile: () => false });
const result = await cutCommand.execute(['-c', '1', 'directory'], '/test');
expect(result.exitCode).not.toBe(0);
expect(result.stderr).toContain('Is a directory');
});
it('should support absolute paths', async () => {
vi.mocked(mockFS.stat).mockResolvedValue({ isDirectory: () => false, isFile: () => true });
vi.mocked(mockFS.readFile).mockResolvedValue('hello\n');
const result = await cutCommand.execute(['-c', '1-3', '/abs/file'], '/test');
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe('hel\n');
});
it('should return empty output for no files', async () => {
const result = await cutCommand.execute(['-c', '1'], '/test');
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe('');
expect(result.stderr).toBe('');
});
});