use std::ffi::OsString;
use std::path::PathBuf;
use crate::daemon::Os;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Status {
Ok,
Skip,
Warn,
Fail,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Check {
pub name: &'static str,
pub status: Status,
pub detail: String,
pub fix: Option<String>,
}
impl Check {
pub fn ok(name: &'static str, detail: impl Into<String>) -> Self {
Check {
name,
status: Status::Ok,
detail: detail.into(),
fix: None,
}
}
pub fn skip(name: &'static str, reason: impl Into<String>) -> Self {
Check {
name,
status: Status::Skip,
detail: reason.into(),
fix: None,
}
}
pub fn warn(name: &'static str, reason: impl Into<String>, fix: impl Into<String>) -> Self {
Check {
name,
status: Status::Warn,
detail: reason.into(),
fix: Some(fix.into()),
}
}
pub fn fail(name: &'static str, reason: impl Into<String>, fix: impl Into<String>) -> Self {
Check {
name,
status: Status::Fail,
detail: reason.into(),
fix: Some(fix.into()),
}
}
}
#[derive(Debug, Clone)]
pub(crate) struct Context {
pub os: Os,
pub home: PathBuf,
pub data_dir: PathBuf,
pub sb_bin: PathBuf,
pub mcp_bin: Result<PathBuf, String>,
pub claude_config_dir: Option<PathBuf>,
pub search_path: Option<OsString>,
pub model_cache: PathBuf,
}
impl Context {
pub fn claude_settings(&self) -> PathBuf {
crate::claude_hooks::settings_path(
self.claude_config_dir
.as_deref()
.map(std::path::Path::as_os_str),
&self.home,
)
}
}
pub(crate) fn render(checks: &[Check]) -> String {
let width = checks.iter().map(|c| c.name.len()).max().unwrap_or(0);
let mut out = String::new();
for check in checks {
let label = match check.status {
Status::Ok => "ok ",
Status::Skip => "skip",
Status::Warn => "WARN",
Status::Fail => "FAIL",
};
out.push_str(&format!(
"{label} {:width$} {}\n",
check.name, check.detail
));
if let Some(fix) = &check.fix {
out.push_str(&format!(" {:width$} fix: {fix}\n", ""));
}
}
out
}
pub(crate) fn failures(checks: &[Check]) -> usize {
checks.iter().filter(|c| c.status == Status::Fail).count()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_failed_check_prints_its_reason_and_its_fix_under_it() {
let checks = [
Check::ok("daemon", "running 0.12.0"),
Check::fail("token", "api.token is missing", "sb restart"),
];
assert_eq!(
render(&checks),
"ok daemon running 0.12.0\n\
FAIL token api.token is missing\n\
\x20 fix: sb restart\n"
);
assert_eq!(failures(&checks), 1);
}
#[test]
fn a_skipped_check_prints_its_reason_without_a_fix_and_is_not_a_failure() {
let checks = [Check::skip("hooks", "no Claude Code settings here")];
assert_eq!(
render(&checks),
"skip hooks no Claude Code settings here\n"
);
assert_eq!(failures(&checks), 0);
}
}