ofsht 0.6.0

Git worktree management tool
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
//! Configuration module
//!
//! This module handles loading and managing ofsht configuration from TOML files.

pub mod loader;
pub mod schema;
pub mod template_generator;

// Re-export public types and functions
// Note: These are part of the public API and used in tests, even if not all are used in main.rs
#[allow(unused_imports)]
pub use schema::{
    Config, FzfConfig, GhConfig, HookActions, Hooks, IntegrationsConfig, TmuxBehavior, TmuxConfig,
    WorktreeConfig, ZoxideConfig,
};

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_config() {
        let config = Config::default();
        assert_eq!(config.worktree.dir, "../{repo}-worktrees/{branch}");
        assert!(config.hooks.create.run.is_empty());
        assert!(config.hooks.delete.run.is_empty());
        assert!(config.integrations.zoxide.enabled);
        assert!(config.integrations.fzf.enabled);
        assert_eq!(config.integrations.tmux.create, "window");
    }

    #[test]
    fn test_zoxide_config_default() {
        let config = ZoxideConfig::default();
        assert!(config.enabled);
    }

    #[test]
    fn test_zoxide_config_from_toml() {
        let toml = r"
            [integration.zoxide]
            enabled = false
        ";
        let config: Config = toml::from_str(toml).unwrap();
        assert!(!config.integrations.zoxide.enabled);
    }

    #[test]
    fn test_zoxide_config_missing_defaults_to_true() {
        let toml = r#"
            [worktree]
            dir = "/tmp/worktrees/{branch}"
        "#;
        let config: Config = toml::from_str(toml).unwrap();
        assert!(config.integrations.zoxide.enabled);
    }

    #[test]
    fn test_fzf_config_default() {
        let config = FzfConfig::default();
        assert!(config.enabled);
        assert!(config.options.is_empty());
    }

    #[test]
    fn test_fzf_config_from_toml() {
        let toml = r#"
            [integration.fzf]
            enabled = false
            options = ["--height=50%", "--border"]
        "#;
        let config: Config = toml::from_str(toml).unwrap();
        assert!(!config.integrations.fzf.enabled);
        assert_eq!(config.integrations.fzf.options.len(), 2);
    }

    #[test]
    fn test_tmux_config_default() {
        let config = TmuxConfig::default();
        assert_eq!(config.behavior, TmuxBehavior::Auto);
        assert_eq!(config.create, "window");
    }

    #[test]
    fn test_tmux_config_from_toml() {
        let toml = r#"
            [integration.tmux]
            behavior = "always"
            create = "pane"
        "#;
        let config: Config = toml::from_str(toml).unwrap();
        assert_eq!(config.integrations.tmux.behavior, TmuxBehavior::Always);
        assert_eq!(config.integrations.tmux.create, "pane");
    }

    #[test]
    fn test_tmux_config_missing_defaults_to_window() {
        let toml = r#"
            [integration.tmux]
            behavior = "never"
        "#;
        let config: Config = toml::from_str(toml).unwrap();
        assert_eq!(config.integrations.tmux.create, "window");
    }

    #[test]
    fn test_tmux_config_behavior_auto() {
        let toml = r#"
            [integration.tmux]
            behavior = "auto"
        "#;
        let config: Config = toml::from_str(toml).unwrap();
        assert_eq!(config.integrations.tmux.behavior, TmuxBehavior::Auto);
    }

    #[test]
    fn test_tmux_config_behavior_always() {
        let toml = r#"
            [integration.tmux]
            behavior = "always"
        "#;
        let config: Config = toml::from_str(toml).unwrap();
        assert_eq!(config.integrations.tmux.behavior, TmuxBehavior::Always);
    }

    #[test]
    fn test_tmux_config_behavior_never() {
        let toml = r#"
            [integration.tmux]
            behavior = "never"
        "#;
        let config: Config = toml::from_str(toml).unwrap();
        assert_eq!(config.integrations.tmux.behavior, TmuxBehavior::Never);
    }

    #[test]
    fn test_tmux_behavior_invalid_value() {
        let toml = r#"
            [integration.tmux]
            behavior = "invalid"
        "#;
        let result: Result<Config, _> = toml::from_str(toml);
        assert!(result.is_err());
    }

    #[test]
    fn test_gh_config_default() {
        let config = GhConfig::default();
        assert!(config.enabled);
    }

    #[test]
    fn test_gh_config_from_toml() {
        let toml = r"
            [integration.gh]
            enabled = false
        ";
        let config: Config = toml::from_str(toml).unwrap();
        assert!(!config.integrations.gh.enabled);
    }

    #[test]
    fn test_gh_config_enabled_by_default() {
        let toml = r#"
            [worktree]
            dir = "/tmp"
        "#;
        let config: Config = toml::from_str(toml).unwrap();
        assert!(config.integrations.gh.enabled);
    }

    #[test]
    fn test_integrations_default() {
        let config = IntegrationsConfig::default();
        assert!(config.zoxide.enabled);
        assert!(config.fzf.enabled);
        assert_eq!(config.tmux.behavior, TmuxBehavior::Auto);
        assert_eq!(config.tmux.create, "window");
        assert!(config.gh.enabled);
    }

    #[test]
    fn test_integrations_config_from_toml() {
        let toml = r#"
            [integration.zoxide]
            enabled = false

            [integration.fzf]
            enabled = true
            options = ["--height=100%"]

            [integration.tmux]
            behavior = "always"
            create = "pane"

            [integration.gh]
            enabled = true
        "#;
        let config: Config = toml::from_str(toml).unwrap();
        assert!(!config.integrations.zoxide.enabled);
        assert!(config.integrations.fzf.enabled);
        assert_eq!(config.integrations.fzf.options, vec!["--height=100%"]);
        assert_eq!(config.integrations.tmux.behavior, TmuxBehavior::Always);
        assert_eq!(config.integrations.tmux.create, "pane");
        assert!(config.integrations.gh.enabled);
    }

    #[test]
    fn test_local_config_path() {
        let path = Config::local_config_path();
        assert_eq!(path, std::path::PathBuf::from(".ofsht.toml"));
    }

    #[test]
    fn test_local_config_path_from_repo_root() {
        let repo_root = std::path::PathBuf::from("/tmp/my-repo");
        let path = Config::local_config_path_from(&repo_root);
        assert_eq!(path, std::path::PathBuf::from("/tmp/my-repo/.ofsht.toml"));
    }

    #[test]
    fn test_global_config_path_is_public() {
        // This test just ensures the method is public
        let _ = Config::global_config_path();
    }

    #[test]
    #[serial_test::serial]
    fn test_global_config_path_default() {
        // Clear XDG_CONFIG_HOME to test default behavior
        std::env::remove_var("XDG_CONFIG_HOME");
        if let Some(path) = Config::global_config_path() {
            assert!(path.ends_with(".config/ofsht/config.toml"));
        }
    }

    #[test]
    #[serial_test::serial]
    fn test_global_config_path_with_xdg_env() {
        // Set XDG_CONFIG_HOME to an absolute path
        let xdg_path = std::env::temp_dir().join("xdg_config");
        std::env::set_var("XDG_CONFIG_HOME", &xdg_path);

        let path = Config::global_config_path();
        assert_eq!(path, Some(xdg_path.join("ofsht/config.toml")));

        // Clean up
        std::env::remove_var("XDG_CONFIG_HOME");
    }

    #[test]
    #[serial_test::serial]
    fn test_global_config_path_relative_xdg_ignored() {
        // Set XDG_CONFIG_HOME to a relative path (should be ignored)
        std::env::set_var("XDG_CONFIG_HOME", "relative/path");

        if let Some(path) = Config::global_config_path() {
            // Should fall back to default (~/.config/ofsht/config.toml)
            assert!(path.ends_with(".config/ofsht/config.toml"));
        }

        // Clean up
        std::env::remove_var("XDG_CONFIG_HOME");
    }

    #[test]
    fn test_load_from_repo_root_fallback_to_global() {
        // Create a temporary repo root without .ofsht.toml
        let temp_dir = std::env::temp_dir().join("ofsht_test_repo");
        std::fs::create_dir_all(&temp_dir).ok();

        // Should fall back to global or default config
        let result = Config::load_from_repo_root(&temp_dir);
        assert!(result.is_ok());

        // Clean up
        std::fs::remove_dir_all(&temp_dir).ok();
    }

    #[test]
    fn test_load_from_specific_repo_root() {
        // Create a temporary repo root with .ofsht.toml
        let temp_dir = std::env::temp_dir().join("ofsht_test_repo_specific");
        std::fs::create_dir_all(&temp_dir).ok();

        let config_path = temp_dir.join(".ofsht.toml");
        std::fs::write(
            &config_path,
            r#"
                [worktree]
                dir = "/tmp/custom/{branch}"
            "#,
        )
        .ok();

        let result = Config::load_from_repo_root(&temp_dir);
        assert!(result.is_ok());
        assert_eq!(result.unwrap().worktree.dir, "/tmp/custom/{branch}");

        // Clean up
        std::fs::remove_dir_all(&temp_dir).ok();
    }

    #[test]
    fn test_merge_configs() {
        let base = Config {
            worktree: WorktreeConfig {
                dir: "/base/{branch}".to_string(),
            },
            hooks: Hooks::default(),
            integrations: IntegrationsConfig::default(),
        };

        let override_config = Config {
            worktree: WorktreeConfig {
                dir: "/override/{branch}".to_string(),
            },
            hooks: Hooks::default(),
            integrations: IntegrationsConfig::default(),
        };

        let merged = base.merge(&override_config);
        assert_eq!(merged.worktree.dir, "/override/{branch}");
    }

    #[test]
    fn test_local_config_ignores_integrations() {
        let temp_dir = std::env::temp_dir().join("ofsht_test_local_integrations");
        std::fs::create_dir_all(&temp_dir).ok();

        let config_path = temp_dir.join(".ofsht.toml");
        std::fs::write(
            &config_path,
            r#"
                [worktree]
                dir = "/tmp/{branch}"

                [integration.zoxide]
                enabled = false

                [integration.fzf]
                enabled = false
            "#,
        )
        .ok();

        let result = Config::load_from_repo_root(&temp_dir);
        assert!(result.is_ok());
        let config = result.unwrap();

        // Worktree config should be loaded from local
        assert_eq!(config.worktree.dir, "/tmp/{branch}");

        // Integration config should be from global (or defaults)
        // Since we don't have a global config in test, it should use defaults
        assert!(config.integrations.zoxide.enabled); // Default is true
        assert!(config.integrations.fzf.enabled); // Default is true

        // Clean up
        std::fs::remove_dir_all(&temp_dir).ok();
    }

    #[test]
    fn test_local_config_uses_global_integration_settings() {
        let temp_dir = std::env::temp_dir().join("ofsht_test_integration_override");
        std::fs::create_dir_all(&temp_dir).ok();

        let local_config_path = temp_dir.join(".ofsht.toml");
        std::fs::write(
            &local_config_path,
            r#"
                [worktree]
                dir = "/local/{branch}"

                [integration.zoxide]
                enabled = false
            "#,
        )
        .ok();

        let config = Config::load_from_repo_root(&temp_dir).unwrap();

        // Local worktree settings should be used
        assert_eq!(config.worktree.dir, "/local/{branch}");

        // Integration settings should be ignored from local config
        // and should use global config or defaults
        assert!(config.integrations.zoxide.enabled); // Default

        // Clean up
        std::fs::remove_dir_all(&temp_dir).ok();
    }

    #[test]
    #[serial_test::serial]
    fn test_local_config_without_global_uses_defaults() {
        // Temporarily override XDG_CONFIG_HOME to a non-existent path
        let fake_xdg = std::env::temp_dir().join("fake_xdg_no_global");
        std::env::set_var("XDG_CONFIG_HOME", &fake_xdg);

        let temp_dir = std::env::temp_dir().join("ofsht_test_no_global");
        std::fs::create_dir_all(&temp_dir).ok();

        let local_config_path = temp_dir.join(".ofsht.toml");
        std::fs::write(
            &local_config_path,
            r#"
                [worktree]
                dir = "/no-global/{branch}"
            "#,
        )
        .ok();

        let config = Config::load_from_repo_root(&temp_dir).unwrap();

        // Local worktree settings
        assert_eq!(config.worktree.dir, "/no-global/{branch}");

        // Integration settings should use defaults (no global config)
        assert!(config.integrations.zoxide.enabled);
        assert!(config.integrations.fzf.enabled);

        // Clean up
        std::env::remove_var("XDG_CONFIG_HOME");
        std::fs::remove_dir_all(&temp_dir).ok();
    }

    #[test]
    fn test_local_config_ignores_zoxide() {
        let temp_dir = std::env::temp_dir().join("ofsht_test_zoxide_ignore");
        std::fs::create_dir_all(&temp_dir).ok();

        let config_path = temp_dir.join(".ofsht.toml");
        std::fs::write(
            &config_path,
            r"
                [integration.zoxide]
                enabled = false
            ",
        )
        .ok();

        let config = Config::load_from_repo_root(&temp_dir).unwrap();
        // Should use global or default (true), not local setting
        assert!(config.integrations.zoxide.enabled);

        // Clean up
        std::fs::remove_dir_all(&temp_dir).ok();
    }

    #[test]
    fn test_local_config_ignores_gh() {
        let temp_dir = std::env::temp_dir().join("ofsht_test_gh_ignore");
        std::fs::create_dir_all(&temp_dir).ok();

        let config_path = temp_dir.join(".ofsht.toml");
        std::fs::write(
            &config_path,
            r"
                [integration.gh]
                enabled = false
            ",
        )
        .ok();

        let config = Config::load_from_repo_root(&temp_dir).unwrap();
        // Should use global or default (true), not local setting
        assert!(config.integrations.gh.enabled);

        // Clean up
        std::fs::remove_dir_all(&temp_dir).ok();
    }

    #[test]
    fn test_template_global_is_valid_toml() {
        let ctx = template_generator::TemplateContext {
            gh_available: true,
            zoxide_available: true,
            fzf_available: true,
            tmux_available: true,
        };
        let template = ctx.generate_global();
        let result: Result<Config, _> = toml::from_str(&template);
        assert!(result.is_ok());
    }

    #[test]
    fn test_template_local_is_valid_toml() {
        let ctx = template_generator::TemplateContext {
            gh_available: true,
            zoxide_available: true,
            fzf_available: true,
            tmux_available: true,
        };
        let template = ctx.generate_local();
        let result: Result<Config, _> = toml::from_str(&template);
        assert!(result.is_ok());
    }

    #[test]
    fn test_template_global_contains_all_sections() {
        let ctx = template_generator::TemplateContext {
            gh_available: true,
            zoxide_available: true,
            fzf_available: true,
            tmux_available: true,
        };
        let template = ctx.generate_global();
        assert!(template.contains("[worktree]"));
        assert!(template.contains("[hooks.create]"));
        assert!(template.contains("[hooks.delete]"));
        assert!(template.contains("[integration.zoxide]"));
        assert!(template.contains("[integration.fzf]"));
        assert!(template.contains("[integration.tmux]"));
        assert!(template.contains("[integration.gh]"));
    }

    #[test]
    fn test_template_local_contains_all_sections() {
        let ctx = template_generator::TemplateContext {
            gh_available: true,
            zoxide_available: true,
            fzf_available: true,
            tmux_available: true,
        };
        let template = ctx.generate_local();
        // Local config should NOT contain [worktree] section
        assert!(!template.contains("[worktree]"));
        assert!(template.contains("[hooks.create]"));
        assert!(template.contains("[hooks.delete]"));
        // Should NOT contain integration sections
        assert!(!template.contains("[integration.zoxide]"));
        assert!(!template.contains("[integration.fzf]"));
        assert!(!template.contains("[integration.tmux]"));
        assert!(!template.contains("[integration.gh]"));
    }

    #[test]
    fn test_template_global_has_explanatory_comments() {
        let ctx = template_generator::TemplateContext {
            gh_available: true,
            zoxide_available: true,
            fzf_available: true,
            tmux_available: true,
        };
        let template = ctx.generate_global();
        assert!(template.contains("# Variables:"));
    }
}