#![allow(clippy::cast_precision_loss)]
use do_memory_core::{ExecutionResult, ExecutionStep};
use proptest::prelude::*;
fn arb_order() -> impl Strategy<Value = usize> {
1..=100usize
}
fn arb_tool_name() -> impl Strategy<Value = String> {
"[a-z][a-z_]{2,15}"
}
fn arb_action() -> impl Strategy<Value = String> {
"[a-zA-Z ]{5,50}"
}
fn arb_result() -> impl Strategy<Value = Option<ExecutionResult>> {
prop_oneof![
Just(None),
Just(Some(ExecutionResult::Success {
output: "OK".to_string()
})),
Just(Some(ExecutionResult::Error {
message: "Error occurred".to_string()
})),
]
}
proptest! {
#[test]
fn step_order_preserved(order in arb_order(), tool in arb_tool_name(), action in arb_action()) {
let step = ExecutionStep::new(order, tool, action);
assert_eq!(step.step_number, order);
}
#[test]
fn step_tool_preserved(tool in arb_tool_name()) {
let step = ExecutionStep::new(1, tool.clone(), "action".to_string());
assert_eq!(step.tool, tool);
}
#[test]
fn step_action_preserved(action in arb_action()) {
let step = ExecutionStep::new(1, "tool".to_string(), action.clone());
assert_eq!(step.action, action);
}
#[test]
fn step_result_defaults_none(order in arb_order(), tool in arb_tool_name(), action in arb_action()) {
let step = ExecutionStep::new(order, tool, action);
assert!(step.result.is_none());
}
#[test]
fn step_result_can_be_set(
order in arb_order(),
tool in arb_tool_name(),
action in arb_action(),
result in arb_result(),
) {
let mut step = ExecutionStep::new(order, tool, action);
step.result = result.clone();
match result {
None => assert!(step.result.is_none()),
Some(ExecutionResult::Success { output }) => {
if let Some(ExecutionResult::Success { output: o }) = step.result {
assert_eq!(o, output);
}
}
Some(ExecutionResult::Error { message }) => {
if let Some(ExecutionResult::Error { message: m }) = step.result {
assert_eq!(m, message);
}
}
_ => {}
}
}
#[test]
fn step_is_clone(order in arb_order(), tool in arb_tool_name(), action in arb_action()) {
let step = ExecutionStep::new(order, tool, action);
let cloned = step.clone();
assert_eq!(step.step_number, cloned.step_number);
assert_eq!(step.tool, cloned.tool);
assert_eq!(step.action, cloned.action);
}
#[test]
fn step_is_debug(order in arb_order(), tool in arb_tool_name(), action in arb_action()) {
let step = ExecutionStep::new(order, tool, action);
let debug_str = format!("{step:?}");
assert!(debug_str.contains("ExecutionStep"));
}
}
#[test]
fn order_must_be_positive() {
let step = ExecutionStep::new(1, "tool".to_string(), "action".to_string());
assert!(step.step_number >= 1);
}
#[test]
fn test_success_result() {
let result = ExecutionResult::Success {
output: "Task completed".to_string(),
};
if let ExecutionResult::Success { output } = result {
assert_eq!(output, "Task completed");
} else {
panic!("Expected Success variant");
}
}
#[test]
fn test_error_result() {
let result = ExecutionResult::Error {
message: "Something went wrong".to_string(),
};
if let ExecutionResult::Error { message } = result {
assert_eq!(message, "Something went wrong");
} else {
panic!("Expected Error variant");
}
}
#[test]
fn test_steps_same_fields() {
let step1 = ExecutionStep::new(1, "tool".to_string(), "action".to_string());
let step2 = ExecutionStep::new(1, "tool".to_string(), "action".to_string());
assert_eq!(step1.step_number, step2.step_number);
assert_eq!(step1.tool, step2.tool);
assert_eq!(step1.action, step2.action);
}
#[test]
fn test_steps_different_order() {
let step1 = ExecutionStep::new(1, "tool".to_string(), "action".to_string());
let step2 = ExecutionStep::new(2, "tool".to_string(), "action".to_string());
assert_ne!(step1.step_number, step2.step_number);
}