|
| 1 | +import { Readable } from 'node:stream'; |
| 2 | +import type { IAgentRuntime } from '@elizaos/core'; |
| 3 | +import { type Context, Telegraf } from 'telegraf'; |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 5 | +import { MediaType, MessageManager } from '../src/messageManager'; |
| 6 | + |
| 7 | +// Create mock functions directly to have access to them |
| 8 | +const sendMessageMock = vi.fn().mockResolvedValue({ message_id: 123 }); |
| 9 | +const sendPhotoMock = vi.fn().mockResolvedValue({ message_id: 124 }); |
| 10 | +const sendChatActionMock = vi.fn().mockResolvedValue(true); |
| 11 | +const sendVideoMock = vi.fn().mockResolvedValue({ message_id: 125 }); |
| 12 | +const sendDocumentMock = vi.fn().mockResolvedValue({ message_id: 126 }); |
| 13 | +const sendAudioMock = vi.fn().mockResolvedValue({ message_id: 127 }); |
| 14 | +const sendAnimationMock = vi.fn().mockResolvedValue({ message_id: 128 }); |
| 15 | + |
| 16 | +// Mock Telegraf |
| 17 | +vi.mock('telegraf', () => { |
| 18 | + return { |
| 19 | + Telegraf: vi.fn().mockImplementation(() => ({ |
| 20 | + telegram: { |
| 21 | + sendMessage: sendMessageMock, |
| 22 | + sendChatAction: sendChatActionMock, |
| 23 | + sendPhoto: sendPhotoMock, |
| 24 | + sendVideo: sendVideoMock, |
| 25 | + sendDocument: sendDocumentMock, |
| 26 | + sendAudio: sendAudioMock, |
| 27 | + sendAnimation: sendAnimationMock, |
| 28 | + }, |
| 29 | + })), |
| 30 | + Markup: { |
| 31 | + markdown: vi.fn(() => ({})), |
| 32 | + inlineKeyboard: vi.fn(() => ({})), |
| 33 | + }, |
| 34 | + }; |
| 35 | +}); |
| 36 | + |
| 37 | +vi.mock('fs', () => ({ |
| 38 | + default: { |
| 39 | + existsSync: vi.fn().mockReturnValue(true), |
| 40 | + createReadStream: vi.fn(() => { |
| 41 | + const stream = new Readable(); |
| 42 | + stream._read = () => {}; |
| 43 | + return stream; |
| 44 | + }), |
| 45 | + }, |
| 46 | +})); |
| 47 | + |
| 48 | +describe('MessageManager', () => { |
| 49 | + let mockRuntime: IAgentRuntime; |
| 50 | + let mockBot: Telegraf<Context>; |
| 51 | + let messageManager: MessageManager; |
| 52 | + const CHAT_ID = 123456789; |
| 53 | + |
| 54 | + beforeEach(() => { |
| 55 | + mockRuntime = { |
| 56 | + getSetting: vi.fn(), |
| 57 | + getCharacter: vi.fn(), |
| 58 | + getFlow: vi.fn(), |
| 59 | + getPlugin: vi.fn(), |
| 60 | + getPlugins: vi.fn(), |
| 61 | + getSafePlugins: vi.fn(), |
| 62 | + hasPlugin: vi.fn(), |
| 63 | + registerPlugin: vi.fn(), |
| 64 | + removePlugin: vi.fn(), |
| 65 | + setCharacter: vi.fn(), |
| 66 | + setFlow: vi.fn(), |
| 67 | + } as Partial<IAgentRuntime> as IAgentRuntime; |
| 68 | + |
| 69 | + mockBot = new Telegraf('mock_token') as any; |
| 70 | + messageManager = new MessageManager(mockBot, mockRuntime); |
| 71 | + vi.clearAllMocks(); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('message sending', () => { |
| 75 | + it('should send a message successfully', async () => { |
| 76 | + const ctx = { |
| 77 | + telegram: mockBot.telegram, |
| 78 | + chat: { id: CHAT_ID }, |
| 79 | + } as Context; |
| 80 | + |
| 81 | + const content = { text: 'Test message' }; |
| 82 | + const result = await messageManager.sendMessageInChunks(ctx, content); |
| 83 | + |
| 84 | + expect(mockBot.telegram.sendMessage).toHaveBeenCalledWith( |
| 85 | + CHAT_ID, |
| 86 | + content.text, |
| 87 | + expect.objectContaining({ |
| 88 | + parse_mode: 'MarkdownV2', |
| 89 | + }) |
| 90 | + ); |
| 91 | + expect(result[0].message_id).toBe(123); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should split long messages', async () => { |
| 95 | + const ctx = { |
| 96 | + telegram: mockBot.telegram, |
| 97 | + chat: { id: CHAT_ID }, |
| 98 | + } as Context; |
| 99 | + |
| 100 | + // Create a message that's just over 4096 characters (Telegram's limit) |
| 101 | + const message1 = 'a'.repeat(4096); |
| 102 | + const message2 = 'b'.repeat(100); |
| 103 | + const content = { text: `${message1}\n${message2}` }; |
| 104 | + await messageManager.sendMessageInChunks(ctx, content); |
| 105 | + |
| 106 | + expect(mockBot.telegram.sendMessage).toHaveBeenCalledTimes(2); |
| 107 | + expect(mockBot.telegram.sendMessage).toHaveBeenNthCalledWith( |
| 108 | + 1, |
| 109 | + CHAT_ID, |
| 110 | + message1, |
| 111 | + expect.objectContaining({ parse_mode: 'MarkdownV2' }) |
| 112 | + ); |
| 113 | + expect(mockBot.telegram.sendMessage).toHaveBeenNthCalledWith( |
| 114 | + 2, |
| 115 | + CHAT_ID, |
| 116 | + message2, |
| 117 | + expect.objectContaining({ parse_mode: 'MarkdownV2' }) |
| 118 | + ); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + describe('image handling', () => { |
| 123 | + it('should send an image from URL', async () => { |
| 124 | + const ctx = { |
| 125 | + telegram: mockBot.telegram, |
| 126 | + chat: { id: CHAT_ID }, |
| 127 | + } as Context; |
| 128 | + |
| 129 | + const imageUrl = 'https://example.com/image.jpg'; |
| 130 | + await messageManager.sendMedia(ctx, imageUrl, MediaType.PHOTO); |
| 131 | + |
| 132 | + expect(mockBot.telegram.sendPhoto).toHaveBeenCalledWith( |
| 133 | + CHAT_ID, |
| 134 | + imageUrl, |
| 135 | + expect.any(Object) |
| 136 | + ); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should send an image from local file', async () => { |
| 140 | + const ctx = { |
| 141 | + telegram: mockBot.telegram, |
| 142 | + chat: { id: CHAT_ID }, |
| 143 | + } as Context; |
| 144 | + |
| 145 | + const localPath = '/path/to/image.jpg'; |
| 146 | + await messageManager.sendMedia(ctx, localPath, MediaType.PHOTO); |
| 147 | + |
| 148 | + expect(mockBot.telegram.sendPhoto).toHaveBeenCalledWith( |
| 149 | + CHAT_ID, |
| 150 | + expect.objectContaining({ source: expect.any(Object) }), |
| 151 | + expect.any(Object) |
| 152 | + ); |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + describe('error handling', () => { |
| 157 | + it('should handle send message errors', async () => { |
| 158 | + const ctx = { |
| 159 | + telegram: mockBot.telegram, |
| 160 | + chat: { id: CHAT_ID }, |
| 161 | + } as Context; |
| 162 | + |
| 163 | + const error = new Error('Network error'); |
| 164 | + sendMessageMock.mockRejectedValueOnce(error); |
| 165 | + |
| 166 | + await expect(messageManager.sendMessageInChunks(ctx, { text: 'test' })).rejects.toThrow( |
| 167 | + 'Network error' |
| 168 | + ); |
| 169 | + }); |
| 170 | + |
| 171 | + it('should handle image send errors', async () => { |
| 172 | + const ctx = { |
| 173 | + telegram: mockBot.telegram, |
| 174 | + chat: { id: CHAT_ID }, |
| 175 | + } as Context; |
| 176 | + |
| 177 | + const error = new Error('Image send failed'); |
| 178 | + sendPhotoMock.mockRejectedValueOnce(error); |
| 179 | + |
| 180 | + await expect(messageManager.sendMedia(ctx, 'test.jpg', MediaType.PHOTO)).rejects.toThrow( |
| 181 | + 'Image send failed' |
| 182 | + ); |
| 183 | + }); |
| 184 | + }); |
| 185 | +}); |
0 commit comments