1use std::path::PathBuf;
2
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6const fn default_max_cyclomatic() -> u16 {
7 20
8}
9
10const fn default_max_cognitive() -> u16 {
11 15
12}
13
14const fn default_max_crap() -> f64 {
18 30.0
19}
20
21const fn default_crap_refactor_band() -> u16 {
22 5
23}
24
25const fn default_max_unit_size() -> u32 {
29 60
30}
31
32#[expect(
33 clippy::trivially_copy_pass_by_ref,
34 reason = "serde skip_serializing_if passes a reference"
35)]
36const fn is_zero_u32(value: &u32) -> bool {
37 *value == 0
38}
39
40const fn default_suggest_inline_suppression() -> bool {
44 true
45}
46
47fn default_bot_patterns() -> Vec<String> {
62 vec![
63 r"*\[bot\]*".to_string(),
64 "dependabot*".to_string(),
65 "renovate*".to_string(),
66 "github-actions*".to_string(),
67 "svc-*".to_string(),
68 "*-service-account*".to_string(),
69 ]
70}
71
72const fn default_email_mode() -> EmailMode {
73 EmailMode::Handle
74}
75
76#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, JsonSchema)]
81#[serde(rename_all = "kebab-case")]
82pub enum EmailMode {
83 Raw,
86 Handle,
89 Anonymized,
95 Hash,
97}
98
99#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
101#[serde(rename_all = "camelCase")]
102pub struct OwnershipConfig {
103 #[serde(default = "default_bot_patterns")]
107 pub bot_patterns: Vec<String>,
108
109 #[serde(default = "default_email_mode")]
113 pub email_mode: EmailMode,
114}
115
116impl Default for OwnershipConfig {
117 fn default() -> Self {
118 Self {
119 bot_patterns: default_bot_patterns(),
120 email_mode: default_email_mode(),
121 }
122 }
123}
124
125#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
132#[serde(deny_unknown_fields, rename_all = "camelCase")]
133pub struct ChangeCouplingConfig {
134 #[serde(default = "default_coupling_max_files_per_commit")]
138 #[schemars(range(min = 2, max = 1000))]
139 pub max_files_per_commit: usize,
140 #[serde(default = "default_coupling_min_revisions")]
142 pub min_revisions: u32,
143 #[serde(default = "default_coupling_min_shared_revisions")]
145 pub min_shared_revisions: u32,
146 #[serde(default = "default_coupling_min_degree")]
150 #[schemars(range(min = 0.0, max = 1.0))]
151 pub min_degree: f64,
152}
153
154const fn default_coupling_max_files_per_commit() -> usize {
155 30
156}
157
158const fn default_coupling_min_revisions() -> u32 {
159 5
160}
161
162const fn default_coupling_min_shared_revisions() -> u32 {
163 5
164}
165
166const fn default_coupling_min_degree() -> f64 {
167 0.3
168}
169
170const MIN_COUPLING_FILES_PER_COMMIT: usize = 2;
172
173const MAX_COUPLING_FILES_PER_COMMIT: usize = 1000;
176
177impl ChangeCouplingConfig {
178 #[must_use]
180 pub fn errors(&self) -> Vec<String> {
181 let mut errors = Vec::new();
182 if !(MIN_COUPLING_FILES_PER_COMMIT..=MAX_COUPLING_FILES_PER_COMMIT)
183 .contains(&self.max_files_per_commit)
184 {
185 errors.push(format!(
186 "health.changeCoupling.maxFilesPerCommit must be from {MIN_COUPLING_FILES_PER_COMMIT} to {MAX_COUPLING_FILES_PER_COMMIT}, found {}",
187 self.max_files_per_commit
188 ));
189 }
190 if !(0.0..=1.0).contains(&self.min_degree) {
191 errors.push(format!(
192 "health.changeCoupling.minDegree must be from 0 to 1, found {}",
193 self.min_degree
194 ));
195 }
196 errors
197 }
198}
199
200impl Default for ChangeCouplingConfig {
201 fn default() -> Self {
202 Self {
203 max_files_per_commit: default_coupling_max_files_per_commit(),
204 min_revisions: default_coupling_min_revisions(),
205 min_shared_revisions: default_coupling_min_shared_revisions(),
206 min_degree: default_coupling_min_degree(),
207 }
208 }
209}
210
211#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize, Serialize, JsonSchema)]
218#[serde(rename_all = "kebab-case")]
219pub enum CyclomaticPolicy {
220 #[default]
222 Standard,
223 LiteralMembership,
230}
231
232impl CyclomaticPolicy {
233 #[must_use]
235 pub const fn is_standard(&self) -> bool {
236 matches!(self, Self::Standard)
237 }
238}
239
240#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
242#[serde(deny_unknown_fields, rename_all = "camelCase")]
243pub struct HealthConfig {
244 #[serde(default = "default_max_cyclomatic")]
249 pub max_cyclomatic: u16,
250
251 #[serde(default, skip_serializing_if = "CyclomaticPolicy::is_standard")]
261 pub cyclomatic_policy: CyclomaticPolicy,
262
263 #[serde(default = "default_max_cognitive")]
267 pub max_cognitive: u16,
268
269 #[serde(default = "default_max_crap")]
285 pub max_crap: f64,
286
287 #[serde(default = "default_crap_refactor_band")]
292 pub crap_refactor_band: u16,
293
294 #[serde(default = "default_max_unit_size")]
305 pub max_unit_size: u32,
306
307 #[serde(default, skip_serializing_if = "is_zero_u32")]
317 pub max_file_lines: u32,
318
319 #[serde(default)]
326 pub coverage: Option<PathBuf>,
327
328 #[serde(default)]
335 pub coverage_root: Option<PathBuf>,
336
337 #[serde(default)]
340 pub ignore: Vec<String>,
341
342 #[serde(default, skip_serializing_if = "Vec::is_empty")]
350 pub tailwind_ignore_patterns: Vec<String>,
351
352 #[serde(default, skip_serializing_if = "Vec::is_empty")]
356 pub threshold_overrides: Vec<HealthThresholdOverride>,
357
358 #[serde(default)]
361 pub ownership: OwnershipConfig,
362
363 #[serde(default)]
368 pub change_coupling: ChangeCouplingConfig,
369
370 #[serde(default = "default_suggest_inline_suppression")]
379 pub suggest_inline_suppression: bool,
380}
381
382impl Default for HealthConfig {
383 fn default() -> Self {
384 Self {
385 max_cyclomatic: default_max_cyclomatic(),
386 cyclomatic_policy: CyclomaticPolicy::Standard,
387 max_cognitive: default_max_cognitive(),
388 max_crap: default_max_crap(),
389 crap_refactor_band: default_crap_refactor_band(),
390 max_unit_size: default_max_unit_size(),
391 max_file_lines: 0,
392 coverage: None,
393 coverage_root: None,
394 ignore: vec![],
395 tailwind_ignore_patterns: vec![],
396 threshold_overrides: vec![],
397 ownership: OwnershipConfig::default(),
398 change_coupling: ChangeCouplingConfig::default(),
399 suggest_inline_suppression: default_suggest_inline_suppression(),
400 }
401 }
402}
403
404#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
406#[serde(deny_unknown_fields, rename_all = "camelCase")]
407pub struct HealthThresholdOverride {
408 pub files: Vec<String>,
411 #[serde(default, skip_serializing_if = "Vec::is_empty")]
418 pub functions: Vec<String>,
419 #[serde(default, skip_serializing_if = "Option::is_none")]
421 pub max_cyclomatic: Option<u16>,
422 #[serde(default, skip_serializing_if = "Option::is_none")]
424 pub max_cognitive: Option<u16>,
425 #[serde(default, skip_serializing_if = "Option::is_none")]
427 pub max_crap: Option<f64>,
428 #[serde(default, skip_serializing_if = "Option::is_none")]
434 pub max_unit_size: Option<u32>,
435 #[serde(default, skip_serializing_if = "Option::is_none")]
439 pub max_file_lines: Option<u32>,
440 #[serde(default, skip_serializing_if = "Option::is_none")]
442 pub reason: Option<String>,
443}
444
445impl HealthThresholdOverride {
446 #[must_use]
448 pub const fn has_any_threshold(&self) -> bool {
449 self.max_cyclomatic.is_some()
450 || self.max_cognitive.is_some()
451 || self.max_crap.is_some()
452 || self.max_unit_size.is_some()
453 || self.max_file_lines.is_some()
454 }
455}
456
457impl HealthConfig {
458 #[must_use]
461 pub fn file_lines_check_enabled(&self) -> bool {
462 self.max_file_lines > 0
463 || self
464 .threshold_overrides
465 .iter()
466 .any(|entry| entry.max_file_lines.is_some_and(|max| max > 0))
467 }
468
469 #[must_use]
471 pub fn threshold_override_errors(&self) -> Vec<String> {
472 let mut errors = Vec::new();
473 for (index, override_entry) in self.threshold_overrides.iter().enumerate() {
474 if override_entry.files.is_empty() {
475 errors.push(format!(
476 "health.thresholdOverrides[{index}].files must contain at least one pattern"
477 ));
478 }
479 if !override_entry.has_any_threshold() {
480 errors.push(format!(
481 "health.thresholdOverrides[{index}] must set at least one of maxCyclomatic, maxCognitive, maxCrap, maxUnitSize, or maxFileLines"
482 ));
483 }
484 if override_entry.max_file_lines.is_some() && !override_entry.functions.is_empty() {
485 errors.push(format!(
486 "health.thresholdOverrides[{index}].maxFileLines applies to whole files; move it to an entry without functions"
487 ));
488 }
489 }
490 errors
491 }
492}
493
494#[cfg(test)]
495mod tests {
496 use super::*;
497
498 #[test]
499 fn change_coupling_ranges_are_validated() {
500 assert!(ChangeCouplingConfig::default().errors().is_empty());
501 let at_bounds = ChangeCouplingConfig {
502 max_files_per_commit: 1000,
503 min_degree: 1.0,
504 ..ChangeCouplingConfig::default()
505 };
506 assert!(at_bounds.errors().is_empty());
507
508 for (max_files_per_commit, min_degree) in [(1, 0.3), (1001, 0.3), (30, -0.1), (30, 1.5)] {
509 let config = ChangeCouplingConfig {
510 max_files_per_commit,
511 min_degree,
512 ..ChangeCouplingConfig::default()
513 };
514 assert_eq!(
515 config.errors().len(),
516 1,
517 "maxFilesPerCommit {max_files_per_commit}, minDegree {min_degree}"
518 );
519 }
520 let nan = ChangeCouplingConfig {
521 min_degree: f64::NAN,
522 ..ChangeCouplingConfig::default()
523 };
524 assert_eq!(nan.errors().len(), 1);
525 }
526
527 #[test]
528 fn health_config_defaults() {
529 let config = HealthConfig::default();
530 assert_eq!(config.max_cyclomatic, 20);
531 assert_eq!(config.max_cognitive, 15);
532 assert!((config.max_crap - 30.0).abs() < f64::EPSILON);
533 assert_eq!(config.crap_refactor_band, 5);
534 assert_eq!(config.max_unit_size, 60);
535 assert!(config.coverage.is_none());
536 assert!(config.coverage_root.is_none());
537 assert!(config.ignore.is_empty());
538 assert!(config.threshold_overrides.is_empty());
539 assert_eq!(config.change_coupling.max_files_per_commit, 30);
540 assert_eq!(config.change_coupling.min_revisions, 5);
541 assert_eq!(config.change_coupling.min_shared_revisions, 5);
542 assert!((config.change_coupling.min_degree - 0.3).abs() < f64::EPSILON);
543 let ownership = OwnershipConfig::default();
544 assert_eq!(config.ownership.bot_patterns, ownership.bot_patterns);
545 assert_eq!(config.ownership.email_mode, ownership.email_mode);
546 }
547
548 #[test]
549 fn health_config_json_all_fields() {
550 let json = r#"{
551 "maxCyclomatic": 30,
552 "maxCognitive": 25,
553 "maxCrap": 50.0,
554 "crapRefactorBand": 3,
555 "coverage": "coverage/coverage-final.json",
556 "coverageRoot": "/ci/workspace",
557 "ignore": ["**/generated/**", "vendor/**"],
558 "thresholdOverrides": [{
559 "files": ["components/auth/src/index.ts"],
560 "functions": ["createAuthModule"],
561 "maxCognitive": 25,
562 "reason": "linear module assembly; agreed 2026-06"
563 }]
564 }"#;
565 let config: HealthConfig = serde_json::from_str(json).unwrap();
566 assert_eq!(config.max_cyclomatic, 30);
567 assert_eq!(config.max_cognitive, 25);
568 assert!((config.max_crap - 50.0).abs() < f64::EPSILON);
569 assert_eq!(config.crap_refactor_band, 3);
570 assert_eq!(
571 config.coverage,
572 Some(PathBuf::from("coverage/coverage-final.json"))
573 );
574 assert_eq!(config.coverage_root, Some(PathBuf::from("/ci/workspace")));
575 assert_eq!(config.ignore, vec!["**/generated/**", "vendor/**"]);
576 assert_eq!(config.threshold_overrides.len(), 1);
577 assert_eq!(
578 config.threshold_overrides[0].files,
579 vec!["components/auth/src/index.ts"]
580 );
581 assert_eq!(
582 config.threshold_overrides[0].functions,
583 vec!["createAuthModule"]
584 );
585 assert_eq!(config.threshold_overrides[0].max_cognitive, Some(25));
586 }
587
588 #[test]
589 fn health_config_json_partial_uses_defaults() {
590 let json = r#"{"maxCyclomatic": 10}"#;
591 let config: HealthConfig = serde_json::from_str(json).unwrap();
592 assert_eq!(config.max_cyclomatic, 10);
593 assert_eq!(config.max_cognitive, 15); assert!((config.max_crap - 30.0).abs() < f64::EPSILON); assert_eq!(config.crap_refactor_band, 5); assert!(config.ignore.is_empty()); assert!(config.threshold_overrides.is_empty()); }
599
600 #[test]
601 fn health_config_json_only_max_crap() {
602 let json = r#"{"maxCrap": 15.5}"#;
603 let config: HealthConfig = serde_json::from_str(json).unwrap();
604 assert!((config.max_crap - 15.5).abs() < f64::EPSILON);
605 assert_eq!(config.max_cyclomatic, 20); assert_eq!(config.cyclomatic_policy, CyclomaticPolicy::Standard);
607 assert_eq!(config.max_cognitive, 15); assert_eq!(config.crap_refactor_band, 5); }
610
611 #[test]
612 fn health_config_json_empty_object_uses_all_defaults() {
613 let config: HealthConfig = serde_json::from_str("{}").unwrap();
614 assert_eq!(config.max_cyclomatic, 20);
615 assert_eq!(config.max_cognitive, 15);
616 assert_eq!(config.crap_refactor_band, 5);
617 assert!(config.ignore.is_empty());
618 assert!(config.threshold_overrides.is_empty());
619 }
620
621 #[test]
622 fn health_config_json_only_ignore() {
623 let json = r#"{"ignore": ["test/**"]}"#;
624 let config: HealthConfig = serde_json::from_str(json).unwrap();
625 assert_eq!(config.max_cyclomatic, 20); assert_eq!(config.max_cognitive, 15); assert_eq!(config.ignore, vec!["test/**"]);
628 assert!(config.threshold_overrides.is_empty());
629 }
630
631 #[test]
632 fn health_config_toml_all_fields() {
633 let toml_str = r#"
634maxCyclomatic = 25
635maxCognitive = 20
636ignore = ["generated/**", "vendor/**"]
637
638[[thresholdOverrides]]
639files = ["src/auth.ts"]
640maxCognitive = 25
641"#;
642 let config: HealthConfig = toml::from_str(toml_str).unwrap();
643 assert_eq!(config.max_cyclomatic, 25);
644 assert_eq!(config.max_cognitive, 20);
645 assert_eq!(config.ignore, vec!["generated/**", "vendor/**"]);
646 assert_eq!(config.threshold_overrides.len(), 1);
647 assert_eq!(config.threshold_overrides[0].max_cognitive, Some(25));
648 }
649
650 #[test]
651 fn health_config_toml_defaults() {
652 let config: HealthConfig = toml::from_str("").unwrap();
653 assert_eq!(config.max_cyclomatic, 20);
654 assert_eq!(config.max_cognitive, 15);
655 assert!(config.ignore.is_empty());
656 assert!(config.threshold_overrides.is_empty());
657 }
658
659 #[test]
660 fn health_config_json_roundtrip() {
661 let config = HealthConfig {
662 max_cyclomatic: 50,
663 cyclomatic_policy: CyclomaticPolicy::LiteralMembership,
664 max_cognitive: 40,
665 max_crap: 75.0,
666 crap_refactor_band: 4,
667 max_unit_size: 120,
668 max_file_lines: 500,
669 ignore: vec!["test/**".to_string()],
670 tailwind_ignore_patterns: Vec::new(),
671 threshold_overrides: vec![HealthThresholdOverride {
672 files: vec!["src/auth.ts".to_string()],
673 functions: Vec::new(),
674 max_cyclomatic: Some(30),
675 max_cognitive: None,
676 max_crap: Some(45.0),
677 max_unit_size: None,
678 max_file_lines: None,
679 reason: Some("framework assembly".to_string()),
680 }],
681 coverage: None,
682 coverage_root: None,
683 ownership: OwnershipConfig::default(),
684 change_coupling: ChangeCouplingConfig {
685 min_degree: 0.4,
686 ..ChangeCouplingConfig::default()
687 },
688 suggest_inline_suppression: false,
689 };
690 let json = serde_json::to_string(&config).unwrap();
691 let restored: HealthConfig = serde_json::from_str(&json).unwrap();
692 assert_eq!(restored.max_cyclomatic, 50);
693 assert_eq!(
694 restored.cyclomatic_policy,
695 CyclomaticPolicy::LiteralMembership
696 );
697 assert!((restored.change_coupling.min_degree - 0.4).abs() < f64::EPSILON);
698 assert_eq!(restored.max_cognitive, 40);
699 assert!((restored.max_crap - 75.0).abs() < f64::EPSILON);
700 assert_eq!(restored.crap_refactor_band, 4);
701 assert_eq!(restored.max_unit_size, 120);
702 assert_eq!(restored.max_file_lines, 500);
703 assert_eq!(restored.ignore, vec!["test/**"]);
704 assert_eq!(restored.threshold_overrides.len(), 1);
705 assert_eq!(restored.threshold_overrides[0].max_cyclomatic, Some(30));
706 assert_eq!(restored.threshold_overrides[0].max_crap, Some(45.0));
707 assert!(!restored.suggest_inline_suppression);
708 }
709
710 #[test]
711 fn health_config_threshold_override_omitted_functions_matches_all() {
712 let json = r#"{
713 "thresholdOverrides": [{
714 "files": ["src/auth.ts"],
715 "maxCognitive": 25
716 }]
717 }"#;
718 let config: HealthConfig = serde_json::from_str(json).unwrap();
719 let override_entry = &config.threshold_overrides[0];
720 assert!(override_entry.functions.is_empty());
721 assert_eq!(override_entry.max_cognitive, Some(25));
722 assert!(config.threshold_override_errors().is_empty());
723 }
724
725 #[test]
726 fn health_config_threshold_override_validation_requires_files() {
727 let json = r#"{
728 "thresholdOverrides": [{
729 "files": [],
730 "maxCognitive": 25
731 }]
732 }"#;
733 let config: HealthConfig = serde_json::from_str(json).unwrap();
734 assert_eq!(
735 config.threshold_override_errors(),
736 vec!["health.thresholdOverrides[0].files must contain at least one pattern"]
737 );
738 }
739
740 #[test]
741 fn health_config_threshold_override_validation_requires_threshold() {
742 let json = r#"{
743 "thresholdOverrides": [{
744 "files": ["src/auth.ts"],
745 "reason": "temporary"
746 }]
747 }"#;
748 let config: HealthConfig = serde_json::from_str(json).unwrap();
749 assert_eq!(
750 config.threshold_override_errors(),
751 vec![
752 "health.thresholdOverrides[0] must set at least one of maxCyclomatic, maxCognitive, maxCrap, maxUnitSize, or maxFileLines"
753 ]
754 );
755 }
756
757 #[test]
758 fn health_config_threshold_override_max_unit_size_only_is_valid() {
759 let json = r#"{
760 "thresholdOverrides": [{
761 "files": ["**/*.test.*"],
762 "maxUnitSize": 500
763 }]
764 }"#;
765 let config: HealthConfig = serde_json::from_str(json).unwrap();
766 let override_entry = &config.threshold_overrides[0];
767 assert_eq!(override_entry.max_unit_size, Some(500));
768 assert!(override_entry.max_cyclomatic.is_none());
769 assert!(override_entry.has_any_threshold());
770 assert!(config.threshold_override_errors().is_empty());
771 }
772
773 #[test]
774 fn health_config_threshold_override_max_file_lines() {
775 let json = r#"{
776 "maxFileLines": 500,
777 "thresholdOverrides": [
778 {"files": ["src/generated/**"], "maxFileLines": 0},
779 {"files": ["src/a.ts"], "functions": ["f"], "maxFileLines": 900}
780 ]
781 }"#;
782 let config: HealthConfig = serde_json::from_str(json).unwrap();
783 assert_eq!(config.max_file_lines, 500);
784 assert_eq!(config.threshold_overrides[0].max_file_lines, Some(0));
785 assert!(config.threshold_overrides[0].has_any_threshold());
786 assert_eq!(
787 config.threshold_override_errors(),
788 vec![
789 "health.thresholdOverrides[1].maxFileLines applies to whole files; move it to an entry without functions"
790 ]
791 );
792 assert_eq!(HealthConfig::default().max_file_lines, 0);
793 }
794
795 #[test]
796 fn health_config_json_only_max_unit_size() {
797 let json = r#"{"maxUnitSize": 100}"#;
798 let config: HealthConfig = serde_json::from_str(json).unwrap();
799 assert_eq!(config.max_unit_size, 100);
800 assert_eq!(config.max_cyclomatic, 20); assert!(config.threshold_overrides.is_empty());
802 }
803
804 #[test]
805 fn health_config_threshold_override_rejects_unknown_keys() {
806 let err = serde_json::from_str::<HealthConfig>(
807 r#"{"thresholdOverrides":[{"files":["src/auth.ts"],"maxCogntive":25}]}"#,
808 )
809 .unwrap_err();
810 assert!(err.to_string().contains("maxCogntive"));
811 }
812
813 #[test]
814 fn health_config_suggest_inline_suppression_default_true() {
815 let config = HealthConfig::default();
816 assert!(config.suggest_inline_suppression);
817 }
818
819 #[test]
820 fn health_config_suggest_inline_suppression_explicit_false() {
821 let json = r#"{"suggestInlineSuppression": false}"#;
822 let config: HealthConfig = serde_json::from_str(json).unwrap();
823 assert!(!config.suggest_inline_suppression);
824 }
825
826 #[test]
827 fn health_config_suggest_inline_suppression_omitted_uses_default() {
828 let config: HealthConfig = serde_json::from_str("{}").unwrap();
829 assert!(config.suggest_inline_suppression);
830 }
831
832 #[test]
833 fn health_config_zero_thresholds() {
834 let json = r#"{"maxCyclomatic": 0, "maxCognitive": 0}"#;
835 let config: HealthConfig = serde_json::from_str(json).unwrap();
836 assert_eq!(config.max_cyclomatic, 0);
837 assert_eq!(config.max_cognitive, 0);
838 }
839
840 #[test]
841 fn health_config_large_thresholds() {
842 let json = r#"{"maxCyclomatic": 65535, "maxCognitive": 65535}"#;
843 let config: HealthConfig = serde_json::from_str(json).unwrap();
844 assert_eq!(config.max_cyclomatic, u16::MAX);
845 assert_eq!(config.max_cognitive, u16::MAX);
846 }
847
848 #[test]
849 fn ownership_config_default_has_bot_patterns() {
850 let cfg = OwnershipConfig::default();
851 assert!(cfg.bot_patterns.iter().any(|p| p == r"*\[bot\]*"));
852 assert!(cfg.bot_patterns.iter().any(|p| p == "dependabot*"));
853 assert!(cfg.bot_patterns.iter().any(|p| p == "github-actions*"));
854 assert!(
855 !cfg.bot_patterns.iter().any(|p| p == "*noreply*"),
856 "*noreply* must not be a default bot pattern (filters real human \
857 contributors using GitHub's privacy default email)"
858 );
859 assert_eq!(cfg.email_mode, EmailMode::Handle);
860 }
861
862 #[test]
863 fn ownership_config_json_overrides_defaults() {
864 let json = r#"{
865 "ownership": {
866 "botPatterns": ["custom-bot*"],
867 "emailMode": "raw"
868 }
869 }"#;
870 let config: HealthConfig = serde_json::from_str(json).unwrap();
871 assert_eq!(config.ownership.bot_patterns, vec!["custom-bot*"]);
872 assert_eq!(config.ownership.email_mode, EmailMode::Raw);
873 }
874
875 #[test]
876 fn ownership_config_email_mode_kebab_case() {
877 for (mode, repr) in [
878 (EmailMode::Raw, "\"raw\""),
879 (EmailMode::Handle, "\"handle\""),
880 (EmailMode::Anonymized, "\"anonymized\""),
881 (EmailMode::Hash, "\"hash\""),
882 ] {
883 let s = serde_json::to_string(&mode).unwrap();
884 assert_eq!(s, repr);
885 let back: EmailMode = serde_json::from_str(repr).unwrap();
886 assert_eq!(back, mode);
887 }
888 }
889
890 #[test]
891 fn ownership_config_email_mode_accepts_legacy_hash_alias() {
892 let back: EmailMode = serde_json::from_str("\"hash\"").unwrap();
893 assert_eq!(back, EmailMode::Hash);
894 }
895}