mirror of
https://github.com/ptarmiganlabs/butler-sos.git
synced 2025-12-19 17:58:18 -05:00
Fix broken tests
This commit is contained in:
@@ -23,6 +23,9 @@ jest.unstable_mockModule('../../globals.js', () => ({
|
||||
verbose: jest.fn(),
|
||||
debug: jest.fn(),
|
||||
},
|
||||
errorTracker: {
|
||||
incrementError: jest.fn(),
|
||||
},
|
||||
config: {
|
||||
get: jest.fn(),
|
||||
has: jest.fn(),
|
||||
|
||||
@@ -8,6 +8,9 @@ jest.unstable_mockModule('../../globals.js', () => ({
|
||||
debug: jest.fn(),
|
||||
verbose: jest.fn(),
|
||||
},
|
||||
errorTracker: {
|
||||
incrementError: jest.fn(),
|
||||
},
|
||||
mqttClient: {
|
||||
publish: jest.fn(),
|
||||
},
|
||||
@@ -19,12 +22,20 @@ jest.unstable_mockModule('../../globals.js', () => ({
|
||||
}));
|
||||
const globals = (await import('../../globals.js')).default;
|
||||
|
||||
// Mock log-error module
|
||||
const mockLogError = jest.fn();
|
||||
jest.unstable_mockModule('../log-error.js', () => ({
|
||||
logError: mockLogError,
|
||||
}));
|
||||
|
||||
// Import the module under test
|
||||
const { postHealthToMQTT, postUserSessionsToMQTT, postUserEventToMQTT } =
|
||||
await import('../post-to-mqtt.js');
|
||||
|
||||
describe('post-to-mqtt', () => {
|
||||
beforeEach(() => {
|
||||
// Reset all mocks before each test
|
||||
jest.clearAllMocks();
|
||||
// Setup default config values
|
||||
globals.config.get.mockImplementation((path) => {
|
||||
if (path === 'Butler-SOS.mqttConfig.baseTopic') {
|
||||
@@ -496,7 +507,7 @@ describe('post-to-mqtt', () => {
|
||||
);
|
||||
});
|
||||
|
||||
test('should handle errors during publishing', () => {
|
||||
test('should handle errors during publishing', async () => {
|
||||
// Force an error by making the MQTT client throw
|
||||
globals.mqttClient.publish.mockImplementation(() => {
|
||||
throw new Error('MQTT publish error');
|
||||
@@ -515,11 +526,12 @@ describe('post-to-mqtt', () => {
|
||||
};
|
||||
|
||||
// Call the function being tested
|
||||
postUserEventToMQTT(userEvent);
|
||||
await postUserEventToMQTT(userEvent);
|
||||
|
||||
// Verify error was logged
|
||||
expect(globals.logger.error).toHaveBeenCalledWith(
|
||||
expect.stringContaining('USER EVENT MQTT: Failed posting message to MQTT')
|
||||
expect(mockLogError).toHaveBeenCalledWith(
|
||||
expect.stringContaining('USER EVENT MQTT: Failed posting message to MQTT'),
|
||||
expect.any(Error)
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -39,6 +39,9 @@ jest.unstable_mockModule('../../globals.js', () => ({
|
||||
debug: jest.fn(),
|
||||
error: jest.fn(),
|
||||
},
|
||||
errorTracker: {
|
||||
incrementError: jest.fn(),
|
||||
},
|
||||
config: {
|
||||
get: jest.fn().mockImplementation((path) => {
|
||||
if (path === 'Butler-SOS.newRelic.enable') return true;
|
||||
|
||||
@@ -52,6 +52,9 @@ jest.unstable_mockModule('../../globals.js', () => ({
|
||||
debug: jest.fn(),
|
||||
error: jest.fn(),
|
||||
},
|
||||
errorTracker: {
|
||||
incrementError: jest.fn(),
|
||||
},
|
||||
config: {
|
||||
get: jest.fn().mockImplementation((path) => {
|
||||
if (path === 'Butler-SOS.cert.clientCert') return '/path/to/cert.pem';
|
||||
|
||||
@@ -217,7 +217,7 @@ describe('Log Event Handler Sanitization', () => {
|
||||
});
|
||||
|
||||
describe('QIX Performance Event Handler', () => {
|
||||
it('should sanitize method and object_type fields', () => {
|
||||
it('should sanitize method and object_type fields', async () => {
|
||||
const msg = [
|
||||
'/qseow-qix-perf/',
|
||||
'1',
|
||||
@@ -247,7 +247,7 @@ describe('Log Event Handler Sanitization', () => {
|
||||
'linechart\x02', // Field 25: object_type
|
||||
];
|
||||
|
||||
const result = processQixPerfEvent(msg);
|
||||
const result = await processQixPerfEvent(msg);
|
||||
if (result) {
|
||||
expect(result.method).not.toMatch(/[\x00-\x1F\x7F]/);
|
||||
expect(result.object_type).not.toMatch(/[\x00-\x1F\x7F]/);
|
||||
|
||||
Reference in New Issue
Block a user