import { DyNTS_SingletonService } from './singleton.service'; import { DyFM_Error, DyFM_Error_Settings } from '@futdevpro/fsm-dynamo'; import { DyNTS_global_settings } from '../../_collections/global-settings.const'; class TestSingletonService extends DyNTS_SingletonService { static getInstance(): TestSingletonService { return TestSingletonService.getSingletonInstance(); } } describe('| DyNTS_SingletonService', () => { let service: TestSingletonService; beforeEach(() => { service = TestSingletonService.getInstance(); }); it('| should be a singleton instance', () => { const instance1 = TestSingletonService.getInstance(); const instance2 = TestSingletonService.getInstance(); expect(instance1).toBe(instance2); expect(instance1).toBeInstanceOf(TestSingletonService); }); it('| should have default error user message', () => { expect(service.defaultErrorUserMsg).toBeDefined(); expect(service.defaultErrorUserMsg).toContain('Control Service Error'); }); describe('| getDefaultErrorSettings', () => { it('| should return error settings with default values', () => { const error = new Error('Test error'); const issuer = 'issuer-123'; const result = (service as any).getDefaultErrorSettings('testFunction', error, issuer); expect(result).toBeDefined(); expect(result.message).toBe('Test error'); // useMessageAsUserMessage defaults to true, so userMessage will be error.message expect(result.userMessage).toBe('Test error'); expect(result.issuer).toBe(issuer); expect(result.issuerService).toBe('TestSingletonService'); expect(result.systemVersion).toBe(DyNTS_global_settings.systemVersion); expect(result.error).toBe(error); }); it('| should use DyFM_Error status when available', () => { const error = new DyFM_Error({ status: 404, message: 'Not found', }); const issuer = 'issuer-123'; const result = (service as any).getDefaultErrorSettings('testFunction', error, issuer); expect(result.status).toBe(404); }); it('| should use default status when error is not DyFM_Error', () => { const error = new Error('Test error'); const issuer = 'issuer-123'; const result = (service as any).getDefaultErrorSettings('testFunction', error, issuer); expect(result.status).toBe(500); }); it('| should use error message when available', () => { const error = new Error('Test error message'); const issuer = 'issuer-123'; const result = (service as any).getDefaultErrorSettings('testFunction', error, issuer); expect(result.message).toBe('Test error message'); }); it('| should use default message when error message is not available', () => { const error = {} as Error; const issuer = 'issuer-123'; const result = (service as any).getDefaultErrorSettings('testFunction', error, issuer); expect(result.message).toContain('testFunction was UNSUCCESSFUL'); }); it('| should use DyFM_Error user message when available', () => { const error = new DyFM_Error({ message: 'Test error', userMessage: 'Custom user message', }); const issuer = 'issuer-123'; const result = (service as any).getDefaultErrorSettings('testFunction', error, issuer); expect(result.userMessage).toBe('Custom user message'); expect(result.addECToUserMsg).toBe(false); }); it('| should use default user message when DyFM_Error user message is not available', () => { const error = new DyFM_Error({ message: 'Test error', }); const issuer = 'issuer-123'; const result = (service as any).getDefaultErrorSettings('testFunction', error, issuer, false); expect(result.userMessage).toBe(service.defaultErrorUserMsg); expect(result.addECToUserMsg).toBe(true); }); }); });