Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276import { describe, expect, it,} from '@monochromatic-dev/module-test/ts';import spawn from 'nano-spawn';import { existsSync, mkdirSync, rmSync, unlinkSync, writeFileSync,} from 'node:fs';import { readFile, } from 'node:fs/promises';import { join, } from 'node:path';
/** Runs a shell command and resolves with its captured output. Wraps `nano-spawn` with `shell: true` to mirror the previous `promisify(exec)` contract: resolves `{ stdout, stderr }` on success and rejects with a `SubprocessError` on non-zero exit. @param command - Full shell command line to execute @returns Captured `stdout` and `stderr` @example ```ts const { stdout } = await execAsync('echo hi'); // stdout === 'hi' ``` */function execAsync(command: string,): Promise<{ readonly stdout: string; readonly stderr: string; }> { return spawn(command, { shell: true, },);}
//region Fixture Setup: per-test fixtures
function setup() { const testFileDir = import.meta.dirname; const cliPath = join(testFileDir, 'append.ts',);
const packageDir = join(testFileDir, '..',); const timestamp = Date.now(); const randomId = Math.random().toString(36,).slice(2, 8,); const testDir = join(packageDir, 'dist', 'temp', 'test', `cli-append-${timestamp}-${randomId}`,);
if (!existsSync(testDir,)) mkdirSync(testDir, { recursive: true, },);
const testFile = join(testDir, 'test.txt',); writeFileSync(testFile, 'Initial content\n',);
return { cliPath, testDir, testFile, };}
function teardown({ testFile, testDir, }: { testFile: string; testDir: string; },) { if (existsSync(testFile,)) unlinkSync(testFile,);
if (existsSync(testDir,)) rmSync(testDir, { recursive: true, },);}
//endregion Fixture Setup
await describe({ name: 'task-append', children: [ it({ name: 'appends single line to existing file', fn: async () => { const fixtures = setup(); const { cliPath, testFile, } = fixtures;
const { stdout, stderr, } = await execAsync( `node ${cliPath} "new line" --to ${testFile}`, );
expect(stderr,).toBe('',);
const content = await readFile(testFile, 'utf8',); expect(content,).toBe('Initial content\nnew line\n',);
teardown(fixtures,); }, },), it({ name: 'appends multiple lines as separate arguments', fn: async () => { const fixtures = setup(); const { cliPath, testFile, } = fixtures;
const { stdout, stderr, } = await execAsync( `node ${cliPath} "line 1" "line 2" "line 3" --to ${testFile}`, );
expect(stderr,).toBe('',);
const content = await readFile(testFile, 'utf8',); expect(content,).toBe('Initial content\nline 1\nline 2\nline 3\n',);
teardown(fixtures,); }, },), it({ name: 'appends multiline text with newline characters', fn: async () => { const fixtures = setup(); const { cliPath, testFile, } = fixtures;
const { stdout, stderr, } = await execAsync( `node ${cliPath} "line 1\\nline 2" --to ${testFile}`, );
expect(stderr,).toBe('',);
const content = await readFile(testFile, 'utf8',); expect(content,).toBe('Initial content\nline 1\\nline 2\n',);
teardown(fixtures,); }, },), it({ name: 'uses short flag -t for target file', fn: async () => { const fixtures = setup(); const { cliPath, testFile, } = fixtures;
const { stdout, stderr, } = await execAsync( `node ${cliPath} "short flag test" -t ${testFile}`, );
expect(stderr,).toBe('',);
const content = await readFile(testFile, 'utf8',); expect(content,).toBe('Initial content\nshort flag test\n',);
teardown(fixtures,); }, },), it({ name: 'fails when no text is provided', fn: async () => { const fixtures = setup(); const { cliPath, testFile, } = fixtures;
await expect(execAsync(`node ${cliPath} --to ${testFile}`,),).rejects.toThrow();
teardown(fixtures,); }, },), it({ name: 'fails when no target file is specified', fn: async () => { const fixtures = setup(); const { cliPath, } = fixtures;
await expect(execAsync(`node ${cliPath} "some text"`,),).rejects.toThrow();
teardown(fixtures,); }, },), it({ name: 'fails when target file does not exist', fn: async () => { const fixtures = setup(); const { cliPath, testDir, } = fixtures;
const nonExistentFile = join(testDir, 'non-existent.txt',); await expect(execAsync(`node ${cliPath} "text" --to ${nonExistentFile}`,),) .rejects .toThrow();
teardown(fixtures,); }, },), it({ name: 'fails when file has no write permissions', skip: process.platform === 'win32', fn: async () => { const fixtures = setup(); const { cliPath, testFile, } = fixtures;
// Make file read-only writeFileSync(testFile, 'read only content\n', { mode: 0o444, },);
// Bun's appendFile may bypass POSIX permissions in some environments; skip when that happens try { const { appendFile: nodeAppendFile, } = await import('node:fs/promises'); await nodeAppendFile(testFile, 'probe',); // If we get here, permissions aren't enforced; skip assertion teardown(fixtures,); return; } catch (error) { expect(error,).toBeInstanceOf(Error,); // Permissions enforced, proceed with test }
await expect(execAsync(`node ${cliPath} "text" --to ${testFile}`,),) .rejects .toThrow();
teardown(fixtures,); }, },), it({ name: 'preserves existing file content', fn: async () => { const fixtures = setup(); const { cliPath, testFile, } = fixtures;
// Add some initial content writeFileSync(testFile, 'Line 1\nLine 2\n',);
await execAsync(`node ${cliPath} "Line 3" --to ${testFile}`,);
const content = await readFile(testFile, 'utf8',); expect(content,).toBe('Line 1\nLine 2\nLine 3\n',);
teardown(fixtures,); }, },), it({ name: 'handles empty string as valid input', fn: async () => { const fixtures = setup(); const { cliPath, testFile, } = fixtures;
await execAsync(`node ${cliPath} "" --to ${testFile}`,);
const content = await readFile(testFile, 'utf8',); expect(content,).toBe('Initial content\n\n',);
teardown(fixtures,); }, },), it({ name: 'handles special characters in text', fn: async () => { const fixtures = setup(); const { cliPath, testFile, } = fixtures;
// Use single quotes to prevent shell expansion of $USER and other special chars const specialText = 'Hello && echo test | cat'; await execAsync(`node ${cliPath} '${specialText}' --to ${testFile}`,);
const content = await readFile(testFile, 'utf8',); expect(content,).toBe(`Initial content\n${specialText}\n`,);
teardown(fixtures,); }, },), it({ name: 'appends multiple times to the same file', fn: async () => { const fixtures = setup(); const { cliPath, testFile, } = fixtures;
await execAsync(`node ${cliPath} "First append" --to ${testFile}`,); await execAsync(`node ${cliPath} "Second append" --to ${testFile}`,); await execAsync(`node ${cliPath} "Third append" --to ${testFile}`,);
const content = await readFile(testFile, 'utf8',); expect(content,).toBe( 'Initial content\nFirst append\nSecond append\nThird append\n', );
teardown(fixtures,); }, },), ],},);