use alfrusco::{config, AsyncRunnable, Item, Runnable, Workflow, WorkflowError};
#[derive(Debug)]
enum TestError {
Simple(String),
}
impl std::fmt::Display for TestError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TestError::Simple(msg) => write!(f, "Test error: {msg}"),
}
}
}
impl std::error::Error for TestError {}
impl WorkflowError for TestError {}
struct TestRunnable {
should_fail: bool,
}
impl Runnable for TestRunnable {
type Error = TestError;
fn run(self, wf: &mut Workflow) -> Result<(), Self::Error> {
if self.should_fail {
Err(TestError::Simple("Intentional failure".to_string()))
} else {
wf.append_item(Item::new("Success"));
Ok(())
}
}
}
struct TestAsyncRunnable {
should_fail: bool,
}
#[async_trait::async_trait]
impl AsyncRunnable for TestAsyncRunnable {
type Error = TestError;
async fn run_async(self, wf: &mut Workflow) -> Result<(), Self::Error> {
if self.should_fail {
Err(TestError::Simple("Intentional async failure".to_string()))
} else {
wf.append_item(Item::new("Async Success"));
Ok(())
}
}
}
#[test]
fn test_execute_success() {
let runnable = TestRunnable { should_fail: false };
let mut buffer = Vec::new();
let dir = tempfile::tempdir().unwrap().keep();
alfrusco::execute(&config::TestingProvider(dir), runnable, &mut buffer);
let output = String::from_utf8(buffer).unwrap();
assert!(output.contains("Success"));
assert!(!output.contains("Test error"));
}
#[test]
fn test_execute_failure() {
let runnable = TestRunnable { should_fail: true };
let mut buffer = Vec::new();
let dir = tempfile::tempdir().unwrap().keep();
alfrusco::execute(&config::TestingProvider(dir), runnable, &mut buffer);
let output = String::from_utf8(buffer).unwrap();
assert!(output.contains("Test error: Intentional failure"));
}
#[tokio::test]
async fn test_execute_async_success() {
let runnable = TestAsyncRunnable { should_fail: false };
let mut buffer = Vec::new();
let dir = tempfile::tempdir().unwrap().keep();
alfrusco::execute_async(&config::TestingProvider(dir), runnable, &mut buffer).await;
let output = String::from_utf8(buffer).unwrap();
assert!(output.contains("Async Success"));
assert!(!output.contains("Test error"));
}
#[tokio::test]
async fn test_execute_async_failure() {
let runnable = TestAsyncRunnable { should_fail: true };
let mut buffer = Vec::new();
let dir = tempfile::tempdir().unwrap().keep();
alfrusco::execute_async(&config::TestingProvider(dir), runnable, &mut buffer).await;
let output = String::from_utf8(buffer).unwrap();
assert!(output.contains("Test error: Intentional async failure"));
}