feat: 初始化多机器人服务端项目框架
基于Go语言构建多机器人服务端框架,包含配置管理、事件总线、依赖注入等核心模块 添加项目基础结构、README、gitignore和初始代码实现
This commit is contained in:
113
internal/config/config_test.go
Normal file
113
internal/config/config_test.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func TestConfigManager_Load(t *testing.T) {
|
||||
// 创建临时配置文件
|
||||
tmpDir := t.TempDir()
|
||||
configPath := filepath.Join(tmpDir, "test_config.toml")
|
||||
configContent := `
|
||||
[server]
|
||||
host = "127.0.0.1"
|
||||
port = 8080
|
||||
|
||||
[log]
|
||||
level = "debug"
|
||||
output = "stdout"
|
||||
max_size = 100
|
||||
max_backups = 3
|
||||
max_age = 7
|
||||
|
||||
[protocol]
|
||||
name = "test"
|
||||
version = "1.0"
|
||||
|
||||
[protocol.options]
|
||||
key = "value"
|
||||
`
|
||||
err := os.WriteFile(configPath, []byte(configContent), 0644)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create config file: %v", err)
|
||||
}
|
||||
|
||||
logger := zap.NewNop()
|
||||
cm := NewConfigManager(configPath, logger)
|
||||
|
||||
err = cm.Load()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to load config: %v", err)
|
||||
}
|
||||
|
||||
cfg := cm.Get()
|
||||
if cfg == nil {
|
||||
t.Fatal("Config is nil")
|
||||
}
|
||||
|
||||
if cfg.Server.Host != "127.0.0.1" {
|
||||
t.Errorf("Expected host '127.0.0.1', got '%s'", cfg.Server.Host)
|
||||
}
|
||||
|
||||
if cfg.Server.Port != 8080 {
|
||||
t.Errorf("Expected port 8080, got %d", cfg.Server.Port)
|
||||
}
|
||||
|
||||
if cfg.Log.Level != "debug" {
|
||||
t.Errorf("Expected log level 'debug', got '%s'", cfg.Log.Level)
|
||||
}
|
||||
|
||||
if cfg.Protocol.Name != "test" {
|
||||
t.Errorf("Expected protocol name 'test', got '%s'", cfg.Protocol.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitLogger(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
cfg *LogConfig
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "stdout logger",
|
||||
cfg: &LogConfig{
|
||||
Level: "info",
|
||||
Output: "stdout",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "stderr logger",
|
||||
cfg: &LogConfig{
|
||||
Level: "error",
|
||||
Output: "stderr",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "file logger",
|
||||
cfg: &LogConfig{
|
||||
Level: "debug",
|
||||
Output: filepath.Join(t.TempDir(), "test.log"),
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
logger, err := InitLogger(tt.cfg)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("InitLogger() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !tt.wantErr && logger == nil {
|
||||
t.Error("Expected non-nil logger")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user