spec_driven_docs/commands/
doctor.rs1use serde::Serialize;
8
9use crate::cli::doctor::DoctorArgs;
10use crate::context::AppContext;
11use crate::error::AppError;
12use crate::output;
13use crate::probes::{self, ProbeClass, ProbeResult, ProbeStatus};
14
15#[derive(Debug, Serialize)]
17struct Report {
18 schema: &'static str,
20 probes: Vec<ProbeResult>,
22 next: Vec<String>,
24}
25
26pub fn run(_ctx: &AppContext, args: &DoctorArgs) -> Result<(), AppError> {
33 let probes = probes::run_all();
34 let next = vec!["sdd status --target . reports the instance".to_owned()];
35 if args.json {
36 return output::json(&Report {
37 schema: "sdd.doctor/1",
38 probes,
39 next,
40 });
41 }
42 for class in [ProbeClass::Hard, ProbeClass::Soft] {
43 output::line(match class {
44 ProbeClass::Hard => "hard",
45 ProbeClass::Soft => "soft",
46 });
47 for probe in probes.iter().filter(|probe| probe.class == class) {
48 let status = match probe.status {
49 ProbeStatus::Ok => "ok ",
50 ProbeStatus::Failed => "failed",
51 };
52 output::line(format!(" {status} {}: {}", probe.id, probe.message));
53 if let Some(remediation) = &probe.remediation {
54 output::line(format!(" next: {remediation}"));
55 }
56 }
57 }
58 output::line("Next:");
59 for line in &next {
60 output::line(format!(" {line}"));
61 }
62 Ok(())
63}
64
65#[cfg(test)]
66mod tests {
67 #![allow(clippy::expect_used)]
68
69 use crate::probes::{ProbeClass, ProbeResult, ProbeStatus};
70
71 #[test]
73 fn the_doctor_report_schema_snapshot_holds() {
74 let report = super::Report {
75 schema: "sdd.doctor/1",
76 probes: vec![ProbeResult {
77 id: "state-root",
78 class: ProbeClass::Hard,
79 status: ProbeStatus::Ok,
80 message: "the state root is writable".into(),
81 remediation: None,
82 }],
83 next: vec!["sdd status --target . reports the instance".into()],
84 };
85 assert_eq!(
86 serde_json::to_string(&report).expect("a report serializes"),
87 r#"{"schema":"sdd.doctor/1","probes":[{"id":"state-root","class":"hard","status":"ok","message":"the state root is writable"}],"next":["sdd status --target . reports the instance"]}"#
88 );
89 }
90
91 #[test]
93 fn the_probe_schema_snapshot_holds() {
94 let ok = ProbeResult {
95 id: "git",
96 class: ProbeClass::Soft,
97 status: ProbeStatus::Ok,
98 message: "git runs".into(),
99 remediation: None,
100 };
101 assert_eq!(
102 serde_json::to_string(&ok).expect("a probe serializes"),
103 r#"{"id":"git","class":"soft","status":"ok","message":"git runs"}"#
104 );
105 let failed = ProbeResult {
106 id: "pre-commit",
107 class: ProbeClass::Soft,
108 status: ProbeStatus::Failed,
109 message: "pre-commit is not on PATH".into(),
110 remediation: Some("install pre-commit; the delivered gates run through it".into()),
111 };
112 assert_eq!(
113 serde_json::to_string(&failed).expect("a probe serializes"),
114 r#"{"id":"pre-commit","class":"soft","status":"failed","message":"pre-commit is not on PATH","remediation":"install pre-commit; the delivered gates run through it"}"#
115 );
116 }
117}