File size: 5,105 Bytes
1766992 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | // Copyright (c) 2025-2026 libaxuan
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package config
import (
"os"
"testing"
)
func TestLoadConfig(t *testing.T) {
// Create a temporary .env file for testing
envContent := `PORT=9000
DEBUG=true
API_KEY=test-key
MODELS=claude-sonnet-4.6
SYSTEM_PROMPT_INJECT=Test prompt
TIMEOUT=60
MAX_INPUT_LENGTH=10000
USER_AGENT=Test Agent
SCRIPT_URL=https://test.com/script.js`
// Write to temporary .env file
err := os.WriteFile(".env", []byte(envContent), 0644)
if err != nil {
t.Fatalf("Failed to create test .env file: %v", err)
}
defer os.Remove(".env") // Clean up
config, err := LoadConfig()
if err != nil {
t.Fatalf("LoadConfig() error = %v", err)
}
// Test loaded values
if config.Port != 9000 {
t.Errorf("Port = %v, want 9000", config.Port)
}
if !config.Debug {
t.Errorf("Debug = %v, want true", config.Debug)
}
if config.APIKey != "test-key" {
t.Errorf("APIKey = %v, want test-key", config.APIKey)
}
if config.SystemPromptInject != "Test prompt" {
t.Errorf("SystemPromptInject = %v, want Test prompt", config.SystemPromptInject)
}
if config.Timeout != 60 {
t.Errorf("Timeout = %v, want 60", config.Timeout)
}
if config.MaxInputLength != 10000 {
t.Errorf("MaxInputLength = %v, want 10000", config.MaxInputLength)
}
if config.FP.UserAgent != "Test Agent" {
t.Errorf("UserAgent = %v, want Test Agent", config.FP.UserAgent)
}
if config.ScriptURL != "https://test.com/script.js" {
t.Errorf("ScriptURL = %v, want https://test.com/script.js", config.ScriptURL)
}
}
func TestGetModels(t *testing.T) {
config := &Config{
Models: "claude-sonnet-4.6",
}
models := config.GetModels()
expected := []string{
"claude-sonnet-4.6",
"claude-sonnet-4.6-thinking",
}
if len(models) != len(expected) {
t.Errorf("GetModels() length = %v, want %v", len(models), len(expected))
}
for i, model := range models {
if model != expected[i] {
t.Errorf("GetModels()[%d] = %v, want %v", i, model, expected[i])
}
}
}
func TestIsValidModel(t *testing.T) {
config := &Config{
Models: "claude-sonnet-4.6",
}
tests := []struct {
name string
model string
expected bool
}{
{"valid base model", "claude-sonnet-4.6", true},
{"valid thinking model", "claude-sonnet-4.6-thinking", true},
{"invalid model", "unknown-model", false},
{"empty model", "", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := config.IsValidModel(tt.model)
if result != tt.expected {
t.Errorf("IsValidModel(%q) = %v, want %v", tt.model, result, tt.expected)
}
})
}
}
func TestValidate(t *testing.T) {
tests := []struct {
name string
config *Config
wantErr bool
}{
{
name: "valid config",
config: &Config{
Port: 8000,
APIKey: "test-key",
Timeout: 30,
MaxInputLength: 1000,
},
wantErr: false,
},
{
name: "invalid port - too low",
config: &Config{
Port: 0,
APIKey: "test-key",
Timeout: 30,
MaxInputLength: 1000,
},
wantErr: true,
},
{
name: "invalid port - too high",
config: &Config{
Port: 70000,
APIKey: "test-key",
Timeout: 30,
MaxInputLength: 1000,
},
wantErr: true,
},
{
name: "missing API key",
config: &Config{
Port: 8000,
APIKey: "",
Timeout: 30,
MaxInputLength: 1000,
},
wantErr: true,
},
{
name: "invalid timeout",
config: &Config{
Port: 8000,
APIKey: "test-key",
Timeout: 0,
MaxInputLength: 1000,
},
wantErr: true,
},
{
name: "invalid max input length",
config: &Config{
Port: 8000,
APIKey: "test-key",
Timeout: 30,
MaxInputLength: 0,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.config.validate()
if (err != nil) != tt.wantErr {
t.Errorf("validate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
|