import { DyNTS_Bot_MessagingProvider_ServiceBase, DyNTS_Bot_MessagingProvider_Config } from './bot-messaging-provider.service-base'; import { DyFM_Msg_Provider_Type } from '@futdevpro/fsm-dynamo/messaging'; import { DyNTS_Bot_ChannelWrapper } from '../_models/bot-channel-wrapper.interface'; import { DyNTS_Bot_MessageWrapper } from '../_models/bot-message-wrapper.interface'; import { DyNTS_Bot_UserWrapper } from '../_models/bot-user-wrapper.interface'; class MockChannel { id: string; name: string; } class MockMessage { id: string; content: string; } class MockUser { id: string; username: string; } class MockRole { id: string; name: string; } class TestMessagingProvider extends DyNTS_Bot_MessagingProvider_ServiceBase< MockChannel, MockMessage, MockUser, DyNTS_Bot_MessagingProvider_Config, MockRole > { readonly name = 'test-provider'; readonly type = DyFM_Msg_Provider_Type.dynamo; readonly config: DyNTS_Bot_MessagingProvider_Config = { reportChannelName: 'test-channel', }; get botId(): string { return 'bot-123'; } get botDisplayName(): string { return 'Test Bot'; } async setup(issuer: string): Promise { // Mock implementation } async start(issuer: string): Promise { // Mock implementation } async getChannelByName(name: string): Promise> { const channel = this.channelCache.get(name) || { id: 'channel-1', name }; return this.wrapChannel(channel); } async getChannelById(id: string): Promise> { const channel = { id, name: 'test-channel' }; return this.wrapChannel(channel); } async sendMessageToChannelByName(channelName: string, message: string, issuer: string): Promise> { const channel = await this.getChannelByName(channelName); return this.sendMessageToChannel(channel, message, issuer); } async getMessagesFromChannelByChannelName(channelName: string, limit?: number): Promise[]> { return []; } async getLastMessageInChannel(channelName: string): Promise> { return this.wrapMessage({ id: 'msg-1', content: 'Last message' }); } async clearChannel(channelWrapper: DyNTS_Bot_ChannelWrapper): Promise { // Mock implementation } async sendMessageToChannel(channelWrapper: DyNTS_Bot_ChannelWrapper, content: string, issuer: string): Promise> { return this.wrapMessage({ id: 'msg-1', content }); } async replyToMessage(messageWrapper: DyNTS_Bot_MessageWrapper, content: string): Promise> { return this.wrapMessage({ id: 'msg-2', content }); } async sendTyping(channelWrapper: DyNTS_Bot_ChannelWrapper): Promise { // Mock implementation } async sendTypingForMessage(messageWrapper: DyNTS_Bot_MessageWrapper): Promise { // Mock implementation } async fetchMessages(channelWrapper: DyNTS_Bot_ChannelWrapper, limit: number): Promise[]> { return []; } async fetchMessagesById(id: string, limit: number): Promise[]> { return []; } async fetchMessagesForMessage(messageWrapper: DyNTS_Bot_MessageWrapper, limit: number): Promise[]> { return []; } async fetchAllMessagesWithPaging(channelWrapper: DyNTS_Bot_ChannelWrapper, maxFetch?: number): Promise[]> { return []; } async deleteMessage(messageWrapper: DyNTS_Bot_MessageWrapper): Promise { // Mock implementation } async updateMessage(message: MockMessage, issuer: string): Promise { // Mock implementation } async getUserByName(username: string): Promise> { return this.wrapUser({ id: 'user-1', username }); } getUserMention(userId: string): string { return `<@${userId}>`; } async readMembersInChannel(channelName: string): Promise[]> { return []; } async readMemberNamesInChannel(channelName: string): Promise { return []; } async getLastMessageSentBy(channelName: string, username: string): Promise> { return this.wrapMessage({ id: 'msg-1', content: 'Last message' }); } async getLastMentionOf(channelName: string, username: string): Promise> { return this.wrapMessage({ id: 'msg-1', content: 'Last mention' }); } async readLastMessageDatesByMembers(channelName: string, memberNames: string[]): Promise { return []; } async readLastMessageWithMemberNamePingInIt(channelName: string, users: DyNTS_Bot_UserWrapper[]): Promise { return []; } getRoleByName?(roleName: string): any { return this.roleCache.get(roleName) || { id: 'role-1', name: roleName }; } async getRolePingByName?(roleName: string): Promise { return `<@&role-1>`; } async getRolePingsByName?(roleNames: string[]): Promise { return roleNames.map(name => `<@&role-1>`).join(' '); } async create_onMessageEventListener( handler: (message: DyNTS_Bot_MessageWrapper, issuer: string) => Promise ): Promise { // Mock implementation } create_onErrorEventListener(handler: (error: any) => void): void { // Mock implementation } async sendReport(message: string): Promise> { return this.sendMessageToChannelByName(this.config.reportChannelName, message, 'system'); } async sendReportIn(message: string): Promise> { return this.sendMessageToChannelByName(this.config.reportChannelName, message, 'system'); } async wrapMessage(message: MockMessage): Promise> { const self = this; return { id: message.id, content: message.content, rawPlatformMessage: message, channelId: 'channel-1', userId: 'user-1', username: 'test-user', timestamp: new Date(), getChannel: async () => self.getChannelById('channel-1'), getUser: async () => self.getUserByName('test-user'), reply: async (content: string) => self.replyToMessage(await self.wrapMessage(message), content), delete: async () => self.deleteMessage(await self.wrapMessage(message)), } as any; } async wrapChannel(channel: MockChannel): Promise> { const self = this; return { id: channel.id, name: channel.name, rawPlatformChannel: channel, sendMessage: async (content: string, issuer: string) => self.sendMessageToChannel(await self.wrapChannel(channel), content, issuer), getMessages: async (limit: number) => self.fetchMessages(await self.wrapChannel(channel), limit), clear: async () => self.clearChannel(await self.wrapChannel(channel)), } as any; } async wrapUser(user: MockUser): Promise> { return { id: user.id, username: user.username, rawPlatformUser: user, mention: this.getUserMention(user.id), } as any; } static getInstance(): TestMessagingProvider { return TestMessagingProvider.getSingletonInstance(); } } xdescribe('| DyNTS_Bot_MessagingProvider_ServiceBase', () => { let provider: TestMessagingProvider; beforeEach(() => { provider = TestMessagingProvider.getInstance(); }); it('| should be a singleton instance', () => { const instance1 = TestMessagingProvider.getInstance(); const instance2 = TestMessagingProvider.getInstance(); expect(instance1).toBe(instance2); expect(instance1).toBeInstanceOf(TestMessagingProvider); }); describe('| properties', () => { it('| should have name property', () => { expect(provider.name).toBe('test-provider'); }); it('| should have type property', () => { expect(provider.type).toBe(DyFM_Msg_Provider_Type.dynamo); }); it('| should have botId getter', () => { expect(provider.botId).toBe('bot-123'); }); it('| should have botDisplayName getter', () => { expect(provider.botDisplayName).toBe('Test Bot'); }); it('| should have config property', () => { expect(provider.config).toBeDefined(); expect(provider.config.reportChannelName).toBe('test-channel'); }); }); describe('| channelCache', () => { it('| should cache channels', () => { const channel: MockChannel = { id: 'channel-1', name: 'test-channel' }; (provider as any).channelCache.set('test-channel', channel); expect((provider as any).channelCache.get('test-channel')).toBe(channel); }); }); describe('| roleCache', () => { it('| should cache roles', () => { const role: MockRole = { id: 'role-1', name: 'test-role' }; (provider as any).roleCache.set('test-role', role); expect((provider as any).roleCache.get('test-role')).toBe(role); }); }); describe('| wrapMessage', () => { it('| should wrap message correctly', async () => { const message: MockMessage = { id: 'msg-1', content: 'Test message' }; const wrapped = await provider.wrapMessage(message); expect(wrapped.id).toBe('msg-1'); expect(wrapped.content).toBe('Test message'); expect(wrapped.rawPlatformMessage).toBe(message); }); }); describe('| wrapChannel', () => { it('| should wrap channel correctly', async () => { const channel: MockChannel = { id: 'channel-1', name: 'test-channel' }; const wrapped = await provider.wrapChannel(channel); expect(wrapped.id).toBe('channel-1'); expect(wrapped.name).toBe('test-channel'); expect(wrapped.rawPlatformChannel).toBe(channel); }); }); describe('| wrapUser', () => { it('| should wrap user correctly', async () => { const user: MockUser = { id: 'user-1', username: 'test-user' }; const wrapped = await provider.wrapUser(user); expect(wrapped.id).toBe('user-1'); expect(wrapped.username).toBe('test-user'); expect(wrapped.rawPlatformUser).toBe(user); }); }); describe('| getUserMention', () => { it('| should return user mention', () => { const mention = provider.getUserMention('user-123'); expect(mention).toBe('<@user-123>'); }); }); describe('| sendReport', () => { it('| should send report to report channel', async () => { spyOn(provider, 'sendMessageToChannelByName').and.returnValue(provider.wrapMessage({ id: 'msg-1', content: 'Report' })); const result = await provider.sendReport('Test report'); expect(provider.sendMessageToChannelByName).toHaveBeenCalledWith('test-channel', 'Test report', 'system'); expect(result).toBeDefined(); }); }); describe('| sendReportIn', () => { it('| should send report in report channel', async () => { spyOn(provider, 'sendMessageToChannelByName').and.returnValue(provider.wrapMessage({ id: 'msg-1', content: 'Report' })); const result = await provider.sendReportIn('Test report'); expect(provider.sendMessageToChannelByName).toHaveBeenCalledWith('test-channel', 'Test report', 'system'); expect(result).toBeDefined(); }); }); });