import { DyNTS_DiBo_Commands_ControlService } from './dibo-commands.control-service'; import { DyNTS_DiBo_Main_ControlService } from './dibo-main.control-service'; import { Message, Guild } from 'discord.js'; import { DyFM_Error, DyFM_EnvironmentFlag } from '@futdevpro/fsm-dynamo'; import { DyNTS_global_settings } from '../../../_collections/global-settings.const'; import { DyNTS_DiBo_global_settings } from '../_collections/dibo-global-settings.conts'; class TestDiBoCommandsService extends DyNTS_DiBo_Commands_ControlService { protected getMainDiscordBotControlService(): DyNTS_DiBo_Main_ControlService { return this.diAs_CS; } static getInstance(): TestDiBoCommandsService { return TestDiBoCommandsService.getSingletonInstance(); } } xdescribe('| DyNTS_DiBo_Commands_ControlService', () => { let service: TestDiBoCommandsService; let mockMain_CS: jasmine.SpyObj; let mockGuild: jasmine.SpyObj; let mockMessage: jasmine.SpyObj; let originalCommandSettings: typeof DyNTS_DiBo_global_settings.commandSettings; beforeAll(() => { if (!DyNTS_global_settings.systemShortCodeName) { (DyNTS_global_settings as { systemShortCodeName?: string }).systemShortCodeName = 'TEST'; } if (!DyNTS_global_settings.env_settings) { (DyNTS_global_settings as { env_settings?: unknown }).env_settings = { environment: DyFM_EnvironmentFlag.local }; } originalCommandSettings = { ...DyNTS_DiBo_global_settings.commandSettings }; }); afterEach(() => { (DyNTS_DiBo_global_settings as { commandSettings: unknown }).commandSettings = originalCommandSettings; }); beforeEach(() => { mockGuild = jasmine.createSpyObj('Guild', [], { id: 'guild-123', }); mockMessage = jasmine.createSpyObj('Message', ['reply'], { id: 'message-123', content: '!test command', author: { id: 'user-123', }, }); mockMain_CS = jasmine.createSpyObj('DyNTS_DiBo_Main_ControlService', [], { discordServer: mockGuild, client: { user: { id: 'bot-123', }, }, }); service = new (TestDiBoCommandsService as any)(); (service as any).diAs_CS = mockMain_CS; }); describe('| properties', () => { it('| should return discordServer from main service', () => { expect(service.discordServer).toBe(mockGuild); }); }); describe('| setup', () => { it('| should setup service with main service', async () => { await service.setup('test-issuer'); expect((service as any).diAs_CS).toBe(mockMain_CS); }); it('| should throw error if main service not found', async () => { (service as any).getMainDiscordBotControlService = () => null; try { await service.setup('test-issuer'); fail('Should have thrown an error'); } catch (error) { expect(error).toBeInstanceOf(DyFM_Error); expect((error as DyFM_Error)._errorCode).toContain('DyNTS-DiBo-CCS-S01'); } }); }); describe('| start', () => { it('| should start service successfully', async () => { await service.start('test-issuer'); expect(service.discordServer).toBe(mockGuild); }); it('| should throw error if discordServer not found', async () => { (service as any).diAs_CS = null; try { await service.start('test-issuer'); fail('Should have thrown an error'); } catch (error) { expect(error).toBeInstanceOf(DyFM_Error); expect((error as DyFM_Error)._errorCode).toContain('DyNTS-DiBo-CCS-ST00'); } }); }); describe('| handleCommand', () => { it('| should handle command successfully', async () => { const commandHandlerSpy = jasmine.createSpy('commandHandler').and.returnValue(Promise.resolve()); (DyNTS_DiBo_global_settings as { commandSettings: { commandOperator: string; commands: unknown[] } }).commandSettings = { commandOperator: '!', commands: [ { command: 'test', commandHandler: commandHandlerSpy, }, ], }; await service.handleCommand(mockMessage, 'test-issuer'); expect(commandHandlerSpy).toHaveBeenCalled(); expect(mockMessage.reply).not.toHaveBeenCalled(); }); it('| should handle command errors', async () => { const commandError = new Error('Command handler error'); (DyNTS_DiBo_global_settings as { commandSettings: { commandOperator: string; commands: unknown[] } }).commandSettings = { commandOperator: '!', commands: [ { command: 'test', commandHandler: jasmine.createSpy('commandHandler').and.returnValue(Promise.reject(commandError)), }, ], }; mockMessage.reply.and.returnValue(Promise.resolve(mockMessage as any)); try { await service.handleCommand(mockMessage, 'test-issuer'); fail('Should have thrown an error'); } catch (error) { expect(error).toBeInstanceOf(DyFM_Error); expect(mockMessage.reply).toHaveBeenCalled(); } }); }); });