package bootstrap import ( "fmt" "os" "path/filepath" "testing" ) func TestInitializeApp(t *testing.T) { // 创建临时配置文件 tempDir := t.TempDir() configPath := filepath.Join(tempDir, "config.toml") logDir := filepath.Join(tempDir, "logs") sqlDir := filepath.Join(tempDir, "sql") auditDBPath := filepath.Join(tempDir, "audit.db") storageDBPath := filepath.Join(tempDir, "storage.db") logDirConfig := filepath.ToSlash(logDir) sqlDirConfig := filepath.ToSlash(sqlDir) auditDBPathConfig := filepath.ToSlash(auditDBPath) storageDBPathConfig := filepath.ToSlash(storageDBPath) if err := os.MkdirAll(logDir, 0o755); err != nil { t.Fatalf("Failed to create log dir: %v", err) } if err := os.MkdirAll(sqlDir, 0o755); err != nil { t.Fatalf("Failed to create sql dir: %v", err) } // 创建测试配置 testConfig := fmt.Sprintf(` [server] ip = "127.0.0.1" port = 8080 [log] level = "info" development = false encoding = "json" output_paths = ["stdout"] log_dir = "%s" [audit] enabled = false database_path = "%s" retention_days = 90 buffer_size = 1000 [storage] type = "db" config_path = "%s" sql_base_dir = "%s" database_path = "%s" `, logDirConfig, auditDBPathConfig, filepath.ToSlash(configPath), sqlDirConfig, storageDBPathConfig) err := os.WriteFile(configPath, []byte(testConfig), 0644) if err != nil { t.Fatalf("Failed to create test config: %v", err) } // 测试初始化 app, err := InitializeApp(configPath) if err != nil { t.Fatalf("InitializeApp failed: %v", err) } // 验证初始化结果 if app.Config == nil { t.Error("Config should not be nil") } if app.Logger == nil { t.Error("Logger should not be nil") } if app.AuditLogger == nil { t.Error("AuditLogger should not be nil") } if app.StorageManager == nil { t.Error("StorageManager should not be nil") } if app.TaskManager == nil { t.Error("TaskManager should not be nil") } if app.ConnectionPool == nil { t.Error("ConnectionPool should not be nil") } if app.Context == nil { t.Error("Context should not be nil") } if app.Cancel == nil { t.Error("Cancel should not be nil") } // 测试关闭 err = app.Shutdown() if err != nil { t.Errorf("Shutdown failed: %v", err) } } func TestLoadConfig(t *testing.T) { // 创建临时配置文件 tempDir := t.TempDir() configPath := filepath.Join(tempDir, "config.toml") testConfig := ` [server] ip = "127.0.0.1" port = 8080 [log] level = "debug" [storage] type = "db" ` err := os.WriteFile(configPath, []byte(testConfig), 0644) if err != nil { t.Fatalf("Failed to create test config: %v", err) } cfg, err := loadConfig(configPath) if err != nil { t.Fatalf("loadConfig failed: %v", err) } 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.Storage.Type != "db" { t.Errorf("Expected storage type 'db', got '%s'", cfg.Storage.Type) } }