use super::super::CheckStatus;
use super::interpret_tier_s_facts;
use crate::prompt_facts::{TierSFact, TIER_S_REAFFIRM_DAYS};
use chrono::{DateTime, Duration, Utc};
fn now() -> DateTime<Utc> {
DateTime::from_timestamp(1_800_000_000, 0).expect("fixed now")
}
fn fact(subject: &str, object: &str, days_ago: i64) -> TierSFact {
TierSFact {
subject: subject.into(),
predicate: "has_convention".into(),
object: object.into(),
affirmed_at: now() - Duration::days(days_ago),
}
}
#[test]
fn no_facts_is_pass() {
let r = interpret_tier_s_facts("Tier S".into(), &[], now());
assert_eq!(r.status, CheckStatus::Pass);
}
#[test]
fn fresh_facts_are_pass() {
let facts = vec![
fact("conv-1", "Write plainly", 3),
fact("conv-2", "Never merge red CI", TIER_S_REAFFIRM_DAYS),
];
let r = interpret_tier_s_facts("Tier S".into(), &facts, now());
assert_eq!(r.status, CheckStatus::Pass, "detail: {:?}", r.detail);
}
#[test]
fn stale_facts_warn_and_name_the_retirement_path() {
let facts = vec![
fact("conv-old", "Clickable links always", 200),
fact("conv-new", "Write plainly", 1),
];
let r = interpret_tier_s_facts("Tier S".into(), &facts, now());
assert_eq!(r.status, CheckStatus::Warn);
let d = r.detail.as_deref().unwrap_or("");
assert!(d.contains("conv-old"), "must name the stale subject: {d}");
assert!(
d.contains("Clickable links always"),
"must show the rule text: {d}"
);
assert!(d.contains("200d ago"), "must show the age: {d}");
assert!(
!d.contains("conv-new"),
"must not list a fact affirmed 1 day ago: {d}"
);
assert!(
d.contains("remove_prompt_fact"),
"must name the retirement path: {d}"
);
assert!(
d.contains("kg_assert"),
"must name the re-affirmation path: {d}"
);
assert!(
d.contains("Nothing was removed"),
"must state that it did not retire anything: {d}"
);
assert!(
d.contains("1 of 2"),
"must report stale-of-total so the scale is legible: {d}"
);
}
#[test]
fn stale_facts_never_fail() {
let facts: Vec<TierSFact> = (0..20)
.map(|i| fact(&format!("conv-{i}"), "an ancient rule", 5_000))
.collect();
let r = interpret_tier_s_facts("Tier S".into(), &facts, now());
assert_eq!(
r.status,
CheckStatus::Warn,
"a report-only check must never escalate to Fail"
);
assert_ne!(r.status, CheckStatus::Fail);
}