feat(interactive): Add interactive console features and refactor spinner module by renaming source-ora to source-interactive and removing ora dependency

This commit is contained in:
2025-05-15 19:53:29 +00:00
parent 09da3a1e2d
commit 7e8a404fcf
13 changed files with 989 additions and 269 deletions

View File

@ -0,0 +1,64 @@
import { expect, tap } from '@push.rocks/tapbundle';
import { SmartlogSourceInteractive, SmartlogProgressBar } from '../ts_source_interactive/index.js';
// Test instances
let testSpinner: SmartlogSourceInteractive;
let testProgressBar: SmartlogProgressBar;
// Original state for restoration
const originalState = {
stdoutTTY: process.stdout.isTTY,
consoleLog: console.log
};
// Log tracking
const logs: string[] = [];
tap.test('should handle non-interactive mode correctly', async (toolsArg) => {
// Setup non-interactive mode
process.stdout.isTTY = false;
console.log = (...args: any[]) => {
logs.push(args.join(' '));
};
// Test spinner creation
testSpinner = new SmartlogSourceInteractive();
expect(testSpinner).toBeTruthy();
// Test spinner text
logs.length = 0;
testSpinner.text('Loading data');
expect(logs.length).toBeGreaterThan(0);
expect(logs[0]).toContain('[Loading]');
expect(logs[0]).toContain('Loading data');
// Test spinner success
logs.length = 0;
testSpinner.finishSuccess('Task completed');
expect(logs.length).toBeGreaterThan(0);
expect(logs[0]).toContain('[Success]');
expect(logs[0]).toContain('Task completed');
// Test progress bar
testProgressBar = new SmartlogProgressBar({ total: 100 });
expect(testProgressBar).toBeTruthy();
// Test progress updates
logs.length = 0;
testProgressBar.update(10);
testProgressBar.update(50);
testProgressBar.update(100);
expect(logs.length).toBeGreaterThan(0);
const progressLogs = logs.join(' ');
expect(progressLogs).toContain('10%');
expect(progressLogs).toContain('50%');
expect(progressLogs).toContain('100%');
// Cleanup
testSpinner.stop();
console.log = originalState.consoleLog;
process.stdout.isTTY = originalState.stdoutTTY;
});
export default tap.start();

View File

@ -0,0 +1,190 @@
import { expect, tap } from '@push.rocks/tapbundle';
import { SmartlogSourceInteractive, SmartlogProgressBar, SmartlogSourceOra } from '../ts_source_interactive/index.js';
// Test spinner functionality
let testSpinner: SmartlogSourceInteractive;
// Helper function to clean up spinners after each test
const cleanupSpinner = (spinner: SmartlogSourceInteractive) => {
if (spinner.isStarted()) {
spinner.stop();
}
};
tap.test('should create a SmartlogSourceInteractive instance', async () => {
testSpinner = new SmartlogSourceInteractive();
testSpinner.setSpeed(10); // Set fast animation speed for tests
expect(testSpinner).toBeTruthy();
expect(testSpinner.isStarted()).toBeFalse();
});
tap.test('should set text and start spinner', async () => {
const testText = 'Testing spinner';
testSpinner.text(testText);
expect(testSpinner.isStarted()).toBeTrue();
cleanupSpinner(testSpinner);
});
tap.test('should update text', async () => {
const newText = 'Updated text';
testSpinner.text(newText);
expect(testSpinner.isStarted()).toBeTrue();
cleanupSpinner(testSpinner);
});
tap.test('should stop spinner', async () => {
testSpinner.stop();
// We can't easily test the visual state, but we can verify it doesn't throw errors
});
tap.test('should finish with success', async () => {
testSpinner = new SmartlogSourceInteractive();
testSpinner.text('Starting again');
const successText = 'Operation successful';
testSpinner.finishSuccess(successText);
expect(testSpinner.isStarted()).toBeFalse();
});
tap.test('should finish with failure', async () => {
testSpinner = new SmartlogSourceInteractive();
testSpinner.text('Starting again');
const failText = 'Operation failed';
testSpinner.finishFail(failText);
expect(testSpinner.isStarted()).toBeFalse();
});
tap.test('should handle success and next', async () => {
testSpinner = new SmartlogSourceInteractive();
testSpinner.setSpeed(10); // Fast animation
testSpinner.text('Starting again');
const nextText = 'Next operation';
testSpinner.successAndNext(nextText);
expect(testSpinner.isStarted()).toBeTrue();
cleanupSpinner(testSpinner);
});
tap.test('should handle fail and next', async () => {
testSpinner = new SmartlogSourceInteractive();
testSpinner.setSpeed(10); // Fast animation
testSpinner.text('Starting again');
const nextText = 'Next operation after failure';
testSpinner.failAndNext(nextText);
expect(testSpinner.isStarted()).toBeTrue();
cleanupSpinner(testSpinner);
});
tap.test('should set spinner style', async () => {
testSpinner = new SmartlogSourceInteractive();
testSpinner.setSpeed(10); // Fast animation
testSpinner.setSpinnerStyle('line');
testSpinner.text('Custom style spinner');
// Visual effect can't be easily tested, but we can verify it doesn't throw errors
expect(testSpinner.isStarted()).toBeTrue();
cleanupSpinner(testSpinner);
});
tap.test('should set spinner color', async () => {
testSpinner = new SmartlogSourceInteractive();
testSpinner.setSpeed(10); // Fast animation
testSpinner.setColor('green');
testSpinner.text('Green spinner');
// Visual effect can't be easily tested, but we can verify it doesn't throw errors
expect(testSpinner.isStarted()).toBeTrue();
cleanupSpinner(testSpinner);
});
tap.test('should set animation speed', async () => {
testSpinner = new SmartlogSourceInteractive();
testSpinner.setSpeed(10); // Actually set fast for testing
testSpinner.text('Slow spinner');
// Visual effect can't be easily tested, but we can verify it doesn't throw errors
expect(testSpinner.isStarted()).toBeTrue();
cleanupSpinner(testSpinner);
});
// Test progress bar functionality
let testProgressBar: SmartlogProgressBar;
tap.test('should create a progress bar instance', async () => {
testProgressBar = new SmartlogProgressBar({
total: 100
});
expect(testProgressBar).toBeTruthy();
});
tap.test('should update progress bar value', async () => {
testProgressBar.update(50);
// Visual effect can't be easily tested, but we can verify it doesn't throw errors
});
tap.test('should increment progress bar', async () => {
const initialValue = 50;
const increment = 10;
testProgressBar = new SmartlogProgressBar({ total: 100 });
testProgressBar.update(initialValue);
testProgressBar.increment(increment);
// Visual effect can't be easily tested, but we can verify it doesn't throw errors
});
tap.test('should complete progress bar', async () => {
testProgressBar = new SmartlogProgressBar({ total: 100 });
testProgressBar.update(50);
testProgressBar.update(100); // Update to 100% to simulate completion
// Visual effect can't be easily tested, but we can verify it doesn't throw errors
});
tap.test('should set progress bar color', async () => {
testProgressBar = new SmartlogProgressBar({ total: 100 });
testProgressBar.setColor('blue');
testProgressBar.update(50);
// Visual effect can't be easily tested, but we can verify it doesn't throw errors
});
tap.test('should handle custom progress bar options', async () => {
testProgressBar = new SmartlogProgressBar({
total: 100,
width: 40,
complete: '=',
incomplete: '-',
showEta: false,
showPercent: true,
showCount: true
});
testProgressBar.update(30);
// Visual effect can't be easily tested, but we can verify it doesn't throw errors
});
// Test backward compatibility with SmartlogSourceOra
let testSourceOra: SmartlogSourceOra;
tap.test('should create a SmartlogSourceOra instance for backward compatibility', async () => {
testSourceOra = new SmartlogSourceOra();
expect(testSourceOra).toBeTruthy();
expect(testSourceOra.isStarted()).toBeFalse();
});
tap.test('should maintain compatibility with old API', async () => {
testSourceOra.setSpeed(10); // Fast animation
testSourceOra.text('Testing backward compatibility');
expect(testSourceOra.isStarted()).toBeTrue();
testSourceOra.finishSuccess('Success');
expect(testSourceOra.isStarted()).toBeFalse();
});
export default tap.start();

View File

@ -1,75 +0,0 @@
import { expect, tap } from '@push.rocks/tapbundle';
import { SmartlogSourceOra } from '../ts_source_ora/index.js';
let testSourceOra: SmartlogSourceOra;
tap.test('should create a SmartlogSourceOra instance', async () => {
testSourceOra = new SmartlogSourceOra();
expect(testSourceOra).toBeTruthy();
expect(testSourceOra.started).toBeFalse();
});
tap.test('should set text and start spinner', async () => {
const testText = 'Testing ora spinner';
testSourceOra.text(testText);
expect(testSourceOra.started).toBeTrue();
expect(testSourceOra.oraInstance.text).toEqual(testText);
});
tap.test('should update text', async () => {
const newText = 'Updated text';
testSourceOra.text(newText);
expect(testSourceOra.oraInstance.text).toEqual(newText);
expect(testSourceOra.started).toBeTrue();
});
tap.test('should stop spinner', async () => {
testSourceOra.stop();
// We can't easily test the visual state, but we can verify it doesn't throw errors
});
tap.test('should finish with success', async () => {
testSourceOra = new SmartlogSourceOra();
testSourceOra.text('Starting again');
const successText = 'Operation successful';
testSourceOra.finishSuccess(successText);
expect(testSourceOra.started).toBeFalse();
});
tap.test('should finish with failure', async () => {
testSourceOra = new SmartlogSourceOra();
testSourceOra.text('Starting again');
const failText = 'Operation failed';
testSourceOra.finishFail(failText);
expect(testSourceOra.started).toBeFalse();
});
tap.test('should handle success and next', async () => {
testSourceOra = new SmartlogSourceOra();
testSourceOra.text('Starting again');
const nextText = 'Next operation';
testSourceOra.successAndNext(nextText);
expect(testSourceOra.started).toBeTrue();
expect(testSourceOra.oraInstance.text).toEqual(nextText);
});
tap.test('should handle fail and next', async () => {
testSourceOra = new SmartlogSourceOra();
testSourceOra.text('Starting again');
const nextText = 'Next operation after failure';
testSourceOra.failAndNext(nextText);
expect(testSourceOra.started).toBeTrue();
expect(testSourceOra.oraInstance.text).toEqual(nextText);
});
export default tap.start();