1#[derive(Debug, Clone, serde::Serialize)]
8#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
9#[allow(
10 clippy::struct_field_names,
11 reason = "triggered in bin but not lib, #[expect] would be unfulfilled in lib"
12)]
13pub struct TargetThresholds {
14 pub fan_in_p95: f64,
16 pub fan_in_p75: f64,
18 pub fan_out_p95: f64,
20 pub fan_out_p90: usize,
22}
23
24#[derive(Debug, Clone, serde::Serialize)]
26#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
27#[serde(rename_all = "snake_case")]
28pub enum RecommendationCategory {
29 UrgentChurnComplexity,
31 BreakCircularDependency,
33 SplitHighImpact,
35 RemoveDeadCode,
37 ExtractComplexFunctions,
39 ExtractDependencies,
41 AddTestCoverage,
43}
44
45impl RecommendationCategory {
46 #[must_use]
48 pub const fn label(&self) -> &'static str {
49 match self {
50 Self::UrgentChurnComplexity => "churn+complexity",
51 Self::BreakCircularDependency => "circular dependency",
52 Self::SplitHighImpact => "high impact",
53 Self::RemoveDeadCode => "dead code",
54 Self::ExtractComplexFunctions => "complexity",
55 Self::ExtractDependencies => "coupling",
56 Self::AddTestCoverage => "untested risk",
57 }
58 }
59
60 #[must_use]
62 pub const fn compact_label(&self) -> &'static str {
63 match self {
64 Self::UrgentChurnComplexity => "churn_complexity",
65 Self::BreakCircularDependency => "circular_dep",
66 Self::SplitHighImpact => "high_impact",
67 Self::RemoveDeadCode => "dead_code",
68 Self::ExtractComplexFunctions => "complexity",
69 Self::ExtractDependencies => "coupling",
70 Self::AddTestCoverage => "untested_risk",
71 }
72 }
73}
74
75#[derive(Debug, Clone, serde::Serialize)]
77#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
78pub struct ContributingFactor {
79 pub metric: &'static str,
81 pub value: f64,
83 pub threshold: f64,
85 pub detail: String,
87}
88
89#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
109#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
110#[serde(rename_all = "snake_case")]
111pub enum EffortEstimate {
112 Low,
114 Medium,
116 High,
118}
119
120impl EffortEstimate {
121 #[must_use]
123 pub const fn label(&self) -> &'static str {
124 match self {
125 Self::Low => "low",
126 Self::Medium => "medium",
127 Self::High => "high",
128 }
129 }
130
131 #[must_use]
133 pub const fn numeric(&self) -> f64 {
134 match self {
135 Self::Low => 1.0,
136 Self::Medium => 2.0,
137 Self::High => 3.0,
138 }
139 }
140}
141
142#[derive(Debug, Clone, serde::Serialize)]
149#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
150#[serde(rename_all = "snake_case")]
151pub enum Confidence {
152 High,
154 Medium,
156 Low,
158}
159
160impl Confidence {
161 #[must_use]
163 pub const fn label(&self) -> &'static str {
164 match self {
165 Self::High => "high",
166 Self::Medium => "medium",
167 Self::Low => "low",
168 }
169 }
170}
171
172#[derive(Debug, Clone, Default, serde::Serialize)]
177#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
178pub struct TargetEvidence {
179 #[serde(default, skip_serializing_if = "Vec::is_empty")]
181 pub unused_exports: Vec<String>,
182 #[serde(default, skip_serializing_if = "Vec::is_empty")]
184 pub complex_functions: Vec<EvidenceFunction>,
185 #[serde(default, skip_serializing_if = "Vec::is_empty")]
187 pub cycle_path: Vec<String>,
188 #[serde(default, skip_serializing_if = "Vec::is_empty")]
190 pub direct_callers: Vec<DirectCallerEvidence>,
191 #[serde(default, skip_serializing_if = "Vec::is_empty")]
193 pub clone_siblings: Vec<CloneSiblingEvidence>,
194}
195
196#[derive(Debug, Clone, serde::Serialize)]
198#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
199pub struct DirectCallerEvidence {
200 #[serde(serialize_with = "fallow_types::serde_path::serialize")]
202 pub path: std::path::PathBuf,
203 #[serde(default, skip_serializing_if = "Vec::is_empty")]
205 pub symbols: Vec<DirectCallerSymbolEvidence>,
206}
207
208#[derive(Debug, Clone, serde::Serialize)]
210#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
211pub struct DirectCallerSymbolEvidence {
212 pub imported: String,
214 pub local: String,
216 pub type_only: bool,
218}
219
220#[derive(Debug, Clone, serde::Serialize)]
222#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
223pub struct CloneSiblingEvidence {
224 #[serde(serialize_with = "fallow_types::serde_path::serialize")]
226 pub path: std::path::PathBuf,
227 pub start_line: usize,
229 pub end_line: usize,
231 pub fingerprint: String,
233}
234
235#[derive(Debug, Clone, serde::Serialize)]
237#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
238pub struct EvidenceFunction {
239 pub name: String,
241 pub line: u32,
243 pub cognitive: u16,
245}
246
247#[derive(Debug, Clone, serde::Serialize)]
248#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
249pub struct RefactoringTarget {
250 #[serde(serialize_with = "fallow_types::serde_path::serialize")]
252 pub path: std::path::PathBuf,
253 pub priority: f64,
255 pub efficiency: f64,
258 pub recommendation: String,
260 pub category: RecommendationCategory,
262 pub effort: EffortEstimate,
264 pub confidence: Confidence,
266 #[serde(default, skip_serializing_if = "Vec::is_empty")]
269 #[cfg_attr(feature = "schema", schemars(default))]
270 pub factors: Vec<ContributingFactor>,
271 #[serde(default, skip_serializing_if = "Option::is_none")]
273 pub evidence: Option<TargetEvidence>,
274}
275
276#[cfg(test)]
277#[allow(
278 clippy::unwrap_used,
279 reason = "tests use unwrap to keep serialization assertions concise"
280)]
281mod tests {
282 use super::*;
283
284 #[test]
285 fn category_labels_are_non_empty() {
286 let categories = [
287 RecommendationCategory::UrgentChurnComplexity,
288 RecommendationCategory::BreakCircularDependency,
289 RecommendationCategory::SplitHighImpact,
290 RecommendationCategory::RemoveDeadCode,
291 RecommendationCategory::ExtractComplexFunctions,
292 RecommendationCategory::ExtractDependencies,
293 RecommendationCategory::AddTestCoverage,
294 ];
295 for cat in &categories {
296 assert!(!cat.label().is_empty(), "{cat:?} should have a label");
297 }
298 }
299
300 #[test]
301 fn category_labels_are_unique() {
302 let categories = [
303 RecommendationCategory::UrgentChurnComplexity,
304 RecommendationCategory::BreakCircularDependency,
305 RecommendationCategory::SplitHighImpact,
306 RecommendationCategory::RemoveDeadCode,
307 RecommendationCategory::ExtractComplexFunctions,
308 RecommendationCategory::ExtractDependencies,
309 RecommendationCategory::AddTestCoverage,
310 ];
311 let labels: Vec<&str> = categories
312 .iter()
313 .map(RecommendationCategory::label)
314 .collect();
315 let unique: std::collections::BTreeSet<&&str> = labels.iter().collect();
316 assert_eq!(labels.len(), unique.len(), "category labels must be unique");
317 }
318
319 #[test]
320 fn category_serializes_as_snake_case() {
321 let json = serde_json::to_string(&RecommendationCategory::UrgentChurnComplexity).unwrap();
322 assert_eq!(json, r#""urgent_churn_complexity""#);
323
324 let json = serde_json::to_string(&RecommendationCategory::BreakCircularDependency).unwrap();
325 assert_eq!(json, r#""break_circular_dependency""#);
326 }
327
328 #[test]
329 fn refactoring_target_skips_empty_factors() {
330 let target = RefactoringTarget {
331 path: std::path::PathBuf::from("/src/foo.ts"),
332 priority: 75.0,
333 efficiency: 75.0,
334 recommendation: "Test recommendation".into(),
335 category: RecommendationCategory::RemoveDeadCode,
336 effort: EffortEstimate::Low,
337 confidence: Confidence::High,
338 factors: vec![],
339 evidence: None,
340 };
341 let json = serde_json::to_string(&target).unwrap();
342 assert!(!json.contains("factors"));
343 assert!(!json.contains("evidence"));
344 }
345
346 #[test]
347 fn effort_numeric_values() {
348 assert!((EffortEstimate::Low.numeric() - 1.0).abs() < f64::EPSILON);
349 assert!((EffortEstimate::Medium.numeric() - 2.0).abs() < f64::EPSILON);
350 assert!((EffortEstimate::High.numeric() - 3.0).abs() < f64::EPSILON);
351 }
352
353 #[test]
354 fn confidence_labels_are_non_empty() {
355 let levels = [Confidence::High, Confidence::Medium, Confidence::Low];
356 for level in &levels {
357 assert!(!level.label().is_empty(), "{level:?} should have a label");
358 }
359 }
360
361 #[test]
362 fn confidence_serializes_as_snake_case() {
363 let json = serde_json::to_string(&Confidence::High).unwrap();
364 assert_eq!(json, r#""high""#);
365 let json = serde_json::to_string(&Confidence::Medium).unwrap();
366 assert_eq!(json, r#""medium""#);
367 let json = serde_json::to_string(&Confidence::Low).unwrap();
368 assert_eq!(json, r#""low""#);
369 }
370
371 #[test]
372 fn contributing_factor_serializes_correctly() {
373 let factor = ContributingFactor {
374 metric: "fan_in",
375 value: 15.0,
376 threshold: 10.0,
377 detail: "15 files depend on this".into(),
378 };
379 let json = serde_json::to_string(&factor).unwrap();
380 let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();
381 assert_eq!(parsed["metric"], "fan_in");
382 assert_eq!(parsed["value"], 15.0);
383 assert_eq!(parsed["threshold"], 10.0);
384 }
385
386 #[test]
387 fn category_compact_labels_are_non_empty() {
388 let categories = [
389 RecommendationCategory::UrgentChurnComplexity,
390 RecommendationCategory::BreakCircularDependency,
391 RecommendationCategory::SplitHighImpact,
392 RecommendationCategory::RemoveDeadCode,
393 RecommendationCategory::ExtractComplexFunctions,
394 RecommendationCategory::ExtractDependencies,
395 RecommendationCategory::AddTestCoverage,
396 ];
397 for cat in &categories {
398 assert!(
399 !cat.compact_label().is_empty(),
400 "{cat:?} should have a compact_label"
401 );
402 }
403 }
404
405 #[test]
406 fn category_compact_labels_are_unique() {
407 let categories = [
408 RecommendationCategory::UrgentChurnComplexity,
409 RecommendationCategory::BreakCircularDependency,
410 RecommendationCategory::SplitHighImpact,
411 RecommendationCategory::RemoveDeadCode,
412 RecommendationCategory::ExtractComplexFunctions,
413 RecommendationCategory::ExtractDependencies,
414 RecommendationCategory::AddTestCoverage,
415 ];
416 let labels: Vec<&str> = categories
417 .iter()
418 .map(RecommendationCategory::compact_label)
419 .collect();
420 let unique: std::collections::BTreeSet<&&str> = labels.iter().collect();
421 assert_eq!(labels.len(), unique.len(), "compact labels must be unique");
422 }
423
424 #[test]
425 fn category_compact_labels_have_no_spaces() {
426 let categories = [
427 RecommendationCategory::UrgentChurnComplexity,
428 RecommendationCategory::BreakCircularDependency,
429 RecommendationCategory::SplitHighImpact,
430 RecommendationCategory::RemoveDeadCode,
431 RecommendationCategory::ExtractComplexFunctions,
432 RecommendationCategory::ExtractDependencies,
433 RecommendationCategory::AddTestCoverage,
434 ];
435 for cat in &categories {
436 assert!(
437 !cat.compact_label().contains(' '),
438 "compact_label for {:?} should not contain spaces: '{}'",
439 cat,
440 cat.compact_label()
441 );
442 }
443 }
444
445 #[test]
446 fn effort_labels_are_non_empty() {
447 let efforts = [
448 EffortEstimate::Low,
449 EffortEstimate::Medium,
450 EffortEstimate::High,
451 ];
452 for effort in &efforts {
453 assert!(!effort.label().is_empty(), "{effort:?} should have a label");
454 }
455 }
456
457 #[test]
458 fn effort_serializes_as_snake_case() {
459 assert_eq!(
460 serde_json::to_string(&EffortEstimate::Low).unwrap(),
461 r#""low""#
462 );
463 assert_eq!(
464 serde_json::to_string(&EffortEstimate::Medium).unwrap(),
465 r#""medium""#
466 );
467 assert_eq!(
468 serde_json::to_string(&EffortEstimate::High).unwrap(),
469 r#""high""#
470 );
471 }
472
473 #[test]
474 fn target_evidence_skips_empty_fields() {
475 let evidence = TargetEvidence {
476 unused_exports: vec![],
477 complex_functions: vec![],
478 cycle_path: vec![],
479 direct_callers: vec![],
480 clone_siblings: vec![],
481 };
482 let json = serde_json::to_string(&evidence).unwrap();
483 assert!(!json.contains("unused_exports"));
484 assert!(!json.contains("complex_functions"));
485 assert!(!json.contains("cycle_path"));
486 assert!(!json.contains("direct_callers"));
487 assert!(!json.contains("clone_siblings"));
488 }
489
490 #[test]
491 fn target_evidence_with_data() {
492 let evidence = TargetEvidence {
493 unused_exports: vec!["foo".to_string(), "bar".to_string()],
494 complex_functions: vec![EvidenceFunction {
495 name: "processData".into(),
496 line: 42,
497 cognitive: 30,
498 }],
499 cycle_path: vec![],
500 direct_callers: vec![DirectCallerEvidence {
501 path: "src/consumer.ts".into(),
502 symbols: vec![DirectCallerSymbolEvidence {
503 imported: "processData".into(),
504 local: "processData".into(),
505 type_only: false,
506 }],
507 }],
508 clone_siblings: vec![CloneSiblingEvidence {
509 path: "src/peer.ts".into(),
510 start_line: 12,
511 end_line: 20,
512 fingerprint: "dup:12345678".into(),
513 }],
514 };
515 let json = serde_json::to_string(&evidence).unwrap();
516 assert!(json.contains("unused_exports"));
517 assert!(json.contains("complex_functions"));
518 assert!(json.contains("processData"));
519 assert!(json.contains("direct_callers"));
520 assert!(json.contains("clone_siblings"));
521 assert!(!json.contains("cycle_path"));
522 }
523}