crap-core 0.5.0

Language-agnostic foundation for the CRAP analyzer family — domain types, port traits, and shared invariants for crap4rs / future crap4ts.
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
//! JSON reporter — formats an `AnalysisView` as structured JSON
//! with a versioned envelope for CI pipelines and tooling consumption.
//!
//! The envelope carries both the unshapeable underlying analysis
//! (`result`) and the View metadata (`view`) describing how the
//! reported rows were filtered, sorted, and truncated. `view.full` is
//! `#[serde(skip)]` so the analysis is emitted exactly once.

use crate::domain::delta::{DeltaSummary, DeltaView, DeltaViewSpec, FunctionChange};
use crate::domain::types::{
    AnalysisDiagnostics, AnalysisResult, AnalysisSummary, ComplexityMetric, FunctionVerdict,
};
use crate::domain::view::{AnalysisView, GroupedView, ViewSpec};
use crate::ports::ParseDiagnostic;
use serde::Serialize;

/// Configuration for the JSON envelope metadata.
///
/// Generic over `P: ParseDiagnostic` since `AnalysisDiagnostics<P>`
/// is generic across adapters. crap4rs concretizes via the
/// `JsonConfig<'a>` type alias in `crap4rs::adapters::reporters::json`
/// so v0.4 callers' type paths stay byte-identical.
#[derive(Debug)]
pub struct JsonConfig<'a, P: ParseDiagnostic> {
    pub tool_version: String,
    pub metric: ComplexityMetric,
    pub threshold: f64,
    pub timestamp: String,
    /// When present, diagnostics are included in the JSON output (--verbose).
    pub diagnostics: Option<&'a AnalysisDiagnostics<P>>,
    /// Git ref used for diff filtering (`--diff <ref>`). `None` when not in diff mode.
    pub diff_ref: Option<&'a str>,
    /// When true, the per-row `view.shown` array is omitted (`--minimal-view`).
    /// All other view metadata (`spec`, `eligible_count`, `truncated`,
    /// `shown_summary`) is preserved so consumers retain scope context.
    pub minimal_view: bool,
    /// When present, the envelope grows a top-level `delta` block
    /// describing changes vs the baseline. None means no `--baseline`
    /// was passed; the `delta` key is omitted entirely.
    pub delta: Option<DeltaContext<'a, P>>,
}

/// Bundles everything the JSON reporter needs to render the `delta`
/// block: the shaped view (post-filter / sort / truncate) plus the
/// underlying delta and baseline metadata captured when the baseline
/// envelope was loaded.
///
/// Generic over `P: ParseDiagnostic` — see `JsonConfig` for the
/// rationale.
#[derive(Debug)]
pub struct DeltaContext<'a, P: ParseDiagnostic> {
    /// Shaped view — drives `shown`, `spec`, `eligible_count`, `truncated`.
    pub view: &'a DeltaView<'a>,
    pub baseline_tool_version: &'a str,
    pub baseline_timestamp: &'a str,
    pub baseline_diagnostics: Option<&'a AnalysisDiagnostics<P>>,
}

/// JSON envelope. Field order is **load-bearing** —
/// `tests/features/cli_ergonomics.feature:243-246` asserts the
/// declaration order is exactly:
///   schema_version, tool_version, language, timestamp, metric,
///   threshold, diff_ref, result, view
/// Per ADR D2 the schema is additive across minor versions; the
/// `schema_version` bump from 1 → 2 in 0.4.0 reflects the
/// `ComplexityContributor.column` 0-based → 1-based convention shift.
/// Older v1 baselines remain loadable for delta reporting (matching
/// is identity-keyed, not column-keyed).
#[derive(Serialize)]
#[serde(bound = "")]
struct JsonEnvelope<'a, P: ParseDiagnostic> {
    schema_version: u32,
    tool_version: &'a str,
    language: &'static str,
    timestamp: &'a str,
    metric: &'a ComplexityMetric,
    threshold: f64,
    diff_ref: Option<&'a str>,
    result: &'a AnalysisResult,
    view: ViewWire<'a>,
    /// Delta block. Present iff `--baseline` was passed; absent (key
    /// elided) otherwise so existing consumers see byte-identical
    /// output for the no-delta case. Additive — does not itself bump
    /// `schema_version` (ADR D2).
    #[serde(skip_serializing_if = "Option::is_none")]
    delta: Option<DeltaWire<'a, P>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    diagnostics: Option<&'a AnalysisDiagnostics<P>>,
}

/// On-the-wire delta representation. Mirrors the `delta` envelope key
/// shape documented in ADR D7 §DeltaView.
#[derive(Serialize)]
#[serde(bound = "")]
struct DeltaWire<'a, P: ParseDiagnostic> {
    /// Aggregate counts over the *unshaped* change set. The gate
    /// keystone — shaping never alters this.
    summary: &'a DeltaSummary,
    /// Echoes the resolved [`DeltaViewSpec`] so consumers can
    /// reconstruct what filters / sort / limit produced `shown`.
    spec: &'a DeltaViewSpec,
    /// Post-filter, pre-truncate count. With `truncated`, lets
    /// consumers render "Showing X of Y".
    eligible_count: usize,
    truncated: bool,
    /// Reserved for a future `--baseline-ref <label>` flag (F2 follow-up).
    /// Always `null` today.
    baseline_ref: Option<&'a str>,
    baseline_tool_version: &'a str,
    baseline_timestamp: &'a str,
    /// Per-change list, post-filter / sort / truncate. References
    /// (the borrows hold for the envelope's lifetime via the View).
    shown: Vec<&'a FunctionChange>,
    #[serde(skip_serializing_if = "Option::is_none")]
    baseline_diagnostics: Option<&'a AnalysisDiagnostics<P>>,
}

impl<'a, P: ParseDiagnostic> DeltaWire<'a, P> {
    fn from_context(ctx: &'a DeltaContext<'a, P>) -> Self {
        DeltaWire {
            summary: &ctx.view.full.summary,
            spec: &ctx.view.spec,
            eligible_count: ctx.view.eligible_count,
            truncated: ctx.view.truncated,
            baseline_ref: None,
            baseline_tool_version: ctx.baseline_tool_version,
            baseline_timestamp: ctx.baseline_timestamp,
            shown: ctx.view.shown.clone(),
            baseline_diagnostics: ctx.baseline_diagnostics,
        }
    }
}

/// On-the-wire view representation. Mirrors `AnalysisView`'s serialized
/// shape exactly when `shown` is `Some`, so the default JSON output is
/// byte-identical to the prior `view: &AnalysisView` serialization.
/// `--minimal-view` sets `shown = None`, which `skip_serializing_if`
/// elides — every other key remains for scope context.
#[derive(Serialize)]
struct ViewWire<'a> {
    spec: &'a ViewSpec,
    eligible_count: usize,
    truncated: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    shown: Option<&'a [&'a FunctionVerdict]>,
    shown_summary: &'a AnalysisSummary,
    /// Per-key aggregation block. Always serialized (emits `null`
    /// when grouping is inactive) so consumers can distinguish
    /// "default invocation" from "schema doesn't carry grouping".
    grouped: Option<&'a GroupedView>,
}

impl<'a> ViewWire<'a> {
    fn from_view(view: &'a AnalysisView<'a>, minimal: bool) -> Self {
        ViewWire {
            spec: &view.spec,
            eligible_count: view.eligible_count,
            truncated: view.truncated,
            shown: if minimal {
                None
            } else {
                Some(view.shown.as_slice())
            },
            shown_summary: &view.shown_summary,
            grouped: view.grouped.as_ref(),
        }
    }
}

// Note: per-function thresholds are already visible in each FunctionVerdict's
// `threshold` field. Consumers can compare individual function thresholds
// against the envelope's global `threshold` to detect overrides.

/// Format a view as pretty-printed JSON with a versioned envelope.
///
/// `view.full` is the canonical analysis (gate); the envelope's
/// `result` field serializes it. The additive `view` field carries
/// the spec, eligible/truncated metadata, and the shaped row list.
pub fn format_json<P: ParseDiagnostic>(
    view: &AnalysisView<'_>,
    config: &JsonConfig<'_, P>,
) -> Result<String, serde_json::Error> {
    let delta_wire: Option<DeltaWire<P>> = config.delta.as_ref().map(DeltaWire::from_context);

    let envelope = JsonEnvelope {
        schema_version: 2,
        tool_version: &config.tool_version,
        language: "rust",
        timestamp: &config.timestamp,
        metric: &config.metric,
        threshold: config.threshold,
        diff_ref: config.diff_ref,
        result: view.full,
        view: ViewWire::from_view(view, config.minimal_view),
        delta: delta_wire,
        diagnostics: config.diagnostics,
    };
    serde_json::to_string_pretty(&envelope)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::adapters::reporters::test_fixtures::*;
    use crate::domain::types::{ComplexityMetric, RiskLevel};
    use crate::test_strategies::DummyParseDiagnostic;

    /// Concrete `P` for in-module tests — the JSON reporter's behavior
    /// is `P`-agnostic for the cases asserted here (no test reaches
    /// into per-variant fields of a parse diagnostic). Pinning to the
    /// dummy stub decouples these tests from any specific adapter's
    /// diagnostic shape.
    type TestJsonConfig = JsonConfig<'static, DummyParseDiagnostic>;

    fn default_config() -> TestJsonConfig {
        JsonConfig {
            tool_version: "0.1.0".to_string(),
            metric: ComplexityMetric::Cognitive,
            threshold: 8.0,
            timestamp: "2026-03-28T12:00:00Z".to_string(),
            diagnostics: None,
            diff_ref: None,
            minimal_view: false,
            delta: None,
        }
    }

    fn parse_json<'a>(
        result: &AnalysisResult,
        config: &JsonConfig<'a, DummyParseDiagnostic>,
    ) -> serde_json::Value {
        let view = make_view_default(result);
        let json_str = format_json(&view, config).expect("format_json should succeed");
        serde_json::from_str(&json_str).expect("output should be valid JSON")
    }

    #[test]
    fn test_envelope_contains_all_fields() {
        let result = make_empty_result();
        let v = parse_json(&result, &default_config());

        assert!(v.get("schema_version").is_some());
        assert!(v.get("tool_version").is_some());
        assert!(v.get("language").is_some());
        assert!(v.get("timestamp").is_some());
        assert!(v.get("metric").is_some());
        assert!(v.get("threshold").is_some());
        assert!(v.get("result").is_some());
    }

    #[test]
    fn test_result_nested_correctly() {
        let result = make_empty_result();
        let v = parse_json(&result, &default_config());
        let r = v.get("result").expect("should have result key");

        assert!(r.get("functions").is_some());
        assert!(r.get("summary").is_some());
        assert!(r.get("passed").is_some());
    }

    #[test]
    fn test_schema_version_is_integer() {
        let result = make_empty_result();
        let v = parse_json(&result, &default_config());
        let sv = v.get("schema_version").unwrap();
        assert!(sv.is_number());
        assert_eq!(sv.as_u64(), Some(2));
    }

    #[test]
    fn test_function_all_scored_fields() {
        let result = make_single_function_result(
            "compute_crap",
            "src/domain/crap.rs",
            5,
            80.0,
            5.16,
            RiskLevel::Acceptable,
            8.0,
        );
        let v = parse_json(&result, &default_config());
        let func = &v["result"]["functions"][0];

        // identity
        assert_eq!(func["scored"]["identity"]["qualified_name"], "compute_crap");
        assert_eq!(
            func["scored"]["identity"]["file_path"],
            "src/domain/crap.rs"
        );
        // complexity
        assert_eq!(func["scored"]["complexity"], 5);
        // coverage
        assert_eq!(func["scored"]["coverage_percent"], 80.0);
        // crap
        assert_eq!(func["scored"]["crap"]["value"], 5.16);
        assert_eq!(func["scored"]["crap"]["risk_level"], "acceptable");
        // threshold fields
        assert_eq!(func["exceeds"], false);
        assert_eq!(func["threshold"], 8.0);
    }

    #[test]
    fn test_summary_aggregate_stats() {
        let result = make_multi_function_result();
        let v = parse_json(&result, &default_config());
        let s = &v["result"]["summary"];

        assert_eq!(s["total_functions"], 3);
        assert_eq!(s["exceeding_threshold"], 2);
        assert!(s["average_crap"].is_number());
        assert!(s["median_crap"].is_number());
    }

    #[test]
    fn test_summary_distribution() {
        let result = make_multi_function_result();
        let v = parse_json(&result, &default_config());
        let d = &v["result"]["summary"]["distribution"];

        assert_eq!(d["low"], 1);
        assert_eq!(d["acceptable"], 0);
        assert_eq!(d["moderate"], 1);
        assert_eq!(d["high"], 1);
    }

    #[test]
    fn test_passed_true() {
        let result =
            make_single_function_result("f", "src/lib.rs", 1, 100.0, 1.0, RiskLevel::Low, 8.0);
        let v = parse_json(&result, &default_config());
        assert_eq!(v["result"]["passed"], true);
    }

    #[test]
    fn test_passed_false() {
        let result = make_multi_function_result();
        let v = parse_json(&result, &default_config());
        assert_eq!(v["result"]["passed"], false);
    }

    #[test]
    fn test_empty_valid_json() {
        let result = make_empty_result();
        let v = parse_json(&result, &default_config());

        let funcs = v["result"]["functions"].as_array().unwrap();
        assert!(funcs.is_empty());
        assert_eq!(v["result"]["summary"]["total_functions"], 0);
        assert_eq!(v["result"]["passed"], true);
    }

    #[test]
    fn test_metric_from_config() {
        let result = make_empty_result();
        let config = JsonConfig {
            metric: ComplexityMetric::Cognitive,
            ..default_config()
        };
        let v = parse_json(&result, &config);
        assert_eq!(v["metric"], "cognitive");

        let config2 = JsonConfig {
            metric: ComplexityMetric::Cyclomatic,
            ..default_config()
        };
        let v2 = parse_json(&result, &config2);
        assert_eq!(v2["metric"], "cyclomatic");
    }

    #[test]
    fn test_threshold_from_config() {
        let result = make_empty_result();
        let config = JsonConfig {
            threshold: 12.5,
            ..default_config()
        };
        let v = parse_json(&result, &config);
        assert_eq!(v["threshold"], 12.5);
    }

    #[test]
    fn test_timestamp_passthrough() {
        let result = make_empty_result();
        let config = JsonConfig {
            timestamp: "2026-01-15T09:30:00Z".to_string(),
            ..default_config()
        };
        let v = parse_json(&result, &config);
        assert_eq!(v["timestamp"], "2026-01-15T09:30:00Z");
    }

    #[test]
    fn test_full_json_snapshot() {
        let result = make_single_function_result(
            "compute_crap",
            "src/domain/crap.rs",
            5,
            80.0,
            5.16,
            RiskLevel::Acceptable,
            8.0,
        );
        let view = make_view_default(&result);
        let json_str = format_json(&view, &default_config()).unwrap();
        let v: serde_json::Value = serde_json::from_str(&json_str).unwrap();
        insta::assert_json_snapshot!(v);
    }

    #[test]
    fn test_envelope_field_declaration_order() {
        // cli_ergonomics.feature:243-246: the envelope key declaration
        // order is exactly schema_version, tool_version, language,
        // timestamp, metric, threshold, diff_ref, result, view.
        // Asserted on the raw `format_json` string (NOT the parsed
        // serde_json::Value, which alphabetizes via BTreeMap).
        let result = make_empty_result();
        let view = make_view_default(&result);
        let json_str = format_json(&view, &default_config()).unwrap();
        let keys = [
            "schema_version",
            "tool_version",
            "language",
            "timestamp",
            "metric",
            "threshold",
            "diff_ref",
            "result",
            "view",
        ];
        // Top-level keys in serde_json's pretty printer sit at indent 2.
        // Anchor the substring search to `\n  "<key>"` so future nested
        // fields with the same name can't shadow the top-level
        // position (CodeRabbit CR-N5).
        let positions: Vec<usize> = keys
            .iter()
            .map(|k| {
                json_str
                    .find(&format!("\n  \"{k}\""))
                    .unwrap_or_else(|| panic!("missing top-level key {k} in:\n{json_str}"))
            })
            .collect();
        for (k_prev, w) in keys.windows(2).zip(positions.windows(2)) {
            assert!(
                w[0] < w[1],
                "envelope key order: expected {} before {}, got positions {} and {}",
                k_prev[0],
                k_prev[1],
                w[0],
                w[1],
            );
        }
    }

    #[test]
    fn test_view_block_present_in_envelope() {
        // The envelope unconditionally carries a `view` block; defaults
        // are echoed (filters empty, sort=crap, limit absent).
        let result = make_multi_function_result();
        let view = make_view_default(&result);
        let json_str = format_json(&view, &default_config()).unwrap();
        let v: serde_json::Value = serde_json::from_str(&json_str).unwrap();
        let view_block = v.get("view").expect("envelope missing view block");
        assert!(view_block.get("spec").is_some());
        assert!(view_block.get("eligible_count").is_some());
        assert!(view_block.get("truncated").is_some());
        assert!(view_block.get("shown").is_some());
        assert!(view_block.get("shown_summary").is_some());
        // view.full is `#[serde(skip)]` — it must NOT appear.
        assert!(
            view_block.get("full").is_none(),
            "view.full should be elided from JSON (envelope's `result` already serializes it)"
        );
        // Default spec echoed
        let spec = view_block.get("spec").unwrap();
        assert_eq!(spec["sort"], "crap");
        assert_eq!(spec["limit"], serde_json::Value::Null);
        assert_eq!(spec["filters"]["only_failing"], false);
    }

    #[test]
    fn test_view_grouped_null_by_default() {
        // No --group-by ⇒ view.grouped is null, view.spec.group_by is null.
        let result = make_multi_function_result();
        let v = parse_json(&result, &default_config());
        let view_block = &v["view"];
        assert_eq!(view_block["grouped"], serde_json::Value::Null);
        assert_eq!(view_block["spec"]["group_by"], serde_json::Value::Null);
    }

    #[test]
    fn test_view_grouped_populated_under_group_by_file() {
        use crate::domain::view::{self, GroupKey, ViewSpec};
        let result = make_multi_function_result();
        let spec = ViewSpec {
            group_by: Some(GroupKey::File),
            ..Default::default()
        };
        let view = view::apply(&result, spec);
        let json_str = format_json(&view, &default_config()).unwrap();
        let v: serde_json::Value = serde_json::from_str(&json_str).unwrap();
        let grouped = &v["view"]["grouped"];
        assert!(grouped.is_object(), "view.grouped must be populated");
        assert_eq!(grouped["key"], "file");
        assert!(grouped["files"].is_array());
        let files = grouped["files"].as_array().unwrap();
        assert_eq!(files.len(), 3); // 3 distinct files in fixture
        // Spot-check first file structure carries all the FileSummary keys.
        let f0 = &files[0];
        assert!(f0["file_path"].is_string());
        assert!(f0["function_count"].is_number());
        assert!(f0["exceeding_count"].is_number());
        assert!(f0["average_crap"].is_number());
        assert!(f0["average_coverage"].is_number());
        assert!(f0["max_complexity"].is_number());
        assert!(f0["distribution"].is_object());
        // spec.group_by echoed as "file"
        assert_eq!(v["view"]["spec"]["group_by"], "file");
    }

    #[test]
    fn test_diff_ref_present_in_json() {
        let result = make_empty_result();
        let config = JsonConfig {
            diff_ref: Some("main"),
            ..default_config()
        };
        let v = parse_json(&result, &config);
        assert_eq!(v["diff_ref"], "main");
    }

    #[test]
    fn test_diff_ref_null_when_none() {
        let result = make_empty_result();
        let v = parse_json(&result, &default_config());
        assert!(
            v.get("diff_ref").is_some(),
            "diff_ref key should be present"
        );
        assert!(v["diff_ref"].is_null(), "diff_ref should be null");
    }

    #[test]
    fn test_diagnostics_omitted_when_none() {
        let result = make_empty_result();
        let v = parse_json(&result, &default_config());
        assert!(
            v.get("diagnostics").is_none(),
            "diagnostics should be absent without --verbose"
        );
    }

    #[test]
    fn test_diagnostics_included_when_present_p_agnostic_top_level() {
        // P-agnostic slice: counts on AnalysisDiagnostics<P> serialize
        // through regardless of how `parse_diagnostics` flatten. The
        // LCOV-specific per-variant wire shape is asserted in the
        // crap4rs-side companion test
        // `tests/json_reporter_lcov_diagnostics.rs::diagnostics_included_when_present`,
        // which lives next to LcovParseDiagnostic rather than in
        // crap-core (which is `P`-generic).
        use crate::domain::types::AnalysisDiagnostics;

        let diag: AnalysisDiagnostics<DummyParseDiagnostic> = AnalysisDiagnostics {
            parse_diagnostics: vec![],
            files_found: 10,
            files_unparseable: 1,
            functions_extracted: 42,
            functions_matched: 40,
            functions_no_coverage: 2,
            files_analyzed: 8,
            files_zero_coverage: 2,
        };

        let result = make_empty_result();
        let config = JsonConfig {
            diagnostics: Some(&diag),
            ..default_config()
        };
        let v = parse_json(&result, &config);

        let d = v.get("diagnostics").expect("should have diagnostics key");
        assert_eq!(d["files_found"], 10);
        assert_eq!(d["files_unparseable"], 1);
        assert_eq!(d["functions_extracted"], 42);
        assert_eq!(d["functions_matched"], 40);
        assert_eq!(d["functions_no_coverage"], 2);
        // parse_diagnostics is empty under DummyParseDiagnostic; the
        // crap4rs-side companion exercises the populated case.
        let parse_diags = d["parse_diagnostics"].as_array().unwrap();
        assert!(parse_diags.is_empty());
    }
}

#[cfg(test)]
mod proptests {
    use super::*;
    use crate::adapters::reporters::test_fixtures::make_view_default;
    use crate::test_strategies::{DummyParseDiagnostic, arb_analysis_result};
    use proptest::prelude::*;

    fn arb_config() -> impl Strategy<Value = JsonConfig<'static, DummyParseDiagnostic>> {
        (1.0..100.0f64,).prop_map(|(threshold,)| JsonConfig {
            tool_version: "0.1.0".to_string(),
            metric: ComplexityMetric::Cognitive,
            threshold,
            timestamp: "2026-01-01T00:00:00Z".to_string(),
            diagnostics: None,
            diff_ref: None,
            minimal_view: false,
            delta: None,
        })
    }

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(256))]

        #[test]
        fn prop_format_json_always_valid(
            result in arb_analysis_result(),
            config in arb_config(),
        ) {
            let view = make_view_default(&result);
            let json_str = format_json(&view, &config)
                .expect("format_json should never fail on valid input");
            let _: serde_json::Value = serde_json::from_str(&json_str)
                .expect("output should be valid JSON");
        }

        #[test]
        fn prop_format_json_functions_count(
            result in arb_analysis_result(),
            config in arb_config(),
        ) {
            let view = make_view_default(&result);
            let json_str = format_json(&view, &config).unwrap();
            let v: serde_json::Value = serde_json::from_str(&json_str).unwrap();
            let funcs = v["result"]["functions"].as_array().unwrap();
            prop_assert_eq!(funcs.len(), result.functions.len());
        }
    }
}