seraph-agent
Version:
An extremely lightweight, SRE autonomous AI agent for seamless integration with common observability tasks.
123 lines (122 loc) • 4.4 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const alerter_1 = require("../alerter");
const node_fetch_1 = __importDefault(require("node-fetch"));
jest.mock('node-fetch', () => jest.fn());
const { Response } = jest.requireActual('node-fetch');
describe('AlerterClient', () => {
const mockFetch = node_fetch_1.default;
let consoleLogSpy;
let consoleErrorSpy;
beforeEach(() => {
consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => { });
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => { });
});
afterEach(() => {
jest.clearAllMocks();
consoleLogSpy.mockRestore();
consoleErrorSpy.mockRestore();
});
it('should send an alert to the configured Alertmanager URL', async () => {
const config = {
port: 8080,
workers: 1,
apiKey: 'test-key',
serverApiKey: null,
alertManager: {
url: 'http://fake-alertmanager:9093/api/v2/alerts',
},
};
const alerter = new alerter_1.AlerterClient(config);
const context = {
source: 'test-source',
type: 'test-type',
details: 'This is a test alert',
log: 'test log entry',
};
mockFetch.mockResolvedValue(new Response('OK', { status: 200 }));
await alerter.sendAlert(context);
expect(mockFetch).toHaveBeenCalledWith(config.alertManager?.url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify([
{
labels: {
alertname: 'SeraphAnomalyDetected',
source: 'test-source',
type: 'test-type',
},
annotations: {
summary: 'Anomaly detected in test-source',
description: 'This is a test alert',
log: 'test log entry',
},
},
]),
});
});
it('should not send an alert if the Alertmanager URL is not configured', async () => {
const config = {
port: 8080,
workers: 1,
apiKey: 'test-key',
serverApiKey: null,
};
const alerter = new alerter_1.AlerterClient(config);
const context = {
source: 'test-source',
type: 'test-type',
details: 'This is a test alert',
log: 'test log entry',
};
await alerter.sendAlert(context);
expect(mockFetch).not.toHaveBeenCalled();
});
it('should handle errors when sending an alert', async () => {
const config = {
port: 8080,
workers: 1,
apiKey: 'test-key',
serverApiKey: null,
alertManager: {
url: 'http://fake-alertmanager:9093/api/v2/alerts',
},
};
const alerter = new alerter_1.AlerterClient(config);
const context = {
source: 'test-source',
type: 'test-type',
details: 'This is a test alert',
log: 'test log entry',
};
mockFetch.mockRejectedValue(new Error('Network error'));
await alerter.sendAlert(context);
expect(mockFetch).toHaveBeenCalled();
});
it('should handle non-ok responses from Alertmanager', async () => {
const config = {
port: 8080,
workers: 1,
apiKey: 'test-key',
serverApiKey: null,
alertManager: {
url: 'http://fake-alertmanager:9093/api/v2/alerts',
},
};
const alerter = new alerter_1.AlerterClient(config);
const context = {
source: 'test-source',
type: 'test-type',
details: 'This is a test alert',
log: 'test log entry',
};
mockFetch.mockResolvedValue(new Response('Internal Server Error', { status: 500 }));
await alerter.sendAlert(context);
expect(mockFetch).toHaveBeenCalled();
});
});