use super::{
Polarity, RuleCategory, RuleException, RuleOrigin, RuleRecord, RuleScope, RuleState, Standing,
conflicted,
};
fn scope() -> RuleScope {
RuleScope {
roles: vec!["LocalPosition".to_owned()],
selector: "control.object.button".to_owned(),
configurations: Vec::new(),
projection: "base".to_owned(),
}
}
fn rule(id: &str, polarity: Polarity, requirement: &str) -> RuleRecord {
RuleRecord {
rule_id: id.to_owned(),
revision: 1,
category: RuleCategory::Route,
origin: RuleOrigin::DerivedCandidate,
authority_ref: None,
scope: scope(),
object: None,
requirement: requirement.to_owned(),
polarity,
expects: None,
exceptions: Vec::new(),
sources: vec![("src/a.rs".to_owned(), "digest-a".to_owned())],
state: RuleState::Proposed,
}
}
#[test]
fn only_an_authority_accepts_a_rule_and_a_candidate_cannot_be_one_yet() {
let mut candidate = rule("r1", Polarity::Require, "calls the layout provider");
assert_eq!(
candidate.standing(&[("src/a.rs".to_owned(), "digest-a".to_owned())]),
Standing::NotConstraining("a proposal is not a constraint"),
"a proposal must not constrain before it is accepted"
);
let refused = candidate
.accept(" ", 2)
.expect_err("an empty authority is not an authority");
assert!(refused.contains("needs an authority"), "{refused}");
let too_early = candidate
.accept("policy:control-v2", 2)
.expect_err("a derived candidate is not a rule yet");
assert!(too_early.contains("is not a project rule"), "{too_early}");
candidate.origin = RuleOrigin::AdoptedSpecimen;
candidate.accept("policy:control-v2", 2).unwrap();
assert_eq!(candidate.state, RuleState::Accepted);
assert_eq!(candidate.revision, 2);
assert_eq!(
candidate.standing(&[("src/a.rs".to_owned(), "digest-a".to_owned())]),
Standing::Constrains
);
}
#[test]
fn a_source_that_moved_or_vanished_stops_the_rule_from_constraining() {
let mut accepted = rule("r1", Polarity::Require, "calls the layout provider");
accepted.origin = RuleOrigin::AdoptedSpecimen;
accepted.accept("policy:control-v2", 2).unwrap();
assert_eq!(
accepted.standing(&[("src/a.rs".to_owned(), "digest-b".to_owned())]),
Standing::NotConstraining("a source this rule was accepted against changed")
);
assert_eq!(
accepted.standing(&[]),
Standing::NotConstraining("a source this rule was accepted against is no longer here"),
"a missing source counts as changed: the bytes it was judged on are not there to judge it"
);
let mut marked = accepted.clone();
marked.state = RuleState::Stale;
assert_eq!(
marked.standing(&[("src/a.rs".to_owned(), "digest-a".to_owned())]),
Standing::Constrains
);
accepted.retire("superseded by the v3 contract", 3).unwrap();
assert_eq!(
accepted.standing(&[("src/a.rs".to_owned(), "digest-a".to_owned())]),
Standing::NotConstraining("this rule was withdrawn on purpose")
);
assert!(
accepted.retire("", 4).is_err(),
"a retirement needs a reason"
);
}
#[test]
fn opposites_over_one_scope_are_both_conflicted_and_neither_wins() {
let mut requires = rule("r-old", Polarity::Require, "calls the layout provider");
requires.origin = RuleOrigin::AdoptedSpecimen;
requires.accept("policy:control-v1", 1).unwrap();
let mut forbids = rule("r-new", Polarity::Forbid, "calls the layout provider");
forbids.origin = RuleOrigin::AcceptedPolicy;
forbids.accept("policy:control-v2", 5).unwrap();
let found = conflicted(&[requires.clone(), forbids.clone()]);
assert_eq!(found, vec!["r-new".to_owned(), "r-old".to_owned()]);
assert!(requires.contradicts(&forbids) && forbids.contradicts(&requires));
let mut also_requires = forbids.clone();
also_requires.rule_id = "r-third".to_owned();
also_requires.polarity = Polarity::Require;
assert!(!requires.contradicts(&also_requires));
let mut elsewhere = forbids.clone();
elsewhere.rule_id = "r-elsewhere".to_owned();
elsewhere.scope.selector = "control.object.slider".to_owned();
assert!(!requires.contradicts(&elsewhere));
let proposal = rule("r-proposal", Polarity::Forbid, "calls the layout provider");
assert!(conflicted(&[requires, proposal]).is_empty());
}
#[test]
fn an_exception_without_an_authority_covers_nothing() {
let mut accepted = rule("r1", Polarity::Require, "calls the layout provider");
accepted.origin = RuleOrigin::AdoptedSpecimen;
accepted.accept("policy:control-v2", 2).unwrap();
accepted.exceptions = vec![
RuleException {
scope: "control.object.button.legacy".to_owned(),
reason: "the legacy path predates the provider".to_owned(),
authority: "maintainer:2026-10-16".to_owned(),
},
RuleException {
scope: "control.object.button.scratch".to_owned(),
reason: "somebody left this here".to_owned(),
authority: " ".to_owned(),
},
];
assert!(
accepted
.exception_for("control.object.button.legacy")
.is_some()
);
assert!(
accepted
.exception_for("control.object.button.scratch")
.is_none(),
"an exception with no authority is a rule that quietly stopped applying"
);
assert!(accepted.exception_for("control.object.slider").is_none());
}
#[test]
fn a_scope_that_does_not_cover_a_subject_says_which_dimension_refused() {
let scope = scope();
assert_eq!(
scope.refuses(
&["LocalPosition".to_owned()],
"control.object.button",
"debug",
"base"
),
None
);
assert!(
scope
.refuses(
&["Persisted".to_owned()],
"control.object.button",
"debug",
"base"
)
.is_some_and(|why| why.contains("role"))
);
assert!(
scope
.refuses(
&["LocalPosition".to_owned()],
"control.object.slider",
"debug",
"base"
)
.is_some_and(|why| why.contains("selects"))
);
assert!(
scope
.refuses(
&["LocalPosition".to_owned()],
"control.object.button",
"debug",
"graft(button_fast)"
)
.is_some_and(|why| why.contains("projection"))
);
let mut any = scope.clone();
any.roles.clear();
any.configurations.clear();
assert_eq!(
any.refuses(&[], "control.object.button", "release", "base"),
None
);
let mut narrowed = scope;
narrowed.configurations = vec!["debug".to_owned()];
assert!(
narrowed
.refuses(
&["LocalPosition".to_owned()],
"control.object.button",
"release",
"base"
)
.is_some_and(|why| why.contains("configuration"))
);
}