|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { validateTwitterConfig } from '../src/environment'; |
| 3 | +import { IAgentRuntime } from '@elizaos/core'; |
| 4 | + |
| 5 | +describe('Twitter Environment Configuration', () => { |
| 6 | + const mockRuntime: IAgentRuntime = { |
| 7 | + env: { |
| 8 | + TWITTER_USERNAME: 'testuser123', |
| 9 | + TWITTER_DRY_RUN: 'true', |
| 10 | + TWITTER_SEARCH_ENABLE: 'false', |
| 11 | + TWITTER_SPACES_ENABLE: 'false', |
| 12 | + TWITTER_TARGET_USERS: 'user1,user2,user3', |
| 13 | + TWITTER_MAX_TWEETS_PER_DAY: '10', |
| 14 | + TWITTER_MAX_TWEET_LENGTH: '280', |
| 15 | + TWITTER_POST_INTERVAL_MIN: '90', |
| 16 | + TWITTER_POST_INTERVAL_MAX: '180', |
| 17 | + TWITTER_ACTION_INTERVAL: '5', |
| 18 | + TWITTER_ENABLE_ACTION_PROCESSING: 'false', |
| 19 | + TWITTER_POST_IMMEDIATELY: 'false', |
| 20 | + TWITTER_EMAIL: 'test@example.com', |
| 21 | + TWITTER_PASSWORD: 'hashedpassword', |
| 22 | + TWITTER_2FA_SECRET: '', |
| 23 | + TWITTER_POLL_INTERVAL: '120', |
| 24 | + TWITTER_RETRY_LIMIT: '5', |
| 25 | + ACTION_TIMELINE_TYPE: 'foryou', |
| 26 | + MAX_ACTIONS_PROCESSING: '1', |
| 27 | + MAX_TWEET_LENGTH: '280' |
| 28 | + }, |
| 29 | + getEnv: function (key: string) { |
| 30 | + return this.env[key] || null; |
| 31 | + }, |
| 32 | + getSetting: function (key: string) { |
| 33 | + return this.env[key] || null; |
| 34 | + } |
| 35 | + } as unknown as IAgentRuntime; |
| 36 | + |
| 37 | + it('should validate correct configuration', async () => { |
| 38 | + const config = await validateTwitterConfig(mockRuntime); |
| 39 | + expect(config).toBeDefined(); |
| 40 | + expect(config.TWITTER_USERNAME).toBe('testuser123'); |
| 41 | + expect(config.TWITTER_DRY_RUN).toBe(true); |
| 42 | + expect(config.TWITTER_SEARCH_ENABLE).toBe(false); |
| 43 | + expect(config.TWITTER_SPACES_ENABLE).toBe(false); |
| 44 | + expect(config.TWITTER_TARGET_USERS).toEqual(['user1', 'user2', 'user3']); |
| 45 | + expect(config.MAX_TWEET_LENGTH).toBe(280); |
| 46 | + expect(config.POST_INTERVAL_MIN).toBe(90); |
| 47 | + expect(config.POST_INTERVAL_MAX).toBe(180); |
| 48 | + expect(config.ACTION_INTERVAL).toBe(5); |
| 49 | + expect(config.ENABLE_ACTION_PROCESSING).toBe(false); |
| 50 | + expect(config.POST_IMMEDIATELY).toBe(false); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should validate wildcard username', async () => { |
| 54 | + const wildcardRuntime = { |
| 55 | + ...mockRuntime, |
| 56 | + env: { |
| 57 | + ...mockRuntime.env, |
| 58 | + TWITTER_USERNAME: '*' |
| 59 | + }, |
| 60 | + getEnv: function(key: string) { |
| 61 | + return this.env[key] || null; |
| 62 | + }, |
| 63 | + getSetting: function(key: string) { |
| 64 | + return this.env[key] || null; |
| 65 | + } |
| 66 | + } as IAgentRuntime; |
| 67 | + |
| 68 | + const config = await validateTwitterConfig(wildcardRuntime); |
| 69 | + expect(config.TWITTER_USERNAME).toBe('*'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should validate username with numbers and underscores', async () => { |
| 73 | + const validRuntime = { |
| 74 | + ...mockRuntime, |
| 75 | + env: { |
| 76 | + ...mockRuntime.env, |
| 77 | + TWITTER_USERNAME: 'test_user_123' |
| 78 | + }, |
| 79 | + getEnv: function(key: string) { |
| 80 | + return this.env[key] || null; |
| 81 | + }, |
| 82 | + getSetting: function(key: string) { |
| 83 | + return this.env[key] || null; |
| 84 | + } |
| 85 | + } as IAgentRuntime; |
| 86 | + |
| 87 | + const config = await validateTwitterConfig(validRuntime); |
| 88 | + expect(config.TWITTER_USERNAME).toBe('test_user_123'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should handle empty target users', async () => { |
| 92 | + const runtimeWithoutTargets = { |
| 93 | + ...mockRuntime, |
| 94 | + env: { |
| 95 | + ...mockRuntime.env, |
| 96 | + TWITTER_TARGET_USERS: '' |
| 97 | + }, |
| 98 | + getEnv: function(key: string) { |
| 99 | + return this.env[key] || null; |
| 100 | + }, |
| 101 | + getSetting: function(key: string) { |
| 102 | + return this.env[key] || null; |
| 103 | + } |
| 104 | + } as IAgentRuntime; |
| 105 | + |
| 106 | + const config = await validateTwitterConfig(runtimeWithoutTargets); |
| 107 | + expect(config.TWITTER_TARGET_USERS).toHaveLength(0); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should use default values when optional configs are missing', async () => { |
| 111 | + const minimalRuntime = { |
| 112 | + env: { |
| 113 | + TWITTER_USERNAME: 'testuser', |
| 114 | + TWITTER_DRY_RUN: 'true', |
| 115 | + TWITTER_EMAIL: 'test@example.com', |
| 116 | + TWITTER_PASSWORD: 'hashedpassword', |
| 117 | + TWITTER_2FA_SECRET: '', |
| 118 | + MAX_TWEET_LENGTH: '280' |
| 119 | + }, |
| 120 | + getEnv: function (key: string) { |
| 121 | + return this.env[key] || null; |
| 122 | + }, |
| 123 | + getSetting: function (key: string) { |
| 124 | + return this.env[key] || null; |
| 125 | + } |
| 126 | + } as unknown as IAgentRuntime; |
| 127 | + |
| 128 | + const config = await validateTwitterConfig(minimalRuntime); |
| 129 | + expect(config).toBeDefined(); |
| 130 | + expect(config.MAX_TWEET_LENGTH).toBe(280); |
| 131 | + expect(config.POST_INTERVAL_MIN).toBe(90); |
| 132 | + expect(config.POST_INTERVAL_MAX).toBe(180); |
| 133 | + }); |
| 134 | +}); |
0 commit comments