import { DyNTS_ApiService_Base } from './api.service-base'; import { DyNTS_ApiService } from '../core/api.service'; import { DyFM_Error, DyFM_HttpCallType, DyFM_Log } from '@futdevpro/fsm-dynamo'; import { DyNTS_ApiCall_Params } from '../../_models/control-models/api-call-params.control-model'; class TestApiService extends DyNTS_ApiService_Base { get baseUrl(): string { return 'https://api.example.com'; } get testEndpoint(): string { return '/test'; } get connectingSystemName(): string { return 'TestSystem'; } static getInstance(): TestApiService { return TestApiService.getSingletonInstance(); } } describe('| DyNTS_ApiService_Base', () => { let service: TestApiService; beforeEach(() => { service = TestApiService.getInstance(); }); it('| should be a singleton instance', () => { const instance1 = TestApiService.getInstance(); const instance2 = TestApiService.getInstance(); expect(instance1).toBe(instance2); expect(instance1).toBeInstanceOf(TestApiService); }); describe('| runApiCallWithAvailabilityCheck', () => { it('| should return result when api call succeeds', async () => { const apiCall = jasmine.createSpy('apiCall').and.returnValue(Promise.resolve('success')); const result = await service.runApiCallWithAvailabilityCheck(apiCall, 'issuer-123'); expect(result).toBe('success'); expect(apiCall).toHaveBeenCalled(); }); it('| should check server availability when api call fails', async () => { const apiCall = jasmine.createSpy('apiCall').and.returnValue( Promise.reject(new Error('API call failed')) ); spyOn(service, 'checkServerAvailability').and.returnValue(Promise.resolve()); await expectAsync( service.runApiCallWithAvailabilityCheck(apiCall, 'issuer-123') ).toBeRejectedWithError('API call failed'); expect(service.checkServerAvailability).toHaveBeenCalledWith('issuer-123'); }); }); describe('| checkServerAvailability', () => { it('| should succeed when testGet succeeds', async () => { spyOn(service, 'testGet').and.returnValue(Promise.resolve()); const logSpy = spyOn(DyFM_Log, 'success'); await service.checkServerAvailability('issuer-123'); expect(service.testGet).toHaveBeenCalledWith('issuer-123'); }); it('| should throw error when testGet fails', async () => { spyOn(service, 'testGet').and.returnValue( Promise.reject(new Error('Test failed')) ); try { await service.checkServerAvailability('issuer-123'); fail('Should have thrown an error'); } catch (error) { expect(error).toBeInstanceOf(DyFM_Error); expect((error as DyFM_Error)._message).toContain('TestSystem server is not available'); expect((error as DyFM_Error)._message).toContain('baseUrl: https://api.example.com'); expect((error as DyFM_Error).__userMessage).toContain('TestSystem server is not available'); expect((error as DyFM_Error)._errorCode).toContain('DyNTS-ASB-CSA0'); } }); }); describe('| testGet', () => { it('| should call ApiService.startApiCall with correct parameters', async () => { const startApiCallSpy = spyOn(DyNTS_ApiService, 'startApiCall').and.returnValue( Promise.resolve() ); const logSpy = spyOn(DyFM_Log, 'success'); await service.testGet('issuer-123'); expect(startApiCallSpy).toHaveBeenCalledWith( jasmine.any(DyNTS_ApiCall_Params) ); const callParams = startApiCallSpy.calls.mostRecent().args[0] as DyNTS_ApiCall_Params; expect(callParams.type).toBe(DyFM_HttpCallType.get); expect(callParams.baseUrl).toBe('https://api.example.com'); expect(callParams.endpoint).toBe('/test'); expect(logSpy).toHaveBeenCalled(); }); it('| should throw error when testEndpoint is not set', async () => { const serviceWithoutEndpoint = new (class extends DyNTS_ApiService_Base { get baseUrl(): string { return 'https://api.example.com'; } get testEndpoint(): string { return ''; } get connectingSystemName(): string { return 'TestSystem'; } static getInstance() { return this.getSingletonInstance(); } } as any)(); await expectAsync( serviceWithoutEndpoint.testGet('issuer-123') ).toBeRejected(); }); }); });