@@ -142,3 +142,162 @@ FlushCount = 10`
142142 })
143143 }
144144}
145+
146+ func TestLogCleanupDefaults (t * testing.T ) {
147+ // Create a temporary directory for test configs
148+ tmpDir , err := os .MkdirTemp ("" , "shelltime-test-*" )
149+ require .NoError (t , err )
150+ defer os .RemoveAll (tmpDir )
151+
152+ // Create config file without LogCleanup section
153+ baseConfigPath := filepath .Join (tmpDir , "config.toml" )
154+ baseConfig := `Token = 'test-token'
155+ APIEndpoint = 'https://api.test.com'`
156+ err = os .WriteFile (baseConfigPath , []byte (baseConfig ), 0644 )
157+ require .NoError (t , err )
158+
159+ cs := NewConfigService (baseConfigPath )
160+ config , err := cs .ReadConfigFile (context .Background ())
161+ require .NoError (t , err )
162+
163+ // Verify LogCleanup defaults are applied
164+ require .NotNil (t , config .LogCleanup , "LogCleanup should be initialized with defaults" )
165+ assert .True (t , * config .LogCleanup .Enabled , "LogCleanup.Enabled should default to true" )
166+ assert .Equal (t , int64 (100 ), config .LogCleanup .ThresholdMB , "LogCleanup.ThresholdMB should default to 100" )
167+ }
168+
169+ func TestLogCleanupCustomValues (t * testing.T ) {
170+ // Create a temporary directory for test configs
171+ tmpDir , err := os .MkdirTemp ("" , "shelltime-test-*" )
172+ require .NoError (t , err )
173+ defer os .RemoveAll (tmpDir )
174+
175+ // Create config file with custom LogCleanup settings
176+ baseConfigPath := filepath .Join (tmpDir , "config.toml" )
177+ baseConfig := `Token = 'test-token'
178+ APIEndpoint = 'https://api.test.com'
179+
180+ [logCleanup]
181+ enabled = false
182+ thresholdMB = 200`
183+ err = os .WriteFile (baseConfigPath , []byte (baseConfig ), 0644 )
184+ require .NoError (t , err )
185+
186+ cs := NewConfigService (baseConfigPath )
187+ config , err := cs .ReadConfigFile (context .Background ())
188+ require .NoError (t , err )
189+
190+ // Verify custom LogCleanup values are used
191+ require .NotNil (t , config .LogCleanup , "LogCleanup should be present" )
192+ assert .False (t , * config .LogCleanup .Enabled , "LogCleanup.Enabled should be false" )
193+ assert .Equal (t , int64 (200 ), config .LogCleanup .ThresholdMB , "LogCleanup.ThresholdMB should be 200" )
194+ }
195+
196+ func TestLogCleanupPartialConfig (t * testing.T ) {
197+ testCases := []struct {
198+ name string
199+ config string
200+ expectedEnabled bool
201+ expectedThreshold int64
202+ }{
203+ {
204+ name : "Only enabled set to false" ,
205+ config : `Token = 'test-token'
206+ [logCleanup]
207+ enabled = false` ,
208+ expectedEnabled : false ,
209+ expectedThreshold : 100 , // default
210+ },
211+ {
212+ name : "Only threshold set" ,
213+ config : `Token = 'test-token'
214+ [logCleanup]
215+ thresholdMB = 50` ,
216+ expectedEnabled : true , // default
217+ expectedThreshold : 50 ,
218+ },
219+ }
220+
221+ for _ , tc := range testCases {
222+ t .Run (tc .name , func (t * testing.T ) {
223+ tmpDir , err := os .MkdirTemp ("" , "shelltime-test-*" )
224+ require .NoError (t , err )
225+ defer os .RemoveAll (tmpDir )
226+
227+ baseConfigPath := filepath .Join (tmpDir , "config.toml" )
228+ err = os .WriteFile (baseConfigPath , []byte (tc .config ), 0644 )
229+ require .NoError (t , err )
230+
231+ cs := NewConfigService (baseConfigPath )
232+ config , err := cs .ReadConfigFile (context .Background ())
233+ require .NoError (t , err )
234+
235+ require .NotNil (t , config .LogCleanup , "LogCleanup should be present" )
236+ assert .Equal (t , tc .expectedEnabled , * config .LogCleanup .Enabled , "LogCleanup.Enabled mismatch" )
237+ assert .Equal (t , tc .expectedThreshold , config .LogCleanup .ThresholdMB , "LogCleanup.ThresholdMB mismatch" )
238+ })
239+ }
240+ }
241+
242+ func TestLogCleanupMergeFromLocal (t * testing.T ) {
243+ // Create a temporary directory for test configs
244+ tmpDir , err := os .MkdirTemp ("" , "shelltime-test-*" )
245+ require .NoError (t , err )
246+ defer os .RemoveAll (tmpDir )
247+
248+ // Create base config file with LogCleanup
249+ baseConfigPath := filepath .Join (tmpDir , "config.toml" )
250+ baseConfig := `Token = 'base-token'
251+ [logCleanup]
252+ enabled = true
253+ thresholdMB = 100`
254+ err = os .WriteFile (baseConfigPath , []byte (baseConfig ), 0644 )
255+ require .NoError (t , err )
256+
257+ // Create local config file that overrides LogCleanup
258+ localConfigPath := filepath .Join (tmpDir , "config.local.toml" )
259+ localConfig := `[logCleanup]
260+ enabled = false
261+ thresholdMB = 500`
262+ err = os .WriteFile (localConfigPath , []byte (localConfig ), 0644 )
263+ require .NoError (t , err )
264+
265+ cs := NewConfigService (baseConfigPath )
266+ config , err := cs .ReadConfigFile (context .Background ())
267+ require .NoError (t , err )
268+
269+ // Verify local config overrides base LogCleanup
270+ require .NotNil (t , config .LogCleanup , "LogCleanup should be present" )
271+ assert .False (t , * config .LogCleanup .Enabled , "LogCleanup.Enabled should be overridden by local config" )
272+ assert .Equal (t , int64 (500 ), config .LogCleanup .ThresholdMB , "LogCleanup.ThresholdMB should be overridden by local config" )
273+ }
274+
275+ func TestCodeTrackingMergeFromLocal (t * testing.T ) {
276+ // Create a temporary directory for test configs
277+ tmpDir , err := os .MkdirTemp ("" , "shelltime-test-*" )
278+ require .NoError (t , err )
279+ defer os .RemoveAll (tmpDir )
280+
281+ // Create base config file with CodeTracking disabled
282+ baseConfigPath := filepath .Join (tmpDir , "config.toml" )
283+ baseConfig := `Token = 'base-token'
284+ [codeTracking]
285+ enabled = false`
286+ err = os .WriteFile (baseConfigPath , []byte (baseConfig ), 0644 )
287+ require .NoError (t , err )
288+
289+ // Create local config file that enables CodeTracking
290+ localConfigPath := filepath .Join (tmpDir , "config.local.toml" )
291+ localConfig := `[codeTracking]
292+ enabled = true`
293+ err = os .WriteFile (localConfigPath , []byte (localConfig ), 0644 )
294+ require .NoError (t , err )
295+
296+ cs := NewConfigService (baseConfigPath )
297+ config , err := cs .ReadConfigFile (context .Background ())
298+ require .NoError (t , err )
299+
300+ // Verify local config overrides base CodeTracking
301+ require .NotNil (t , config .CodeTracking , "CodeTracking should be present" )
302+ assert .True (t , * config .CodeTracking .Enabled , "CodeTracking.Enabled should be overridden by local config" )
303+ }
0 commit comments