|
| 1 | +package model |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestReadConfigFileWithLocal(t *testing.T) { |
| 14 | + // Create a temporary directory for test configs |
| 15 | + tmpDir, err := os.MkdirTemp("", "shelltime-test-*") |
| 16 | + require.NoError(t, err) |
| 17 | + defer os.RemoveAll(tmpDir) |
| 18 | + |
| 19 | + // Create base config file |
| 20 | + baseConfigPath := filepath.Join(tmpDir, "config.toml") |
| 21 | + baseConfig := `Token = 'base-token' |
| 22 | +APIEndpoint = 'https://api.base.com' |
| 23 | +WebEndpoint = 'https://base.com' |
| 24 | +FlushCount = 5 |
| 25 | +GCTime = 7 |
| 26 | +dataMasking = false |
| 27 | +enableMetrics = false |
| 28 | +encrypted = false` |
| 29 | + err = os.WriteFile(baseConfigPath, []byte(baseConfig), 0644) |
| 30 | + require.NoError(t, err) |
| 31 | + |
| 32 | + // Create local config file that overrides some settings |
| 33 | + localConfigPath := filepath.Join(tmpDir, "config.local.toml") |
| 34 | + localConfig := `Token = 'local-token' |
| 35 | +APIEndpoint = 'https://api.local.com' |
| 36 | +FlushCount = 10 |
| 37 | +dataMasking = true` |
| 38 | + err = os.WriteFile(localConfigPath, []byte(localConfig), 0644) |
| 39 | + require.NoError(t, err) |
| 40 | + |
| 41 | + // Test reading config with local override |
| 42 | + cs := NewConfigService(baseConfigPath) |
| 43 | + config, err := cs.ReadConfigFile(context.Background()) |
| 44 | + require.NoError(t, err) |
| 45 | + |
| 46 | + // Verify local config overrides base config |
| 47 | + assert.Equal(t, "local-token", config.Token, "Token should be overridden by local config") |
| 48 | + assert.Equal(t, "https://api.local.com", config.APIEndpoint, "APIEndpoint should be overridden by local config") |
| 49 | + assert.Equal(t, 10, config.FlushCount, "FlushCount should be overridden by local config") |
| 50 | + assert.True(t, *config.DataMasking, "DataMasking should be overridden by local config") |
| 51 | + |
| 52 | + // Verify base config values that weren't overridden |
| 53 | + assert.Equal(t, "https://base.com", config.WebEndpoint, "WebEndpoint should keep base value") |
| 54 | + assert.Equal(t, 7, config.GCTime, "GCTime should keep base value") |
| 55 | + assert.False(t, *config.EnableMetrics, "EnableMetrics should keep base value") |
| 56 | + assert.False(t, *config.Encrypted, "Encrypted should keep base value") |
| 57 | +} |
| 58 | + |
| 59 | +func TestReadConfigFileWithoutLocal(t *testing.T) { |
| 60 | + // Create a temporary directory for test configs |
| 61 | + tmpDir, err := os.MkdirTemp("", "shelltime-test-*") |
| 62 | + require.NoError(t, err) |
| 63 | + defer os.RemoveAll(tmpDir) |
| 64 | + |
| 65 | + // Create only base config file (no local file) |
| 66 | + baseConfigPath := filepath.Join(tmpDir, "config.toml") |
| 67 | + baseConfig := `Token = 'base-token' |
| 68 | +APIEndpoint = 'https://api.base.com' |
| 69 | +WebEndpoint = 'https://base.com' |
| 70 | +FlushCount = 5 |
| 71 | +GCTime = 7` |
| 72 | + err = os.WriteFile(baseConfigPath, []byte(baseConfig), 0644) |
| 73 | + require.NoError(t, err) |
| 74 | + |
| 75 | + // Test reading config without local file |
| 76 | + cs := NewConfigService(baseConfigPath) |
| 77 | + config, err := cs.ReadConfigFile(context.Background()) |
| 78 | + require.NoError(t, err) |
| 79 | + |
| 80 | + // Verify base config values are used |
| 81 | + assert.Equal(t, "base-token", config.Token) |
| 82 | + assert.Equal(t, "https://api.base.com", config.APIEndpoint) |
| 83 | + assert.Equal(t, "https://base.com", config.WebEndpoint) |
| 84 | + assert.Equal(t, 5, config.FlushCount) |
| 85 | + assert.Equal(t, 7, config.GCTime) |
| 86 | +} |
| 87 | + |
| 88 | +func TestReadConfigFileWithDifferentExtensions(t *testing.T) { |
| 89 | + testCases := []struct { |
| 90 | + name string |
| 91 | + configFile string |
| 92 | + localFile string |
| 93 | + }{ |
| 94 | + { |
| 95 | + name: "TOML files", |
| 96 | + configFile: "config.toml", |
| 97 | + localFile: "config.local.toml", |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "Custom config name", |
| 101 | + configFile: "shelltime-config.toml", |
| 102 | + localFile: "shelltime-config.local.toml", |
| 103 | + }, |
| 104 | + { |
| 105 | + name: "Different extension", |
| 106 | + configFile: "settings.conf", |
| 107 | + localFile: "settings.local.conf", |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + for _, tc := range testCases { |
| 112 | + t.Run(tc.name, func(t *testing.T) { |
| 113 | + // Create a temporary directory for test configs |
| 114 | + tmpDir, err := os.MkdirTemp("", "shelltime-test-*") |
| 115 | + require.NoError(t, err) |
| 116 | + defer os.RemoveAll(tmpDir) |
| 117 | + |
| 118 | + // Create base config file |
| 119 | + baseConfigPath := filepath.Join(tmpDir, tc.configFile) |
| 120 | + baseConfig := `Token = 'base-token' |
| 121 | +APIEndpoint = 'https://api.base.com' |
| 122 | +FlushCount = 5` |
| 123 | + err = os.WriteFile(baseConfigPath, []byte(baseConfig), 0644) |
| 124 | + require.NoError(t, err) |
| 125 | + |
| 126 | + // Create local config file |
| 127 | + localConfigPath := filepath.Join(tmpDir, tc.localFile) |
| 128 | + localConfig := `Token = 'local-token' |
| 129 | +FlushCount = 10` |
| 130 | + err = os.WriteFile(localConfigPath, []byte(localConfig), 0644) |
| 131 | + require.NoError(t, err) |
| 132 | + |
| 133 | + // Test reading config with local override |
| 134 | + cs := NewConfigService(baseConfigPath) |
| 135 | + config, err := cs.ReadConfigFile(context.Background()) |
| 136 | + require.NoError(t, err) |
| 137 | + |
| 138 | + // Verify local config overrides base config |
| 139 | + assert.Equal(t, "local-token", config.Token, "Token should be overridden by local config for %s", tc.name) |
| 140 | + assert.Equal(t, 10, config.FlushCount, "FlushCount should be overridden by local config for %s", tc.name) |
| 141 | + assert.Equal(t, "https://api.base.com", config.APIEndpoint, "APIEndpoint should keep base value for %s", tc.name) |
| 142 | + }) |
| 143 | + } |
| 144 | +} |
0 commit comments