requisite 0.1.0

Data-handling requirements encoded in Rust types.
Documentation
//! Runtime behavior tests for trust transitions, TTLs, confidence gates, and
//! scoped values. `tests/compile_fail.rs` covers the type-level contracts.

use requisite::fresh::scoped::with_live;
use requisite::prelude::*;
use requisite::trust::{sanitize, try_sanitize};
use std::time::Duration;

#[test]
fn trusted_can_widen_to_untrusted_but_value_survives() {
    let t = sanitize(
        Tainted::<_, Untrusted>::from_input("99x".to_string()),
        |s| s.chars().filter(|c| c.is_ascii_digit()).collect(),
    );
    // A trusted value satisfies an untrusted parameter.
    let u: Tainted<String, Untrusted> = t.widen();
    assert_eq!(u.as_ref(), "99");
}

#[test]
fn sanitizer_can_change_the_value_type() {
    let raw = Tainted::<_, Untrusted>::from_input("42".to_string());
    let trusted = try_sanitize(raw, |value| value.parse::<u64>()).unwrap();
    assert_eq!(trusted.into_inner(), 42);

    let raw = Tainted::<_, Untrusted>::from_input("customer".to_string());
    assert!(try_sanitize(raw, |value| value.parse::<u64>()).is_err());
}

#[test]
fn fresh_value_is_readable_during_its_ttl() {
    let f = Fresh::fetch(7u32, Duration::from_secs(1));
    assert_eq!(f.get().copied(), Ok(7));
    assert!(f.remaining() > Duration::ZERO);
}

#[test]
fn confidence_gate_tiers() {
    // Highest tier includes proof.
    let hi = Confident::new("kp9", 0.97).unwrap();
    let r = match hi.gate() {
        Gate::HighConfidence(_proof, value) => format!("woke:{value}"),
        Gate::Likely(value) => format!("buzz:{value}"),
        Gate::Unsure(value) => format!("log:{value}"),
    };
    assert_eq!(r, "woke:kp9");

    // Middle tier selects likely handling.
    let mid = Confident::new("kp6", 0.7).unwrap();
    let r = match mid.gate() {
        Gate::HighConfidence(_proof, value) => format!("woke:{value}"),
        Gate::Likely(value) => format!("buzz:{value}"),
        Gate::Unsure(value) => format!("log:{value}"),
    };
    assert_eq!(r, "buzz:kp6");

    // Lowest tier selects unsure handling.
    let lo = Confident::new("kp3", 0.2).unwrap();
    let r = match lo.gate() {
        Gate::HighConfidence(_proof, value) => format!("woke:{value}"),
        Gate::Likely(value) => format!("buzz:{value}"),
        Gate::Unsure(value) => format!("log:{value}"),
    };
    assert_eq!(r, "log:kp3");
}

#[test]
fn custom_confidence_thresholds_are_applied() {
    let thresholds = Thresholds::new(0.25, 0.98).unwrap();
    let value = Confident::new("signal", 0.99).unwrap();

    assert!(matches!(
        value.gate_with(thresholds),
        Gate::HighConfidence(_, "signal")
    ));
}

#[test]
fn invalid_confidence_inputs_are_rejected() {
    assert!(Confident::new((), f64::NAN).is_err());
    assert!(Confident::new((), -0.01).is_err());
    assert!(Confident::new((), 1.01).is_err());
    assert!(Confident::new((), f64::NAN).unwrap_err().value.is_nan());
    assert!(Thresholds::new(0.95, 0.60).is_err());
    assert!(Thresholds::new(0.60, 0.60).is_err());
    assert!(Thresholds::new(0.60, 0.94).is_err());
}

#[test]
fn scoped_live_allows_deriving_a_plain_value() {
    let out = with_live(10u32, |live| *live.get() * 2);
    assert_eq!(out, 20);
}