@@ -11,7 +11,10 @@ pub(super) async fn run(json: bool, strict: bool, timeout_ms: u64) -> anyhow::Re
1111 . await ?;
1212
1313 if json {
14- println ! ( "{}" , serde_json:: to_string_pretty( & report) ?) ;
14+ println ! (
15+ "{}" ,
16+ serde_json:: to_string_pretty( & build_doctor_json( & report) ?) ?
17+ ) ;
1518 } else {
1619 println ! ( "{}" , report. to_text( ) ) ;
1720 }
@@ -22,3 +25,50 @@ pub(super) async fn run(json: bool, strict: bool, timeout_ms: u64) -> anyhow::Re
2225 }
2326 Ok ( ( ) )
2427}
28+
29+ fn build_doctor_json ( report : & doctor:: DoctorReport ) -> anyhow:: Result < serde_json:: Value > {
30+ Ok ( serde_json:: to_value ( report) ?)
31+ }
32+
33+ #[ cfg( test) ]
34+ mod tests {
35+ use super :: * ;
36+ use crate :: doctor:: { CheckStatus , DoctorCheck , DoctorReport , DoctorSummary } ;
37+ use serde_json:: json;
38+
39+ #[ test]
40+ fn build_doctor_json_keeps_expected_shape_and_omits_empty_fields ( ) {
41+ let report = DoctorReport {
42+ checks : vec ! [
43+ DoctorCheck {
44+ id: "alpha" . to_string( ) ,
45+ status: CheckStatus :: Ok ,
46+ message: String :: new( ) ,
47+ } ,
48+ DoctorCheck {
49+ id: "beta" . to_string( ) ,
50+ status: CheckStatus :: Warn ,
51+ message: "missing env" . to_string( ) ,
52+ } ,
53+ ] ,
54+ summary : DoctorSummary {
55+ ok : 1 ,
56+ warn : 1 ,
57+ error : 0 ,
58+ } ,
59+ next_actions : Vec :: new ( ) ,
60+ } ;
61+
62+ let out = build_doctor_json ( & report) . unwrap ( ) ;
63+ assert_eq ! (
64+ out,
65+ json!( {
66+ "checks" : [
67+ { "id" : "alpha" , "status" : "ok" } ,
68+ { "id" : "beta" , "status" : "warn" , "message" : "missing env" } ,
69+ ] ,
70+ "summary" : { "ok" : 1 , "warn" : 1 , "error" : 0 } ,
71+ } )
72+ ) ;
73+ }
74+ }
0 commit comments