use super::{ask, quiet};
use crate::boundary::Query;
use crate::boundary::config::ConfigFile;
use crate::boundary::config::read::roster;
use crate::boundary::reply::Reply;
use crate::config_edit::branch::edit::EditOrigin;
use crate::config_edit::brazen::BzOutcome;
use crate::git_tree::tests::fixture::Fixture;
use std::path::Path;
use tempfile::tempdir;
fn reading(file: ConfigFile) -> Query {
Query::ReadConfig { file }
}
fn lineage_file(workspace: &Path, path: &str) -> ConfigFile {
ConfigFile::Branch {
workspace: crate::naming::leaf(workspace),
lineage: "default".to_owned(),
origin: EditOrigin::Advance,
path: path.to_owned(),
}
}
#[test]
fn a_lineage_read_answers_the_bytes_at_its_tip() {
let root = tempdir().unwrap();
let fx = Fixture::new();
let deps = super::seeing(&quiet(root.path()), &[fx.path.as_path()]);
assert_eq!(
ask(&deps, &reading(lineage_file(&fx.path, "version"))),
Ok(Reply::Config {
text: "1\n".to_owned()
})
);
}
#[test]
fn a_path_the_lineage_does_not_hold_refuses_in_gits_words() {
let root = tempdir().unwrap();
let fx = Fixture::new();
let deps = super::seeing(&quiet(root.path()), &[fx.path.as_path()]);
let err = ask(&deps, &reading(lineage_file(&fx.path, "no-such-file"))).unwrap_err();
assert!(err.contains("no-such-file"), "{err}");
}
#[test]
fn the_browse_lists_every_lineage_with_its_own_files() {
let root = tempdir().unwrap();
let fx = Fixture::new();
let deps = super::seeing(&quiet(root.path()), &[fx.path.as_path()]);
fx.commit_other("workflow.yaml", "events: {}\n");
fx.orphan_config("island");
let Ok(Reply::Lineages(rows)) = ask(
&deps,
&Query::Lineages {
workspace: crate::naming::leaf(&(fx.path.clone())),
},
) else {
panic!("lineages answers lineages");
};
let named = |name: &str| {
rows.iter()
.find(|r| r.branch.name == name)
.unwrap_or_else(|| panic!("{name} is not listed: {rows:?}"))
.clone()
};
let default = named("default");
assert!(default.files.contains(&"workflow.yaml".to_owned()));
assert_eq!(default.branch.tip_oid.len(), 40);
assert_eq!(
Some(default.branch.tip_short_oid.as_str()),
default.branch.tip_oid.get(..8)
);
assert!(!named("island").files.contains(&"workflow.yaml".to_owned()));
}
#[test]
fn a_workspace_with_no_repo_refuses_the_browse_rather_than_listing_nothing() {
let root = tempdir().unwrap();
let nowhere = root.path().join("nowhere");
let deps = super::seeing(&quiet(root.path()), &[nowhere.as_path()]);
assert!(
ask(
&deps,
&Query::Lineages {
workspace: crate::naming::leaf(&nowhere),
},
)
.is_err()
);
}
#[test]
fn a_roster_for_a_row_this_wall_does_not_have_refuses_by_name() {
let root = tempdir().unwrap();
let deps = quiet(root.path());
let err = ask(
&deps,
&Query::Models {
workspace: crate::naming::leaf(&(crate::test_support::fixture_workspace())),
provider: "not-a-row".to_owned(),
},
)
.unwrap_err();
assert!(err.contains("not-a-row"), "{err}");
}
#[test]
fn the_roster_fold_answers_ids_or_says_why_there_are_none() {
let out = |success: bool, stdout: &str, stderr: &str| BzOutcome {
success,
stdout: stdout.to_owned(),
stderr: stderr.to_owned(),
};
assert_eq!(
roster(&out(
true,
"{\"models\":[{\"id\":\"m-9\"},{\"id\":\"m-1\"}]}",
""
)),
Ok(vec!["m-9".to_owned(), "m-1".to_owned()])
);
assert_eq!(
roster(&out(true, "{\"models\":[]}", "")),
Err(crate::model_pick::query::EMPTY_ROSTER.to_owned())
);
assert_eq!(
roster(&out(true, "not json at all", "")),
Err(crate::model_pick::query::EMPTY_ROSTER.to_owned())
);
assert_eq!(
roster(&out(false, "", " unknown provider `x`\n")),
Err("unknown provider `x`".to_owned())
);
assert_eq!(
roster(&out(false, "", "")),
Err("bz --list-models failed".to_owned())
);
}