UNPKG

@cere/rob-cli

Version:

CLI tool for deploying and managing rafts and data sources

273 lines 12.2 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const axios_1 = __importDefault(require("axios")); const api_client_1 = require("../lib/api-client"); // Create a mockAxiosInstance that we can reference in tests const mockAxiosInstance = { get: jest.fn().mockResolvedValue({ data: { data: [] } }), post: jest.fn().mockResolvedValue({ data: { data: {} } }), put: jest.fn().mockResolvedValue({ data: { data: {} } }), delete: jest.fn().mockResolvedValue({ data: { success: true } }), interceptors: { request: { use: jest.fn() }, response: { use: jest.fn() }, }, }; // Mock axios jest.mock('axios', () => { return { create: jest.fn(() => mockAxiosInstance), }; }); // Mock Logger jest.mock('../lib/logger', () => { return { Logger: jest.fn().mockImplementation(() => ({ info: jest.fn(), success: jest.fn(), error: jest.fn(), warn: jest.fn(), debug: jest.fn(), })), }; }); describe('API Client', () => { beforeEach(() => { jest.clearAllMocks(); }); describe('configureApiClient', () => { it('should create an axios instance with the provided config', () => { const config = { baseUrl: 'http://test-api.com', apiKey: 'test-key', timeout: 5000, }; (0, api_client_1.configureApiClient)(config); expect(axios_1.default.create).toHaveBeenCalledWith({ baseURL: 'http://test-api.com/api', timeout: 5000, headers: { 'Content-Type': 'application/json', }, }); }); it('should create an axios instance without Authorization header if no auth provided', () => { const config = { baseUrl: 'http://test-api.com', }; (0, api_client_1.configureApiClient)(config); expect(axios_1.default.create).toHaveBeenCalledWith({ baseURL: 'http://test-api.com/api', timeout: 30000, // Default timeout headers: { 'Content-Type': 'application/json', }, }); }); it('should create an axios instance with basic auth if provided', () => { const config = { baseUrl: 'http://test-api.com', basicAuth: { username: 'testuser', password: 'testpass', }, }; (0, api_client_1.configureApiClient)(config); // Base64 of testuser:testpass is dGVzdHVzZXI6dGVzdHBhc3M= expect(axios_1.default.create).toHaveBeenCalledWith({ baseURL: 'http://test-api.com/api', timeout: 30000, // Default timeout headers: { 'Content-Type': 'application/json', Authorization: 'Basic dGVzdHVzZXI6dGVzdHBhc3M=', }, }); }); it('should set up request and response interceptors', () => { (0, api_client_1.configureApiClient)({ baseUrl: 'http://test-api.com' }); expect(mockAxiosInstance.interceptors.request.use).toHaveBeenCalled(); expect(mockAxiosInstance.interceptors.response.use).toHaveBeenCalled(); }); }); describe('getApiClient', () => { it('should throw an error if called before configuring', () => { // Reset the module to clear the apiClient variable jest.resetModules(); // Import the module again // eslint-disable-next-line @typescript-eslint/no-var-requires const { getApiClient } = require('../lib/api-client'); expect(() => getApiClient()).toThrow('API client not configured'); }); it('should return the configured axios instance', () => { (0, api_client_1.configureApiClient)({ baseUrl: 'http://test-api.com' }); expect((0, api_client_1.getApiClient)()).toBe(mockAxiosInstance); }); }); describe('DataSourceApi', () => { beforeEach(() => { // Configure the API client (0, api_client_1.configureApiClient)({ baseUrl: 'http://test-api.com' }); // Setup mock responses const mockAxiosInstance = (0, api_client_1.getApiClient)(); mockAxiosInstance.get.mockResolvedValue({ data: { data: [{ id: 'ds1' }] }, }); mockAxiosInstance.post.mockResolvedValue({ data: { data: { id: 'ds1', name: 'Test DS' } }, }); mockAxiosInstance.put.mockResolvedValue({ data: { data: { id: 'ds1', name: 'Updated DS' } }, }); mockAxiosInstance.delete.mockResolvedValue({ data: { success: true }, }); }); it('should get all data sources', async () => { const result = await api_client_1.DataSourceApi.getAllDataSources('test-service'); expect((0, api_client_1.getApiClient)().get).toHaveBeenCalledWith('/data-sources', { params: { dataServiceId: 'test-service' }, }); expect(result).toEqual([{ id: 'ds1' }]); }); it('should get a data source by ID', async () => { await api_client_1.DataSourceApi.getDataSourceById('ds1'); expect((0, api_client_1.getApiClient)().get).toHaveBeenCalledWith('/data-sources/ds1', { params: {} }); }); it('should get a data source by ID with dataServiceId', async () => { await api_client_1.DataSourceApi.getDataSourceById('ds1', 'test-service'); expect((0, api_client_1.getApiClient)().get).toHaveBeenCalledWith('/data-sources/ds1', { params: { dataServiceId: 'test-service' }, }); }); it('should create a data source', async () => { const dataSource = { id: 'ds1', name: 'Test DS', type: 'postgresql', host: 'localhost', port: 5432, dataServiceId: 'test-service', }; await api_client_1.DataSourceApi.createDataSource(dataSource); expect((0, api_client_1.getApiClient)().post).toHaveBeenCalledWith('/data-sources', dataSource); }); it('should update a data source', async () => { const dataSource = { name: 'Updated DS', }; await api_client_1.DataSourceApi.updateDataSource('ds1', dataSource); expect((0, api_client_1.getApiClient)().put).toHaveBeenCalledWith('/data-sources/ds1', dataSource, { params: {}, }); }); it('should update a data source with dataServiceId', async () => { const dataSource = { name: 'Updated DS', }; await api_client_1.DataSourceApi.updateDataSource('ds1', dataSource, 'test-service'); expect((0, api_client_1.getApiClient)().put).toHaveBeenCalledWith('/data-sources/ds1', dataSource, { params: { dataServiceId: 'test-service' }, }); }); it('should delete a data source', async () => { await api_client_1.DataSourceApi.deleteDataSource('ds1'); expect((0, api_client_1.getApiClient)().delete).toHaveBeenCalledWith('/data-sources/ds1', { params: {} }); }); it('should delete a data source with dataServiceId', async () => { await api_client_1.DataSourceApi.deleteDataSource('ds1', 'test-service'); expect((0, api_client_1.getApiClient)().delete).toHaveBeenCalledWith('/data-sources/ds1', { params: { dataServiceId: 'test-service' }, }); }); it('should check data source connection', async () => { const config = { type: 'postgresql', host: 'localhost', port: 5432, }; await api_client_1.DataSourceApi.checkConnection(config); expect((0, api_client_1.getApiClient)().post).toHaveBeenCalledWith('/data-sources/check-connection', config); }); }); describe('RaftApi', () => { beforeEach(() => { // Configure the API client (0, api_client_1.configureApiClient)({ baseUrl: 'http://test-api.com' }); // Setup mock responses const mockAxiosInstance = (0, api_client_1.getApiClient)(); mockAxiosInstance.get.mockResolvedValue({ data: { data: [{ id: 'raft1' }] }, }); mockAxiosInstance.post.mockResolvedValue({ data: { data: { id: 'raft1', name: 'Test Raft' } }, }); mockAxiosInstance.put.mockResolvedValue({ data: { data: { id: 'raft1', name: 'Updated Raft' } }, }); mockAxiosInstance.delete.mockResolvedValue({ data: { success: true }, }); }); it('should get all rafts', async () => { const result = await api_client_1.RaftApi.getAllRafts('test-service'); expect((0, api_client_1.getApiClient)().get).toHaveBeenCalledWith('/rafts', { params: { dataServiceId: 'test-service' }, }); expect(result).toEqual([{ id: 'raft1' }]); }); it('should get a raft by ID', async () => { await api_client_1.RaftApi.getRaftById('raft1'); expect((0, api_client_1.getApiClient)().get).toHaveBeenCalledWith('/rafts/raft1', { params: {} }); }); it('should get a raft by ID with dataServiceId', async () => { await api_client_1.RaftApi.getRaftById('raft1', 'test-service'); expect((0, api_client_1.getApiClient)().get).toHaveBeenCalledWith('/rafts/raft1', { params: { dataServiceId: 'test-service' }, }); }); it('should create a raft', async () => { const raft = { id: 'raft1', name: 'Test Raft', triggers: [], dataSourcesIds: ['ds1'], dataServiceId: 'test-service', indexing: { scriptFile: './test.ts', }, }; await api_client_1.RaftApi.createRaft(raft); expect((0, api_client_1.getApiClient)().post).toHaveBeenCalledWith('/rafts', raft); }); it('should update a raft', async () => { const raft = { name: 'Updated Raft', }; await api_client_1.RaftApi.updateRaft('raft1', raft); expect((0, api_client_1.getApiClient)().put).toHaveBeenCalledWith('/rafts/raft1', raft, { params: {} }); }); it('should update a raft with dataServiceId', async () => { const raft = { name: 'Updated Raft', }; await api_client_1.RaftApi.updateRaft('raft1', raft, 'test-service'); expect((0, api_client_1.getApiClient)().put).toHaveBeenCalledWith('/rafts/raft1', raft, { params: { dataServiceId: 'test-service' }, }); }); it('should delete a raft', async () => { await api_client_1.RaftApi.deleteRaft('raft1'); expect((0, api_client_1.getApiClient)().delete).toHaveBeenCalledWith('/rafts/raft1', { params: {} }); }); it('should delete a raft with dataServiceId', async () => { await api_client_1.RaftApi.deleteRaft('raft1', 'test-service'); expect((0, api_client_1.getApiClient)().delete).toHaveBeenCalledWith('/rafts/raft1', { params: { dataServiceId: 'test-service' }, }); }); }); }); //# sourceMappingURL=api-client.test.js.map