import { DyNTS_Ass_IO_ControlService } from './ass-io.control-service'; import { DyNTS_Ass_Main_ControlService } from './ass-main.control-service'; import { DyNTS_Bot_IO_ControlService } from '../../bot/_services/bot-io.control-service'; import { DyNTS_Bot_MessageWrapper } from '../../bot/_models/bot-message-wrapper.interface'; import { DyNTS_AI_LLMChat_ServiceBase } from '../../ai/_services/ai-llm-chat.service-base'; import { DyFM_Error } from '@futdevpro/fsm-dynamo'; import { DyNTS_global_settings } from '../../../_collections/global-settings.const'; import { DyNTS_Ass_Util } from '../_collections/ass.util'; class TestAssIOService extends DyNTS_Ass_IO_ControlService { protected declare mainBot_CS: any; protected getMainBotControlService(): any { return { gatherMessagesForMessage: jasmine.createSpy('gatherMessagesForMessage').and.returnValue( Promise.resolve([]) ), defaultSystemPrompt: 'Test prompt', aiProvider: { requestSimpleMessageInConversation: jasmine.createSpy('requestSimpleMessageInConversation').and.returnValue( Promise.resolve('AI response') ), } as any, }; } protected getCommandsControlService(): any { return {} as any; } static getInstance(): TestAssIOService { return TestAssIOService.getSingletonInstance(); } } describe('| DyNTS_Ass_IO_ControlService', () => { let service: TestAssIOService; beforeEach(() => { service = TestAssIOService.getInstance(); }); it('| should be a singleton instance', () => { const instance1 = TestAssIOService.getInstance(); const instance2 = TestAssIOService.getInstance(); expect(instance1).toBe(instance2); expect(instance1).toBeInstanceOf(TestAssIOService); }); it('| should get AI provider from main bot service', () => { (service as any).mainBot_CS = (service as any).getMainBotControlService(); expect(service.aiProvider).toBeDefined(); }); describe('| handleMessage', () => { it('| should handle message with AI and reply', async () => { const mockMessage = { provider: { botId: 'bot-123', botDisplayName: 'Bot', replyToMessage: jasmine.createSpy('replyToMessage').and.returnValue( Promise.resolve({ content: 'AI response' } as any) ), }, content: 'User message', } as any as DyNTS_Bot_MessageWrapper; (service as any).mainBot_CS = (service as any).getMainBotControlService(); spyOn(DyNTS_Ass_Util, 'convertBotMessagesToAIConversation').and.returnValue([ { role: 'user' as any, content: 'User message' }, ]); const result = await service.handleMessage(mockMessage, 'issuer-123'); expect((service as any).mainBot_CS.gatherMessagesForMessage).toHaveBeenCalledWith(mockMessage); expect((service as any).mainBot_CS.aiProvider.requestSimpleMessageInConversation).toHaveBeenCalled(); expect(mockMessage.provider.replyToMessage).toHaveBeenCalledWith(mockMessage, 'AI response'); expect(result).toBeDefined(); }); it('| should send error reply when error occurs and debugLevel >= 1', async () => { const mockMessage = { provider: { botId: 'bot-123', botDisplayName: 'Bot', replyToMessage: jasmine.createSpy('replyToMessage').and.returnValue( Promise.resolve({} as any) ), }, content: 'User message', } as any as DyNTS_Bot_MessageWrapper; (service as any).mainBot_CS = (service as any).getMainBotControlService(); (service as any).mainBot_CS.gatherMessagesForMessage = jasmine.createSpy('gatherMessagesForMessage').and.returnValue( Promise.reject(new Error('Test error')) ); service.dontSendErrorReply = false; // Set bot_settings directly instead of using spyOn const originalBotSettings = DyNTS_global_settings.bot_settings; (DyNTS_global_settings as any).bot_settings = { debugLevel: 1 }; try { await expectAsync( service.handleMessage(mockMessage, 'issuer-123') ).toBeRejected(); expect(mockMessage.provider.replyToMessage).toHaveBeenCalled(); } finally { // Restore original bot_settings (DyNTS_global_settings as any).bot_settings = originalBotSettings; } }); it('| should not send error reply when dontSendErrorReply is true', async () => { const mockMessage = { provider: { botId: 'bot-123', botDisplayName: 'Bot', replyToMessage: jasmine.createSpy('replyToMessage'), }, content: 'User message', } as any as DyNTS_Bot_MessageWrapper; (service as any).mainBot_CS = (service as any).getMainBotControlService(); (service as any).mainBot_CS.gatherMessagesForMessage = jasmine.createSpy('gatherMessagesForMessage').and.returnValue( Promise.reject(new Error('Test error')) ); service.dontSendErrorReply = true; await expectAsync( service.handleMessage(mockMessage, 'issuer-123') ).toBeRejected(); expect(mockMessage.provider.replyToMessage).not.toHaveBeenCalled(); }); }); });