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

72 lines
2.0 KiB
TypeScript

import { expect, tap } from '@push.rocks/tapbundle';
import { SmartlogDestinationReceiver } from '../ts_destination_receiver/index.js';
import { Smartlog } from '../ts/index.js';
import * as smartlogInterfaces from '../ts_interfaces/index.js';
let testDestination: SmartlogDestinationReceiver;
let testSmartlog: Smartlog;
// 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 Smartlog instance', async () => {
testSmartlog = new Smartlog({
logContext: {
environment: 'test',
runtime: 'node',
zone: 'test-zone',
company: 'Test Company',
companyunit: 'Test Unit',
containerName: 'test-container',
},
});
expect(testSmartlog).toBeTruthy();
});
tap.test('should create a destination receiver instance with valid options', async () => {
testDestination = new SmartlogDestinationReceiver({
passphrase: 'test-passphrase',
receiverEndpoint: 'https://example.com/logs',
});
expect(testDestination).toBeTruthy();
});
tap.test('should attempt to send logs to the receiver endpoint', async () => {
// Create a mock version of the webrequest.postJson method to avoid actual HTTP calls
const originalPostJson = testDestination['webrequest'].postJson;
testDestination['webrequest'].postJson = async () => {
return {
body: { status: 'ok' },
statusCode: 200
};
};
try {
const logPackage = createMockLogPackage('info', 'Test receiver message');
const result = await testDestination.handleLog(logPackage);
expect(result).toEqual({ status: 'ok' });
} finally {
// Restore the original method
testDestination['webrequest'].postJson = originalPostJson;
}
});
export default tap.start();