Files
smartshell/test/test.silent.ts

104 lines
3.9 KiB
TypeScript
Raw Permalink Normal View History

import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as smartshell from '../ts/index.js';
let testSmartshell: smartshell.Smartshell;
tap.test('setup for silent execution tests', async () => {
testSmartshell = new smartshell.Smartshell({
executor: 'bash',
sourceFilePaths: [],
});
expect(testSmartshell).toBeInstanceOf(smartshell.Smartshell);
});
tap.test('execSilent should capture output without printing to console', async () => {
const result = await testSmartshell.execSilent('echo "Hidden from console"');
expect(result.stdout).toContain('Hidden from console');
expect(result.exitCode).toEqual(0);
});
tap.test('execSilent should capture multi-line output', async () => {
const result = await testSmartshell.execSilent('echo "Line 1" && echo "Line 2" && echo "Line 3"');
const lines = result.stdout.trim().split('\n');
expect(lines).toHaveLength(3);
expect(lines[0]).toEqual('Line 1');
expect(lines[1]).toEqual('Line 2');
expect(lines[2]).toEqual('Line 3');
});
tap.test('execSilent should capture error output', async () => {
const result = await testSmartshell.execSilent('echo "This works" && exit 1');
expect(result.stdout).toContain('This works');
expect(result.exitCode).toEqual(1);
});
tap.test('execSilent should parse command output programmatically', async () => {
// Test that we can programmatically process the captured output
const result = await testSmartshell.execSilent('echo "apple,banana,cherry"');
const items = result.stdout.trim().split(',');
expect(items).toHaveLength(3);
expect(items[0]).toEqual('apple');
expect(items[1]).toEqual('banana');
expect(items[2]).toEqual('cherry');
});
tap.test('execStrictSilent should capture output and throw on error', async () => {
// Test successful command
const successResult = await testSmartshell.execStrictSilent('echo "Success"');
expect(successResult.stdout).toContain('Success');
expect(successResult.exitCode).toEqual(0);
// Test that it throws on error
let errorThrown = false;
try {
await testSmartshell.execStrictSilent('echo "Error output" && exit 1');
} catch (error) {
errorThrown = true;
}
expect(errorThrown).toBeTrue();
});
tap.test('execStreamingSilent should capture streaming output without console display', async () => {
const streamingResult = await testSmartshell.execStreamingSilent('echo "Line 1" && sleep 0.1 && echo "Line 2"');
let capturedData = '';
streamingResult.childProcess.stdout.on('data', (data) => {
capturedData += data.toString();
});
await streamingResult.finalPromise;
expect(capturedData).toContain('Line 1');
expect(capturedData).toContain('Line 2');
});
tap.test('execAndWaitForLineSilent should wait for pattern without console output', async () => {
// This should complete without printing to console
await testSmartshell.execAndWaitForLineSilent('echo "Starting..." && echo "Ready to go"', /Ready to go/);
});
tap.test('execSilent should handle commands with quotes', async () => {
const result = await testSmartshell.execSilent('echo "Hello World"');
expect(result.stdout.trim()).toEqual('Hello World');
});
tap.test('execSilent should capture large output', async () => {
// Generate 100 lines of output
const result = await testSmartshell.execSilent('for i in {1..100}; do echo "Line $i"; done');
const lines = result.stdout.trim().split('\n');
expect(lines).toHaveLength(100);
expect(lines[0]).toEqual('Line 1');
expect(lines[99]).toEqual('Line 100');
});
tap.test('execSilent vs exec output comparison', async () => {
// Both should capture the same output, but exec prints to console
const silentResult = await testSmartshell.execSilent('echo "Test output"');
const normalResult = await testSmartshell.exec('echo "Test output"');
expect(silentResult.stdout).toEqual(normalResult.stdout);
expect(silentResult.exitCode).toEqual(normalResult.exitCode);
});
export default tap.start({
throwOnError: true,
});