use super::knobs::{pick, workspace};
use super::{deps_at, fire, quiet, script};
use crate::boundary::Action;
use crate::boundary::reply::Reply;
use crate::model_pick::{Effort, Tuning};
use crate::test_support::TEMPLATE_PROVIDERS;
use std::fs;
use std::path::Path;
use tempfile::tempdir;
fn tune(tuning: crate::model_pick::Tuning) -> Action {
Action::Tune(tuning)
}
#[test]
fn a_tuning_gesture_commits_the_same_file_the_pick_does() {
let root = tempdir().unwrap();
let bin = tempdir().unwrap();
let log = bin.path().join("log");
let litany = script(
bin.path(),
"litany",
&format!(
"cat \"$YOG_EDIT_SRC/providers.yaml\" > {}\nexit 0\n",
log.display()
),
);
let fx = workspace();
let deps = super::seeing(
&deps_at(root.path(), &litany, Path::new("/no/bl")),
&[fx.path.as_path()],
);
let reply = fire(
&deps,
&tune(Tuning::Effort {
workspace: crate::naming::leaf(&fx.path),
role: "worker".to_owned(),
level: Some(Effort::High),
}),
);
assert!(
matches!(&reply, Ok(Reply::Outcome(o)) if o.ok()),
"{reply:?}"
);
let staged = fs::read_to_string(&log).unwrap();
assert!(staged.contains("effort: high"), "{staged}");
let untouched: Vec<&str> = TEMPLATE_PROVIDERS
.lines()
.filter(|l| l.trim_start().starts_with("provider:") || l.trim_start().starts_with("tools:"))
.collect();
assert!(!untouched.is_empty(), "the fixture states a pointer");
for line in untouched {
assert!(staged.contains(line), "{line} lost from {staged}");
}
}
#[test]
fn a_tuning_gesture_reads_no_provider_table_and_so_cannot_be_gated_by_one() {
let root = tempdir().unwrap();
let bin = tempdir().unwrap();
let log = bin.path().join("log");
let litany = script(
bin.path(),
"litany",
&format!(
"cat \"$YOG_EDIT_SRC/providers.yaml\" > {}\nexit 0\n",
log.display()
),
);
let fx = workspace();
let deps = super::seeing(
&deps_at(root.path(), &litany, Path::new("/no/bl")),
&[fx.path.as_path()],
);
let reply = fire(
&deps,
&tune(Tuning::Priority {
workspace: crate::naming::leaf(&fx.path),
role: "compactor".to_owned(),
on: true,
}),
);
assert!(
matches!(&reply, Ok(Reply::Outcome(o)) if o.ok()),
"{reply:?}"
);
assert!(fs::read_to_string(&log).unwrap().contains("priority: true"));
let err = fire(&deps, &pick("worker", "acme", "m-9", &fx.path)).unwrap_err();
assert!(err.contains("no provider row `acme`"), "{err}");
}
#[test]
fn a_tuning_gesture_on_an_undeclared_role_refuses_at_the_executor() {
let root = tempdir().unwrap();
let fx = workspace();
let deps = super::seeing(&quiet(root.path()), &[fx.path.as_path()]);
let err = fire(
&deps,
&tune(Tuning::Effort {
workspace: crate::naming::leaf(&fx.path),
role: "nonesuch".to_owned(),
level: None,
}),
)
.unwrap_err();
assert!(err.contains("nonesuch"), "{err}");
}
#[test]
fn a_tuning_gesture_needs_a_lineage_it_can_read() {
let root = tempdir().unwrap();
let deps = quiet(root.path());
let err = fire(
&deps,
&tune(Tuning::Priority {
workspace: crate::naming::leaf(root.path()),
role: "worker".to_owned(),
on: false,
}),
)
.unwrap_err();
assert!(!err.is_empty(), "{err}");
}