smartlog/test/test.destination-local.node.ts

114 lines
3.4 KiB
TypeScript

import { expect, tap } from '@git.zone/tstest/tapbundle';
import { DestinationLocal } from '../ts_destination_local/index.js';
import * as smartlogInterfaces from '../ts_interfaces/index.js';
let testDestination: DestinationLocal;
// Mock log package
const createMockLogPackage = (level: smartlogInterfaces.TLogLevel, message: string): smartlogInterfaces.ILogPackage => {
return {
timestamp: Date.now(),
type: 'log',
level,
message,
context: {
environment: 'test',
runtime: 'node'
},
correlation: {
id: '123',
type: 'none'
}
};
};
// Tests
tap.test('should create a local destination instance', async () => {
testDestination = new DestinationLocal();
expect(testDestination).toBeTruthy();
});
tap.test('should handle logs with different levels', async () => {
// Testing with a spy would be ideal, but since we don't have a mocking framework,
// we'll just verify the method runs without errors for different log levels
// Test info level
const logPackageInfo = createMockLogPackage('info', 'Info message');
await testDestination.handleLog(logPackageInfo);
// Test error level
const logPackageError = createMockLogPackage('error', 'Error message');
await testDestination.handleLog(logPackageError);
// Test warn level
const logPackageWarn = createMockLogPackage('warn', 'Warning message');
await testDestination.handleLog(logPackageWarn);
// Test silly level
const logPackageSilly = createMockLogPackage('silly', 'Silly message');
await testDestination.handleLog(logPackageSilly);
});
tap.test('should handle reduced logging', async () => {
testDestination = new DestinationLocal();
// Note: In a real test environment with a mocking framework,
// we would capture console output and verify it's only written
// according to the expected behavior. Here we just ensure
// the methods execute without errors.
// First call with message
testDestination.logReduced('Test message');
// Same message immediately after
testDestination.logReduced('Test message');
// Different message
testDestination.logReduced('Different message');
});
tap.test('should handle repeated logging with repeatEveryTimesArg', async () => {
testDestination = new DestinationLocal();
// First call with message
testDestination.logReduced('Repeated with count', 3);
// Second call
testDestination.logReduced('Repeated with count', 3);
// Third call
testDestination.logReduced('Repeated with count', 3);
// Fourth call (3rd repetition)
testDestination.logReduced('Repeated with count', 3);
});
tap.test('should create new line(s)', async () => {
testDestination = new DestinationLocal();
// Default 1 line
testDestination.newLine();
// Multiple lines
testDestination.newLine(3);
});
// Test debug level rendering and fallback for unknown levels
tap.test('should handle debug and unknown log levels', async () => {
testDestination = new DestinationLocal();
let out = '';
const originalLog = console.log;
console.log = (msg: string) => { out += msg; };
// debug level should render message correctly
await testDestination.handleLog(createMockLogPackage('debug', 'debug 🎉'));
expect(out).toContain('debug 🎉');
// fallback for unknown level should still render message
out = '';
await testDestination.handleLog(createMockLogPackage('verbose' as any, 'verbose ⚠️'));
expect(out).toContain('verbose ⚠️');
console.log = originalLog;
});
export default tap.start();