barad-dur 0.18.0

The all-seeing repository analyzer
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
use anyhow::Result;
use serde_json::{json, Value};

use crate::scorer::AnalysisReport;
use crate::trend::TrendSummary;

/// Render the analysis report as JSON.
///
/// When `trend` is `Some(summary)`, injects a top-level "trend" object into the
/// JSON output. When `None`, the output is structurally identical to pre-trend
/// runs (no "trend" key).
pub fn render(
    report: &AnalysisReport,
    pretty: bool,
    trend: Option<&TrendSummary>,
) -> Result<String> {
    let mut value: Value = serde_json::to_value(report)?;

    if let Some(summary) = trend {
        let trend_object = build_trend_object(summary);
        if let Value::Object(ref mut map) = value {
            map.insert("trend".to_string(), trend_object);
        }
    }

    if pretty {
        Ok(serde_json::to_string_pretty(&value)?)
    } else {
        Ok(serde_json::to_string(&value)?)
    }
}

fn build_trend_object(summary: &TrendSummary) -> Value {
    let velocity_per_week = match &summary.velocity {
        None => Value::Null,
        Some(v) => json!(v.points_per_run),
    };

    let direction = match &summary.velocity {
        None => "stable",
        Some(v) => match v.direction {
            crate::trend::VelocityDirection::Improving => "improving",
            crate::trend::VelocityDirection::Declining => "declining",
            crate::trend::VelocityDirection::Stable => "stable",
        },
    };

    let delta_vs_last = if summary.delta.is_first {
        Value::Null
    } else {
        json!(summary.delta.overall)
    };

    let delta_vs_oldest = if summary.delta.is_first {
        Value::Null
    } else {
        json!(summary.delta.delta_vs_oldest)
    };

    let snapshots: Vec<Value> = summary
        .history
        .iter()
        .map(|entry| {
            json!({
                "timestamp": entry.timestamp.to_rfc3339(),
                "commit": entry.head,
                "branch": entry.branch,
                "overall_score": entry.overall_score,
                "category_scores": entry.categories,
            })
        })
        .collect();

    json!({
        "schema_version": 1,
        "direction": direction,
        "delta_vs_last": delta_vs_last,
        "delta_vs_oldest": delta_vs_oldest,
        "velocity_per_week": velocity_per_week,
        "snapshots": snapshots,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::metrics::{CategoryResult, MetricValue, RawValue};
    use crate::scorer::ActionItem;

    fn make_report() -> AnalysisReport {
        AnalysisReport {
            repo_name: "test-repo".into(),
            branch: "main".into(),
            time_window_months: 6,
            total_commits: 100,
            total_authors: 5,
            total_files: 50,
            overall_score: 72,
            categories: vec![CategoryResult {
                name: "Health".into(),
                score: 72,
                metrics: vec![MetricValue {
                    name: "Bus factor".into(),
                    description: "2 (risky)".into(),
                    raw_value: RawValue::Integer(2),
                    score: Some(50),
                }],
            }],
            top_actions: vec![ActionItem {
                text: "Fix bus factor".into(),
                target_tab: Some("ownership".into()),
                sort_by: None,
            }],
            remote_meta: None,
            file_hotspots: vec![],
            coupling_pairs: vec![],
            author_ownership: vec![],
            file_ages: vec![],
            author_cards: vec![],
            history: vec![],
            dep_ecosystem_reports: vec![],
            audit: None,
            per_file_coupling: vec![],
            import_edges: vec![],
            import_cycles: vec![],
            score_thresholds: Default::default(),
        }
    }

    #[test]
    fn json_unscored_metric_serializes_as_null() {
        let mut report = make_report();
        report.categories[0].metrics.push(MetricValue {
            name: "Knowledge distribution".into(),
            description: "Solo project — not applicable".into(),
            raw_value: RawValue::Text("N/A".into()),
            score: None,
        });
        let output = render(&report, false, None).unwrap();
        assert!(
            output.contains(r#""score":null"#),
            "insufficient-data metrics must serialize score as null, not a number"
        );
    }

    #[test]
    fn json_output_is_valid() {
        let report = make_report();
        let output = render(&report, false, None).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&output).unwrap();
        assert!(parsed.is_object());
    }

    #[test]
    fn json_contains_expected_fields() {
        let report = make_report();
        let output = render(&report, false, None).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&output).unwrap();
        assert!(parsed["overall_score"].is_number());
        assert!(parsed["categories"].is_array());
        assert!(parsed["top_actions"].is_array());
        assert!(parsed["repo_name"].is_string());
    }

    #[test]
    fn pretty_mode_is_indented() {
        let report = make_report();
        let output = render(&report, true, None).unwrap();
        assert!(output.contains('\n'));
        assert!(output.contains("  ")); // indentation
    }

    #[test]
    fn compact_mode_is_single_line() {
        let report = make_report();
        let output = render(&report, false, None).unwrap();
        // Compact JSON should not have newlines (except possibly within string values)
        assert!(!output.starts_with("{\n"));
    }

    #[test]
    fn json_render_without_trend_data_has_no_trend_key() {
        let report = make_report();
        let output = render(&report, false, None).unwrap();
        assert!(
            !output.contains("\"trend\""),
            "JSON output should not contain 'trend' key when trend_data is None"
        );
    }

    #[test]
    fn json_render_with_trend_first_run_has_null_velocity() {
        use crate::trend::{TrendDelta, TrendSummary};
        use std::collections::HashMap;

        let report = make_report();
        let summary = TrendSummary {
            delta: TrendDelta {
                overall: 0,
                delta_vs_oldest: 0,
                categories: HashMap::new(),
                is_first: true,
            },
            sparkline: vec![],
            velocity: None,
            branch_mismatch_warning: false,
            history: vec![],
        };

        let output = render(&report, false, Some(&summary)).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&output).unwrap();

        let trend = parsed
            .get("trend")
            .expect("trend key must be present when Some(summary) is passed");
        assert!(
            trend.as_object().unwrap().contains_key("velocity_per_week"),
            "trend.velocity_per_week key must be present (as null)"
        );
        assert!(
            trend["velocity_per_week"].is_null(),
            "trend.velocity_per_week should be JSON null when velocity is None, got: {}",
            trend["velocity_per_week"]
        );
        assert_eq!(
            trend["schema_version"].as_i64().unwrap(),
            1,
            "trend.schema_version should be 1"
        );
    }

    #[test]
    fn json_output_contains_per_file_coupling_array() {
        use crate::scorer::FileCouplingMetrics;

        let mut report = make_report();
        report.per_file_coupling = vec![FileCouplingMetrics {
            path: "src/lib.rs".into(),
            ca: 3,
            ce: 5,
            instability: 0.625,
        }];

        let output = render(&report, false, None).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&output).unwrap();

        let arr = parsed["per_file_coupling"]
            .as_array()
            .expect("per_file_coupling must be a JSON array");
        assert_eq!(arr.len(), 1, "array must contain exactly one entry");

        let entry = &arr[0];
        assert_eq!(
            entry["path"].as_str().unwrap(),
            "src/lib.rs",
            "entry.path must match"
        );
        assert_eq!(entry["ca"].as_u64().unwrap(), 3, "entry.ca must match");
        assert_eq!(entry["ce"].as_u64().unwrap(), 5, "entry.ce must match");
        assert!(
            (entry["instability"].as_f64().unwrap() - 0.625).abs() < 1e-9,
            "entry.instability must match"
        );
    }

    #[test]
    fn json_output_contains_import_edges_array() {
        use crate::scorer::ImportEdge;

        let mut report = make_report();
        report.import_edges = vec![ImportEdge {
            from: "src/main.rs".into(),
            to: "src/lib.rs".into(),
        }];

        let output = render(&report, false, None).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&output).unwrap();

        let arr = parsed["import_edges"]
            .as_array()
            .expect("import_edges must be a JSON array");
        assert_eq!(arr.len(), 1, "array must contain exactly one edge");
        assert_eq!(arr[0]["from"].as_str().unwrap(), "src/main.rs");
        assert_eq!(arr[0]["to"].as_str().unwrap(), "src/lib.rs");
    }

    #[test]
    fn json_output_contains_import_cycles_array() {
        let mut report = make_report();
        report.import_cycles = vec![vec!["src/a.rs".into(), "src/b.rs".into()]];

        let output = render(&report, false, None).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&output).unwrap();

        let arr = parsed["import_cycles"]
            .as_array()
            .expect("import_cycles must be a JSON array");
        assert_eq!(arr.len(), 1);
        assert_eq!(arr[0][0].as_str().unwrap(), "src/a.rs");
        assert_eq!(arr[0][1].as_str().unwrap(), "src/b.rs");
    }

    #[test]
    fn json_output_per_file_coupling_empty_when_no_data() {
        let report = make_report(); // per_file_coupling is already vec![]

        let output = render(&report, false, None).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&output).unwrap();

        let arr = parsed["per_file_coupling"]
            .as_array()
            .expect("per_file_coupling must be present as an empty JSON array, not absent");
        assert!(arr.is_empty(), "per_file_coupling array must be empty");
    }

    #[test]
    fn json_render_trend_snapshots_have_required_fields() {
        use crate::scorer::HistoryEntry;
        use crate::trend::{TrendDelta, TrendSummary};
        use std::collections::HashMap;

        let report = make_report();

        let mut categories = HashMap::new();
        categories.insert("Health".to_string(), 30u32);
        categories.insert("Team".to_string(), 18u32);
        categories.insert("Evolution".to_string(), 14u32);
        categories.insert("Git Hygiene".to_string(), 10u32);

        let entry = HistoryEntry {
            timestamp: chrono::DateTime::parse_from_rfc3339("2025-01-01T00:00:00Z")
                .unwrap()
                .with_timezone(&chrono::Utc),
            head: "abc1234def5678901234567890abcdef12345678".to_string(),
            overall_score: 72,
            categories,
            metrics: HashMap::new(),
            counts: crate::scorer::HistoryCounts::default(),
            branch: "main".to_string(),
            schema_version: 1,
            source: None,
        };

        let summary = TrendSummary {
            delta: TrendDelta {
                overall: 0,
                delta_vs_oldest: 0,
                categories: HashMap::new(),
                is_first: true,
            },
            sparkline: vec![],
            velocity: None,
            branch_mismatch_warning: false,
            history: vec![entry],
        };

        let output = render(&report, false, Some(&summary)).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&output).unwrap();

        let trend = parsed.get("trend").expect("trend key must be present");
        let snapshots = trend["snapshots"]
            .as_array()
            .expect("trend.snapshots should be an array");
        assert_eq!(snapshots.len(), 1, "should have exactly 1 snapshot");

        let snap = &snapshots[0];
        assert!(
            snap["timestamp"].is_string(),
            "snapshot.timestamp should be a string"
        );
        let ts = snap["timestamp"].as_str().unwrap();
        assert!(
            ts.ends_with('Z') || ts.contains('+'),
            "snapshot.timestamp should be ISO8601 UTC, got: {ts}"
        );
        assert!(
            snap["commit"].is_string(),
            "snapshot.commit should be a string (mapped from head)"
        );
        assert_eq!(
            snap["commit"].as_str().unwrap(),
            "abc1234def5678901234567890abcdef12345678",
            "snapshot.commit should contain the full SHA"
        );
        assert!(
            snap["branch"].is_string(),
            "snapshot.branch should be a string"
        );
        assert_eq!(
            snap["branch"].as_str().unwrap(),
            "main",
            "snapshot.branch should match entry branch"
        );
        assert!(
            snap["overall_score"].is_number(),
            "snapshot.overall_score should be a number"
        );
        assert_eq!(
            snap["overall_score"].as_u64().unwrap(),
            72,
            "snapshot.overall_score should match entry value"
        );

        let cat = snap["category_scores"]
            .as_object()
            .expect("snapshot.category_scores should be an object");
        for key in &["Health", "Team", "Evolution", "Git Hygiene"] {
            assert!(
                cat.contains_key(*key),
                "snapshot.category_scores should contain key '{key}'"
            );
        }
    }
}