seraph-agent
Version:
An extremely lightweight, SRE autonomous AI agent for seamless integration with common observability tasks.
120 lines (119 loc) • 5.19 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
const config_1 = require("../config");
const fs = __importStar(require("fs"));
jest.mock('fs', () => ({
promises: {
access: jest.fn(),
readFile: jest.fn(),
},
}));
const mockedFs = fs.promises;
describe('loadConfig', () => {
const originalEnv = process.env;
beforeEach(() => {
jest.resetModules(); // Clears the cache
process.env = { ...originalEnv }; // Make a copy
mockedFs.readFile.mockClear();
mockedFs.access.mockClear();
});
afterAll(() => {
process.env = originalEnv; // Restore old environment
});
it('should return the default config if no config file is found', async () => {
const error = new Error('File not found');
error.code = 'ENOENT';
mockedFs.access.mockRejectedValue(error);
const config = await (0, config_1.loadConfig)();
expect(config.port).toBe(8080);
expect(config.workers).toBe(4);
});
it('should load the user config from seraph.config.json', async () => {
const userConfig = {
port: 9000,
workers: 8,
llm: { provider: 'openai', model: 'gpt-4' },
};
mockedFs.access.mockResolvedValue(undefined);
mockedFs.readFile.mockResolvedValue(JSON.stringify(userConfig));
const config = await (0, config_1.loadConfig)();
expect(config.port).toBe(9000);
expect(config.workers).toBe(8);
expect(config.llm?.provider).toBe('openai');
expect(config.llm?.model).toBe('gpt-4');
});
it('should use GEMINI_API_KEY for gemini provider', async () => {
process.env.GEMINI_API_KEY = 'gemini-key';
const error = new Error('File not found');
error.code = 'ENOENT';
mockedFs.access.mockRejectedValue(error); // No config file
const config = await (0, config_1.loadConfig)();
expect(config.apiKey).toBe('gemini-key');
});
it('should use ANTHROPIC_API_KEY for anthropic provider', async () => {
process.env.ANTHROPIC_API_KEY = 'anthropic-key';
const userConfig = { llm: { provider: 'anthropic' } };
mockedFs.access.mockResolvedValue(undefined);
mockedFs.readFile.mockResolvedValue(JSON.stringify(userConfig));
const config = await (0, config_1.loadConfig)();
expect(config.apiKey).toBe('anthropic-key');
});
it('should use OPENAI_API_KEY for openai provider', async () => {
process.env.OPENAI_API_KEY = 'openai-key';
const userConfig = { llm: { provider: 'openai' } };
mockedFs.access.mockResolvedValue(undefined);
mockedFs.readFile.mockResolvedValue(JSON.stringify(userConfig));
const config = await (0, config_1.loadConfig)();
expect(config.apiKey).toBe('openai-key');
});
it('should prioritize the api key from the config file over environment variables', async () => {
process.env.GEMINI_API_KEY = 'env-key';
const userConfig = { apiKey: 'file-key' };
mockedFs.access.mockResolvedValue(undefined);
mockedFs.readFile.mockResolvedValue(JSON.stringify(userConfig));
const config = await (0, config_1.loadConfig)();
expect(config.apiKey).toBe('file-key');
});
it('should handle invalid JSON in the config file gracefully', async () => {
mockedFs.access.mockResolvedValue(undefined);
mockedFs.readFile.mockResolvedValue('invalid json');
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => { });
const config = await (0, config_1.loadConfig)();
expect(config.port).toBe(8080); // Should fall back to default
expect(consoleErrorSpy).toHaveBeenCalled();
consoleErrorSpy.mockRestore();
});
});