use std::path::Path;
use crate::test_support::write_site_policy;
async fn report_with_site(dir: &Path, site_path: &Path) -> String {
super::report_scoped_with_policy(dir, site_path).await
}
fn write_provider(dir: &Path) {
std::fs::write(
dir.join("drep.toml"),
"[[llm]]\nendpoint = \"http://e/v1\"\nmodel = \"m\"\n",
)
.expect("config");
}
fn write_source(dir: &Path) {
std::fs::write(dir.join("a.py"), "x = 1\n").expect("source");
}
#[tokio::test]
async fn no_site_file_says_so_and_names_the_path_it_looked_for() {
let dir = tempfile::tempdir().expect("tempdir");
write_source(dir.path());
write_provider(dir.path());
let site = dir.path().join("absent-site.toml");
let report = report_with_site(dir.path(), &site).await;
assert!(report.contains("Site policy:"), "got {report}");
assert!(
report.contains(&site.display().to_string()),
"an operator with nowhere to install policy has learned nothing; got {report}"
);
}
#[tokio::test]
async fn a_site_file_in_effect_is_named_with_its_ceiling() {
let dir = tempfile::tempdir().expect("tempdir");
write_source(dir.path());
write_provider(dir.path());
let site = dir.path().join("site.toml");
std::fs::write(&site, "max_concurrent_ceiling = 4\n").expect("site.toml");
let report = report_with_site(dir.path(), &site).await;
assert!(
report.contains(&format!("in effect from {}", site.display())),
"a report silent about a policy that is changing behaviour is the \
report this block exists to replace; got {report}"
);
assert!(report.contains("max_concurrent ceiling: 4"), "got {report}");
}
#[tokio::test]
async fn a_clamped_provider_says_so_on_its_own_line() {
let dir = tempfile::tempdir().expect("tempdir");
std::fs::write(dir.path().join("a.py"), "x = 1\n").expect("source");
std::fs::write(
dir.path().join("drep.toml"),
"[[llm]]\nendpoint = \"http://high/v1\"\nmodel = \"high\"\nmax_concurrent = 8\n\n\
[[llm]]\nendpoint = \"http://low/v1\"\nmodel = \"low\"\nmax_concurrent = 2\n",
)
.expect("config");
let site = dir.path().join("site.toml");
std::fs::write(&site, "max_concurrent_ceiling = 4\n").expect("site.toml");
let report = report_with_site(dir.path(), &site).await;
assert!(
report.contains("max_concurrent: 8 lowered to 4"),
"got {report}"
);
assert_eq!(
report.matches("lowered to").count(),
1,
"the entry already below the ceiling was not clamped, so saying it was \
is a report of a change that did not happen; got {report}"
);
}
#[tokio::test]
async fn a_broken_site_file_is_described_rather_than_failing_doctor() {
let dir = tempfile::tempdir().expect("tempdir");
write_source(dir.path());
write_provider(dir.path());
let site = dir.path().join("site.toml");
std::fs::write(&site, "not toml at all\n").expect("site.toml");
let report = report_with_site(dir.path(), &site).await;
assert!(report.contains(&site.display().to_string()), "got {report}");
assert!(
report.contains("refuses to run"),
"the reader has to be told that `drep check` will not run until this is \
fixed; got {report}"
);
assert!(
report.contains("LLM analysis"),
"and the rest of the report still has to arrive; got {report}"
);
}
#[tokio::test]
async fn the_site_block_precedes_the_llm_block_with_and_without_source_files() {
for source in [true, false] {
let dir = tempfile::tempdir().expect("tempdir");
if source {
write_source(dir.path());
}
write_provider(dir.path());
let site = dir.path().join("site.toml");
std::fs::write(&site, "max_concurrent_ceiling = 4\n").expect("site.toml");
let report = report_with_site(dir.path(), &site).await;
let policy = report.find("Site policy:");
let llm = report.find("LLM analysis");
assert!(
policy.is_some() && policy < llm,
"with source = {source}, the policy that governs the chain is read \
before the chain it governs; got {report}"
);
assert!(
report.starts_with("drep in "),
"with source = {source}, the header still comes first; got {report}"
);
}
}
#[tokio::test]
async fn a_marked_repository_says_semantic_review_is_refused_here() {
let dir = tempfile::tempdir().expect("tempdir");
crate::test_support::git_init(dir.path());
write_source(dir.path());
write_provider(dir.path());
let site = write_site_policy(dir.path(), &[".drep-no-llm"]);
std::fs::write(dir.path().join(".drep-no-llm"), "").expect("marker");
let report = report_with_site(dir.path(), &site).await;
assert!(
report.contains("refuse_markers: .drep-no-llm"),
"got {report}"
);
assert!(
report.contains("refused"),
"an operator staring at exit 2 needs the word; got {report}"
);
assert!(
report.contains(".drep-no-llm is present"),
"and which file it was; got {report}"
);
}
#[tokio::test]
async fn a_marked_nested_repository_is_refused_in_the_doctor_report() {
let outer = tempfile::tempdir().expect("tempdir");
crate::test_support::git_init(outer.path());
write_provider(outer.path());
write_source(outer.path());
let inner = outer.path().join("nested");
std::fs::create_dir(&inner).expect("nested repo");
crate::test_support::git_init(&inner);
std::fs::write(inner.join("inner.py"), "y = 2\n").expect("inner source");
std::fs::write(inner.join(".drep-no-llm"), "").expect("inner marker");
let site = write_site_policy(outer.path(), &[".drep-no-llm"]);
let report = report_with_site(outer.path(), &site).await;
assert!(
report.contains(&inner.join(".drep-no-llm").display().to_string()),
"doctor and check disagreed about the source repositories: {report}"
);
assert!(
report.contains("semantic review is refused"),
"got {report}"
);
}
#[tokio::test]
async fn a_configured_marker_that_is_absent_is_reported_as_not_refusing() {
let dir = tempfile::tempdir().expect("tempdir");
crate::test_support::git_init(dir.path());
write_source(dir.path());
write_provider(dir.path());
let site = write_site_policy(dir.path(), &[".drep-no-llm"]);
let report = report_with_site(dir.path(), &site).await;
assert!(
report.contains("refuse_markers: .drep-no-llm"),
"got {report}"
);
assert!(
report.contains("none of those files is here, so review runs"),
"the list alone leaves the operator guessing at its effect; got {report}"
);
assert!(
!report.contains("is present"),
"reporting a refusal that is not happening is worse than silence; got {report}"
);
}
#[tokio::test]
async fn a_policy_that_cannot_be_evaluated_is_described_rather_than_failing_doctor() {
let dir = tempfile::tempdir().expect("tempdir");
write_source(dir.path());
write_provider(dir.path());
crate::test_support::git_unresolvable(dir.path());
let site = write_site_policy(dir.path(), &[".drep-no-llm"]);
let report = report_with_site(dir.path(), &site).await;
assert!(
report.contains("could not be resolved"),
"a policy naming markers outside a repository cannot be evaluated at \
all, and the report has to say so; got {report}"
);
assert!(
report.contains("LLM analysis"),
"and the rest of the report still has to arrive; got {report}"
);
}