use super::{
Allowance, Budget, GRANT_CHANNELS, GRANT_COMPARED_DIMENSIONS, GRANT_DIMENSIONS,
GRANT_OPERATIONS, GRANT_RECORDED_DIMENSIONS, Grant, UseRequest,
};
fn dimensions() -> Vec<(&'static str, String)> {
let candidates = vec![
("approver", "maintainer".to_owned()),
("budget", "files=8,executions=8".to_owned()),
("candidate", "574196-1-0".to_owned()),
("channel", "interactive-cli".to_owned()),
("operation", "land".to_owned()),
("policy", "xirang.candidate/v1".to_owned()),
("project", "example-package".to_owned()),
("proposal", "3".to_owned()),
("revision", "1".to_owned()),
("scope", "src,README.md".to_owned()),
("task", "b3c1de9a28d6260f".to_owned()),
];
candidates
}
fn grant() -> Grant {
Grant::issue(dimensions()).expect("the legal control has to issue")
}
fn use_request<'a>(paths: &'a [String]) -> UseRequest<'a> {
UseRequest {
project: "example-package",
task: "b3c1de9a28d6260f",
candidate: "574196-1-0",
proposal: "3",
revision: "1",
operation: "land",
paths,
policy: "xirang.candidate/v1",
cost: Budget {
files: Some(paths.len() as u64),
executions: Some(1),
},
}
}
fn paths(values: &[&str]) -> Vec<String> {
values.iter().map(|value| (*value).to_owned()).collect()
}
#[test]
fn a_legal_grant_covers_a_use_inside_its_scope_and_a_subset_of_it() {
let grant = grant();
let whole = paths(&["src/lib.rs", "README.md"]);
assert_eq!(
grant.covers(&use_request(&whole)),
Allowance::Covered,
"the scope the grant names is covered"
);
let one_edit = paths(&["src/lib.rs"]);
assert!(
grant.covers(&use_request(&one_edit)).is_covered(),
"an edit inside the granted scope does not need a second approval"
);
let no_change = paths(&[]);
assert!(
grant.covers(&use_request(&no_change)).is_covered(),
"a landing that writes nothing is covered by construction"
);
}
#[test]
fn a_grant_missing_a_dimension_is_refused_and_the_refusal_names_it() {
for name in GRANT_DIMENSIONS {
let incomplete: Vec<(&'static str, String)> = dimensions()
.into_iter()
.filter(|(field, _)| field != name)
.collect();
let refusal = Grant::issue(incomplete)
.expect_err("a grant that leaves a dimension out is not issued");
assert!(
refusal.contains(name),
"the refusal has to name `{name}`: {refusal}"
);
assert!(
refusal.contains("missing"),
"and say what is wrong with it: {refusal}"
);
}
}
#[test]
fn a_forged_approval_cannot_be_issued() {
let mut named = dimensions();
named.push(("signed_by_somebody", "the agent".to_owned()));
let refusal = Grant::issue(named).expect_err("an unknown field is not a dimension");
assert!(
refusal.contains("signed_by_somebody") && refusal.contains("not a grant dimension"),
"{refusal}"
);
for (field, value) in [
("approver", "unknown"),
("channel", "unknown"),
("channel", "xirang-mcp"),
("operation", "delete-everything"),
] {
let mut forged = dimensions();
for entry in forged.iter_mut() {
if entry.0 == field {
entry.1 = value.to_owned();
}
}
let refusal = Grant::issue(forged).expect_err("this cannot be issued");
assert!(
refusal.contains(field),
"the refusal names `{field}`: {refusal}"
);
}
assert_eq!(
GRANT_OPERATIONS,
&["land", "publish", "widen"],
"a new operation needs its own surface before it needs a grant"
);
assert_eq!(
GRANT_CHANNELS.len(),
3,
"the ruling names three channels; a fourth is invented"
);
}
#[test]
fn an_old_approval_never_vouches_for_another_thing() {
let grant = grant();
let inside = paths(&["src/lib.rs"]);
let moves: [(&str, UseRequest<'_>); 6] = [
(
"candidate",
UseRequest {
candidate: "574196-2-0",
..use_request(&inside)
},
),
(
"proposal",
UseRequest {
proposal: "4",
..use_request(&inside)
},
),
(
"revision",
UseRequest {
revision: "2",
..use_request(&inside)
},
),
(
"operation",
UseRequest {
operation: "publish",
..use_request(&inside)
},
),
(
"project",
UseRequest {
project: "another-package",
..use_request(&inside)
},
),
(
"task",
UseRequest {
task: "ffffffffffffffff",
..use_request(&inside)
},
),
];
for (dimension, request) in moves {
match grant.covers(&request) {
Allowance::Refused(reason) => {
assert!(
reason.contains(dimension),
"the refusal names the dimension `{dimension}`: {reason}"
);
assert!(
reason.contains("never vouches"),
"and why an approval does not travel: {reason}"
);
}
Allowance::Covered => panic!("`{dimension}` moved and the grant still covered it"),
}
}
}
#[test]
fn one_approval_can_authorise_a_widening_and_the_landing_that_follows() {
let mut both = dimensions();
for entry in both.iter_mut() {
if entry.0 == "operation" {
entry.1 = "land,widen".to_owned();
}
}
let grant = Grant::issue(both.clone()).expect("a set of implemented verbs is a value");
let inside = paths(&["src/lib.rs"]);
assert!(
grant
.covers(&UseRequest {
operation: "widen",
..use_request(&inside)
})
.is_covered(),
"the widening the person approved"
);
assert!(
grant
.covers(&UseRequest {
operation: "land",
..use_request(&inside)
})
.is_covered(),
"and the landing that follows it, from the same decision"
);
match grant.covers(&UseRequest {
operation: "publish",
..use_request(&inside)
}) {
Allowance::Refused(reason) => assert!(
reason.contains("publish") && reason.contains("land, widen"),
"the refusal names the operation asked for and the ones granted: {reason}"
),
Allowance::Covered => panic!("an approval for land+widen covered a publish"),
}
for entry in both.iter_mut() {
if entry.0 == "operation" {
entry.1 = "land,delete".to_owned();
}
}
let refusal = Grant::issue(both).expect_err("`delete` is not a verb here");
assert!(
refusal.contains("delete") && refusal.contains("not an operation"),
"{refusal}"
);
}
#[test]
fn a_newer_approval_covers_an_older_reliance_but_not_the_reverse() {
let inside = paths(&["src/lib.rs"]);
let newer = {
let mut moved = dimensions();
for entry in moved.iter_mut() {
if entry.0 == "revision" {
entry.1 = "2".to_owned();
}
}
Grant::issue(moved).expect("the re-issued approval")
};
let older_reliance = UseRequest {
revision: "1",
..use_request(&inside)
};
assert!(
newer.covers(&older_reliance).is_covered(),
"a re-issued approval still covers a candidate that relied on the previous revision"
);
let newer_reliance = UseRequest {
revision: "3",
..use_request(&inside)
};
match grant().covers(&newer_reliance) {
Allowance::Refused(reason) => assert!(
reason.contains("revision") && reason.contains("never vouches"),
"an old approval cannot cover a reliance on a newer one: {reason}"
),
Allowance::Covered => panic!("an old approval covered a newer reliance"),
}
}
#[test]
fn a_path_outside_the_scope_is_refused_by_name() {
let grant = grant();
let outside = paths(&["src/lib.rs", "src-other/lib.rs"]);
match grant.covers(&use_request(&outside)) {
Allowance::Refused(reason) => {
assert!(
reason.contains("src-other/lib.rs"),
"the refusal points at the path, not just at the rule: {reason}"
);
assert!(
reason.contains("outside the approved scope"),
"and names the rule: {reason}"
);
assert!(
reason.contains("src, README.md"),
"and shows the scope that was granted: {reason}"
);
}
Allowance::Covered => panic!("a string prefix is not a scope"),
}
}
#[test]
fn the_same_dimensions_in_another_order_are_the_same_grant() {
let mut shuffled = dimensions();
shuffled.reverse();
assert_eq!(
grant().digest(),
Grant::issue(shuffled)
.expect("the same dimensions")
.digest(),
"the digest is over the bound set, not over the order it was written in"
);
assert_ne!(
grant().digest(),
{
let mut moved = dimensions();
for entry in moved.iter_mut() {
if entry.0 == "proposal" {
entry.1 = "4".to_owned();
}
}
Grant::issue(moved).expect("still a grant").digest()
},
"a different proposal is a different grant"
);
}
#[test]
fn the_compared_and_recorded_dimensions_are_disjoint_and_complete() {
let mut both: Vec<&str> = GRANT_COMPARED_DIMENSIONS
.iter()
.chain(GRANT_RECORDED_DIMENSIONS.iter())
.copied()
.collect();
both.sort_unstable();
let mut all: Vec<&str> = GRANT_DIMENSIONS.to_vec();
all.sort_unstable();
assert_eq!(
both, all,
"the two halves must cover the contract exactly once"
);
assert!(
GRANT_COMPARED_DIMENSIONS
.iter()
.all(|name| !GRANT_RECORDED_DIMENSIONS.contains(name)),
"a dimension is either compared or recorded, never both"
);
}
#[test]
fn a_use_under_another_policy_version_is_refused_by_name() {
let grant = grant();
let paths = paths(&["src/lib.rs"]);
let mut request = use_request(&paths);
request.policy = "xirang.candidate/v2";
match grant.covers(&request) {
Allowance::Refused(reason) => assert!(
reason.contains("policy") && reason.contains("xirang.candidate/v2"),
"the refusal names the policy dimension and the value the use runs under: {reason}"
),
Allowance::Covered => panic!("a grant under one policy version vouched for another"),
}
}
#[test]
fn a_use_costing_more_than_the_approved_budget_is_refused_with_both_numbers() {
let grant = grant();
let paths = paths(&["src/lib.rs"]);
let mut request = use_request(&paths);
request.cost = Budget {
files: Some(9),
executions: Some(1),
};
match grant.covers(&request) {
Allowance::Refused(reason) => assert!(
reason.contains("9 file(s)") && reason.contains("covers 8"),
"the refusal puts the two numbers side by side: {reason}"
),
Allowance::Covered => panic!("a landing cost more files than the approval covered"),
}
request.cost = Budget {
files: Some(8),
executions: Some(1),
};
assert!(grant.covers(&request).is_covered());
let unreadable = Grant::issue(dimensions().into_iter().map(|(name, value)| {
if name == "budget" {
("budget", "executions=lots".to_owned())
} else {
(name, value)
}
}));
assert!(
unreadable.is_err_and(|reason| reason.contains("not a number of executions")),
"an unreadable budget is refused at issue time"
);
}