sr-core 7.0.0

Pure domain logic for sr
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
874
875
876
877
878
879
880
881
882
use std::path::Path;

use serde::{Deserialize, Serialize};

use crate::commit::CommitType;
use crate::error::ReleaseError;
use crate::version::BumpLevel;
use crate::version_files::detect_version_files;

/// Preferred config file name for new projects.
pub const DEFAULT_CONFIG_FILE: &str = "sr.yaml";

/// Legacy config file name (deprecated, will be removed in a future release).
pub const LEGACY_CONFIG_FILE: &str = ".urmzd.sr.yml";

/// Config file candidates, checked in priority order.
pub const CONFIG_CANDIDATES: &[&str] = &["sr.yaml", "sr.yml", LEGACY_CONFIG_FILE];

// ---------------------------------------------------------------------------
// Top-level config
// ---------------------------------------------------------------------------

/// Root configuration. Six top-level concerns:
/// - `git` — tag prefix, floating tags, signing
/// - `commit` — type→bump classification
/// - `changelog` — file, template, groups
/// - `channels` — branch→release mapping
/// - `vcs` — provider-specific config
/// - `packages` — version files, artifacts, hooks
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct Config {
    pub git: GitConfig,
    pub commit: CommitConfig,
    pub changelog: ChangelogConfig,
    pub channels: ChannelsConfig,
    pub vcs: VcsConfig,
    #[serde(default = "default_packages")]
    pub packages: Vec<PackageConfig>,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            git: GitConfig::default(),
            commit: CommitConfig::default(),
            changelog: ChangelogConfig::default(),
            channels: ChannelsConfig::default(),
            vcs: VcsConfig::default(),
            packages: default_packages(),
        }
    }
}

fn default_packages() -> Vec<PackageConfig> {
    vec![PackageConfig {
        path: ".".into(),
        ..Default::default()
    }]
}

// ---------------------------------------------------------------------------
// Git config
// ---------------------------------------------------------------------------

/// Git-level settings — tags and signing.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct GitConfig {
    /// Prefix for git tags (e.g. "v" → "v1.2.0").
    pub tag_prefix: String,
    /// Create floating major version tags (e.g. "v3" → latest v3.x.x).
    pub floating_tag: bool,
    /// Sign tags with GPG/SSH.
    pub sign_tags: bool,
    /// Prevent breaking changes from bumping 0.x.y to 1.0.0.
    /// When true, major bumps at v0 are downshifted to minor.
    pub v0_protection: bool,
}

impl Default for GitConfig {
    fn default() -> Self {
        Self {
            tag_prefix: "v".into(),
            floating_tag: true,
            sign_tags: false,
            v0_protection: true,
        }
    }
}

// ---------------------------------------------------------------------------
// Commit config
// ---------------------------------------------------------------------------

/// How commits are classified by semver bump level.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct CommitConfig {
    /// Commit types grouped by bump level.
    pub types: CommitTypesConfig,
}

/// Commit type names grouped by the semver bump level they trigger.
/// Breaking changes always bump major regardless of configured level.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct CommitTypesConfig {
    /// Types that trigger a minor version bump.
    pub minor: Vec<String>,
    /// Types that trigger a patch version bump.
    pub patch: Vec<String>,
    /// Types that do not trigger a release on their own.
    pub none: Vec<String>,
}

impl Default for CommitTypesConfig {
    fn default() -> Self {
        Self {
            minor: vec!["feat".into()],
            patch: vec!["fix".into(), "perf".into(), "refactor".into()],
            none: vec![
                "docs".into(),
                "revert".into(),
                "chore".into(),
                "ci".into(),
                "test".into(),
                "build".into(),
                "style".into(),
            ],
        }
    }
}

impl CommitTypesConfig {
    /// All type names across all bump levels.
    pub fn all_type_names(&self) -> Vec<&str> {
        self.minor
            .iter()
            .chain(self.patch.iter())
            .chain(self.none.iter())
            .map(|s| s.as_str())
            .collect()
    }

    /// Convert to internal `Vec<CommitType>` representation.
    pub fn into_commit_types(&self) -> Vec<CommitType> {
        let mut types = Vec::new();
        for name in &self.minor {
            types.push(CommitType {
                name: name.clone(),
                bump: Some(BumpLevel::Minor),
            });
        }
        for name in &self.patch {
            types.push(CommitType {
                name: name.clone(),
                bump: Some(BumpLevel::Patch),
            });
        }
        for name in &self.none {
            types.push(CommitType {
                name: name.clone(),
                bump: None,
            });
        }
        types
    }
}

// ---------------------------------------------------------------------------
// Changelog config
// ---------------------------------------------------------------------------

/// Changelog generation — file, template, and commit grouping.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct ChangelogConfig {
    /// Path to the changelog file. None = skip changelog generation.
    pub file: Option<String>,
    /// Jinja template — path to file or inline string. None = built-in default.
    pub template: Option<String>,
    /// Ordered groups for organizing commits in the changelog.
    pub groups: Vec<ChangelogGroup>,
}

impl Default for ChangelogConfig {
    fn default() -> Self {
        Self {
            file: Some("CHANGELOG.md".into()),
            template: None,
            groups: default_changelog_groups(),
        }
    }
}

/// A named group of commit types for changelog rendering.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChangelogGroup {
    /// Machine-readable name (e.g. "breaking", "features").
    pub name: String,
    /// Commit types included in this group (e.g. ["feat"]).
    pub content: Vec<String>,
}

pub fn default_changelog_groups() -> Vec<ChangelogGroup> {
    vec![
        ChangelogGroup {
            name: "breaking".into(),
            content: vec!["breaking".into()],
        },
        ChangelogGroup {
            name: "features".into(),
            content: vec!["feat".into()],
        },
        ChangelogGroup {
            name: "bug-fixes".into(),
            content: vec!["fix".into()],
        },
        ChangelogGroup {
            name: "performance".into(),
            content: vec!["perf".into()],
        },
        ChangelogGroup {
            name: "refactoring".into(),
            content: vec!["refactor".into()],
        },
        ChangelogGroup {
            name: "misc".into(),
            content: vec![
                "docs".into(),
                "revert".into(),
                "chore".into(),
                "ci".into(),
                "test".into(),
                "build".into(),
                "style".into(),
            ],
        },
    ]
}

// ---------------------------------------------------------------------------
// Channels config
// ---------------------------------------------------------------------------

/// Release channels for trunk-based promotion.
/// All channels release from the same branch — channels control the release
/// strategy (stable vs prerelease vs draft), not the branch.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct ChannelsConfig {
    /// Default channel when no --channel flag given.
    pub default: String,
    /// The trunk branch that triggers releases.
    pub branch: String,
    /// Channel definitions.
    pub content: Vec<ChannelConfig>,
}

impl Default for ChannelsConfig {
    fn default() -> Self {
        Self {
            default: "stable".into(),
            branch: "main".into(),
            content: vec![ChannelConfig {
                name: "stable".into(),
                prerelease: None,
                draft: false,
            }],
        }
    }
}

/// A named release channel.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChannelConfig {
    /// Channel name (e.g. "stable", "rc", "canary").
    pub name: String,
    /// Pre-release identifier (e.g. "rc", "canary"). None = stable release.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub prerelease: Option<String>,
    /// Create GitHub release as a draft.
    #[serde(default)]
    pub draft: bool,
}

// ---------------------------------------------------------------------------
// VCS config
// ---------------------------------------------------------------------------

/// VCS provider-specific configuration.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(default)]
pub struct VcsConfig {
    pub github: GitHubConfig,
}

/// GitHub-specific release settings.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(default)]
pub struct GitHubConfig {
    /// Minijinja template for the GitHub release name.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub release_name_template: Option<String>,
}

// ---------------------------------------------------------------------------
// Package config
// ---------------------------------------------------------------------------

/// A releasable package — version files, artifacts, build/publish hooks.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct PackageConfig {
    /// Directory path relative to repo root.
    pub path: String,
    /// Whether this package versions independently in a monorepo.
    pub independent: bool,
    /// Tag prefix override (default: derived from git.tag_prefix or "{dir}/v").
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tag_prefix: Option<String>,
    /// Manifest files to bump.
    pub version_files: Vec<String>,
    /// Fail on unsupported version file formats.
    pub version_files_strict: bool,
    /// Additional files to stage in the release commit.
    pub stage_files: Vec<String>,
    /// Glob patterns for artifact files to upload to the release.
    pub artifacts: Vec<String>,
    /// Changelog config override for this package.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub changelog: Option<ChangelogConfig>,
    /// Package-level lifecycle hooks.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub hooks: Option<HooksConfig>,
}

impl Default for PackageConfig {
    fn default() -> Self {
        Self {
            path: ".".into(),
            independent: false,
            tag_prefix: None,
            version_files: vec![],
            version_files_strict: false,
            stage_files: vec![],
            artifacts: vec![],
            changelog: None,
            hooks: None,
        }
    }
}

/// Package lifecycle hooks — shell commands at release events.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(default)]
pub struct HooksConfig {
    /// Runs after version bump, before commit (build with bumped versions).
    pub pre_release: Vec<String>,
    /// Runs after tag + GitHub release (publish to registries).
    pub post_release: Vec<String>,
}

// ---------------------------------------------------------------------------
// Config methods
// ---------------------------------------------------------------------------

impl Config {
    /// Find the first config file that exists in the given directory.
    pub fn find_config(dir: &Path) -> Option<(std::path::PathBuf, bool)> {
        for &candidate in CONFIG_CANDIDATES {
            let path = dir.join(candidate);
            if path.exists() {
                let is_legacy = candidate == LEGACY_CONFIG_FILE;
                return Some((path, is_legacy));
            }
        }
        None
    }

    /// Load config from a YAML file. Falls back to defaults if the file doesn't exist.
    pub fn load(path: &Path) -> Result<Self, ReleaseError> {
        if !path.exists() {
            return Ok(Self::default());
        }
        let contents =
            std::fs::read_to_string(path).map_err(|e| ReleaseError::Config(e.to_string()))?;
        let config: Self =
            serde_yaml_ng::from_str(&contents).map_err(|e| ReleaseError::Config(e.to_string()))?;
        config.validate()?;
        Ok(config)
    }

    /// Validate config consistency.
    fn validate(&self) -> Result<(), ReleaseError> {
        // Check for duplicate type names across bump levels.
        let mut seen = std::collections::HashSet::new();
        for name in self.commit.types.all_type_names() {
            if !seen.insert(name) {
                return Err(ReleaseError::Config(format!(
                    "duplicate commit type: {name}"
                )));
            }
        }

        // Need at least one type with a bump level.
        if self.commit.types.minor.is_empty() && self.commit.types.patch.is_empty() {
            return Err(ReleaseError::Config(
                "commit.types must have at least one minor or patch type".into(),
            ));
        }

        // Check for duplicate channel names.
        let mut channel_names = std::collections::HashSet::new();
        for ch in &self.channels.content {
            if !channel_names.insert(&ch.name) {
                return Err(ReleaseError::Config(format!(
                    "duplicate channel name: {}",
                    ch.name
                )));
            }
        }

        Ok(())
    }

    /// Resolve a named release channel, returning the channel config.
    pub fn resolve_channel(&self, name: &str) -> Result<&ChannelConfig, ReleaseError> {
        self.channels
            .content
            .iter()
            .find(|ch| ch.name == name)
            .ok_or_else(|| {
                let available: Vec<&str> = self
                    .channels
                    .content
                    .iter()
                    .map(|c| c.name.as_str())
                    .collect();
                ReleaseError::Config(format!(
                    "channel '{name}' not found. Available: {}",
                    if available.is_empty() {
                        "(none)".to_string()
                    } else {
                        available.join(", ")
                    }
                ))
            })
    }

    /// Resolve the default channel.
    pub fn default_channel(&self) -> Result<&ChannelConfig, ReleaseError> {
        self.resolve_channel(&self.channels.default)
    }

    /// Find a package by path.
    pub fn find_package(&self, path: &str) -> Result<&PackageConfig, ReleaseError> {
        self.packages
            .iter()
            .find(|p| p.path == path)
            .ok_or_else(|| {
                let available: Vec<&str> = self.packages.iter().map(|p| p.path.as_str()).collect();
                ReleaseError::Config(format!(
                    "package '{path}' not found. Available: {}",
                    if available.is_empty() {
                        "(none)".to_string()
                    } else {
                        available.join(", ")
                    }
                ))
            })
    }

    /// Find a package by name (last component of path).
    pub fn find_package_by_name(&self, name: &str) -> Result<&PackageConfig, ReleaseError> {
        self.packages
            .iter()
            .find(|p| p.path.rsplit('/').next().unwrap_or(&p.path) == name)
            .ok_or_else(|| {
                let available: Vec<&str> = self
                    .packages
                    .iter()
                    .map(|p| p.path.rsplit('/').next().unwrap_or(&p.path))
                    .collect();
                ReleaseError::Config(format!(
                    "package '{name}' not found. Available: {}",
                    if available.is_empty() {
                        "(none)".to_string()
                    } else {
                        available.join(", ")
                    }
                ))
            })
    }

    /// Resolve effective tag prefix for a package.
    pub fn tag_prefix_for(&self, pkg: &PackageConfig) -> String {
        if let Some(ref prefix) = pkg.tag_prefix {
            return prefix.clone();
        }
        if pkg.path == "." {
            self.git.tag_prefix.clone()
        } else {
            let dir_name = pkg.path.rsplit('/').next().unwrap_or(&pkg.path);
            format!("{}/v", dir_name)
        }
    }

    /// Resolve effective changelog config for a package.
    pub fn changelog_for<'a>(&'a self, pkg: &'a PackageConfig) -> &'a ChangelogConfig {
        pkg.changelog.as_ref().unwrap_or(&self.changelog)
    }

    /// Resolve effective version files for a package, with auto-detection.
    pub fn version_files_for(&self, pkg: &PackageConfig) -> Vec<String> {
        if !pkg.version_files.is_empty() {
            return pkg.version_files.clone();
        }
        let detected = detect_version_files(Path::new(&pkg.path));
        if pkg.path == "." {
            detected
        } else {
            detected
                .into_iter()
                .map(|f| format!("{}/{f}", pkg.path))
                .collect()
        }
    }

    /// Get all non-independent packages (for fixed versioning).
    pub fn fixed_packages(&self) -> Vec<&PackageConfig> {
        self.packages.iter().filter(|p| !p.independent).collect()
    }

    /// Get all independent packages.
    pub fn independent_packages(&self) -> Vec<&PackageConfig> {
        self.packages.iter().filter(|p| p.independent).collect()
    }

    /// Collect all artifacts glob patterns from all packages.
    pub fn all_artifacts(&self) -> Vec<String> {
        self.packages
            .iter()
            .flat_map(|p| p.artifacts.clone())
            .collect()
    }
}

// ---------------------------------------------------------------------------
// Template generation
// ---------------------------------------------------------------------------

pub fn default_config_template(version_files: &[String]) -> String {
    let vf = if version_files.is_empty() {
        "    version_files: []\n".to_string()
    } else {
        let mut s = "    version_files:\n".to_string();
        for f in version_files {
            s.push_str(&format!("      - {f}\n"));
        }
        s
    };

    format!(
        r#"# sr configuration
# Full reference: https://github.com/urmzd/sr#configuration

git:
  tag_prefix: "v"
  floating_tag: true
  sign_tags: false
  v0_protection: true

commit:
  types:
    minor:
      - feat
    patch:
      - fix
      - perf
      - refactor
    none:
      - docs
      - revert
      - chore
      - ci
      - test
      - build
      - style

changelog:
  file: CHANGELOG.md
  # template: changelog.md.j2
  groups:
    - name: breaking
      content:
        - breaking
    - name: features
      content:
        - feat
    - name: bug-fixes
      content:
        - fix
    - name: performance
      content:
        - perf
    - name: misc
      content:
        - chore
        - ci
        - test
        - build
        - style

channels:
  default: stable
  branch: main
  content:
    - name: stable
  # - name: rc
  #   prerelease: rc
  #   draft: true
  # - name: canary
  #   branch: develop
  #   prerelease: canary

# vcs:
#   github:
#     release_name_template: "{{{{ tag_name }}}}"

packages:
  - path: .
{vf}    # version_files_strict: false
    # stage_files: []
    # artifacts: []
    # hooks:
    #   pre_release:
    #     - cargo build --release
    #   post_release:
    #     - cargo publish
"#
    )
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn default_values() {
        let config = Config::default();
        assert_eq!(config.git.tag_prefix, "v");
        assert!(config.git.floating_tag);
        assert!(!config.git.sign_tags);
        assert_eq!(config.commit.types.minor, vec!["feat"]);
        assert!(config.commit.types.patch.contains(&"fix".to_string()));
        assert!(config.commit.types.none.contains(&"chore".to_string()));
        assert_eq!(config.changelog.file.as_deref(), Some("CHANGELOG.md"));
        assert!(!config.changelog.groups.is_empty());
        assert_eq!(config.channels.default, "stable");
        assert_eq!(config.channels.content.len(), 1);
        assert_eq!(config.channels.content[0].name, "stable");
        assert_eq!(config.channels.branch, "main");
        assert_eq!(config.packages.len(), 1);
        assert_eq!(config.packages[0].path, ".");
    }

    #[test]
    fn load_missing_file() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("nonexistent.yml");
        let config = Config::load(&path).unwrap();
        assert_eq!(config.git.tag_prefix, "v");
    }

    #[test]
    fn load_partial_yaml() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("config.yml");
        std::fs::write(&path, "git:\n  tag_prefix: rel-\n").unwrap();

        let config = Config::load(&path).unwrap();
        assert_eq!(config.git.tag_prefix, "rel-");
        assert_eq!(config.channels.default, "stable");
    }

    #[test]
    fn load_yaml_with_packages() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("config.yml");
        std::fs::write(
            &path,
            "packages:\n  - path: crates/core\n    version_files:\n      - crates/core/Cargo.toml\n",
        )
        .unwrap();

        let config = Config::load(&path).unwrap();
        assert_eq!(config.packages.len(), 1);
        assert_eq!(config.packages[0].path, "crates/core");
    }

    #[test]
    fn commit_types_conversion() {
        let types = CommitTypesConfig::default();
        let commit_types = types.into_commit_types();
        let feat = commit_types.iter().find(|t| t.name == "feat").unwrap();
        assert_eq!(feat.bump, Some(BumpLevel::Minor));
        let fix = commit_types.iter().find(|t| t.name == "fix").unwrap();
        assert_eq!(fix.bump, Some(BumpLevel::Patch));
        let chore = commit_types.iter().find(|t| t.name == "chore").unwrap();
        assert_eq!(chore.bump, None);
    }

    #[test]
    fn all_type_names() {
        let types = CommitTypesConfig::default();
        let names = types.all_type_names();
        assert!(names.contains(&"feat"));
        assert!(names.contains(&"fix"));
        assert!(names.contains(&"chore"));
    }

    #[test]
    fn resolve_channel() {
        let config = Config::default();
        let channel = config.resolve_channel("stable").unwrap();
        assert!(channel.prerelease.is_none());
    }

    #[test]
    fn resolve_channel_not_found() {
        let config = Config::default();
        assert!(config.resolve_channel("missing").is_err());
    }

    #[test]
    fn tag_prefix_root_package() {
        let config = Config::default();
        let pkg = &config.packages[0];
        assert_eq!(config.tag_prefix_for(pkg), "v");
    }

    #[test]
    fn tag_prefix_subpackage() {
        let config = Config::default();
        let pkg = PackageConfig {
            path: "crates/core".into(),
            ..Default::default()
        };
        assert_eq!(config.tag_prefix_for(&pkg), "core/v");
    }

    #[test]
    fn tag_prefix_override() {
        let config = Config::default();
        let pkg = PackageConfig {
            path: "crates/cli".into(),
            tag_prefix: Some("cli-v".into()),
            ..Default::default()
        };
        assert_eq!(config.tag_prefix_for(&pkg), "cli-v");
    }

    #[test]
    fn validate_duplicate_types() {
        let config = Config {
            commit: CommitConfig {
                types: CommitTypesConfig {
                    minor: vec!["feat".into()],
                    patch: vec!["feat".into()],
                    none: vec![],
                },
            },
            ..Default::default()
        };
        assert!(config.validate().is_err());
    }

    #[test]
    fn validate_no_bump_types() {
        let config = Config {
            commit: CommitConfig {
                types: CommitTypesConfig {
                    minor: vec![],
                    patch: vec![],
                    none: vec!["chore".into()],
                },
            },
            ..Default::default()
        };
        assert!(config.validate().is_err());
    }

    #[test]
    fn validate_duplicate_channels() {
        let config = Config {
            channels: ChannelsConfig {
                default: "stable".into(),
                branch: "main".into(),
                content: vec![
                    ChannelConfig {
                        name: "stable".into(),
                        prerelease: None,
                        draft: false,
                    },
                    ChannelConfig {
                        name: "stable".into(),
                        prerelease: None,
                        draft: false,
                    },
                ],
            },
            ..Default::default()
        };
        assert!(config.validate().is_err());
    }

    #[test]
    fn default_template_parses() {
        let template = default_config_template(&[]);
        let config: Config = serde_yaml_ng::from_str(&template).unwrap();
        assert_eq!(config.git.tag_prefix, "v");
        assert!(config.git.floating_tag);
        assert_eq!(config.channels.default, "stable");
    }

    #[test]
    fn default_template_with_version_files() {
        let template = default_config_template(&["Cargo.toml".into(), "package.json".into()]);
        let config: Config = serde_yaml_ng::from_str(&template).unwrap();
        assert_eq!(
            config.packages[0].version_files,
            vec!["Cargo.toml", "package.json"]
        );
    }

    #[test]
    fn find_package_by_name_works() {
        let config = Config {
            packages: vec![
                PackageConfig {
                    path: "crates/core".into(),
                    ..Default::default()
                },
                PackageConfig {
                    path: "crates/cli".into(),
                    ..Default::default()
                },
            ],
            ..Default::default()
        };
        let pkg = config.find_package_by_name("core").unwrap();
        assert_eq!(pkg.path, "crates/core");
    }

    #[test]
    fn collect_all_artifacts() {
        let config = Config {
            packages: vec![
                PackageConfig {
                    path: "crates/core".into(),
                    artifacts: vec!["core-*".into()],
                    ..Default::default()
                },
                PackageConfig {
                    path: "crates/cli".into(),
                    artifacts: vec!["cli-*".into()],
                    ..Default::default()
                },
            ],
            ..Default::default()
        };
        let artifacts = config.all_artifacts();
        assert_eq!(artifacts, vec!["core-*", "cli-*"]);
    }
}