|
| 1 | +package config_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/BurntSushi/toml" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/urfave/cli/v2" |
| 11 | + |
| 12 | + "github.com/rubiojr/hashup/internal/config" |
| 13 | +) |
| 14 | + |
| 15 | +func TestDefaultConfig(t *testing.T) { |
| 16 | + cfg := config.DefaultConfig() |
| 17 | + |
| 18 | + homeDir, _ := os.UserHomeDir() |
| 19 | + expectedDBPath := filepath.Join(homeDir, ".local", "share", "hashup", "hashup.db") |
| 20 | + expectedCachePath := filepath.Join(homeDir, ".cache", "hashup", "cache") |
| 21 | + |
| 22 | + assert.Equal(t, "http://localhost:4222", cfg.Main.NatsServerURL) |
| 23 | + assert.Equal(t, "HASHUP", cfg.Main.NatsStream) |
| 24 | + assert.Equal(t, "FILES", cfg.Main.NatsSubject) |
| 25 | + assert.Equal(t, 30, cfg.Store.StatsInterval) |
| 26 | + assert.Equal(t, expectedDBPath, cfg.Store.DBPath) |
| 27 | + assert.Equal(t, 3600, cfg.Scanner.ScanningInterval) |
| 28 | + assert.Equal(t, 5, cfg.Scanner.ScanningConcurrency) |
| 29 | + assert.Equal(t, expectedCachePath, cfg.Scanner.CachePath) |
| 30 | +} |
| 31 | + |
| 32 | +func TestNormalizePath(t *testing.T) { |
| 33 | + homeDir, _ := os.UserHomeDir() |
| 34 | + cfg := config.Config{Path: "/some/config/path/config.toml"} |
| 35 | + |
| 36 | + tests := []struct { |
| 37 | + name string |
| 38 | + input string |
| 39 | + expected string |
| 40 | + }{ |
| 41 | + {"Empty path", "", ""}, |
| 42 | + {"Relative path", "relative/path", "/some/config/path/relative/path"}, |
| 43 | + {"Absolute path", "/absolute/path", "/absolute/path"}, |
| 44 | + {"Home tilde path", "~/something", filepath.Join(homeDir, "something")}, |
| 45 | + } |
| 46 | + |
| 47 | + for _, tt := range tests { |
| 48 | + t.Run(tt.name, func(t *testing.T) { |
| 49 | + result := cfg.NormalizePath(tt.input) |
| 50 | + assert.Equal(t, tt.expected, result) |
| 51 | + }) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestLoadConfig(t *testing.T) { |
| 56 | + // Create a temporary config file |
| 57 | + tempDir := t.TempDir() |
| 58 | + configPath := filepath.Join(tempDir, "test_config.toml") |
| 59 | + |
| 60 | + testCfg := config.DefaultConfig() |
| 61 | + testCfg.Main.NatsServerURL = "nats://testserver:4222" |
| 62 | + testCfg.Store.StatsInterval = 60 |
| 63 | + |
| 64 | + f, err := os.Create(configPath) |
| 65 | + assert.NoError(t, err) |
| 66 | + defer f.Close() |
| 67 | + |
| 68 | + err = toml.NewEncoder(f).Encode(testCfg) |
| 69 | + assert.NoError(t, err) |
| 70 | + f.Close() |
| 71 | + |
| 72 | + // Test loading the config |
| 73 | + cfg, err := config.LoadConfig(configPath) |
| 74 | + assert.NoError(t, err) |
| 75 | + assert.Equal(t, "nats://testserver:4222", cfg.Main.NatsServerURL) |
| 76 | + assert.Equal(t, 60, cfg.Store.StatsInterval) |
| 77 | + |
| 78 | + // Test loading non-existent config |
| 79 | + _, err = config.LoadConfig(filepath.Join(tempDir, "nonexistent.toml")) |
| 80 | + assert.Error(t, err) |
| 81 | + assert.Contains(t, err.Error(), "config file not found") |
| 82 | +} |
| 83 | + |
| 84 | +func TestSaveConfig(t *testing.T) { |
| 85 | + tempDir := t.TempDir() |
| 86 | + configPath := filepath.Join(tempDir, "config.toml") |
| 87 | + |
| 88 | + cfg := config.DefaultConfig() |
| 89 | + cfg.Main.NatsServerURL = "nats://customserver:4222" |
| 90 | + |
| 91 | + err := config.SaveConfig(cfg, configPath) |
| 92 | + assert.NoError(t, err) |
| 93 | + |
| 94 | + // Verify saved config can be loaded correctly |
| 95 | + loadedCfg, err := config.LoadConfig(configPath) |
| 96 | + assert.NoError(t, err) |
| 97 | + assert.Equal(t, "nats://customserver:4222", loadedCfg.Main.NatsServerURL) |
| 98 | +} |
| 99 | + |
| 100 | +func TestLoadConfigFromCLI(t *testing.T) { |
| 101 | + t.Skip("broken") |
| 102 | + tempDir := t.TempDir() |
| 103 | + configPath := filepath.Join(tempDir, "cli_config.toml") |
| 104 | + |
| 105 | + // Create a basic config file |
| 106 | + basicCfg := config.DefaultConfig() |
| 107 | + err := config.SaveConfig(basicCfg, configPath) |
| 108 | + assert.NoError(t, err) |
| 109 | + |
| 110 | + tests := []struct { |
| 111 | + name string |
| 112 | + args []string |
| 113 | + validate func(*testing.T, *config.Config) |
| 114 | + }{ |
| 115 | + { |
| 116 | + name: "Custom config file", |
| 117 | + args: []string{"--config", configPath}, |
| 118 | + validate: func(t *testing.T, cfg *config.Config) { |
| 119 | + assert.Equal(t, configPath, cfg.Path) |
| 120 | + }, |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "Custom encryption key", |
| 124 | + args: []string{"--config", configPath, "--encryption-key", "testkey123"}, |
| 125 | + validate: func(t *testing.T, cfg *config.Config) { |
| 126 | + assert.Equal(t, "testkey123", cfg.Main.EncryptionKey) |
| 127 | + }, |
| 128 | + }, |
| 129 | + { |
| 130 | + name: "Custom DB path", |
| 131 | + args: []string{"--config", configPath, "--db-path", "/custom/db/path"}, |
| 132 | + validate: func(t *testing.T, cfg *config.Config) { |
| 133 | + assert.Equal(t, "/custom/db/path", cfg.Store.DBPath) |
| 134 | + }, |
| 135 | + }, |
| 136 | + { |
| 137 | + name: "Custom stats interval", |
| 138 | + args: []string{"--config", configPath, "--stats-interval", "120"}, |
| 139 | + validate: func(t *testing.T, cfg *config.Config) { |
| 140 | + assert.Equal(t, 120, cfg.Store.StatsInterval) |
| 141 | + }, |
| 142 | + }, |
| 143 | + { |
| 144 | + name: "Custom NATS URL", |
| 145 | + args: []string{"--config", configPath, "--nats-url", "nats://custom:4222"}, |
| 146 | + validate: func(t *testing.T, cfg *config.Config) { |
| 147 | + assert.Equal(t, "nats://custom:4222", cfg.Main.NatsServerURL) |
| 148 | + }, |
| 149 | + }, |
| 150 | + } |
| 151 | + |
| 152 | + for _, tt := range tests { |
| 153 | + t.Run(tt.name, func(t *testing.T) { |
| 154 | + app := cli.NewApp() |
| 155 | + ctx := cli.NewContext(app, nil, nil) |
| 156 | + |
| 157 | + // Set up flags |
| 158 | + flags := map[string]any{ |
| 159 | + "config": configPath, |
| 160 | + "encryption-key": "", |
| 161 | + "db-path": "", |
| 162 | + "stats-interval": 0, |
| 163 | + "nats-url": "", |
| 164 | + "stream": "", |
| 165 | + "client-cert": "", |
| 166 | + "client-key": "", |
| 167 | + "ca-cert": "", |
| 168 | + } |
| 169 | + |
| 170 | + // Apply command line args |
| 171 | + for i := 0; i < len(tt.args); i += 2 { |
| 172 | + if i+1 < len(tt.args) { |
| 173 | + flagName := tt.args[i][2:] // Remove -- |
| 174 | + flagValue := tt.args[i+1] |
| 175 | + flags[flagName] = flagValue |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + // Set values in the context |
| 180 | + for name, value := range flags { |
| 181 | + switch v := value.(type) { |
| 182 | + case string: |
| 183 | + ctx.Set(name, v) |
| 184 | + case int: |
| 185 | + if v != 0 { |
| 186 | + ctx.Set(name, string(rune(v))) |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + cfg, err := config.LoadConfigFromCLI(ctx) |
| 192 | + assert.NoError(t, err) |
| 193 | + tt.validate(t, cfg) |
| 194 | + }) |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +func TestDefaultPaths(t *testing.T) { |
| 199 | + homeDir, _ := os.UserHomeDir() |
| 200 | + |
| 201 | + configDir, err := config.DefaultConfigDir() |
| 202 | + assert.NoError(t, err) |
| 203 | + assert.Equal(t, filepath.Join(homeDir, ".config", "hashup"), configDir) |
| 204 | + |
| 205 | + dbPath := config.DefaultDBPath() |
| 206 | + assert.Equal(t, filepath.Join(homeDir, ".local", "share", "hashup", "hashup.db"), dbPath) |
| 207 | + |
| 208 | + dbDir := config.DefaultDBDir() |
| 209 | + assert.Equal(t, filepath.Join(homeDir, ".local", "share", "hashup"), dbDir) |
| 210 | + |
| 211 | + natsDir := config.DefaultNATSDataDir() |
| 212 | + assert.Equal(t, filepath.Join(homeDir, ".local", "share", "hashup", "nats"), natsDir) |
| 213 | + |
| 214 | + cachePath := config.DefaultCachePath() |
| 215 | + assert.Equal(t, filepath.Join(homeDir, ".cache", "hashup", "cache"), cachePath) |
| 216 | +} |
0 commit comments