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, 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}
189
190#[derive(Debug, Clone, serde::Serialize)]
192#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
193pub struct EvidenceFunction {
194 pub name: String,
196 pub line: u32,
198 pub cognitive: u16,
200}
201
202#[derive(Debug, Clone, serde::Serialize)]
203#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
204pub struct RefactoringTarget {
205 #[serde(serialize_with = "fallow_types::serde_path::serialize")]
207 pub path: std::path::PathBuf,
208 pub priority: f64,
210 pub efficiency: f64,
213 pub recommendation: String,
215 pub category: RecommendationCategory,
217 pub effort: EffortEstimate,
219 pub confidence: Confidence,
221 #[serde(default, skip_serializing_if = "Vec::is_empty")]
224 #[cfg_attr(feature = "schema", schemars(default))]
225 pub factors: Vec<ContributingFactor>,
226 #[serde(default, skip_serializing_if = "Option::is_none")]
228 pub evidence: Option<TargetEvidence>,
229}
230
231#[cfg(test)]
232mod tests {
233 use super::*;
234
235 #[test]
236 fn category_labels_are_non_empty() {
237 let categories = [
238 RecommendationCategory::UrgentChurnComplexity,
239 RecommendationCategory::BreakCircularDependency,
240 RecommendationCategory::SplitHighImpact,
241 RecommendationCategory::RemoveDeadCode,
242 RecommendationCategory::ExtractComplexFunctions,
243 RecommendationCategory::ExtractDependencies,
244 RecommendationCategory::AddTestCoverage,
245 ];
246 for cat in &categories {
247 assert!(!cat.label().is_empty(), "{cat:?} should have a label");
248 }
249 }
250
251 #[test]
252 fn category_labels_are_unique() {
253 let categories = [
254 RecommendationCategory::UrgentChurnComplexity,
255 RecommendationCategory::BreakCircularDependency,
256 RecommendationCategory::SplitHighImpact,
257 RecommendationCategory::RemoveDeadCode,
258 RecommendationCategory::ExtractComplexFunctions,
259 RecommendationCategory::ExtractDependencies,
260 RecommendationCategory::AddTestCoverage,
261 ];
262 let labels: Vec<&str> = categories
263 .iter()
264 .map(RecommendationCategory::label)
265 .collect();
266 let unique: rustc_hash::FxHashSet<&&str> = labels.iter().collect();
267 assert_eq!(labels.len(), unique.len(), "category labels must be unique");
268 }
269
270 #[test]
271 fn category_serializes_as_snake_case() {
272 let json = serde_json::to_string(&RecommendationCategory::UrgentChurnComplexity).unwrap();
273 assert_eq!(json, r#""urgent_churn_complexity""#);
274
275 let json = serde_json::to_string(&RecommendationCategory::BreakCircularDependency).unwrap();
276 assert_eq!(json, r#""break_circular_dependency""#);
277 }
278
279 #[test]
280 fn refactoring_target_skips_empty_factors() {
281 let target = RefactoringTarget {
282 path: std::path::PathBuf::from("/src/foo.ts"),
283 priority: 75.0,
284 efficiency: 75.0,
285 recommendation: "Test recommendation".into(),
286 category: RecommendationCategory::RemoveDeadCode,
287 effort: EffortEstimate::Low,
288 confidence: Confidence::High,
289 factors: vec![],
290 evidence: None,
291 };
292 let json = serde_json::to_string(&target).unwrap();
293 assert!(!json.contains("factors"));
294 assert!(!json.contains("evidence"));
295 }
296
297 #[test]
298 fn effort_numeric_values() {
299 assert!((EffortEstimate::Low.numeric() - 1.0).abs() < f64::EPSILON);
300 assert!((EffortEstimate::Medium.numeric() - 2.0).abs() < f64::EPSILON);
301 assert!((EffortEstimate::High.numeric() - 3.0).abs() < f64::EPSILON);
302 }
303
304 #[test]
305 fn confidence_labels_are_non_empty() {
306 let levels = [Confidence::High, Confidence::Medium, Confidence::Low];
307 for level in &levels {
308 assert!(!level.label().is_empty(), "{level:?} should have a label");
309 }
310 }
311
312 #[test]
313 fn confidence_serializes_as_snake_case() {
314 let json = serde_json::to_string(&Confidence::High).unwrap();
315 assert_eq!(json, r#""high""#);
316 let json = serde_json::to_string(&Confidence::Medium).unwrap();
317 assert_eq!(json, r#""medium""#);
318 let json = serde_json::to_string(&Confidence::Low).unwrap();
319 assert_eq!(json, r#""low""#);
320 }
321
322 #[test]
323 fn contributing_factor_serializes_correctly() {
324 let factor = ContributingFactor {
325 metric: "fan_in",
326 value: 15.0,
327 threshold: 10.0,
328 detail: "15 files depend on this".into(),
329 };
330 let json = serde_json::to_string(&factor).unwrap();
331 let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();
332 assert_eq!(parsed["metric"], "fan_in");
333 assert_eq!(parsed["value"], 15.0);
334 assert_eq!(parsed["threshold"], 10.0);
335 }
336
337 #[test]
338 fn category_compact_labels_are_non_empty() {
339 let categories = [
340 RecommendationCategory::UrgentChurnComplexity,
341 RecommendationCategory::BreakCircularDependency,
342 RecommendationCategory::SplitHighImpact,
343 RecommendationCategory::RemoveDeadCode,
344 RecommendationCategory::ExtractComplexFunctions,
345 RecommendationCategory::ExtractDependencies,
346 RecommendationCategory::AddTestCoverage,
347 ];
348 for cat in &categories {
349 assert!(
350 !cat.compact_label().is_empty(),
351 "{cat:?} should have a compact_label"
352 );
353 }
354 }
355
356 #[test]
357 fn category_compact_labels_are_unique() {
358 let categories = [
359 RecommendationCategory::UrgentChurnComplexity,
360 RecommendationCategory::BreakCircularDependency,
361 RecommendationCategory::SplitHighImpact,
362 RecommendationCategory::RemoveDeadCode,
363 RecommendationCategory::ExtractComplexFunctions,
364 RecommendationCategory::ExtractDependencies,
365 RecommendationCategory::AddTestCoverage,
366 ];
367 let labels: Vec<&str> = categories
368 .iter()
369 .map(RecommendationCategory::compact_label)
370 .collect();
371 let unique: rustc_hash::FxHashSet<&&str> = labels.iter().collect();
372 assert_eq!(labels.len(), unique.len(), "compact labels must be unique");
373 }
374
375 #[test]
376 fn category_compact_labels_have_no_spaces() {
377 let categories = [
378 RecommendationCategory::UrgentChurnComplexity,
379 RecommendationCategory::BreakCircularDependency,
380 RecommendationCategory::SplitHighImpact,
381 RecommendationCategory::RemoveDeadCode,
382 RecommendationCategory::ExtractComplexFunctions,
383 RecommendationCategory::ExtractDependencies,
384 RecommendationCategory::AddTestCoverage,
385 ];
386 for cat in &categories {
387 assert!(
388 !cat.compact_label().contains(' '),
389 "compact_label for {:?} should not contain spaces: '{}'",
390 cat,
391 cat.compact_label()
392 );
393 }
394 }
395
396 #[test]
397 fn effort_labels_are_non_empty() {
398 let efforts = [
399 EffortEstimate::Low,
400 EffortEstimate::Medium,
401 EffortEstimate::High,
402 ];
403 for effort in &efforts {
404 assert!(!effort.label().is_empty(), "{effort:?} should have a label");
405 }
406 }
407
408 #[test]
409 fn effort_serializes_as_snake_case() {
410 assert_eq!(
411 serde_json::to_string(&EffortEstimate::Low).unwrap(),
412 r#""low""#
413 );
414 assert_eq!(
415 serde_json::to_string(&EffortEstimate::Medium).unwrap(),
416 r#""medium""#
417 );
418 assert_eq!(
419 serde_json::to_string(&EffortEstimate::High).unwrap(),
420 r#""high""#
421 );
422 }
423
424 #[test]
425 fn target_evidence_skips_empty_fields() {
426 let evidence = TargetEvidence {
427 unused_exports: vec![],
428 complex_functions: vec![],
429 cycle_path: vec![],
430 };
431 let json = serde_json::to_string(&evidence).unwrap();
432 assert!(!json.contains("unused_exports"));
433 assert!(!json.contains("complex_functions"));
434 assert!(!json.contains("cycle_path"));
435 }
436
437 #[test]
438 fn target_evidence_with_data() {
439 let evidence = TargetEvidence {
440 unused_exports: vec!["foo".to_string(), "bar".to_string()],
441 complex_functions: vec![EvidenceFunction {
442 name: "processData".into(),
443 line: 42,
444 cognitive: 30,
445 }],
446 cycle_path: vec![],
447 };
448 let json = serde_json::to_string(&evidence).unwrap();
449 assert!(json.contains("unused_exports"));
450 assert!(json.contains("complex_functions"));
451 assert!(json.contains("processData"));
452 assert!(!json.contains("cycle_path"));
453 }
454}