import { DyNTS_Msg_Events_Service } from './msg-events.service'; import { DyFM_Msg_EventKey } from '@futdevpro/fsm-dynamo/messaging'; import { DyFM_Log } from '@futdevpro/fsm-dynamo'; describe('| DyNTS_Msg_Events_Service', () => { let service: DyNTS_Msg_Events_Service; let mockSocketServer: any; beforeEach(() => { service = DyNTS_Msg_Events_Service.getInstance(); mockSocketServer = { emitToRoom: jasmine.createSpy('emitToRoom'), emitToUser: jasmine.createSpy('emitToUser'), }; spyOn(service, 'getSocketServer' as any).and.returnValue(mockSocketServer); }); it('| should be a singleton instance', () => { const instance1 = DyNTS_Msg_Events_Service.getInstance(); const instance2 = DyNTS_Msg_Events_Service.getInstance(); expect(instance1).toBe(instance2); expect(instance1).toBeInstanceOf(DyNTS_Msg_Events_Service); }); describe('| emitMessageSent', () => { it('| should emit message sent event to conversation room', () => { const message = { _id: 'msg-123', content: 'Test message' }; service.emitMessageSent(message, 'conv-123'); expect(mockSocketServer.emitToRoom).toHaveBeenCalledWith( 'conversation:conv-123', DyFM_Msg_EventKey.messageSent, message ); }); it('| should handle error gracefully when socket server is not available', () => { (service as any).getSocketServer = jasmine.createSpy('getSocketServer').and.returnValue(null); const logSpy = spyOn(DyFM_Log, 'error'); service.emitMessageSent({}, 'conv-123'); expect(logSpy).not.toHaveBeenCalled(); }); }); describe('| emitMessageUpdated', () => { it('| should emit message updated event', () => { const message = { _id: 'msg-123', content: 'Updated message' }; service.emitMessageUpdated(message, 'conv-123'); expect(mockSocketServer.emitToRoom).toHaveBeenCalledWith( 'conversation:conv-123', DyFM_Msg_EventKey.messageUpdated, message ); }); }); describe('| emitTypingIndicator', () => { it('| should emit typing start event', () => { service.emitTypingIndicator('user-123', 'conv-123', true); expect(mockSocketServer.emitToRoom).toHaveBeenCalledWith( 'conversation:conv-123', DyFM_Msg_EventKey.typingStart, { userId: 'user-123', conversationId: 'conv-123' } ); }); it('| should emit typing stop event', () => { service.emitTypingIndicator('user-123', 'conv-123', false); expect(mockSocketServer.emitToRoom).toHaveBeenCalledWith( 'conversation:conv-123', DyFM_Msg_EventKey.typingStop, { userId: 'user-123', conversationId: 'conv-123' } ); }); }); describe('| emitConversationCreated', () => { it('| should emit conversation created event to all participants', () => { const conversation = { _id: 'conv-123', name: 'Test Conversation' }; const participantIds = ['user-1', 'user-2', 'user-3']; service.emitConversationCreated(conversation, participantIds); expect(mockSocketServer.emitToUser).toHaveBeenCalledTimes(3); expect(mockSocketServer.emitToUser).toHaveBeenCalledWith( 'user-1', DyFM_Msg_EventKey.conversationCreated, conversation ); expect(mockSocketServer.emitToUser).toHaveBeenCalledWith( 'user-2', DyFM_Msg_EventKey.conversationCreated, conversation ); expect(mockSocketServer.emitToUser).toHaveBeenCalledWith( 'user-3', DyFM_Msg_EventKey.conversationCreated, conversation ); }); }); describe('| emitMessageDeleted', () => { it('| should emit message deleted event', () => { service.emitMessageDeleted('msg-123', 'conv-123'); expect(mockSocketServer.emitToRoom).toHaveBeenCalledWith( 'conversation:conv-123', DyFM_Msg_EventKey.messageDeleted, { messageId: 'msg-123', conversationId: 'conv-123' } ); }); }); describe('| emitConversationUpdated', () => { it('| should emit conversation updated event to all participants', () => { const conversation = { _id: 'conv-123', participants: [ { userId: 'user-1' }, { userId: 'user-2' }, ], }; service.emitConversationUpdated(conversation); expect(mockSocketServer.emitToUser).toHaveBeenCalledTimes(2); expect(mockSocketServer.emitToUser).toHaveBeenCalledWith( 'user-1', DyFM_Msg_EventKey.conversationUpdated, conversation ); }); }); describe('| emitConversationDeleted', () => { it('| should emit conversation deleted event', () => { service.emitConversationDeleted('conv-123'); expect(mockSocketServer.emitToRoom).toHaveBeenCalledWith( 'conversation:conv-123', DyFM_Msg_EventKey.conversationDeleted, { conversationId: 'conv-123' } ); }); }); describe('| emitParticipantAdded', () => { it('| should emit participant added event', () => { service.emitParticipantAdded('conv-123', 'user-456'); expect(mockSocketServer.emitToUser).toHaveBeenCalledWith( 'user-456', DyFM_Msg_EventKey.participantAdded, { conversationId: 'conv-123', userId: 'user-456' } ); }); }); describe('| emitParticipantRemoved', () => { it('| should emit participant removed event', () => { service.emitParticipantRemoved('conv-123', 'user-456'); expect(mockSocketServer.emitToUser).toHaveBeenCalledWith( 'user-456', DyFM_Msg_EventKey.participantRemoved, { conversationId: 'conv-123', userId: 'user-456' } ); }); }); describe('| emitMessageRead', () => { it('| should emit message read event', () => { service.emitMessageRead('msg-123', 'user-456', 'conv-123'); expect(mockSocketServer.emitToRoom).toHaveBeenCalledWith( 'conversation:conv-123', DyFM_Msg_EventKey.messageRead, { messageId: 'msg-123', userId: 'user-456', conversationId: 'conv-123' } ); }); }); describe('| emitReactionAdded', () => { it('| should emit reaction added event', () => { const reaction = { emoji: '👍', userId: 'user-123', conversationId: 'conv-123' }; service.emitReactionAdded('msg-123', reaction); expect(mockSocketServer.emitToRoom).toHaveBeenCalledWith( 'conversation:conv-123', DyFM_Msg_EventKey.reactionAdded, { messageId: 'msg-123', reaction } ); }); }); describe('| emitReactionRemoved', () => { it('| should emit reaction removed event', () => { service.emitReactionRemoved('msg-123', 'user-123', '👍'); expect(mockSocketServer.emitToRoom).toHaveBeenCalledWith( 'conversation:user-123', DyFM_Msg_EventKey.reactionRemoved, { messageId: 'msg-123', userId: 'user-123', emoji: '👍' } ); }); }); });