smartlog/test/test.source-interactive.node.ts

190 lines
6.0 KiB
TypeScript

import { expect, tap } from '@git.zone/tstest/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();