use std::path::Path;
use crate::test_support::{write_executable, write_site_policy};
async fn report_with_policy(dir: &Path, policy: &Path) -> String {
super::report_scoped_with_policy(dir, policy).await
}
async fn report_for(dir: &Path) -> String {
report_with_policy(dir, &dir.join("absent-site.toml")).await
}
fn write_config_running(dir: &Path, argv: &str) {
std::fs::write(
dir.join("drep.toml"),
format!("[[llm]]\nendpoint = \"http://e/v1\"\nmodel = \"m\"\napi_key_command = [{argv}]\n"),
)
.expect("drep.toml");
}
#[tokio::test]
async fn a_working_command_is_reported_as_working_without_its_output() {
let dir = tempfile::tempdir().expect("tempdir");
let stub = dir.path().join("print-token");
write_executable(&stub, "#!/bin/sh\nprintf '%s' 'sk-live-sekrit'\n");
write_config_running(dir.path(), &format!("{:?}", stub.to_string_lossy()));
let report = report_for(dir.path()).await;
assert!(
report.contains("from api_key_command"),
"the source must be named, so `doctor` and `check` agree about it: {report}"
);
assert!(
report.contains("the command ran and printed a credential"),
"a helper that works is worth saying so: {report}"
);
assert!(
!report.contains("sk-live-sekrit"),
"doctor output is what people paste into bug reports and CI logs: {report}"
);
}
#[tokio::test]
async fn a_failing_command_is_reported_with_its_status_and_never_its_output() {
let dir = tempfile::tempdir().expect("tempdir");
let stub = dir.path().join("print-token");
write_executable(
&stub,
"#!/bin/sh\nprintf '%s' 'sk-live-sekrit'\nprintf '%s' 'sk-live-sekrit' >&2\nexit 9\n",
);
write_config_running(dir.path(), &format!("{:?}", stub.to_string_lossy()));
let report = report_for(dir.path()).await;
assert!(report.contains("print-token"), "got {report}");
assert!(
report.contains('9'),
"the exit status is the actionable half: {report}"
);
assert!(
!report.contains("sk-live-sekrit"),
"a misconfigured helper prints the token to both streams; the diagnostic \
is thin so neither reaches the report: {report}"
);
}
#[tokio::test]
async fn a_malformed_command_reference_never_echoes_argv_secrets() {
let dir = tempfile::tempdir().expect("tempdir");
write_config_running(dir.path(), "\"helper\", \"--token=doctor-sentinel${\"");
let report = report_for(dir.path()).await;
assert!(
!report.contains("doctor-sentinel"),
"doctor leaked argv: {report}"
);
assert!(report.contains("unterminated"), "got {report}");
}
#[tokio::test]
async fn a_command_naming_an_unset_variable_is_reported_as_not_attempted() {
let dir = tempfile::tempdir().expect("tempdir");
write_config_running(
dir.path(),
"\"print-token\", \"${DREP_DOCTOR_KEY_CMD_UNSET}\"",
);
let report = report_for(dir.path()).await;
assert!(
report.contains("not attempted"),
"an argv drep cannot expand is not a command it can probe: {report}"
);
assert!(
report.contains("DREP_DOCTOR_KEY_CMD_UNSET is NOT set"),
"and the reader is told which variable to export: {report}"
);
}
#[tokio::test]
async fn a_refused_repository_is_not_probed_for_a_credential() {
let dir = tempfile::tempdir().expect("tempdir");
crate::test_support::git_init(dir.path());
let sentinel = dir.path().join("command-ran");
let stub = dir.path().join("print-token");
write_executable(
&stub,
format!(
"#!/bin/sh\nprintf '%s' ran > {}\nprintf '%s' tok\n",
sentinel.to_string_lossy()
),
);
write_config_running(dir.path(), &format!("{:?}", stub.to_string_lossy()));
let policy = write_site_policy(dir.path(), &[".drep-no-llm"]);
std::fs::write(dir.path().join(".drep-no-llm"), "").expect("marker");
let report = report_with_policy(dir.path(), &policy).await;
assert!(
!sentinel.exists(),
"the helper ran for a repository whose review is refused: {report}"
);
assert!(
report.contains("not attempted, because site policy refuses semantic review here"),
"and the reader has to be told why the line says nothing about the helper: {report}"
);
}
#[tokio::test]
async fn a_refused_repository_does_not_read_the_auth_store() {
let dir = tempfile::tempdir().expect("tempdir");
crate::test_support::git_init(dir.path());
write_config_running(dir.path(), "\"unused-helper\"");
std::fs::write(dir.path().join("auth.toml"), "not valid toml {{{").expect("corrupt store");
let policy = write_site_policy(dir.path(), &[".drep-no-llm"]);
std::fs::write(dir.path().join(".drep-no-llm"), "").expect("marker");
let report = report_with_policy(dir.path(), &policy).await;
assert!(
report.contains("site policy refuses semantic review"),
"got {report}"
);
assert!(
!report.contains("auth store could not be read"),
"doctor opened state the refused check never reaches: {report}"
);
}
#[tokio::test]
async fn an_unmarked_repository_under_a_marker_policy_is_still_probed() {
let dir = tempfile::tempdir().expect("tempdir");
crate::test_support::git_init(dir.path());
let sentinel = dir.path().join("command-ran");
let stub = dir.path().join("print-token");
write_executable(
&stub,
format!(
"#!/bin/sh\nprintf '%s' ran > {}\nprintf '%s' tok\n",
sentinel.to_string_lossy()
),
);
write_config_running(dir.path(), &format!("{:?}", stub.to_string_lossy()));
let policy = write_site_policy(dir.path(), &[".drep-no-llm"]);
let report = report_with_policy(dir.path(), &policy).await;
assert!(sentinel.exists(), "got {report}");
assert!(
report.contains("the command ran and printed a credential"),
"got {report}"
);
}
#[tokio::test]
async fn an_unloadable_policy_is_not_probed() {
let dir = tempfile::tempdir().expect("tempdir");
crate::test_support::git_init(dir.path());
let sentinel = dir.path().join("command-ran");
let stub = dir.path().join("print-token");
write_executable(
&stub,
format!(
"#!/bin/sh\nprintf '%s' ran > {}\nprintf '%s' tok\n",
sentinel.to_string_lossy()
),
);
write_config_running(dir.path(), &format!("{:?}", stub.to_string_lossy()));
let policy = dir.path().join("site.toml");
std::fs::write(&policy, "refuse_markers = \"not a list\"\n").expect("site.toml");
let report = report_with_policy(dir.path(), &policy).await;
assert!(
!sentinel.exists(),
"the helper ran for a repository whose policy could not be evaluated: {report}"
);
assert!(
report.contains("not attempted, because the site policy above could not be evaluated"),
"got {report}"
);
}
#[tokio::test]
async fn a_policy_whose_marker_probe_cannot_resolve_a_root_is_not_probed() {
let dir = tempfile::tempdir().expect("tempdir");
crate::test_support::git_unresolvable(dir.path());
let sentinel = dir.path().join("command-ran");
let stub = dir.path().join("print-token");
write_executable(
&stub,
format!(
"#!/bin/sh\nprintf '%s' ran > {}\nprintf '%s' tok\n",
sentinel.to_string_lossy()
),
);
write_config_running(dir.path(), &format!("{:?}", stub.to_string_lossy()));
let policy = write_site_policy(dir.path(), &[".drep-no-llm"]);
let report = report_with_policy(dir.path(), &policy).await;
assert!(
!sentinel.exists(),
"the helper ran for a repository whose marker probe could not be evaluated: {report}"
);
assert!(
report.contains("not attempted, because the site policy above could not be evaluated"),
"got {report}"
);
}