import { DyNTS_GlobalService } from './global.service'; import { DyNTS_AuthService } from './auth.service'; import { DyNTS_EmailService } from './email.service'; import { DyNTS_DBService } from '../base/db.service'; import { DyNTS_Service_Collection } from './service-collection.service'; import { DyNTS_App_Params } from '../../_models/control-models/app-params.control-model'; import { DyFM_DataModel_Params, DyFM_Error, DyFM_Metadata, DyFM_EnvironmentFlag } from '@futdevpro/fsm-dynamo'; import { DyNTS_GlobalService_Settings } from '../../_models/interfaces/global-service-settings.interface'; import { DyNTS_global_settings } from '../../_collections/global-settings.const'; class TestMetadata extends DyFM_Metadata { name: string; } class TestAuthService extends DyNTS_AuthService { static getInstance(): TestAuthService { return TestAuthService.getSingletonInstance(); } authenticate_token = async (req: any, res: any): Promise => {}; authenticate_tokenSelf = async (req: any, res: any): Promise => {}; authenticate_tokenPerm_accUsageData = async (req: any, res: any): Promise => {}; getIssuerFromRequest(req: any): string { return 'test'; } getUsernameFromRequest(req: any): string { return 'test'; } } describe('| DyNTS_GlobalService', () => { // Ensure env_settings exists before tests beforeAll(() => { if (!DyNTS_global_settings.env_settings) { (DyNTS_global_settings as any).env_settings = { environment: DyFM_EnvironmentFlag.local, discord: {}, mongoUri: undefined, openAi: {}, }; } else if (!DyNTS_global_settings.env_settings.environment) { DyNTS_global_settings.env_settings.environment = DyFM_EnvironmentFlag.local; } }); beforeEach(() => { // Reset singleton instance (DyNTS_GlobalService as any).instance = undefined; }); it('| should be a singleton instance', () => { const instance1 = DyNTS_GlobalService.getInstance(); const instance2 = DyNTS_GlobalService.getInstance(); expect(instance1).toBe(instance2); expect(instance1).toBeInstanceOf(DyNTS_GlobalService); }); describe('| params', () => { it('| should return undefined when not set', () => { // Reset params before test (DyNTS_GlobalService as any)._params = undefined; expect(DyNTS_GlobalService.params).toBeUndefined(); }); it('| should return set params', () => { const params = new DyNTS_App_Params({ name: 'Test App', version: '1.0.0', dbName: 'test_db', }); DyNTS_GlobalService.setParams(params); expect(DyNTS_GlobalService.params).toBe(params); expect(DyNTS_GlobalService.params.name).toBe('Test App'); }); }); describe('| upSince', () => { it('| should return a Date object', () => { expect(DyNTS_GlobalService.upSince).toBeInstanceOf(Date); }); it('| should return the same date on multiple calls', () => { const date1 = DyNTS_GlobalService.upSince; const date2 = DyNTS_GlobalService.upSince; expect(date1).toBe(date2); }); }); describe('| upTime', () => { it('| should return a number', () => { expect(typeof DyNTS_GlobalService.upTime).toBe('number'); }); it('| should return positive value', () => { expect(DyNTS_GlobalService.upTime).toBeGreaterThanOrEqual(0); }); }); describe('| setParams', () => { it('| should set params', () => { const params = new DyNTS_App_Params({ name: 'Test App', version: '1.0.0', dbName: 'test_db', }); DyNTS_GlobalService.setParams(params); expect(DyNTS_GlobalService.params).toBe(params); }); }); describe('| getAuthService', () => { it('| should throw error when auth service not set', () => { expect(() => { DyNTS_GlobalService.getAuthService(); }).toThrow(); }); it('| should return auth service when set', async () => { const authService = TestAuthService.getInstance(); await DyNTS_GlobalService.setServices({ authService: authService, }); const result = DyNTS_GlobalService.getAuthService(); expect(result).toBe(authService); }); it('| should NOT throw and return undefined when auth_settings.optional is true', () => { // Suppress: public / no-auth szerver — a beállítás elnyomja a // "Unique Authentication Service missing!" dobást. A flag-et finally-ben // visszaállítjuk, hogy a randomizált futásban ne szivárogjon más tesztbe. const prev = DyNTS_global_settings.auth_settings?.optional; try { DyNTS_global_settings.auth_settings = { optional: true }; let result: DyNTS_AuthService; expect(() => { result = DyNTS_GlobalService.getAuthService(); }).not.toThrow(); expect(result).toBeUndefined(); } finally { DyNTS_global_settings.auth_settings = { optional: prev ?? false }; } }); }); describe('| getDBServiceCollection', () => { it('| should throw error when db service collection not set', () => { expect(() => { DyNTS_GlobalService.getDBServiceCollection(); }).toThrow(); }); it('| should return db service collection when set', async () => { const dbModel = new DyFM_DataModel_Params({ dataName: 'test_data', properties: { name: { key: 'name', type: 'string' } }, }); await DyNTS_GlobalService.setServices({ dbModels: [dbModel], }); const result = DyNTS_GlobalService.getDBServiceCollection(); expect(result).toBeDefined(); expect(result['test_data']).toBeDefined(); }); }); describe('| getDBService', () => { it('| should return db service by data params', async () => { const dbModel = new DyFM_DataModel_Params({ dataName: 'test_data', properties: { name: { key: 'name', type: 'string' } }, }); await DyNTS_GlobalService.setServices({ dbModels: [dbModel], }); const result = DyNTS_GlobalService.getDBService(dbModel); expect(result).toBeDefined(); expect(result).toBeInstanceOf(DyNTS_DBService); }); it('| should throw error when db service not found', async () => { const dbModel = new DyFM_DataModel_Params({ dataName: 'non_existent', properties: { name: { key: 'name', type: 'string' } }, }); await DyNTS_GlobalService.setServices({ dbModels: [], }); expect(() => { DyNTS_GlobalService.getDBService(dbModel); }).toThrow(); }); }); describe('| getDBServiceByKey', () => { it('| should return db service by key', async () => { const dbModel = new DyFM_DataModel_Params({ dataName: 'test_data', properties: { name: { key: 'name', type: 'string' } }, }); await DyNTS_GlobalService.setServices({ dbModels: [dbModel], }); const result = DyNTS_GlobalService.getDBServiceByKey('test_data'); expect(result).toBeDefined(); expect(result).toBeInstanceOf(DyNTS_DBService); }); it('| should throw error when key not found', async () => { await DyNTS_GlobalService.setServices({ dbModels: [], }); expect(() => { DyNTS_GlobalService.getDBServiceByKey('non_existent'); }).toThrow(); }); }); describe('| getEmailServiceCollection', () => { it('| should throw error when email service collection not set', () => { expect(() => { DyNTS_GlobalService.getEmailServiceCollection(); }).toThrow(); }); it('| should return email service collection when set', async () => { const emailServiceCollection = {} as DyNTS_Service_Collection; await DyNTS_GlobalService.setServices({ emailServiceCollection: emailServiceCollection, }); const result = DyNTS_GlobalService.getEmailServiceCollection(); expect(result).toBe(emailServiceCollection); }); }); describe('| setServices', () => { it('| should setup all services', async () => { const authService = TestAuthService.getInstance(); const dbModel = new DyFM_DataModel_Params({ dataName: 'test_data', properties: { name: { key: 'name', type: 'string' } }, }); const emailServiceCollection = {} as DyNTS_Service_Collection; await DyNTS_GlobalService.setServices({ authService: authService, dbModels: [dbModel], emailServiceCollection: emailServiceCollection, }); expect(DyNTS_GlobalService.getAuthService()).toBe(authService); expect(DyNTS_GlobalService.getDBServiceCollection()).toBeDefined(); expect(DyNTS_GlobalService.getEmailServiceCollection()).toBe(emailServiceCollection); }); it('| should handle empty settings', async () => { await expectAsync( DyNTS_GlobalService.setServices({}) ).toBeResolved(); }); it('| should throw error on setup failure', async () => { const invalidDbModel = null as any; await expectAsync( DyNTS_GlobalService.setServices({ dbModels: [invalidDbModel], }) ).toBeRejected(); }); }); });