use super::{
Completion, ExecutionIdentity, Receipt, Reuse, UnknownReason, VerificationKind,
VerificationOutcome, Witness, classify,
};
#[test]
fn timeout_and_cancellation_are_different_typed_reasons() {
assert_eq!(
classify(VerificationKind::Tests, true, false, None, 0, 0),
VerificationOutcome::Unknown(UnknownReason::Timeout)
);
assert_eq!(
classify(VerificationKind::Tests, false, true, None, 0, 0),
VerificationOutcome::Unknown(UnknownReason::Cancelled)
);
assert_eq!(UnknownReason::Timeout.label(), "timeout");
assert_eq!(UnknownReason::Cancelled.label(), "cancelled");
}
#[test]
fn a_zero_test_run_and_a_compile_error_are_not_a_pass() {
assert_eq!(
classify(VerificationKind::Tests, false, false, Some(0), 1, 0),
VerificationOutcome::Unknown(UnknownReason::ZeroExecuted)
);
assert_eq!(
classify(VerificationKind::Tests, false, false, Some(0), 0, 0),
VerificationOutcome::Unknown(UnknownReason::NoResultLine)
);
assert_eq!(
classify(VerificationKind::Tests, false, false, Some(101), 0, 0),
VerificationOutcome::Failed
);
assert_eq!(
classify(VerificationKind::Tests, false, false, Some(0), 1, 1),
VerificationOutcome::Passed
);
}
#[test]
fn compilation_success_is_typed_apart_from_executed_tests() {
assert_eq!(
classify(VerificationKind::Compilation, false, false, Some(0), 0, 0),
VerificationOutcome::Passed
);
assert_eq!(
classify(VerificationKind::Compilation, false, false, Some(101), 0, 0),
VerificationOutcome::Failed
);
assert_eq!(
classify(VerificationKind::Compilation, true, false, None, 0, 0),
VerificationOutcome::Unknown(UnknownReason::Timeout)
);
assert_eq!(VerificationKind::Tests.label(), "tests");
assert_eq!(VerificationKind::Compilation.label(), "compilation");
}
#[test]
fn answered_is_not_applied_is_not_verified_is_not_accepted() {
let answered = Completion::answered();
assert!(answered.answered && !answered.applied && !answered.accepted);
assert!(!answered.verification_closes());
let applied = Completion::applied();
assert!(applied.applied && !applied.verification_closes());
let verified = Completion::verified(VerificationOutcome::Passed);
assert!(verified.verification_closes());
assert!(!verified.accepted);
let accepted = Completion::accepted();
assert!(accepted.accepted && !accepted.verification_closes());
}
#[test]
fn acceptance_is_required_only_when_the_policy_says_so() {
let applied_and_verified = Completion {
answered: true,
applied: true,
verified: Some(VerificationOutcome::Passed),
accepted: false,
};
assert!(
applied_and_verified.is_complete(false),
"an ordinary verified maintenance step needs no new approval gate"
);
assert!(
!applied_and_verified.is_complete(true),
"a policy that requires acceptance still rejects it"
);
let accepted = Completion {
answered: true,
applied: true,
verified: Some(VerificationOutcome::Passed),
accepted: true,
};
assert!(accepted.is_complete(true));
}
#[test]
fn a_historical_pass_never_settles_an_unperformed_operation() {
let verified_only = Completion {
answered: true,
applied: false,
verified: Some(VerificationOutcome::Passed),
accepted: true,
};
assert!(!verified_only.is_complete(false) && !verified_only.is_complete(true));
let applied_only = Completion {
answered: true,
applied: true,
verified: None,
accepted: true,
};
assert!(!applied_only.is_complete(false));
assert!(
!Completion::verified(VerificationOutcome::Unknown(UnknownReason::Timeout))
.verification_closes(),
"an unknown outcome never closes a verification obligation"
);
}
#[test]
fn each_reuse_condition_refuses_on_its_own_line() {
let reuse = |receipt: &Receipt, current: &Receipt| receipt.reuse_against(current);
let identity = ExecutionIdentity::of([
("candidate", "c"),
("toolchain", "rustc 1.96.0"),
("target", "x86_64-unknown-linux-gnu"),
("profile", "debug"),
("features", "default"),
("no_default_features", "false"),
("selection", "workspace=true; package=none"),
("mode", "test"),
("checker", "xirang-toolchain 0.1.0"),
("isolation", "in-tree (cargo default)"),
])
.unwrap();
let base = Receipt {
obligation: "regression".to_owned(),
execution: identity.clone(),
input_manifest: "closure-1".to_owned(),
oracle_and_policy: "policy-1".to_owned(),
executed: 3,
outcome: VerificationOutcome::Passed,
coverage_confirmed: true,
anomaly_undisposed: Witness::Known(false),
scope_expanded: Witness::Known(false),
published_form_changed: Witness::Known(false),
};
assert_eq!(reuse(&base, &base.clone()), Reuse::Allowed);
let other_identity = ExecutionIdentity::of([
("candidate", "c"),
("toolchain", "rustc 1.96.0"),
("target", "x86_64-unknown-linux-gnu"),
("profile", "release"),
("features", "default"),
("no_default_features", "false"),
("selection", "workspace=true; package=none"),
("mode", "test"),
("checker", "xirang-toolchain 0.1.0"),
("isolation", "in-tree (cargo default)"),
])
.unwrap();
let cases: Vec<(&str, Receipt, &str)> = vec![
(
"a different obligation",
Receipt {
obligation: "other".to_owned(),
..base.clone()
},
"different obligation",
),
(
"a different execution identity",
Receipt {
execution: other_identity,
..base.clone()
},
"execution identity differs",
),
(
"a different input closure",
Receipt {
input_manifest: "closure-2".to_owned(),
..base.clone()
},
"input closure",
),
(
"a different oracle or policy",
Receipt {
oracle_and_policy: "policy-2".to_owned(),
..base.clone()
},
"oracle or policy",
),
(
"zero executed tests",
Receipt {
executed: 0,
..base.clone()
},
"executed no tests",
),
(
"a run that did not pass",
Receipt {
outcome: VerificationOutcome::Unknown(UnknownReason::ZeroExecuted),
..base.clone()
},
"only a passed run",
),
(
"assumed coverage",
Receipt {
coverage_confirmed: false,
..base.clone()
},
"assumed rather than confirmed",
),
(
"an undisposed anomaly",
Receipt {
anomaly_undisposed: Witness::Known(true),
..base.clone()
},
"undisposed anomaly",
),
(
"a grown scope",
Receipt {
scope_expanded: Witness::Known(true),
..base.clone()
},
"scope",
),
(
"a published form that changed",
Receipt {
published_form_changed: Witness::Known(true),
..base.clone()
},
"published form",
),
];
for (label, receipt, expected) in cases {
match reuse(&receipt, &base) {
Reuse::Refused(reason) => assert!(
reason.contains(expected),
"{label}: the refusal must name the condition, got `{reason}`"
),
Reuse::Allowed => panic!("{label} must refuse reuse"),
}
}
let grown = Receipt {
scope_expanded: Witness::Known(true),
..base.clone()
};
assert!(matches!(reuse(&base, &grown), Reuse::Refused(_)));
}
#[test]
fn an_unobserved_dimension_refuses_reuse_and_says_what_would_make_it_observable() {
let reuse = |receipt: &Receipt, current: &Receipt| receipt.reuse_against(current);
let identity = ExecutionIdentity::of([
("candidate", "c"),
("toolchain", "rustc 1.96.0"),
("target", "x86_64-unknown-linux-gnu"),
("profile", "debug"),
("features", "default"),
("no_default_features", "false"),
("selection", "workspace=true; package=none"),
("mode", "test"),
("checker", "xirang-toolchain 0.1.0"),
("isolation", "in-tree (cargo default)"),
])
.unwrap();
let base = Receipt {
obligation: "regression".to_owned(),
execution: identity,
input_manifest: "closure-1".to_owned(),
oracle_and_policy: "policy-1".to_owned(),
executed: 3,
outcome: VerificationOutcome::Passed,
coverage_confirmed: true,
anomaly_undisposed: Witness::Known(false),
scope_expanded: Witness::Known(false),
published_form_changed: Witness::Known(false),
};
assert_eq!(
reuse(&base, &base.clone()),
Reuse::Allowed,
"the legal control: three observed-unchanged dimensions allow reuse"
);
for dimension in ["anomaly", "scope", "published form"] {
let witness = Witness::Unknown("the plan did not record it; rerun to record one");
let mut receipt = base.clone();
match dimension {
"anomaly" => receipt.anomaly_undisposed = witness,
"scope" => receipt.scope_expanded = witness,
_ => receipt.published_form_changed = witness,
}
match reuse(&receipt, &base) {
Reuse::Refused(reason) => {
assert!(
reason.contains("nobody observed this dimension"),
"{dimension}: the refusal says nobody looked, not that the fact happened: {reason}"
);
assert!(
reason.contains("rerun to record one"),
"{dimension}: and says what would make it observable: {reason}"
);
}
Reuse::Allowed => panic!("{dimension}: an unobserved dimension allowed reuse"),
}
}
let happened = Receipt {
scope_expanded: Witness::Known(true),
..base.clone()
};
let unobserved = Receipt {
scope_expanded: Witness::Unknown("nobody looked"),
..base.clone()
};
let (Reuse::Refused(fact), Reuse::Refused(looked)) =
(reuse(&happened, &base), reuse(&unobserved, &base))
else {
panic!("both sides must refuse");
};
assert_ne!(
fact, looked,
"`it happened` and `nobody looked` are different sentences"
);
}
#[test]
fn a_credential_from_another_checker_is_refused_by_name() {
let identity = |checker: &str| {
ExecutionIdentity::of([
("candidate", "c"),
("toolchain", "rustc 1.96.0"),
("target", "x86_64-unknown-linux-gnu"),
("profile", "debug"),
("features", "default"),
("no_default_features", "false"),
("selection", "workspace=true; package=none"),
("mode", "test"),
("checker", checker),
("isolation", "in-tree (cargo default)"),
])
.unwrap()
};
let receipt = |checker: &str| Receipt {
obligation: "regression".to_owned(),
execution: identity(checker),
input_manifest: "closure-1".to_owned(),
oracle_and_policy: "policy-1".to_owned(),
executed: 3,
outcome: VerificationOutcome::Passed,
coverage_confirmed: true,
anomaly_undisposed: Witness::Known(false),
scope_expanded: Witness::Known(false),
published_form_changed: Witness::Known(false),
};
let stored = receipt("xirang-toolchain 0.1.0");
let current = receipt("xirang-toolchain 0.2.0");
match stored.reuse_against(¤t) {
Reuse::Refused(why) => assert!(
why.contains("checker"),
"the refusal has to name the dimension that moved: {why}"
),
other => panic!("another checker's credential must be refused, got {other:?}"),
}
assert_eq!(
stored.reuse_against(&receipt("xirang-toolchain 0.1.0")),
Reuse::Allowed,
"the same checker, bytes and oracle is still the same credential"
);
}