package config import ( "os" "path/filepath" "testing" ) func TestMySQLDSN(t *testing.T) { cfg := &Config{ MySQLUser: "testuser", MySQLPassword: "testpass", MySQLHost: "192.168.1.1", MySQLPort: "3307", MySQLDatabase: "testdb", } got := cfg.MySQLDSN() want := "testuser:testpass@tcp(192.168.1.1:3307)/testdb?charset=utf8mb4&parseTime=True&loc=Local" if got != want { t.Errorf("MySQLDSN() = %q, want %q", got, want) } } func TestMySQLDSN_EmptyPassword(t *testing.T) { cfg := &Config{ MySQLUser: "root", MySQLPassword: "", MySQLHost: "127.0.0.1", MySQLPort: "3306", MySQLDatabase: "pr_helper", } got := cfg.MySQLDSN() want := "root:@tcp(127.0.0.1:3306)/pr_helper?charset=utf8mb4&parseTime=True&loc=Local" if got != want { t.Errorf("MySQLDSN() = %q, want %q", got, want) } } func TestReposDir(t *testing.T) { cfg := &Config{DataDir: "/tmp/testdata"} got := cfg.ReposDir() want := filepath.Join("/tmp/testdata", "repos") if got != want { t.Errorf("ReposDir() = %q, want %q", got, want) } } func TestLoad_Defaults(t *testing.T) { // Clear all relevant env vars to test defaults envVars := []string{"PORT", "GIN_MODE", "DATA_DIR", "SESSION_SECRET", "MYSQL_HOST", "MYSQL_PORT", "MYSQL_USER", "MYSQL_PASSWORD", "MYSQL_DATABASE"} origValues := make(map[string]string) for _, k := range envVars { origValues[k] = os.Getenv(k) os.Unsetenv(k) } defer func() { for k, v := range origValues { if v != "" { os.Setenv(k, v) } } }() cfg := Load() if cfg.Port != "8080" { t.Errorf("Port = %q, want %q", cfg.Port, "8080") } if cfg.GinMode != "debug" { t.Errorf("GinMode = %q, want %q", cfg.GinMode, "debug") } if cfg.DataDir != "data" { t.Errorf("DataDir = %q, want %q", cfg.DataDir, "data") } if cfg.MySQLHost != "127.0.0.1" { t.Errorf("MySQLHost = %q, want %q", cfg.MySQLHost, "127.0.0.1") } if cfg.MySQLPort != "3306" { t.Errorf("MySQLPort = %q, want %q", cfg.MySQLPort, "3306") } if cfg.MySQLUser != "root" { t.Errorf("MySQLUser = %q, want %q", cfg.MySQLUser, "root") } if cfg.MySQLDatabase != "pr_helper" { t.Errorf("MySQLDatabase = %q, want %q", cfg.MySQLDatabase, "pr_helper") } // Session secret should be auto-generated (64 hex chars) if len(cfg.SessionSecret) != 64 { t.Errorf("SessionSecret length = %d, want 64", len(cfg.SessionSecret)) } } func TestLoad_WithEnvVars(t *testing.T) { os.Setenv("PORT", "9090") os.Setenv("GIN_MODE", "release") os.Setenv("SESSION_SECRET", "my-secret-key") defer func() { os.Unsetenv("PORT") os.Unsetenv("GIN_MODE") os.Unsetenv("SESSION_SECRET") }() cfg := Load() if cfg.Port != "9090" { t.Errorf("Port = %q, want %q", cfg.Port, "9090") } if cfg.GinMode != "release" { t.Errorf("GinMode = %q, want %q", cfg.GinMode, "release") } if cfg.SessionSecret != "my-secret-key" { t.Errorf("SessionSecret = %q, want %q", cfg.SessionSecret, "my-secret-key") } } func TestLoad_CreatesDataDirs(t *testing.T) { tmpDir := t.TempDir() dataDir := filepath.Join(tmpDir, "test-data") os.Setenv("DATA_DIR", dataDir) defer os.Unsetenv("DATA_DIR") Load() // Verify data dir was created if _, err := os.Stat(dataDir); os.IsNotExist(err) { t.Errorf("DataDir %q was not created", dataDir) } // Verify repos subdir was created reposDir := filepath.Join(dataDir, "repos") if _, err := os.Stat(reposDir); os.IsNotExist(err) { t.Errorf("ReposDir %q was not created", reposDir) } }