|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { validateEnv, validateCharacterConfig } from '../environment'; |
| 3 | +import { Clients, ModelProviderName } from '../types'; |
| 4 | + |
| 5 | +describe('Environment Configuration', () => { |
| 6 | + const originalEnv = process.env; |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + process.env = { |
| 10 | + ...originalEnv, |
| 11 | + OPENAI_API_KEY: 'sk-test123', |
| 12 | + REDPILL_API_KEY: 'test-key', |
| 13 | + GROK_API_KEY: 'test-key', |
| 14 | + GROQ_API_KEY: 'gsk_test123', |
| 15 | + OPENROUTER_API_KEY: 'test-key', |
| 16 | + GOOGLE_GENERATIVE_AI_API_KEY: 'test-key', |
| 17 | + ELEVENLABS_XI_API_KEY: 'test-key', |
| 18 | + }; |
| 19 | + }); |
| 20 | + |
| 21 | + afterEach(() => { |
| 22 | + process.env = originalEnv; |
| 23 | + }); |
| 24 | + |
| 25 | + it('should validate correct environment variables', () => { |
| 26 | + expect(() => validateEnv()).not.toThrow(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should throw error for invalid OpenAI API key format', () => { |
| 30 | + process.env.OPENAI_API_KEY = 'invalid-key'; |
| 31 | + expect(() => validateEnv()).toThrow("OpenAI API key must start with 'sk-'"); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should throw error for invalid GROQ API key format', () => { |
| 35 | + process.env.GROQ_API_KEY = 'invalid-key'; |
| 36 | + expect(() => validateEnv()).toThrow("GROQ API key must start with 'gsk_'"); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should throw error for missing required keys', () => { |
| 40 | + delete process.env.REDPILL_API_KEY; |
| 41 | + expect(() => validateEnv()).toThrow('REDPILL_API_KEY: Required'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should throw error for multiple missing required keys', () => { |
| 45 | + delete process.env.REDPILL_API_KEY; |
| 46 | + delete process.env.GROK_API_KEY; |
| 47 | + delete process.env.OPENROUTER_API_KEY; |
| 48 | + expect(() => validateEnv()).toThrow( |
| 49 | + 'Environment validation failed:\n' + |
| 50 | + 'REDPILL_API_KEY: Required\n' + |
| 51 | + 'GROK_API_KEY: Required\n' + |
| 52 | + 'OPENROUTER_API_KEY: Required' |
| 53 | + ); |
| 54 | + }); |
| 55 | +}); |
| 56 | + |
| 57 | +describe('Character Configuration', () => { |
| 58 | + const validCharacterConfig = { |
| 59 | + name: 'Test Character', |
| 60 | + modelProvider: ModelProviderName.OPENAI, |
| 61 | + bio: 'Test bio', |
| 62 | + lore: ['Test lore'], |
| 63 | + messageExamples: [[ |
| 64 | + { |
| 65 | + user: 'user1', |
| 66 | + content: { |
| 67 | + text: 'Hello', |
| 68 | + } |
| 69 | + } |
| 70 | + ]], |
| 71 | + postExamples: ['Test post'], |
| 72 | + topics: ['topic1'], |
| 73 | + adjectives: ['friendly'], |
| 74 | + clients: [Clients.DISCORD], |
| 75 | + plugins: ['test-plugin'], |
| 76 | + style: { |
| 77 | + all: ['style1'], |
| 78 | + chat: ['chat-style'], |
| 79 | + post: ['post-style'] |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + it('should validate correct character configuration', () => { |
| 84 | + expect(() => validateCharacterConfig(validCharacterConfig)).not.toThrow(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should validate configuration with optional fields', () => { |
| 88 | + const configWithOptionals = { |
| 89 | + ...validCharacterConfig, |
| 90 | + id: '123e4567-e89b-12d3-a456-426614174000', |
| 91 | + system: 'Test system', |
| 92 | + templates: { |
| 93 | + greeting: 'Hello!' |
| 94 | + }, |
| 95 | + knowledge: ['fact1'], |
| 96 | + settings: { |
| 97 | + secrets: { |
| 98 | + key: 'value' |
| 99 | + }, |
| 100 | + voice: { |
| 101 | + model: 'test-model', |
| 102 | + url: 'http://example.com' |
| 103 | + } |
| 104 | + } |
| 105 | + }; |
| 106 | + expect(() => validateCharacterConfig(configWithOptionals)).not.toThrow(); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should throw error for missing required fields', () => { |
| 110 | + const invalidConfig = { ...validCharacterConfig }; |
| 111 | + delete (invalidConfig as any).name; |
| 112 | + expect(() => validateCharacterConfig(invalidConfig)).toThrow(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should validate plugin objects in plugins array', () => { |
| 116 | + const configWithPluginObjects = { |
| 117 | + ...validCharacterConfig, |
| 118 | + plugins: [{ |
| 119 | + name: 'test-plugin', |
| 120 | + description: 'Test description' |
| 121 | + }] |
| 122 | + }; |
| 123 | + expect(() => validateCharacterConfig(configWithPluginObjects)).not.toThrow(); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should validate client-specific configurations', () => { |
| 127 | + const configWithClientConfig = { |
| 128 | + ...validCharacterConfig, |
| 129 | + clientConfig: { |
| 130 | + discord: { |
| 131 | + shouldIgnoreBotMessages: true, |
| 132 | + shouldIgnoreDirectMessages: false |
| 133 | + }, |
| 134 | + telegram: { |
| 135 | + shouldIgnoreBotMessages: true, |
| 136 | + shouldIgnoreDirectMessages: true |
| 137 | + } |
| 138 | + } |
| 139 | + }; |
| 140 | + expect(() => validateCharacterConfig(configWithClientConfig)).not.toThrow(); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should validate twitter profile configuration', () => { |
| 144 | + const configWithTwitter = { |
| 145 | + ...validCharacterConfig, |
| 146 | + twitterProfile: { |
| 147 | + username: 'testuser', |
| 148 | + screenName: 'Test User', |
| 149 | + bio: 'Test bio', |
| 150 | + nicknames: ['test'] |
| 151 | + } |
| 152 | + }; |
| 153 | + expect(() => validateCharacterConfig(configWithTwitter)).not.toThrow(); |
| 154 | + }); |
| 155 | + |
| 156 | + it('should validate model endpoint override', () => { |
| 157 | + const configWithEndpoint = { |
| 158 | + ...validCharacterConfig, |
| 159 | + modelEndpointOverride: 'custom-endpoint' |
| 160 | + }; |
| 161 | + expect(() => validateCharacterConfig(configWithEndpoint)).not.toThrow(); |
| 162 | + }); |
| 163 | + |
| 164 | + it('should validate message examples with additional properties', () => { |
| 165 | + const configWithComplexMessage = { |
| 166 | + ...validCharacterConfig, |
| 167 | + messageExamples: [[{ |
| 168 | + user: 'user1', |
| 169 | + content: { |
| 170 | + text: 'Hello', |
| 171 | + action: 'wave', |
| 172 | + source: 'chat', |
| 173 | + url: 'http://example.com', |
| 174 | + inReplyTo: '123e4567-e89b-12d3-a456-426614174000', |
| 175 | + attachments: ['file1'], |
| 176 | + customField: 'value' |
| 177 | + } |
| 178 | + }]] |
| 179 | + }; |
| 180 | + expect(() => validateCharacterConfig(configWithComplexMessage)).not.toThrow(); |
| 181 | + }); |
| 182 | +}); |
0 commit comments