|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { InstagramClientInterface } from '../src'; |
| 3 | +import { IAgentRuntime, elizaLogger } from '@elizaos/core'; |
| 4 | +import { InstagramInteractionService } from '../src/services/interaction'; |
| 5 | +import { InstagramPostService } from '../src/services/post'; |
| 6 | + |
| 7 | +// Mock dependencies |
| 8 | +vi.mock('@elizaos/core', async (importOriginal) => { |
| 9 | + const actual = await importOriginal(); |
| 10 | + return { |
| 11 | + ...actual, |
| 12 | + elizaLogger: { |
| 13 | + log: vi.fn(), |
| 14 | + error: vi.fn(), |
| 15 | + }, |
| 16 | + parseBooleanFromText: (value: string | undefined) => value === 'true', |
| 17 | + }; |
| 18 | +}); |
| 19 | + |
| 20 | +// Mock service instances |
| 21 | +const mockPostService = { |
| 22 | + start: vi.fn().mockResolvedValue(undefined), |
| 23 | +}; |
| 24 | + |
| 25 | +const mockInteractionService = { |
| 26 | + start: vi.fn().mockResolvedValue(undefined), |
| 27 | +}; |
| 28 | + |
| 29 | +vi.mock('../src/lib/auth', () => ({ |
| 30 | + initializeClient: vi.fn().mockResolvedValue({ |
| 31 | + ig: {}, |
| 32 | + config: { |
| 33 | + INSTAGRAM_DRY_RUN: false, |
| 34 | + INSTAGRAM_ENABLE_ACTION_PROCESSING: true, |
| 35 | + }, |
| 36 | + }), |
| 37 | +})); |
| 38 | + |
| 39 | +vi.mock('../src/services/post', () => ({ |
| 40 | + InstagramPostService: vi.fn().mockImplementation(() => mockPostService), |
| 41 | +})); |
| 42 | + |
| 43 | +vi.mock('../src/services/interaction', () => ({ |
| 44 | + InstagramInteractionService: vi.fn().mockImplementation(() => mockInteractionService), |
| 45 | +})); |
| 46 | + |
| 47 | +describe('InstagramClientInterface', () => { |
| 48 | + let mockRuntime: IAgentRuntime; |
| 49 | + const mockConfig = { |
| 50 | + INSTAGRAM_DRY_RUN: false, |
| 51 | + INSTAGRAM_USERNAME: 'test_user', |
| 52 | + INSTAGRAM_PASSWORD: 'test_password', |
| 53 | + INSTAGRAM_APP_ID: 'test_app_id', |
| 54 | + INSTAGRAM_APP_SECRET: 'test_app_secret', |
| 55 | + INSTAGRAM_POST_INTERVAL_MIN: 60, |
| 56 | + INSTAGRAM_POST_INTERVAL_MAX: 120, |
| 57 | + INSTAGRAM_ENABLE_ACTION_PROCESSING: true, |
| 58 | + INSTAGRAM_ACTION_INTERVAL: 5, |
| 59 | + INSTAGRAM_MAX_ACTIONS: 1, |
| 60 | + }; |
| 61 | + |
| 62 | + beforeEach(() => { |
| 63 | + vi.clearAllMocks(); |
| 64 | + mockRuntime = { |
| 65 | + getSetting: vi.fn((key: string) => { |
| 66 | + if (key === 'INSTAGRAM_DRY_RUN' || key === 'INSTAGRAM_ENABLE_ACTION_PROCESSING') { |
| 67 | + return String(mockConfig[key as keyof typeof mockConfig]); |
| 68 | + } |
| 69 | + return mockConfig[key as keyof typeof mockConfig]; |
| 70 | + }), |
| 71 | + } as unknown as IAgentRuntime; |
| 72 | + }); |
| 73 | + |
| 74 | + it('starts successfully with all services', async () => { |
| 75 | + const result = await InstagramClientInterface.start(mockRuntime); |
| 76 | + |
| 77 | + expect(result).toBeDefined(); |
| 78 | + expect(result.post).toBeDefined(); |
| 79 | + expect(result.interaction).toBeDefined(); |
| 80 | + expect(InstagramPostService).toHaveBeenCalled(); |
| 81 | + expect(InstagramInteractionService).toHaveBeenCalled(); |
| 82 | + expect(result.post.start).toHaveBeenCalled(); |
| 83 | + expect(result.interaction.start).toHaveBeenCalled(); |
| 84 | + expect(elizaLogger.log).toHaveBeenCalledWith('Instagram client configuration validated'); |
| 85 | + expect(elizaLogger.log).toHaveBeenCalledWith('Instagram client initialized'); |
| 86 | + expect(elizaLogger.log).toHaveBeenCalledWith('Instagram post service started'); |
| 87 | + expect(elizaLogger.log).toHaveBeenCalledWith('Instagram interaction service started'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('starts in dry-run mode', async () => { |
| 91 | + const dryRunConfig = { ...mockConfig, INSTAGRAM_DRY_RUN: true }; |
| 92 | + mockRuntime.getSetting = vi.fn((key: string) => { |
| 93 | + if (key === 'INSTAGRAM_DRY_RUN') return 'true'; |
| 94 | + if (key === 'INSTAGRAM_ENABLE_ACTION_PROCESSING') return String(dryRunConfig.INSTAGRAM_ENABLE_ACTION_PROCESSING); |
| 95 | + return dryRunConfig[key as keyof typeof dryRunConfig]; |
| 96 | + }); |
| 97 | + |
| 98 | + const result = await InstagramClientInterface.start(mockRuntime); |
| 99 | + |
| 100 | + expect(result).toBeDefined(); |
| 101 | + expect(elizaLogger.log).toHaveBeenCalledWith('Instagram client running in dry-run mode'); |
| 102 | + expect(mockPostService.start).not.toHaveBeenCalled(); |
| 103 | + expect(mockInteractionService.start).not.toHaveBeenCalled(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('handles errors during startup', async () => { |
| 107 | + const error = new Error('Startup failed'); |
| 108 | + vi.mocked(mockRuntime.getSetting).mockImplementation(() => { |
| 109 | + throw error; |
| 110 | + }); |
| 111 | + |
| 112 | + await expect(InstagramClientInterface.start(mockRuntime)).rejects.toThrow('Startup failed'); |
| 113 | + expect(elizaLogger.error).toHaveBeenCalledWith('Failed to start Instagram client:', error); |
| 114 | + }); |
| 115 | + |
| 116 | + it('stops gracefully', async () => { |
| 117 | + await InstagramClientInterface.stop(mockRuntime); |
| 118 | + expect(elizaLogger.log).toHaveBeenCalledWith('Stopping Instagram client services...'); |
| 119 | + }); |
| 120 | +}); |
0 commit comments