Skip to main content

Module fake

Module fake 

Source
Available on crate feature fake only.
Expand description

An in-memory SystemOne for tests, enabled by the fake feature.

FakeSystemOne answers every question it receives, so code under test gets a complete, well-typed response without network access. Unconfigured questions get maximally uncertain answers; set the answers a test cares about, script responses and failures, and inspect the requests that were sent.

use std::sync::Arc;
use std::time::Duration;
use typesafe_client::fake::FakeSystemOne;
use typesafe_client::{ApiError, Error, NoulQuestion, Questions, SystemOne};

struct Triage {
    typesafe: Arc<dyn SystemOne>,
}

impl Triage {
    async fn is_urgent(&self, ticket: &str) -> Result<bool, Error> {
        let mut questions = Questions::new();
        let urgent = questions.add("is_urgent", NoulQuestion::new("Does this convey urgency?"));
        let response = self.typesafe.system_one(ticket, questions).send().await?;
        Ok(response.answer(&urgent)?.noul > 0.7)
    }
}

let fake = Arc::new(FakeSystemOne::new());
let triage = Triage { typesafe: fake.clone() };

fake.set_noul("is_urgent", 0.92);
assert!(triage.is_urgent("Payouts have failed for 3 days!").await?);
assert_eq!(fake.last_request().unwrap().state.as_text(), Some("Payouts have failed for 3 days!"));

fake.push_error(ApiError::new(429, "Too many requests").with_retry_after(Duration::from_secs(1)));
let error = triage.is_urgent("again").await.unwrap_err();
assert_eq!(error.retry_after(), Some(Duration::from_secs(1)));

Structs§

FakeSystemOne
An in-memory SystemOne that answers from configuration instead of a model.

Constants§

UNCERTAIN_NOUL
The probability given to Noul questions without a configured answer.