#[derive(Debug, Clone, serde::Serialize)]
pub struct CheckResult {
pub name: String,
pub passed: bool,
pub detail: String,
}
impl CheckResult {
fn new(name: String, passed: bool, detail: String) -> Self {
Self {
name,
passed,
detail,
}
}
}
#[derive(Debug, Clone, serde::Serialize)]
pub struct DoctorReport {
pub checks: Vec<CheckResult>,
pub all_passed: bool,
}
pub trait EnvironmentProbe {
fn has_tool(&self, name: &str) -> bool;
fn op_authed(&self) -> bool;
fn stale_worktrees(&self) -> Vec<String>;
}
const REQUIRED_TOOLS: &[&str] = &[
"cargo",
"git",
"rustfmt",
"clippy-driver",
"op",
"doob",
"hj",
];
pub fn run_doctor(probe: &dyn EnvironmentProbe) -> DoctorReport {
run_doctor_at(probe, None)
}
pub fn run_doctor_at(
probe: &dyn EnvironmentProbe,
plugin_root: Option<&std::path::Path>,
) -> DoctorReport {
let mut checks = Vec::new();
for &tool in REQUIRED_TOOLS {
let found = probe.has_tool(tool);
checks.push(CheckResult::new(
format!("tool:{tool}"),
found,
if found {
"found".into()
} else {
"not found on PATH".into()
},
));
}
let op_ok = probe.op_authed();
checks.push(CheckResult::new(
"op:auth".into(),
op_ok,
if op_ok {
"authenticated".into()
} else {
"not authenticated".into()
},
));
let stale = probe.stale_worktrees();
let wt_ok = stale.is_empty();
checks.push(CheckResult::new(
"worktrees:stale".into(),
wt_ok,
if wt_ok {
"none".into()
} else {
format!("stale: {}", stale.join(", "))
},
));
if let Some(root) = plugin_root {
use crate::hooks::registry::{CoverageIssueKind, HookClient};
if root.join("hooks").is_dir() {
match crate::hooks::registry::diagnose_repository(root, HookClient::Claude) {
Ok(issues) => {
for (name, kind) in [
("unregistered", CoverageIssueKind::Unregistered),
("missing", CoverageIssueKind::Missing),
("duplicate", CoverageIssueKind::Duplicate),
("superseded", CoverageIssueKind::Superseded),
] {
let matching: Vec<_> =
issues.iter().filter(|issue| issue.kind == kind).collect();
checks.push(CheckResult::new(
format!("hooks:{name}"),
matching.is_empty(),
if matching.is_empty() {
"none".into()
} else {
matching
.iter()
.map(|issue| format!("{} ({})", issue.hook, issue.detail))
.collect::<Vec<_>>()
.join(", ")
},
));
}
}
Err(error) => checks.push(CheckResult::new(
"hooks:missing".into(),
false,
format!("cannot inspect hook manifest: {error}"),
)),
}
}
}
let all_passed = checks.iter().all(|c| c.passed);
DoctorReport { checks, all_passed }
}
pub struct RealProbe;
impl EnvironmentProbe for RealProbe {
fn has_tool(&self, name: &str) -> bool {
which::which(name).is_ok()
}
fn op_authed(&self) -> bool {
std::process::Command::new("op")
.args(["account", "list"])
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
.is_ok_and(|s| s.success())
}
fn stale_worktrees(&self) -> Vec<String> {
let output = std::process::Command::new("git")
.args(["worktree", "list", "--porcelain"])
.output();
let Ok(out) = output else { return vec![] };
if !out.status.success() {
return vec![];
}
let text = String::from_utf8_lossy(&out.stdout);
let mut stale = Vec::new();
let mut current_path = String::new();
for line in text.lines() {
if let Some(path) = line.strip_prefix("worktree ") {
current_path = path.to_string();
}
if line.starts_with("prunable") && !current_path.is_empty() {
stale.push(current_path.clone());
}
}
stale
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::BTreeMap;
struct FakeProbe {
tools: BTreeMap<String, bool>,
op_auth: bool,
stale: Vec<String>,
}
impl EnvironmentProbe for FakeProbe {
fn has_tool(&self, name: &str) -> bool {
self.tools.get(name).copied().unwrap_or(false)
}
fn op_authed(&self) -> bool {
self.op_auth
}
fn stale_worktrees(&self) -> Vec<String> {
self.stale.clone()
}
}
#[test]
fn all_pass_when_everything_available() {
let mut tools = BTreeMap::new();
for &t in REQUIRED_TOOLS {
tools.insert(t.to_string(), true);
}
let probe = FakeProbe {
tools,
op_auth: true,
stale: vec![],
};
let report = run_doctor(&probe);
assert!(report.all_passed);
assert!(report.checks.iter().all(|c| c.passed));
}
#[test]
fn fails_when_tool_missing() {
let mut tools = BTreeMap::new();
for &t in REQUIRED_TOOLS {
tools.insert(t.to_string(), t != "doob");
}
let probe = FakeProbe {
tools,
op_auth: true,
stale: vec![],
};
let report = run_doctor(&probe);
assert!(!report.all_passed);
let doob_check = report
.checks
.iter()
.find(|c| c.name == "tool:doob")
.unwrap();
assert!(!doob_check.passed);
}
#[test]
fn fails_when_op_not_authed() {
let mut tools = BTreeMap::new();
for &t in REQUIRED_TOOLS {
tools.insert(t.to_string(), true);
}
let probe = FakeProbe {
tools,
op_auth: false,
stale: vec![],
};
let report = run_doctor(&probe);
assert!(!report.all_passed);
}
#[test]
fn reports_stale_worktrees() {
let mut tools = BTreeMap::new();
for &t in REQUIRED_TOOLS {
tools.insert(t.to_string(), true);
}
let probe = FakeProbe {
tools,
op_auth: true,
stale: vec!["/tmp/stale-wt".into()],
};
let report = run_doctor(&probe);
assert!(!report.all_passed);
let wt = report
.checks
.iter()
.find(|c| c.name == "worktrees:stale")
.unwrap();
assert!(!wt.passed);
assert!(wt.detail.contains("/tmp/stale-wt"));
}
#[test]
fn doctor_uses_which_v8_contract() {
let manifest = include_str!("../Cargo.toml");
assert!(manifest.lines().any(|line| line.trim() == "which = \"8\""));
}
}