115 lines
3.2 KiB
TypeScript
115 lines
3.2 KiB
TypeScript
import { expect, tap } from '@push.rocks/tapbundle';
|
|
import { SmartlogReceiver } from '../ts_receiver/index.js';
|
|
import { Smartlog } from '../dist_ts/index.js';
|
|
import * as smartlogInterfaces from '../ts_interfaces/index.js';
|
|
import * as smarthash from '@push.rocks/smarthash';
|
|
|
|
let testSmartlog: Smartlog;
|
|
let testReceiver: SmartlogReceiver;
|
|
const testPassphrase = 'test-secret-passphrase';
|
|
|
|
// Helper to create authenticated log package
|
|
const createAuthenticatedLogPackage = (
|
|
level: smartlogInterfaces.TLogLevel,
|
|
message: string
|
|
): smartlogInterfaces.ILogPackageAuthenticated => {
|
|
const logPackage: smartlogInterfaces.ILogPackage = {
|
|
timestamp: Date.now(),
|
|
type: 'log',
|
|
level,
|
|
message,
|
|
context: {
|
|
environment: 'test',
|
|
runtime: 'node'
|
|
},
|
|
correlation: {
|
|
id: '123',
|
|
type: 'none'
|
|
}
|
|
};
|
|
|
|
return {
|
|
auth: smarthash.sha256FromStringSync(testPassphrase),
|
|
logPackage
|
|
};
|
|
};
|
|
|
|
// Tests
|
|
tap.test('should create a Smartlog instance for receiver', 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 SmartlogReceiver instance', async () => {
|
|
// Create a validator function that always returns true
|
|
const validatorFunction = async () => true;
|
|
|
|
testReceiver = new SmartlogReceiver({
|
|
smartlogInstance: testSmartlog,
|
|
passphrase: testPassphrase,
|
|
validatorFunction
|
|
});
|
|
|
|
expect(testReceiver).toBeTruthy();
|
|
expect(testReceiver.passphrase).toEqual(testPassphrase);
|
|
});
|
|
|
|
tap.test('should handle authenticated log with correct passphrase', async () => {
|
|
const authLogPackage = createAuthenticatedLogPackage('info', 'Test authenticated message');
|
|
|
|
const result = await testReceiver.handleAuthenticatedLog(authLogPackage);
|
|
|
|
expect(result).toBeTruthy();
|
|
expect(result.status).toEqual('ok');
|
|
});
|
|
|
|
tap.test('should reject authenticated log with incorrect passphrase', async () => {
|
|
const logPackage: smartlogInterfaces.ILogPackage = {
|
|
timestamp: Date.now(),
|
|
type: 'log',
|
|
level: 'info',
|
|
message: 'Test unauthorized message',
|
|
context: {
|
|
environment: 'test',
|
|
runtime: 'node'
|
|
},
|
|
correlation: {
|
|
id: '123',
|
|
type: 'none'
|
|
}
|
|
};
|
|
|
|
const badAuthPackage = {
|
|
auth: 'incorrect-hash',
|
|
logPackage
|
|
};
|
|
|
|
const result = await testReceiver.handleAuthenticatedLog(badAuthPackage);
|
|
|
|
expect(result).toBeTruthy();
|
|
expect(result.status).toEqual('error');
|
|
});
|
|
|
|
tap.test('should handle many authenticated logs', async () => {
|
|
const authLogPackage1 = createAuthenticatedLogPackage('info', 'Test batch message 1');
|
|
const authLogPackage2 = createAuthenticatedLogPackage('warn', 'Test batch message 2');
|
|
const authLogPackage3 = createAuthenticatedLogPackage('error', 'Test batch message 3');
|
|
|
|
const authLogPackages = [authLogPackage1, authLogPackage2, authLogPackage3];
|
|
|
|
await testReceiver.handleManyAuthenticatedLogs(authLogPackages);
|
|
|
|
// No assertions needed as we're just testing it doesn't throw errors
|
|
});
|
|
|
|
export default tap.start(); |