nupst/test/test.logger.ts

147 lines
4.2 KiB
TypeScript

import { tap, expect } from '@push.rocks/tapbundle';
import { Logger } from '../ts/logger.js';
// Create a Logger instance for testing
const logger = new Logger();
tap.test('should create a logger instance', async () => {
expect(logger instanceof Logger).toBeTruthy();
});
tap.test('should log messages with different log levels', async () => {
// We're not testing console output directly, just ensuring no errors
logger.log('Regular log message');
logger.error('Error message');
logger.warn('Warning message');
logger.success('Success message');
// Just assert that the test runs without errors
expect(true).toBeTruthy();
});
tap.test('should create a logbox with title, content, and end', async () => {
// Just ensuring no errors occur
logger.logBoxTitle('Test Box', 40);
logger.logBoxLine('This is a test line');
logger.logBoxEnd();
// Just assert that the test runs without errors
expect(true).toBeTruthy();
});
tap.test('should handle width persistence between logbox calls', async () => {
logger.logBoxTitle('Width Test', 45);
// These should use the width from the title
logger.logBoxLine('Line 1');
logger.logBoxLine('Line 2');
logger.logBoxEnd();
let errorThrown = false;
try {
// This should work fine after the reset in logBoxEnd
logger.logBoxTitle('New Box', 30);
logger.logBoxLine('New line');
logger.logBoxEnd();
} catch (error) {
errorThrown = true;
}
expect(errorThrown).toBeFalsy();
});
tap.test('should throw error when using logBoxLine without width', async () => {
let errorThrown = false;
let errorMessage = '';
try {
// Should throw because no width is set
logger.logBoxLine('This should fail');
} catch (error) {
errorThrown = true;
errorMessage = (error as Error).message;
}
expect(errorThrown).toBeTruthy();
expect(errorMessage).toBeTruthy();
expect(errorMessage.includes('No box width')).toBeTruthy();
});
tap.test('should create a complete logbox in one call', async () => {
// Just ensuring no errors occur
logger.logBox('Complete Box', [
'Line 1',
'Line 2',
'Line 3'
], 40);
// Just assert that the test runs without errors
expect(true).toBeTruthy();
});
tap.test('should handle content that exceeds box width', async () => {
// Just ensuring no errors occur when content is too long
logger.logBox('Truncation Test', [
'This line is way too long and should be truncated because it exceeds the available space'
], 30);
// Just assert that the test runs without errors
expect(true).toBeTruthy();
});
tap.test('should create dividers with custom characters', async () => {
// Just ensuring no errors occur
logger.logDivider(30);
logger.logDivider(20, '*');
// Just assert that the test runs without errors
expect(true).toBeTruthy();
});
tap.test('Logger Demo', async () => {
console.log('\n=== LOGGER DEMO ===\n');
// Basic logging
logger.log('Regular log message');
logger.error('Error message');
logger.warn('Warning message');
logger.success('Success message');
// Logbox with title, content lines, and end
logger.logBoxTitle('Configuration Loaded', 50);
logger.logBoxLine('SNMP Settings:');
logger.logBoxLine(' Host: 127.0.0.1');
logger.logBoxLine(' Port: 161');
logger.logBoxLine(' Version: 1');
logger.logBoxEnd();
// Complete logbox in one call
logger.logBox('UPS Status', [
'Power Status: onBattery',
'Battery Capacity: 75%',
'Runtime Remaining: 30 minutes'
], 45);
// Logbox with content that's too long for the width
logger.logBox('Truncation Example', [
'This line is short enough to fit within the box width',
'This line is way too long and will be truncated because it exceeds the available space for content within the logbox'
], 40);
// Demonstrating logbox width being remembered
logger.logBoxTitle('Width Persistence Example', 60);
logger.logBoxLine('These lines use the width from the title');
logger.logBoxLine('No need to specify the width again');
logger.logBoxEnd();
// Divider example
logger.log('\nDivider example:');
logger.logDivider(30);
logger.logDivider(30, '*');
expect(true).toBeTruthy();
});
// Export the default tap object
export default tap.start();