import { DyFM_Error, DyFM_Log } from '@futdevpro/fsm-dynamo'; import { DyNTS_DiBo_Operations_Util } from './dibo-operations.util'; import { Guild, GuildChannelManager, TextChannel, VoiceChannel, Client, GuildMember, Message, Collection } from 'discord.js'; import { DyNTS_DiBo_global_settings } from './dibo-global-settings.conts'; xdescribe('| DyNTS_DiBo_Operations_Util', () => { describe('| findChannelByName', () => { it('| should find text channel by name', () => { const mockChannel = { name: 'test-channel', isTextBased: () => true, } as any; const mockChannels = { cache: { find: jasmine.createSpy('find').and.returnValue(mockChannel), }, } as any as GuildChannelManager; const result = DyNTS_DiBo_Operations_Util.findChannelByName(mockChannels, 'test-channel'); expect(result).toBe(mockChannel); expect(mockChannels.cache.find).toHaveBeenCalled(); }); it('| should return undefined when channel not found', () => { const logSpy = spyOn(DyFM_Log, 'error'); const mockChannels = { cache: { find: jasmine.createSpy('find').and.returnValue(undefined), }, } as any as GuildChannelManager; const result = DyNTS_DiBo_Operations_Util.findChannelByName(mockChannels, 'non-existent'); expect(result).toBeUndefined(); expect(logSpy).toHaveBeenCalled(); }); }); describe('| findTextChannelByName', () => { it('| should find and return text channel', () => { const mockChannel = { name: 'test-channel', isTextBased: () => true, } as any; const mockChannels = { cache: { find: jasmine.createSpy('find').and.returnValue(mockChannel), }, } as any as GuildChannelManager; const result = DyNTS_DiBo_Operations_Util.findTextChannelByName(mockChannels, 'test-channel'); expect(result).toBe(mockChannel); }); it('| should throw error when channel is not text based', () => { const mockChannel = { name: 'test-channel', isTextBased: () => false, } as any; const mockChannels = { cache: { find: jasmine.createSpy('find').and.returnValue(mockChannel), }, } as any as GuildChannelManager; expect(() => { DyNTS_DiBo_Operations_Util.findTextChannelByName(mockChannels, 'test-channel'); }).toThrowError(/Channel is not text based with name "test-channel"/); }); }); describe('| findVoiceChannelByName', () => { it('| should find and return voice channel', () => { const mockChannel = { name: 'test-channel', isVoiceBased: () => true, } as any; const mockChannels = { cache: { find: jasmine.createSpy('find').and.returnValue(mockChannel), }, } as any as GuildChannelManager; const result = DyNTS_DiBo_Operations_Util.findVoiceChannelByName(mockChannels, 'test-channel'); expect(result).toBe(mockChannel); }); it('| should throw error when channel is not voice based', () => { const mockChannel = { name: 'test-channel', isVoiceBased: () => false, } as any; const mockChannels = { cache: { find: jasmine.createSpy('find').and.returnValue(mockChannel), }, } as any as GuildChannelManager; expect(() => { DyNTS_DiBo_Operations_Util.findVoiceChannelByName(mockChannels, 'test-channel'); }).toThrowError(/Channel is not voice based with name "test-channel"/); }); }); describe('| sendMessageToChannelByName', () => { it('| should send message to text channel', () => { const sendSpy = jasmine.createSpy('send'); const logSpy = spyOn(DyFM_Log, 'success'); const mockChannel = { name: 'test-channel', isTextBased: () => true, send: sendSpy, } as any; const mockGuild = { channels: { cache: { find: jasmine.createSpy('find').and.returnValue(mockChannel), }, }, } as any as Guild; DyNTS_DiBo_Operations_Util.sendMessageToChannelByName(mockGuild, 'test-channel', 'Test message'); expect(sendSpy).toHaveBeenCalledWith('Test message'); expect(logSpy).toHaveBeenCalled(); }); it('| should log error when channel is not text based', () => { const logSpy = spyOn(DyFM_Log, 'error'); const mockChannel = { name: 'test-channel', isTextBased: () => false, } as any; const mockGuild = { channels: { cache: { find: jasmine.createSpy('find').and.returnValue(mockChannel), }, }, } as any as Guild; DyNTS_DiBo_Operations_Util.sendMessageToChannelByName(mockGuild, 'test-channel', 'Test message'); expect(logSpy).toHaveBeenCalled(); }); }); describe('| getMemberIdByName', () => { it('| should return member id by username', () => { const mockMember = { user: { id: 'user-123', username: 'testuser', }, } as any as GuildMember; const members = new Collection(); members.set('user-123', mockMember); const result = DyNTS_DiBo_Operations_Util.getMemberIdByName(members, 'testuser'); expect(result).toBe('user-123'); }); it('| should return undefined when member not found', () => { const members = new Collection(); const result = DyNTS_DiBo_Operations_Util.getMemberIdByName(members, 'non-existent'); expect(result).toBeUndefined(); }); }); describe('| deleteMessage', () => { it('| should delete message successfully', async () => { const logSpy = spyOn(DyFM_Log, 'success'); const mockMessage = { id: 'msg-123', content: 'Test message', delete: jasmine.createSpy('delete').and.returnValue(Promise.resolve()), } as any as Message; await DyNTS_DiBo_Operations_Util.deleteMessage(mockMessage); expect(mockMessage.delete).toHaveBeenCalled(); expect(logSpy).toHaveBeenCalled(); }); it('| should handle delete error gracefully', async () => { const errorSpy = spyOn(DyFM_Error, 'logSimple'); const mockMessage = { id: 'msg-123', content: 'Test message', delete: jasmine.createSpy('delete').and.returnValue(Promise.reject(new Error('Delete failed'))), } as any as Message; await DyNTS_DiBo_Operations_Util.deleteMessage(mockMessage); expect(errorSpy).toHaveBeenCalled(); }); }); });