osp-cli 1.5.1

CLI and REPL for querying and managing OSP infrastructure data
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
use serde::Deserialize;
use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};

use crate::config::{ConfigSource, ResolvedConfig};
use crate::ui::style::is_valid_style_spec;
use crate::ui::theme::{
    ThemeDefinition, ThemeOverrides, ThemePalette, builtin_themes, display_name_from_id,
    find_builtin_theme, normalize_theme_name,
};

#[derive(Debug, Clone)]
pub(crate) struct ThemeLoadIssue {
    pub(crate) path: PathBuf,
    pub(crate) message: String,
}

#[derive(Debug, Clone, Default)]
pub(crate) struct ThemeCatalog {
    pub(crate) entries: BTreeMap<String, ThemeEntry>,
    pub(crate) issues: Vec<ThemeLoadIssue>,
}

impl ThemeCatalog {
    pub(crate) fn ids(&self) -> Vec<String> {
        self.entries.keys().cloned().collect()
    }

    pub(crate) fn resolve(&self, input: &str) -> Option<&ThemeEntry> {
        let normalized = normalize_theme_name(input);
        if normalized.is_empty() {
            return None;
        }
        self.entries.get(&normalized)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ThemeSource {
    Builtin,
    Custom,
}

#[derive(Debug, Clone)]
pub(crate) struct ThemeEntry {
    pub(crate) theme: ThemeDefinition,
    pub(crate) source: ThemeSource,
    pub(crate) origin: Option<PathBuf>,
}

#[derive(Debug, Clone, Default)]
struct CustomThemeLoad {
    themes: Vec<ThemeDefinition>,
    origins: BTreeMap<String, PathBuf>,
    issues: Vec<ThemeLoadIssue>,
}

#[derive(Debug, Clone)]
struct ThemeSpec {
    id: String,
    name: String,
    base: Option<String>,
    palette: ThemePaletteFile,
    overrides: ThemeOverrides,
}

struct ThemePathSelection {
    paths: Vec<PathBuf>,
    explicit: bool,
}

pub(crate) fn load_theme_catalog(config: &ResolvedConfig) -> ThemeCatalog {
    let custom = load_custom_themes(config);
    let mut entries: BTreeMap<String, ThemeEntry> = BTreeMap::new();
    for theme in builtin_themes() {
        entries.insert(
            theme.id.clone(),
            ThemeEntry {
                theme,
                source: ThemeSource::Builtin,
                origin: None,
            },
        );
    }

    let mut issues = custom.issues;
    for theme in custom.themes {
        let origin = custom.origins.get(&theme.id).cloned();
        if let Some(path) = origin.clone()
            && entries.contains_key(&theme.id)
        {
            issues.push(ThemeLoadIssue {
                path,
                message: format!("custom theme overrides builtin: {}", theme.id),
            });
        }
        entries.insert(
            theme.id.clone(),
            ThemeEntry {
                theme,
                source: ThemeSource::Custom,
                origin,
            },
        );
    }

    ThemeCatalog { entries, issues }
}

fn load_custom_themes(config: &ResolvedConfig) -> CustomThemeLoad {
    let mut issues = Vec::new();
    let mut specs: BTreeMap<String, ThemeSpec> = BTreeMap::new();
    let mut origins: BTreeMap<String, PathBuf> = BTreeMap::new();

    let selection = resolve_theme_paths(config);
    for dir in selection.paths {
        if !dir.is_dir() {
            if selection.explicit {
                issues.push(ThemeLoadIssue {
                    path: dir,
                    message: "theme path is not a directory".to_string(),
                });
            }
            continue;
        }

        let mut entries = match fs::read_dir(&dir) {
            Ok(entries) => entries
                .filter_map(|entry| entry.ok())
                .map(|entry| entry.path())
                .collect::<Vec<_>>(),
            Err(err) => {
                if selection.explicit {
                    issues.push(ThemeLoadIssue {
                        path: dir,
                        message: format!("failed to read theme directory: {err}"),
                    });
                }
                continue;
            }
        };
        entries.sort();

        for path in entries {
            if path.extension().and_then(|ext| ext.to_str()) != Some("toml") {
                continue;
            }

            match parse_theme_spec(&path) {
                Ok(spec) => {
                    let theme = match resolve_theme_spec(
                        &spec.id,
                        &specs,
                        &mut BTreeMap::new(),
                        &mut Vec::new(),
                    ) {
                        Ok(theme) => apply_theme_overrides(theme, &spec),
                        Err(_) => {
                            // The full recursive resolve runs after all specs are known.
                            // Validate only the direct patch values at parse time here.
                            apply_theme_overrides(
                                empty_theme(&spec.id, &spec.name, spec.base.clone()),
                                &spec,
                            )
                        }
                    };
                    for message in validate_theme_specs(&theme) {
                        issues.push(ThemeLoadIssue {
                            path: path.clone(),
                            message,
                        });
                    }
                    if let Some(existing) = origins.get(&spec.id) {
                        issues.push(ThemeLoadIssue {
                            path: path.clone(),
                            message: format!(
                                "theme id collision: {} overridden (previous: {})",
                                spec.id,
                                existing.display()
                            ),
                        });
                    }
                    origins.insert(spec.id.clone(), path.clone());
                    specs.insert(spec.id.clone(), spec);
                }
                Err(err) => {
                    issues.push(ThemeLoadIssue { path, message: err });
                }
            }
        }
    }

    let mut resolved = BTreeMap::new();
    for id in specs.keys().cloned().collect::<Vec<_>>() {
        let mut stack = Vec::new();
        match resolve_theme_spec(&id, &specs, &mut resolved, &mut stack) {
            Ok(_) => {}
            Err(message) => {
                if let Some(path) = origins.get(&id).cloned() {
                    issues.push(ThemeLoadIssue { path, message });
                }
            }
        }
    }

    CustomThemeLoad {
        themes: resolved.into_values().collect(),
        origins,
        issues,
    }
}

pub(crate) fn log_theme_issues(issues: &[ThemeLoadIssue]) {
    for issue in issues {
        tracing::warn!(path = %issue.path.display(), "{message}", message = issue.message);
    }
}

fn resolve_theme_paths(config: &ResolvedConfig) -> ThemePathSelection {
    if let Some(paths) = config.get_string_list("theme.path") {
        let explicit = config
            .get_value_entry("theme.path")
            .map(|entry| {
                !matches!(
                    entry.source,
                    ConfigSource::BuiltinDefaults | ConfigSource::Derived
                )
            })
            .unwrap_or(false);
        return ThemePathSelection {
            paths: normalize_theme_paths(paths),
            explicit,
        };
    }
    ThemePathSelection {
        paths: default_theme_paths(),
        explicit: false,
    }
}

fn normalize_theme_paths(paths: Vec<String>) -> Vec<PathBuf> {
    paths
        .into_iter()
        .filter_map(|raw| expand_theme_path(&raw))
        .collect()
}

fn expand_theme_path(raw: &str) -> Option<PathBuf> {
    let trimmed = raw.trim();
    if trimmed.is_empty() {
        return None;
    }

    if trimmed == "~" {
        return crate::config::default_home_dir();
    }

    if let Some(home) = crate::config::default_home_dir()
        && let Some(stripped) = trimmed
            .strip_prefix("~/")
            .or_else(|| trimmed.strip_prefix("~\\"))
    {
        return Some(home.join(stripped));
    }

    Some(PathBuf::from(trimmed))
}

fn default_theme_paths() -> Vec<PathBuf> {
    crate::config::default_config_root_dir()
        .map(|mut root| {
            root.push("themes");
            root
        })
        .into_iter()
        .collect()
}

#[derive(Debug, Deserialize)]
struct ThemeFile {
    base: Option<String>,
    id: Option<String>,
    name: Option<String>,
    palette: Option<ThemePaletteFile>,
    #[serde(default)]
    overrides: ThemeOverridesFile,
}

#[derive(Debug, Clone, Deserialize, Default)]
struct ThemePaletteFile {
    text: Option<String>,
    muted: Option<String>,
    accent: Option<String>,
    info: Option<String>,
    warning: Option<String>,
    success: Option<String>,
    error: Option<String>,
    border: Option<String>,
    title: Option<String>,
    selection: Option<String>,
    link: Option<String>,
    bg: Option<String>,
    bg_alt: Option<String>,
}

#[derive(Debug, Deserialize, Default)]
struct ThemeOverridesFile {
    value_number: Option<String>,
    repl_completion_text: Option<String>,
    repl_completion_background: Option<String>,
    repl_completion_highlight: Option<String>,
}

fn parse_theme_spec(path: &Path) -> Result<ThemeSpec, String> {
    let raw =
        fs::read_to_string(path).map_err(|err| format!("failed to read theme file: {err}"))?;
    let parsed: ThemeFile =
        toml::from_str(&raw).map_err(|err| format!("failed to parse toml: {err}"))?;

    let stem = path
        .file_stem()
        .and_then(|value| value.to_str())
        .unwrap_or_default();
    let mut id = parsed
        .id
        .as_deref()
        .map(normalize_theme_name)
        .unwrap_or_default();
    if id.is_empty() {
        id = normalize_theme_name(stem);
    }
    if id.is_empty() {
        return Err("theme id is empty".to_string());
    }

    let name = parsed
        .name
        .as_deref()
        .map(str::trim)
        .filter(|value| !value.is_empty())
        .map(str::to_string)
        .unwrap_or_else(|| display_name_from_id(&id));

    let base = parsed
        .base
        .as_deref()
        .map(normalize_theme_name)
        .filter(|value| !value.is_empty())
        .filter(|value| value != "none");

    let overrides = ThemeOverrides {
        value_number: parsed.overrides.value_number,
        repl_completion_text: parsed.overrides.repl_completion_text,
        repl_completion_background: parsed.overrides.repl_completion_background,
        repl_completion_highlight: parsed.overrides.repl_completion_highlight,
    };

    Ok(ThemeSpec {
        id,
        name,
        base,
        palette: parsed.palette.unwrap_or_default(),
        overrides,
    })
}

fn resolve_theme_spec(
    id: &str,
    specs: &BTreeMap<String, ThemeSpec>,
    resolved: &mut BTreeMap<String, ThemeDefinition>,
    stack: &mut Vec<String>,
) -> Result<ThemeDefinition, String> {
    if let Some(theme) = resolved.get(id) {
        return Ok(theme.clone());
    }
    if stack.iter().any(|entry| entry == id) {
        stack.push(id.to_string());
        return Err(format!("theme base cycle detected: {}", stack.join(" -> ")));
    }

    let spec = specs
        .get(id)
        .ok_or_else(|| format!("theme missing during resolution: {id}"))?;
    stack.push(id.to_string());

    let base_theme = match spec.base.as_deref() {
        Some(base) if specs.contains_key(base) => {
            Some(resolve_theme_spec(base, specs, resolved, stack)?)
        }
        Some(base) => find_builtin_theme(base)
            .ok_or_else(|| format!("unknown base theme: {base}"))
            .map(Some)?,
        None => None,
    };

    let theme = apply_theme_overrides(
        base_theme.unwrap_or_else(|| empty_theme(&spec.id, &spec.name, spec.base.clone())),
        spec,
    );
    stack.pop();
    resolved.insert(id.to_string(), theme.clone());
    Ok(theme)
}

fn empty_theme(id: &str, name: &str, base: Option<String>) -> ThemeDefinition {
    ThemeDefinition::new(id, name, base, empty_palette(), ThemeOverrides::default())
}

fn apply_theme_overrides(theme: ThemeDefinition, spec: &ThemeSpec) -> ThemeDefinition {
    let mut palette = theme.palette.clone();
    if let Some(value) = spec.palette.text.as_ref() {
        palette.text = value.clone();
    }
    if let Some(value) = spec.palette.muted.as_ref() {
        palette.muted = value.clone();
    }
    if let Some(value) = spec.palette.accent.as_ref() {
        palette.accent = value.clone();
    }
    if let Some(value) = spec.palette.info.as_ref() {
        palette.info = value.clone();
    }
    if let Some(value) = spec.palette.warning.as_ref() {
        palette.warning = value.clone();
    }
    if let Some(value) = spec.palette.success.as_ref() {
        palette.success = value.clone();
    }
    if let Some(value) = spec.palette.error.as_ref() {
        palette.error = value.clone();
    }
    if let Some(value) = spec.palette.border.as_ref() {
        palette.border = value.clone();
    }
    if let Some(value) = spec.palette.title.as_ref() {
        palette.title = value.clone();
    }
    if let Some(value) = spec.palette.selection.as_ref() {
        palette.selection = value.clone();
    }
    if let Some(value) = spec.palette.link.as_ref() {
        palette.link = value.clone();
    }
    if let Some(value) = spec.palette.bg.as_ref() {
        palette.bg = Some(value.clone());
    }
    if let Some(value) = spec.palette.bg_alt.as_ref() {
        palette.bg_alt = Some(value.clone());
    }

    ThemeDefinition::new(
        spec.id.clone(),
        spec.name.clone(),
        spec.base.clone(),
        palette,
        spec.overrides.clone(),
    )
}

fn validate_theme_specs(theme: &ThemeDefinition) -> Vec<String> {
    let mut issues = Vec::new();

    check_spec(&mut issues, "palette.text", &theme.palette.text);
    check_spec(&mut issues, "palette.muted", &theme.palette.muted);
    check_spec(&mut issues, "palette.accent", &theme.palette.accent);
    check_spec(&mut issues, "palette.info", &theme.palette.info);
    check_spec(&mut issues, "palette.warning", &theme.palette.warning);
    check_spec(&mut issues, "palette.success", &theme.palette.success);
    check_spec(&mut issues, "palette.error", &theme.palette.error);
    check_spec(&mut issues, "palette.border", &theme.palette.border);
    check_spec(&mut issues, "palette.title", &theme.palette.title);
    check_spec(&mut issues, "palette.selection", &theme.palette.selection);
    check_spec(&mut issues, "palette.link", &theme.palette.link);
    if let Some(value) = &theme.palette.bg {
        check_spec(&mut issues, "palette.bg", value);
    }
    if let Some(value) = &theme.palette.bg_alt {
        check_spec(&mut issues, "palette.bg_alt", value);
    }
    if let Some(value) = &theme.overrides.value_number {
        check_spec(&mut issues, "overrides.value_number", value);
    }
    if let Some(value) = &theme.overrides.repl_completion_text {
        check_spec(&mut issues, "overrides.repl_completion_text", value);
    }
    if let Some(value) = &theme.overrides.repl_completion_background {
        check_spec(&mut issues, "overrides.repl_completion_background", value);
    }
    if let Some(value) = &theme.overrides.repl_completion_highlight {
        check_spec(&mut issues, "overrides.repl_completion_highlight", value);
    }

    issues
}

fn check_spec(issues: &mut Vec<String>, key: &str, value: &str) {
    if is_valid_color_spec(value) {
        return;
    }
    issues.push(format!("invalid color spec for {key}: {value}"));
}

fn is_valid_color_spec(value: &str) -> bool {
    is_valid_style_spec(value)
}

fn empty_palette() -> ThemePalette {
    ThemePalette {
        text: String::new(),
        muted: String::new(),
        accent: String::new(),
        info: String::new(),
        warning: String::new(),
        success: String::new(),
        error: String::new(),
        border: String::new(),
        title: String::new(),
        selection: String::new(),
        link: String::new(),
        bg: None,
        bg_alt: None,
    }
}

#[cfg(test)]
mod tests {
    use super::{
        ThemeCatalog, ThemePaletteFile, ThemeSource, ThemeSpec, apply_theme_overrides,
        default_theme_paths, empty_theme, expand_theme_path, is_valid_color_spec,
        load_theme_catalog, log_theme_issues, normalize_theme_paths, parse_theme_spec,
        resolve_theme_spec,
    };
    use crate::config::{ConfigLayer, ConfigResolver, ResolveOptions};
    use std::collections::BTreeMap;
    use std::fs;
    use std::path::Path;
    use std::sync::Mutex;

    fn env_lock() -> &'static Mutex<()> {
        crate::tests::env_lock()
    }

    fn unique_temp_dir(prefix: &str) -> crate::tests::TestTempDir {
        crate::tests::make_temp_dir(prefix)
    }

    fn resolved_config_with_theme_paths(paths: Vec<String>) -> crate::config::ResolvedConfig {
        let mut defaults = ConfigLayer::default();
        defaults.set("profile.default", "default");
        let mut file = ConfigLayer::default();
        file.set("theme.path", paths);

        let mut resolver = ConfigResolver::default();
        resolver.set_defaults(defaults);
        resolver.set_file(file);
        resolver
            .resolve(ResolveOptions::default().with_terminal("cli"))
            .expect("theme test config should resolve")
    }

    #[test]
    fn theme_file_defaults_id_and_name_from_file_stem() {
        let dir = unique_temp_dir("osp-theme-loader-test");
        let path = dir.join("solarized-dark.toml");
        fs::write(
            &path,
            r##"
[palette]
text = "#eee8d5"
muted = "#93a1a1"
accent = "#268bd2"
info = "#2aa198"
warning = "#b58900"
success = "#859900"
error = "bold #dc322f"
border = "#586e75"
title = "#586e75"
"##,
        )
        .expect("theme file should be written");

        let spec = parse_theme_spec(&path).expect("theme should parse");
        let theme =
            apply_theme_overrides(empty_theme(&spec.id, &spec.name, spec.base.clone()), &spec);
        assert_eq!(theme.id, "solarized-dark");
        assert_eq!(theme.name, "Solarized Dark");
    }

    #[test]
    fn theme_file_inherits_from_base() {
        let dir = unique_temp_dir("osp-theme-loader-test-base");
        let path = dir.join("custom.toml");
        fs::write(
            &path,
            r##"
base = "dracula"

[palette]
accent = "#123456"
"##,
        )
        .expect("theme file should be written");

        let spec = parse_theme_spec(&path).expect("theme should parse");
        let mut specs = BTreeMap::new();
        specs.insert(spec.id.clone(), spec);
        let theme = resolve_theme_spec("custom", &specs, &mut BTreeMap::new(), &mut Vec::new())
            .expect("theme should resolve");
        assert_eq!(theme.palette.accent, "#123456");
        assert_eq!(theme.palette.text, "#f8f8f2");
    }

    #[test]
    fn custom_theme_can_inherit_from_custom_base() {
        let mut specs = BTreeMap::new();
        specs.insert(
            "brand-base".to_string(),
            ThemeSpec {
                id: "brand-base".to_string(),
                name: "Brand Base".to_string(),
                base: Some("nord".to_string()),
                palette: ThemePaletteFile {
                    accent: Some("#123456".to_string()),
                    ..ThemePaletteFile::default()
                },
                overrides: Default::default(),
            },
        );
        specs.insert(
            "brand-child".to_string(),
            ThemeSpec {
                id: "brand-child".to_string(),
                name: "Brand Child".to_string(),
                base: Some("brand-base".to_string()),
                palette: ThemePaletteFile {
                    warning: Some("#abcdef".to_string()),
                    ..ThemePaletteFile::default()
                },
                overrides: Default::default(),
            },
        );

        let theme =
            resolve_theme_spec("brand-child", &specs, &mut BTreeMap::new(), &mut Vec::new())
                .expect("custom base chain should resolve");

        assert_eq!(theme.palette.accent, "#123456");
        assert_eq!(theme.palette.warning, "#abcdef");
        assert_eq!(theme.palette.text, "#d8dee9");
    }

    #[test]
    fn color_spec_validation_accepts_known_tokens() {
        assert!(is_valid_color_spec(""));
        assert!(is_valid_color_spec("bold #ff00ff"));
        assert!(is_valid_color_spec("bright-blue"));
    }

    #[test]
    fn color_spec_validation_rejects_unknown_tokens() {
        assert!(!is_valid_color_spec("nope"));
        assert!(!is_valid_color_spec("#12345"));
    }

    #[test]
    fn theme_catalog_resolve_normalizes_input_and_rejects_blank_unit() {
        let mut catalog = ThemeCatalog::default();
        catalog.entries.insert(
            "rose-pine".to_string(),
            super::ThemeEntry {
                theme: empty_theme("rose-pine", "Rose Pine", None),
                source: ThemeSource::Builtin,
                origin: None,
            },
        );

        assert!(catalog.resolve("  ").is_none());
        assert!(catalog.resolve("Rose Pine").is_some());
        assert_eq!(catalog.ids(), vec!["rose-pine".to_string()]);
    }

    #[test]
    fn theme_path_helpers_expand_home_and_drop_blank_entries_unit() {
        let _guard = env_lock().lock().expect("env lock should not be poisoned");
        let original = std::env::var("HOME").ok();
        unsafe { std::env::set_var("HOME", "/tmp/theme-home") };

        assert_eq!(expand_theme_path("   "), None);
        assert_eq!(
            expand_theme_path("~"),
            Some(std::path::PathBuf::from("/tmp/theme-home"))
        );
        assert_eq!(
            expand_theme_path("~/themes"),
            Some(std::path::PathBuf::from("/tmp/theme-home/themes"))
        );
        assert_eq!(
            expand_theme_path("~\\themes"),
            Some(std::path::PathBuf::from("/tmp/theme-home/themes"))
        );
        assert_eq!(
            normalize_theme_paths(vec![" ".to_string(), "~/themes".to_string()]),
            vec![std::path::PathBuf::from("/tmp/theme-home/themes")]
        );

        match original {
            Some(value) => unsafe { std::env::set_var("HOME", value) },
            None => unsafe { std::env::remove_var("HOME") },
        }
    }

    #[test]
    fn theme_catalog_load_reports_invalid_specs_and_preserves_custom_origins_unit() {
        let root = unique_temp_dir("osp-theme-loader-catalog");
        let themes_dir = root.join("themes");
        let missing_dir = root.join("missing");
        let dracula_path = themes_dir.join("dracula.toml");
        let broken_path = themes_dir.join("broken.toml");
        let cycle_a_path = themes_dir.join("cycle-a.toml");
        let cycle_b_path = themes_dir.join("cycle-b.toml");
        let dupe_a_path = themes_dir.join("dupe-a.toml");
        let dupe_b_path = themes_dir.join("dupe-b.toml");
        fs::create_dir_all(&themes_dir).expect("themes dir should be created");

        fs::write(
            &dracula_path,
            r##"
[palette]
accent = "#123456"
"##,
        )
        .expect("override theme should be written");
        fs::write(&broken_path, "not = [valid").expect("broken theme writes");
        fs::write(
            &cycle_a_path,
            r##"
id = "cycle-a"
base = "cycle-b"
"##,
        )
        .expect("cycle a writes");
        fs::write(
            &cycle_b_path,
            r##"
id = "cycle-b"
base = "cycle-a"
"##,
        )
        .expect("cycle b writes");
        fs::write(
            &dupe_a_path,
            r##"
id = "dupe"
[palette]
text = "bogus"
selection = "#111111"
link = "#222222"
bg = "#000000"
bg_alt = "#010101"

[overrides]
value_number = "broken"
repl_completion_text = "#eeeeee"
repl_completion_background = "#111111"
repl_completion_highlight = "bad"
"##,
        )
        .expect("dupe a writes");
        fs::write(
            &dupe_b_path,
            r##"
id = "dupe"
name = "Dupe Final"
base = "none"
[palette]
text = "#ffffff"
"##,
        )
        .expect("dupe b writes");

        let config = resolved_config_with_theme_paths(vec![
            missing_dir.display().to_string(),
            themes_dir.display().to_string(),
        ]);
        let catalog = load_theme_catalog(&config);

        let dracula = catalog
            .resolve("dracula")
            .expect("custom builtin override should resolve");
        assert_eq!(dracula.source, ThemeSource::Custom);
        assert_eq!(dracula.theme.palette.accent, "#123456");
        assert_eq!(dracula.origin.as_deref(), Some(dracula_path.as_path()));

        let dupe = catalog
            .resolve("dupe")
            .expect("latest duplicate should win");
        assert_eq!(dupe.theme.name, "Dupe Final");
        assert_eq!(dupe.origin.as_deref(), Some(dupe_b_path.as_path()));

        let messages = catalog
            .issues
            .iter()
            .map(|issue| issue.message.clone())
            .collect::<Vec<_>>();
        assert!(
            messages
                .iter()
                .any(|message| message.contains("theme path is not a directory"))
        );
        assert!(
            messages
                .iter()
                .any(|message| message.contains("custom theme overrides builtin: dracula"))
        );
        assert!(
            messages
                .iter()
                .any(|message| message.contains("failed to parse toml"))
        );
        assert!(
            messages
                .iter()
                .any(|message| message.contains("theme id collision: dupe overridden"))
        );
        assert!(
            messages
                .iter()
                .any(|message| message.contains("theme base cycle detected"))
        );
        assert!(
            messages
                .iter()
                .any(|message| message.contains("invalid color spec for palette.text"))
        );
        assert!(
            messages
                .iter()
                .any(|message| message.contains("invalid color spec for overrides.value_number"))
        );

        log_theme_issues(&catalog.issues);
    }

    #[test]
    fn default_theme_paths_tracks_home_config_root_unit() {
        let _guard = env_lock().lock().expect("env lock should not be poisoned");
        let original_home = std::env::var("HOME").ok();
        let original_xdg_config_home = std::env::var("XDG_CONFIG_HOME").ok();
        unsafe { std::env::set_var("HOME", "/tmp/osp-theme-loader-home") };
        unsafe { std::env::remove_var("XDG_CONFIG_HOME") };

        assert_eq!(
            default_theme_paths(),
            vec![Path::new("/tmp/osp-theme-loader-home/.config/osp/themes").to_path_buf()]
        );

        unsafe { std::env::set_var("XDG_CONFIG_HOME", "/tmp/osp-theme-loader-xdg") };
        assert_eq!(
            default_theme_paths(),
            vec![Path::new("/tmp/osp-theme-loader-xdg/osp/themes").to_path_buf()]
        );

        match original_home {
            Some(value) => unsafe { std::env::set_var("HOME", value) },
            None => unsafe { std::env::remove_var("HOME") },
        }
        match original_xdg_config_home {
            Some(value) => unsafe { std::env::set_var("XDG_CONFIG_HOME", value) },
            None => unsafe { std::env::remove_var("XDG_CONFIG_HOME") },
        }
    }
}